Sack Library Documentation
ContentsIndexReferenceHome
PreviousUpNext
sack::sql::required_table_tag Structure
C++
struct required_table_tag {
  CTEXTSTR name;
  struct {
    int count;
    PFIELD field;
  } fields;
  struct {
    int count;
    PDB_KEY_DEF key;
  } keys;
  struct {
    BIT_FIELD bDynamic : 1;
    BIT_FIELD bTemporary : 1;
    BIT_FIELD bIfNotExist : 1;
  } flags;
  CTEXTSTR create_like_table_name;
  CTEXTSTR database;
  CTEXTSTR type;
  CTEXTSTR comment;
};
typedef struct required_table_tag TABLE;
typedef struct required_table_tag * PTABLE;
Members 
Description 
CTEXTSTR name; 
This is the name of the table. 
struct {
int count;
PFIELD field;
} fields; 
describes the columns (fields) in a table. 
int count; 
number of fields in the array pointed at by field. 
PFIELD field; 
pointer to an array of FIELD
struct {
int count;
PDB_KEY_DEF key;
} keys; 
Describes the keys on the table.
 
int count; 
number of keys pointed at by key. 
PDB_KEY_DEF key; 
pointer to an array of DB_REQ_KEY. 
struct {
BIT_FIELD bDynamic : 1;
BIT_FIELD bTemporary : 1;
BIT_FIELD bIfNotExist : 1;
} flags; 
flags controlling the table. 
BIT_FIELD bDynamic : 1; 
set this if defined dynamically (from getfields in SQL) 
BIT_FIELD bTemporary : 1; 
This is a table that is allocated in memory, static table definitions should leave this 0. 
BIT_FIELD bIfNotExist : 1; 
Issue the create statement always, but include 'if not exists'. Don't try and compare the table structure. 
CTEXTSTR create_like_table_name; 
name of another table that already exists. Creates this table using that table's description. 
CTEXTSTR database; 
name of the database that contains this table. 
CTEXTSTR type; 
an additional field that can specify the database storage engine to use. (Hmm maybe use this to specify sqlite target?) 
CTEXTSTR comment; 
This is an additional field to add as a description to the database if supported by the target database. 

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 );
Created with a commercial version of Doc-O-Matic. In order to make this message disappear you need to register this software. If you have problems registering this software please contact us at support@toolsfactory.com.
Copyright (c) 2000+. All rights reserved.
What do you think about this topic? Send feedback!