📄 createcpp.cpp
字号:
static const char* sccsid = "@(#)CreateCpp.cpp v1.0.0 2000-08-18";
/* Copyright(C) 1999, 2000 by JiangSu Bell Software CO.,LTD. */
/*
Name: CreateCpp.cpp Version: 1.0.0
Created by HanBing Date: 2000-08-08
Comment:Create Table classes' cpp and hpp files
Modified:
1) 2000-08-18 HanBing - Create;
*/
#include "common.h"
#include "dbbase.h"
#include <time.h>
char SQL[512];
CRecords Table;
T_LIST<CField> TableCols;
char KeyName[50];
char TableName[40];
char ClassName[40];
char pcDate[10];
char pcTime[10];
int ReadKey()
{
sprintf( SQL, "select CONSTRAINT_NAME from USER_CONSTRAINTS where TABLE_NAME='%s'"
" and CONSTRAINT_TYPE='P'", TableName );
CRecords PriKey;
int i = PriKey.Query( SQL );
if ( i > 0 )
strcpy( KeyName, PriKey.Field(0).Char() );
else
strcpy( KeyName, "" );
return i;
}
int KeyOrder( const char* ColName )
{
sprintf( SQL, "select POSITION from USER_CONS_COLUMNS where CONSTRAINT_NAME='%s'"
" and COLUMN_NAME='%s'", KeyName, ColName );
CRecords Order;
int i = Order.Query( SQL );
if ( i > 0 )
i = Order.Field(0).Float();
else
i = 0;
return i;
}
int OneIndex( FILE* hFile, const char* IndName )
{
fprintf( hFile, " inline int Queryby%s()\n", IndName );
fprintf( hFile, " {\n" );
fprintf( hFile, " T_LIST<CField> FieldList;\n" );
sprintf( SQL, "select COLUMN_NAME from USER_IND_COLUMNS where INDEX_NAME='%s'"
" order by COLUMN_POSITION",
IndName );
CRecords IndCol;
int i = IndCol.Query( SQL );
while ( i > 0 )
{
fprintf( hFile, " FieldList.AddTail( &%s );\n", IndCol.Field(0).Char() );
i = IndCol.Next();
}
fprintf( hFile, " return QuerybyIndex( FieldList );\n" );
fprintf( hFile, " }\n" );
return i;
}
int CreateIndex( FILE* hFile )
{
sprintf( SQL, "select INDEX_NAME from USER_INDEXES where TABLE_NAME='%s'",
TableName );
CRecords Index;
int i = Index.Query( SQL );
while ( i > 0 )
{
OneIndex( hFile, Index.Field(0).Char() );
i = Index.Next();
}
return i;
}
int ReadColumns()
{
ReadKey();
CRecords Columns;
sprintf( SQL, "select COLUMN_NAME,DATA_TYPE,DATA_LENGTH,DATA_PRECISION,DATA_SCALE"
" from USER_TAB_COLUMNS where TABLE_NAME='%s' order by COLUMN_ID",
TableName );
int i = Columns.Query( SQL );
while ( i > 0 )
{
CField *Tmp = new CField;
Tmp->SetName( Columns.Field( "COLUMN_NAME" ).Char() );
if ( !strcmp( Columns.Field( "DATA_TYPE" ).Char(), "NUMBER" ) )
{
if ( Columns.Field( "DATA_SCALE" ).IsNull() ||
Columns.Field( "DATA_SCALE" ).Float() != 0 ||
Columns.Field( "DATA_PRECISION" ).Float() > 8 )
Tmp->SetType( FloatT, Columns.Field( "DATA_PRECISION" ).Float(), 1 );
else
Tmp->SetType( IntT, Columns.Field( "DATA_PRECISION" ).Float(), 1 );
}
else if ( !strcmp( Columns.Field( "DATA_TYPE" ).Char(), "ROWID" ) )
Tmp->SetType( FloatT, Columns.Field( "DATA_PRECISION" ).Float(), 1 );
else
Tmp->SetType( CharT, Columns.Field( "DATA_LENGTH" ).Float(), 1 );
if ( strlen( KeyName ) )
Tmp->SetKey( KeyOrder( Tmp->Name() ) );
TableCols.AddTail( Tmp );
i = Columns.Next();
}
return i;
}
void ClearColumns()
{
TableCols.GoHead();
while ( TableCols.Current() )
{
delete TableCols.Current();
TableCols.Next();
}
TableCols.EmptyList();
}
int CreateHppFile()
{
char FileName[40];
strcpy( FileName, ClassName );
strcat( FileName, ".hpp" );
FILE *hFile = fopen( FileName, "w+" );
if ( hFile )
{
/* 头文件头部说明 */
fprintf( hFile, "/* Copyright(C) 1999, 2000 by JiangSu Bell Software CO.,LTD. */\n"
"/*\n"
" Name: %-30s Version: 1.0.0\n"
" Created by ProgramCreator Date: %s\n"
" Comment: Our Table class for Table: %s\n"
" Modified:\n"
"1) %s ProgramCreator - Create;\n"
"*/\n"
"\n",
FileName,
pcDate,
TableName,
pcDate
);
/* 头文件宏定义 */
fprintf( hFile, "#ifndef __TABLE_%s__\n"
"#define __TABLE_%s__\n"
"\n",
TableName,
TableName
);
/* 表类的定义 */
fprintf( hFile, "class %s : public CTable\n", ClassName );
fprintf( hFile, "{\n" );
fprintf( hFile, " public:\n" );
/* 构造函数 */
fprintf( hFile, " %s( XW_Connection& conn = DefaultConnect, uint size = 1 );\n", ClassName );
/* 构造函数(确省联接参数) */
fprintf( hFile, " %s( uint size );\n", ClassName );
/* 拷贝构造函数 */
fprintf( hFile, " %s( %s& Tab%s );\n", ClassName, ClassName, TableName );
/* 重载 = 操作符 */
fprintf( hFile, " inline %s& operator = ( %s& Tab )\n"
" {\n"
" CTable::operator = ( Tab );\n"
" return *this;\n"
" }\n",
ClassName,
ClassName
);
bool NoKey = true;
/* 声明各字段成员 */
TableCols.GoHead();
while ( TableCols.Current() )
{
if ( TableCols.Current()->Key() )
NoKey = false;
fprintf( hFile, " CField %s;\n", TableCols.Current()->Name() );
TableCols.Next();
}
CreateIndex( hFile );
fprintf( hFile, " private:\n" );
/* 初始化字段及链表 */
fprintf( hFile, " void Initial();\n" );
fprintf( hFile, "};\n" );
if ( NoKey )
{
fprintf( hFile, "/* NO KEY defined - Notice: this table has No Primary Key be defined! please verify! */\n" );
cout << TableName << " -- No primary key defined!" << endl << flush;
}
fprintf( hFile, "\n#endif __TABLE_%s__\n", TableName );
fclose( hFile );
}
return int(hFile);
}
const char* Type()
{
static char *type;
switch ( TableCols.Current()->Type() )
{
case CharT:
type = "CharT";
break;
case IntT:
type = "IntT";
break;
case FloatT:
type = "FloatT";
break;
case VoidT:
type = "VoidT";
break;
}
return type;
}
void WriteFields( FILE *hFile, const char* size = "1" )
{
TableCols.GoHead();
if ( TableCols.Current() )
{
fprintf( hFile, "%s( \"%s\", %s, %ld, %ld, %s )",
TableCols.Current()->Name(),
TableCols.Current()->Name(),
Type(),
TableCols.Current()->Len(),
TableCols.Current()->Key(),
size
);
while ( TableCols.Next() )
{
if ( TableCols.Current() )
fprintf( hFile, ",\n%s( \"%s\", %s, %ld, %ld, %s )",
TableCols.Current()->Name(),
TableCols.Current()->Name(),
Type(),
TableCols.Current()->Len(),
TableCols.Current()->Key(),
size
);
}
fprintf( hFile, "\n" );
}
}
int CreateCppFile()
{
char FileName[40];
strcpy( FileName, ClassName );
strcat( FileName, ".cpp" );
FILE *hFile = fopen( FileName, "w+" );
if ( hFile )
{
/* 实现文件头部说明 */
fprintf( hFile, "/* Copyright(C) 1999, 2000 by JiangSu Bell Software CO.,LTD. */\n"
"/*\n"
" Name: %-30s Version: 1.0.0\n"
" Created by ProgramCreator Date: %s\n"
" Comment: Our Table class for Table: %s\n"
" Modified:\n"
"1) %s ProgramCreator - Create;\n"
"*/\n\n",
FileName,
pcDate,
TableName,
pcDate
);
/* 包含的头文件说明 */
fprintf( hFile, "#include \"common.h\"\n"
"#include \"dbbase.h\"\n"
"#include \"%s.hpp\"\n\n",
ClassName
);
/* 构造函数 */
fprintf( hFile, "%s :: %s( XW_Connection& conn, uint size ) : CTable( conn, size ),\n",
ClassName,
ClassName
);
WriteFields( hFile, "size" );
fprintf( hFile, "{\n"
" Initial();\n"
"}\n"
"\n"
);
/* 构造函数(确省联接参数) */
fprintf( hFile, "%s :: %s( uint size ) : CTable( DefaultConnect, size ),\n",
ClassName,
ClassName
);
WriteFields( hFile, "size" );
fprintf( hFile, "{\n"
" Initial();\n"
"}\n"
"\n"
);
/* 拷贝构造函数 */
fprintf( hFile, "%s :: %s( %s& Tab ) : CTable( Tab.Connect() ),\n",
ClassName,
ClassName,
ClassName
);
WriteFields( hFile );
fprintf( hFile, "{\n"
" Initial();\n"
" *this = Tab;\n"
"}\n"
"\n"
);
/* 初始化字段及链表 */
fprintf( hFile, "void %s :: Initial()\n", ClassName );
fprintf( hFile, "{\n" );
fprintf( hFile, " SetTableName( \"%s\" );\n", TableName );
TableCols.GoHead();
while ( TableCols.Current() )
{
char *type;
switch ( TableCols.Current()->Type() )
{
case CharT:
type = "CharT";
break;
case IntT:
type = "IntT";
break;
case FloatT:
type = "FloatT";
break;
case VoidT:
type = "VoidT";
break;
}
fprintf( hFile, " Fields.AddTail( &%s );\n", TableCols.Current()->Name() );
TableCols.Next();
}
fprintf( hFile, "}\n" );
fclose( hFile );
}
return int(hFile);
}
int main()
{
char tables[128];
if ( Login( "oracle" ) )
{
cout << endl << "Please Input the table name: " << flush;
cin >> tables;
sprintf( SQL, "select TNAME from TAB where TNAME like '%s'", tables );
int num = Table.Query( SQL );
if ( num > 0 )
{
while ( num > 0 )
{
cout << strcpy( TableName, Table.Field(0).Char() ) << endl << flush;
ReadTime( pcDate, pcTime );
sprintf( ClassName, "T_%s", TableName );
ReadColumns();
if ( !CreateHppFile() )
cout << "Create File: T_" << TableName << ".hpp Failed!" << endl << flush;
if ( !CreateCppFile() )
cout << "Create File: T_" << TableName << ".cpp Failed!" << endl << flush;
ClearColumns();
num = Table.Next();
}
cout << "Create Files Finished!" << endl << flush;
}
else
cout << "Table: <" << tables << ">not found!" << endl << flush;
XW_Logout();
}
else
cout << "Connect to ORACLE failed!" << endl << flush;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -