class AddSystemSettings < ActiveRecord::Migration
def up
create_table :system_settings do |t|
t.string :name
t.string :label
t.text :value
t.string :type
t.integer :position
end
SystemSetting.create :name => "notice",
:label => "Use notice?",
:value => 1
end
def down
drop_table :system_settings
end
end
struct fields {
char * name;
int type;
ptr_t location;
};
struct player {
int id;
char * name;
long vnum;
};
struct fields table = {
{"id", INT32, offsetof(struct player, id)},
{"name", VAR, offsetof(struct player,name)},
{"location", BIGINT, offsetof(struct player, vnum)}
};
struct player p;
foo = table_lookup("id");
*(p+foo) = something;
bar = table_lookup("name");
strcpy(p+bar, something);
struct fields {
char * name;
int type;
ptr_t location;
};
struct player {
int id;
char * name;
long vnum;
};
struct fields table[] = {
{"id", INT32, offsetof(struct player, id)},
{"name", VAR, offsetof(struct player,name)},
{"location", BIGINT, offsetof(struct player, vnum)}
};
<EDIT>
void buildQuery(player *p, char * where_clause)
{
sprintf(query, "UPDATE player_data");
for(;;)
{
if(table[i].type == INT32)
sprintf(query_field, " %s = '%d'", table[i].name, p+table[i].location);
else if (table[i].type == BIGINT)
sprintf(query_field, " %s = '%l'", table[i].name, p+table[i].location)
else
sprintf(query_field, " %s = '%s'", table[i].name, p+table[i].location)
strcat(query, query_field);
i++;
}
p_where = table_lookup(where_clause);
sprintf(query_end, " WHERE %s = '%d'", where_clause, p+p_where);
strcat(query, query_end)
…
}
Basically Im looking to create "defintion" tables for DB Tables so I can mimic functionality I have in my PHP MVC application regarding SQL statement creation. PHP is lovely because of non type restricted arrays, but Im stumping myself on how to go about this in an efficent way in C.
Basically, id like to build a "defintion" table for example, the player struct: (Psuedo code)
The struct would hold
int varType, char fieldName, PointerToMemberInStruct
You would then have a table that had:
INT32 (CONST define), "name", &player->name
Which I could then access, and depending on the player pointer pull the data of player name when referencing "name" in the table.
I know there are various other ways to go about this, I just wanted to try and find a fluid way that allowed me to add and remove fields at ease, and ease the creation of automatically built SQL queries through a function, given a passed table definition struct.
Ive worked on a few high end MMOs, a commercial aspect and only one has slightly approached it in this method, but still not in the way I like. Any suggestions would be great.