Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "AppBase.Console.HXX"
- #include "MySQL/SQL.HXX"
- #include "ConfigSQL.Description.TXT"
- class CAPPLICATION : public CAPPLICATION_BASECLASS
- {
- public:
- bool ProcessInit(CCONFIG&);
- void ProcessExec();
- void CreateDBTables(CStringList & tables);
- private:
- CSLIST DataFiles;
- CPATH outputpath;
- CSTR outputname;
- SQL_Connect sql_connect;
- };
- CAPPLICATION application;
- CXXAPP * cxxapp = &application;
- bool AddDBNames = true;
- typedef struct
- {
- char * table;
- uint * fields;
- uint fieldcount;
- CSLIST * fieldmap;
- } DB_TABLE_INFO;
- CHAR * db_table_name[1];
- CHAR * db_field_name[1];
- DB_TABLE_INFO db_table_info[1];
- uint db_table_name_count = 0;
- uint db_field_name_count = 0;
- uint db_table_info_count = 0;
- #include <MySQL/DB_TABLES.INC>
- bool CAPPLICATION::ProcessInit(CCONFIG & config)
- {
- description = DESCRIPTION;
- if(!CAPPLICATION_BASECLASS::ProcessInit(config) )
- {
- return false;
- }
- Output(EOL);
- if( !sql_connect.Config(config) )
- {
- return false;
- }
- Output(EOL);
- if( !ConfigOptionalPath (config,"OutputPath",outputpath) )
- {
- outputpath.SetName(configfile);
- outputpath.SetNamePart();
- }
- if( !ConfigOptionalEntry(config,"OutputName",outputname) )
- {
- outputname = "DB_TABLES";
- }
- if( !ConfigMustHaveEntry(config,"DataFiles",DataFiles) )
- {
- return false;
- }
- Output(EOL);
- return true;
- }
- void CAPPLICATION::ProcessExec()
- {
- for(char * s; DataFiles.Next(&s); )
- {
- CStringList database;
- if( CommaFields(s,database) )
- {
- CreateDBTables(database);
- }
- }
- }
- class CSQLTable
- {
- public:
- CSTR64 id;
- SQL_Table table;
- };
- CREATECLASS_CPOINTERARRAY(CSQLTable)
- int __cdecl sortcomp_CSQLTable(CSQLTable ** t1,CSQLTable ** t2,void * unused)
- {
- int comp = sz_comp((*t1)->table.db,(*t2)->table.db);
- if( comp == 0 )
- {
- comp = sz_comp((*t1)->table.table,(*t2)->table.table);
- }
- return comp;
- }
- void CAPPLICATION::CreateDBTables(CStringList & database)
- {
- CSTR256 s1,s2; CHAR * s;
- CPOINTERARRAY_CSQLTable tables; CSLIST db_fields;
- SQL_Select sql_select(sql_connect);
- for( CHAR * db = NULL; database.Next(&db); )
- {
- if(!sql_connect.Query(s1.Format("USE `%s`",db)) )
- {
- continue;
- }
- if( sql_select.SelectBegin("SHOW TABLES") )
- {
- CHAR * field = sql_select.GetFieldName(0);
- while( sql_select.SelectNext() )
- {
- CSQLTable * table = tables.Append();
- table->table.db = db;
- table->table.table = sql_select.GetFieldValue((UINT)0);
- CHAR * t = s2.Copy(table->table.table);
- while( (s = sz_token(&t,"-")) != NULL )
- {
- table->id.Concat(ch_toupper(*s));
- table->id.Concat(++s);
- }
- }
- sql_select.SelectClear();
- }
- }
- tables.ArraySort((SORTCOMP)sortcomp_CSQLTable);
- db_fields.Add("0");
- for( UINT i = 0; i < tables.GetCount(); ++i )
- {
- CSQLTable * table = tables.GetAt(i);
- if(!sql_connect.Query(s1.Format("USE `%s`",sz(table->table.db))) )
- {
- continue;
- }
- if( SQL_GetTableInfo(sql_connect,table->table) )
- {
- db_fields.Concat(table->table.fields);
- }
- }
- CString64 filename(outputname);
- for( CHAR * db = NULL; database.Next(&db); )
- {
- filename.Concat("-",db);
- }
- CFILEOUTPUT output(outputpath,filename,"HXX");
- if( output.OpenWRITE() )
- {
- output.PutEOL();
- output.PutLine("#define DB_TABLE(id) db_table_##id,");
- output.PutEOL();
- output.PutLine("typedef enum");
- output.PutLine("{");
- for( UINT i = 0; i < tables.GetCount(); ++i )
- {
- CSQLTable * table = tables.GetAt(i);
- output.PrintLine(" DB_TABLE(%s)",(CHAR*)table->id);
- }
- output.PutLine("} DB_TABLE_enum;");
- output.PutEOL();
- output.PutLine("#undef DB_TABLE");
- output.PutLine("#define DB_TABLE(id) db_table_##id");
- output.PutLine("#define DB_Table(id) db_table_name[db_table_##id]");
- output.PutEOL();
- output.PutLine("extern CHAR * db_table_name[];");
- output.PutLine("extern uint db_table_name_count;");
- output.PutEOL();
- output.PutLine("#define DB_FIELD(id) db_field_##id,");
- output.PutEOL();
- output.PutLine("typedef enum");
- output.PutLine("{");
- for( s = NULL; db_fields.Next(&s); )
- {
- output.PrintLine(" DB_FIELD(%s)",sz_strip(s1.Copy(s)," "));
- }
- output.PutLine("} DB_FIELD_enum;");
- output.PutEOL();
- output.PutLine("#undef DB_FIELD");
- output.PutLine("#define DB_FIELD(id) db_field_##id");
- output.PutLine("#define DB_Field(id) db_field_name[db_field_##id]");
- output.PutEOL();
- output.PutLine("extern CHAR * db_field_name[];");
- output.PutLine("extern uint db_field_name_count;");
- output.Close();
- }
- output.SetName(outputpath,filename,"CXX");
- if( output.OpenWRITE() )
- {
- output.PutEOL();
- output.PutLine("#include \"$CXXLIB.HXX\"");
- output.PutEOL();
- output.PutLine(s1.Copycat("#include \"",filename,".HXX\""));
- output.PutEOL();
- output.PutLine("CHAR * db_table_name[] =");
- output.PutLine("{");
- for( UINT i = 0; i < tables.GetCount(); ++i )
- {
- CSQLTable * table = tables.GetAt(i);
- output.PrintLine(" \"%s.%s\",",(CHAR*)table->table.db,(CHAR*)table->table.table);
- }
- output.PutLine("};");
- output.PutLine("uint db_table_name_count = (sizeof(db_table_name)/sizeof(db_table_name[0]));");
- output.PutEOL();
- output.PutEOL();
- output.PutLine("CHAR * db_field_name[] =");
- output.PutLine("{");
- for( s = NULL; db_fields.Next(&s); )
- {
- output.PrintLine(" \"%s\",",s);
- }
- output.PutLine("};");
- output.PutLine("uint db_field_name_count = (sizeof(db_field_name)/sizeof(db_field_name[0]));");
- output.PutEOL();
- for( i = 0; i < tables.GetCount(); ++i )
- {
- CSQLTable * table = tables.GetAt(i);
- sz_substitute(s1.Copy(table->table.table),'-','_');
- output.PrintLine("static uint required_%s[]=",(CHAR*)s1);
- output.PutLine("{");
- for( CHAR * s = NULL; table->table.fields.Next(&s); )
- {
- output.PrintLine(" DB_FIELD(%s),",sz_strip(s1.Copy(s)," "));
- }
- output.PutLine("};"EOL);
- }
- output.PutEOL();
- output.PutLine("typedef struct");
- output.PutLine("{");
- output.PutLine(" char * table;");
- output.PutLine(" uint * fields;");
- output.PutLine(" uint fieldcount;");
- output.PutLine(" CSLIST * fieldmap;");
- output.PutLine("} DB_TABLE_INFO;");
- output.PutEOL();
- output.PutLine("DB_TABLE_INFO db_table_info[] =");
- output.PutLine("{");
- for( i = 0; i < tables.GetCount(); ++i )
- {
- CSQLTable * table = tables.GetAt(i);
- sz_substitute(s2.Copy(table->table.table),'-','_');
- output.PrintLine(" {\"%s\",required_%s,(sizeof(required_%s)/sizeof(required_%s[0]))},",(CHAR*)table->id,(CHAR*)s2,(CHAR*)s2,(CHAR*)s2);
- }
- output.PutLine("};"EOL);
- output.PutLine("uint db_table_info_count = (sizeof(db_table_info)/sizeof(db_table_info[0]));");
- output.PutEOL();
- output.PutLine("#include <MySQL/DB_TABLES.INC>");
- output.PutEOL();
- output.Close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement