📄 table.cpp
字号:
static const char* sccsid = "@(#)table.cpp v1.0.0 2000-08-28";
/* Copyright(C) 1999, 2000 by JiangSu Bell Software CO.,LTD. */
/*
Name: table.cpp Version: 2.0.0
Created by HanBing Date: 2000-08-08
Comment: Our group defining all base-class sets
Modified:
2) 2000-08-28 HanBing - Modifed to be array_size;
1) 2000-08-08 HanBing - Create;
*/
#include "common.h"
#include "dbbase.h"
/*
Class: CTable Version: 2.0
Created by HanBing Date: 2000-08-08
Definition File: table.hpp
Comment:
Modified:
3) 2000-08-31 HanBing - Add the member op_tab to indicate the current operate on the table;
2) 2000-08-28 HanBing - Modifed to be array_size;
1) 2000-08-08 HanBing - Create;
*/
//hyl on 20030922
//static char pcSQL[ 2048 ];
//
/*
Name: QueryNext()
Comment: go to next row in the array;
Parameters:
Return Value: the current position
Modified:
1) 2000-08-28 HanBing - Create;
*/
long CTable :: QueryNext()
{
long l_ret = 0;
if ( is_query )
{
if ( nRow == 0 || ++CurPos >= BatSize )
{
Clear();
CurSize = QueryCursor.Fetch( BatSize );
}
if ( CurSize > nRow )
{
FirstCol();
while ( CurPos != 0 && CurCol() )
{
CurCol()->Next();
NextCol();
}
l_ret = ++nRow;
}
else
{
if ( CurSize < 0 )
l_ret = CurSize;
else
l_ret = 0;
CurSize = 0;
is_query = false;
op_tab = op_null;
nRow = Clear();
}
}
return l_ret;
}
/*
Name: ParseQuery()
Comment:
Parameters: condition - input - query SQL's condition
Return Value:
Modified:
1) 2000-09-31 HanBing - Create;
*/
bool CTable :: ParseQuery( const char *condition )
{
bool b_ret = false;
if ( BatSize > 1 )
Assert( ( op_tab == op_null || op_tab == op_query ), "Can't Query Now!" );
op_tab = op_query;
nRow = 0;
if ( !TableName || !strlen( TableName ) )
return false;
if ( condition && pcIndex && !strcmp( condition, pcIndex ) && QueryCursor.IsOpen() )
return ( is_query = true );
if ( !pcIndex )
pcIndex = new char [1024];
if ( condition )
strcpy( pcIndex, condition );
else
pcIndex[0] = 0;
QueryCursor.Open( connect );
if ( QueryCursor.IsOpen() )
{
int i = 0;
FirstCol();
while ( CurCol() )
{
if ( i == 0 )
{
sprintf( pcSQL, "select %s", CurCol()->Name() );
i = 1;
}
else
{
strcat( pcSQL, "," );
strcat( pcSQL, CurCol()->Name() );
}
NextCol();
}
strcat( pcSQL, " from " );
strcat( pcSQL, TableName );
if ( condition && strlen( condition ) )
{
char *pc_condition = new char [ strlen( condition ) + 1 ];
strcpy( pc_condition, condition );
Ltrim( pc_condition );
if ( strstr( pc_condition, "order" ) != pc_condition &&
strstr( pc_condition, "ORDER" ) != pc_condition &&
strstr( pc_condition, "group" ) != pc_condition &&
strstr( pc_condition, "GROUP" ) != pc_condition )
strcat( pcSQL, " where " );
else
strcat( pcSQL, " " );
strcat( pcSQL, pc_condition );
delete pc_condition;
}
if ( QueryCursor.Parse( pcSQL ) )
b_ret = true;
}
if ( !b_ret )
QueryCursor.Close();
else
is_query = true;
return b_ret;
}
/*
Name: Query()
Comment:
Parameters: condition - input - query SQL's condition
Return Value:
Modified:
2) 2000-08-28 HanBing - Added parameter size for array operate;
1) 2000-08-08 HanBing - Create;
*/
long CTable :: Query( const char *condition )
{
long j = -1;
if ( ParseQuery( condition ) )
j = QueryCursor.Execute( BatSize );
if ( j > 0 )
{
int i = 0;
FirstCol();
while ( CurCol() && j > 0 )
{
if ( !QueryCursor.DefineCol( i++, CurCol() ) )
j = -1;
NextCol();
}
}
if ( j > 0 )
j = Next();
if ( j <= 0 )
op_tab = op_null;
return j;
}
long CTable :: QuerybyIndex( T_LIST<CField>& Ind )
{
char *pcCon = new char [1024];
pcCon[0] = 0;
int i = 0;
Ind.GoHead();
while ( Ind.Current() )
{
if ( i == 0 )
{
sprintf( pcCon, "%s=:%s", Ind.Current()->Name(), Ind.Current()->Name() );
i = 1;
}
else
{
strcat( pcCon, " and " );
strcat( pcCon, Ind.Current()->Name() );
strcat( pcCon, "=:" );
strcat( pcCon, Ind.Current()->Name() );
}
Ind.Next();
}
long j = -1;
if ( ParseQuery( pcCon ) )
{
j = 1;
Ind.GoHead();
while ( Ind.Current() && j > 0 )
{
j = QueryCursor.BindCol( Ind.Current() );
Ind.Next();
}
}
if ( j > 0 )
j = QueryCursor.Execute( BatSize );
if ( j > 0 )
{
i = 0;
FirstCol();
while ( CurCol() && j > 0 )
{
if ( !QueryCursor.DefineCol( i++, CurCol() ) )
j = -1;
NextCol();
}
}
if ( j > 0 )
j = Next();
if ( j <= 0 )
op_tab = op_null;
delete pcCon;
return j;
}
/* Class CTable Insert implement */
long CTable :: InsertTab()
{
long j = -1;
if ( !InsertCursor.IsOpen() )
{
InsertCursor.Open( connect );
if ( InsertCursor.IsOpen() )
{
char *pcVal = new char [ 1024 ];
int i = 0;
FirstCol();
while ( CurCol() )
{
if ( i == 0 )
{
sprintf( pcSQL, "insert into %s ( %s", TableName, CurCol()->Name() );
sprintf( pcVal, " values ( :%s", CurCol()->Name() );
i = 1;
}
else
{
strcat( pcSQL, "," );
strcat( pcSQL, CurCol()->Name() );
strcat( pcVal, ",:" );
strcat( pcVal, CurCol()->Name() );
}
NextCol();
}
strcat( pcSQL, " )" );
strcat( pcVal, " )" );
strcat( pcSQL, pcVal );
delete pcVal;
if ( InsertCursor.Parse( pcSQL ) )
{
j = 1;
FirstCol();
while ( CurCol() && j > 0 )
{
j = InsertCursor.BindCol( CurCol() );
NextCol();
}
}
if ( j < 0 )
InsertCursor.Close();
}
}
if ( InsertCursor.IsOpen() )
j = InsertCursor.Execute( CurPos );
else
j = -1;
return j;
}
/* Class CTable Update implement */
long CTable :: UpdateTab()
{
long j = -1;
if ( !UpdateCursor.IsOpen() )
{
UpdateCursor.Open( connect );
if ( UpdateCursor.IsOpen() )
{
char *pcCon = new char [1024];
int m = 0, n = 0;
FirstCol();
while ( CurCol() )
{
if ( CurCol()->Key() )
{
if ( m == 0 )
{
sprintf( pcCon, " where %s=:%s", CurCol()->Name(), CurCol()->Name() );
m = 1;
}
else
{
strcat( pcCon, " and " );
strcat( pcCon, CurCol()->Name() );
strcat( pcCon, "=:" );
strcat( pcCon, CurCol()->Name() );
}
}
else
{
if ( n == 0 )
{
sprintf( pcSQL, "update %s set %s=:%s", TableName, CurCol()->Name(), CurCol()->Name() );
n = 1;
}
else
{
strcat( pcSQL, "," );
strcat( pcSQL, CurCol()->Name() );
strcat( pcSQL, "=:" );
strcat( pcSQL, CurCol()->Name() );
}
}
NextCol();
}
strcat( pcSQL, pcCon );
delete pcCon;
if ( UpdateCursor.Parse( pcSQL ) )
{
j = 1;
FirstCol();
while ( CurCol() && j > 0 )
{
j = UpdateCursor.BindCol( CurCol() );
NextCol();
}
}
if ( j < 0 )
UpdateCursor.Close();
}
}
if ( UpdateCursor.IsOpen() )
j = UpdateCursor.Execute( CurPos );
else
j = -1;
return j;
}
/* Class CTable Delete implement */
long CTable :: DeleteTab()
{
long j = -1;
if ( !DeleteCursor.IsOpen() )
{
DeleteCursor.Open( connect );
if ( DeleteCursor.IsOpen() )
{
char *pcCon = new char [1024];
int n = 0;
FirstCol();
while ( CurCol() )
{
if ( CurCol()->Key() )
{
if ( n == 0 )
{
sprintf( pcCon, "%s=:%s", CurCol()->Name(), CurCol()->Name() );
n = 1;
}
else
{
strcat( pcCon, " and " );
strcat( pcCon, CurCol()->Name() );
strcat( pcCon, "=:" );
strcat( pcCon, CurCol()->Name() );
}
}
NextCol();
}
sprintf( pcSQL, "delete from %s where %s", TableName, pcCon );
delete pcCon;
if ( DeleteCursor.Parse( pcSQL ) )
{
j = 1;
FirstCol();
while ( CurCol() && j > 0 )
{
if ( CurCol()->Key() )
j = DeleteCursor.BindCol( CurCol() );
NextCol();
}
}
if ( j < 0 )
DeleteCursor.Close();
}
}
if ( DeleteCursor.IsOpen() )
j = DeleteCursor.Execute( CurPos );
else
j = -1;
return j;
}
void CTable :: PrintName()
{
FirstCol();
while ( CurCol() )
{
//cout << "'" << CurCol()->Name() << "' " << cout.width( CurCol()->Len() );
NextCol();
}
}
/* Display the Fields in the current Buffer */
void CTable :: Print()
{
FirstCol();
while ( CurCol() )
{
CurCol()->Print( CurPos );
printf( " " );
NextCol();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -