By default, CreateTable( CTEXTSTR tablename, CTEXTSTR filename ) which reads a 'create table' statement from a file to create a table, this now parses the create table structure into an internal structure TABLE which has FIELDs and DB_KEY_DEFs. This structure is now passed to CheckODBCTable which is able to compare the structure with the table definition available from the database via DESCRIBE TABLE, and then update the table in the database to match the TABLE definition.
One can use the table structure to define tables instead of maintaining external files... and without having to create a temporary external file which could then contain a create table statement to create the table.
// declare some fields... FIELD some_table_field_array_name[] = { { "field one", "int", NULL } , { "field two", "varchar(100)", NULL } , { "ID field", "int", "auto_increment" } , { "some other field", "int", "NOT NULL default '8'" } }; // define some keys... DB_KEY_DEF some_table_key_array_name[] = { { .flags = { .bPrimary = 1 }, NULL, {"ID Field"} } , { {0}, "namekey", { "field two", NULL } } };
// the structure for DB_KEY_DEF takes an array of column names used to define the key, there should be a NULL to end the list. The value after the array of field names is called 'null' which should always be set to NULL. If these are declared in global data space, then any unset value will be initialized to zero.
TABLE some_table_var_name = { "table name", FIELDS( some_table_field_array_name ), TABLE_KEYS( some_table_key_array_name ), 1 ); LOGICAL CheckODBCTable( PODBC odbc, PTABLE table, _32 options ) PODBC odbc - may be left NULL to use the default database connection. PTABLE table - a pointer to a TABLE structure which has been initialized. _32 options - zero or more of the following symbols or'ed together. #define CTO_MATCH 4 // attempt to figure out alter statements to drop or add columns to exact match definition #define CTO_MERGE 8 // attempt to figure out alter statements to add missing columns, do not drop. Rename?
Then some routine later
{ ... CheckODBCTable( NULL, &some_table_var_name, CTO_MERGE ); .. }
alternatively tables may be checked and updated using the following code, given an internal constant text string that is the create table statement, this may be parsed into a PTABLE structure which the resulting table can be used in CheckODBCTable();
static CTEXTSTR create_player_info = "CREATE TABLE `players_info` (" " `player_id` int(11) NOT NULL auto_increment, " " PRIMARY KEY (`player_id`), " ") "; PTABLE table = GetFieldsInSQL( create_player_info, FALSE ); CheckODBCTable( NULL, table, CTO_MERGE ); DestroySQLTable( table );