summaryrefslogtreecommitdiff
path: root/src/sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/sql')
-rwxr-xr-xsrc/sql/xbcrix.cpp54
-rwxr-xr-xsrc/sql/xbcrtbl.cpp13
-rwxr-xr-xsrc/sql/xbcrtbl.save.cpp256
-rwxr-xr-xsrc/sql/xbdelete.cpp2
-rwxr-xr-xsrc/sql/xbdrptbl.cpp11
5 files changed, 52 insertions, 284 deletions
diff --git a/src/sql/xbcrix.cpp b/src/sql/xbcrix.cpp
index 6a391f7..8efdc39 100755
--- a/src/sql/xbcrix.cpp
+++ b/src/sql/xbcrix.cpp
@@ -66,6 +66,7 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
xbString sNode;
xbBool bUnique = xbFalse;
xbDbf * dbf = NULL;
+ xbBool bTableLocked = xbFalse;
try{
@@ -113,6 +114,7 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
xbFile f( xbase );
f.SetFileName( sIxName );
if( f.FileExists()){
+
iErrorStop = 110;
iRc = XB_FILE_EXISTS;
throw iRc;
@@ -140,12 +142,14 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
// if not open, attempt to open it
dbf = xbase->GetDbfPtr( fDbf.GetFqFileName());
+
if( !dbf ){
if(( iRc = xbase->OpenHighestVersion( sTableName, "", &dbf )) != XB_NO_ERROR ){
iErrorStop = 120;
throw iRc;
}
}
+
if( dbf == NULL ){
iErrorStop = 130;
iRc = XB_FILE_NOT_FOUND;
@@ -153,8 +157,6 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
}
sCmd.Ltrunc( ulPos );
- // std::cout << "cp1 ulPos = " << ulPos << " sCmd = [" << sCmd << "]\n";
-
//ulPos = sCmd.GetLastPos( ')' );
xbString sKeyExpression;
xbBool bDone = xbFalse;
@@ -181,7 +183,6 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
}
// std::cout << "Key Expression =[" << sKeyExpression << "]\n";
-
sCmd.Ltrunc( lPos );
sCmd.Trim();
@@ -199,7 +200,6 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
}
}
-
// std::cout << "sCmd - looking for FILTER stuff [" << sCmd << "]\n";
xbString sFilter;
if( sCmd.Len() > 6 ){
@@ -218,11 +218,11 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
if(( iRc = dbf->LockTable( XB_LOCK )) != XB_NO_ERROR ){
iErrorStop = 140;
throw iRc;
+ } else {
+ bTableLocked = xbTrue;
}
#endif // XB_LOCKING_SUPPORT
- // std::cout << "SqlCreateIndex() - ixtype = " << sIxType << "\n";
-
xbIx *pIx;
void *vpTag;
@@ -230,14 +230,16 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
iErrorStop = 150;
throw iRc;
}
- // std::cout << "SqlCreateIndex() - back from tag create\n";
+
#ifdef XB_NDX_SUPPORT
+ xbBool bAssociate = xbFalse;
if( sIxType == "NDX"){
sCmd.Ltrunc( ulPos );
sCmd.Trim();
if( sCmd.Len() > 0 ){
sCmd.ToUpperCase();
if( sCmd.Pos( "ASSOCIATE" )){
+ bAssociate = xbTrue;
if(( iRc = dbf->AssociateIndex( "NDX", sIxName, 0 )) != XB_NO_ERROR ){
iErrorStop = 160;
throw iRc;
@@ -245,25 +247,36 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
}
}
}
-// if( sIxType == "NDX"){
-// if(( iRc = pIx->Reindex( &vpTag )) != XB_NO_ERROR ){
-// iErrorStop = 160;
-// throw iRc;
-// }
-// }
#endif // XB_NDX_SUPPORT
+ iRc = dbf->Reindex( 2, 1, &pIx, &vpTag );
- if(( iRc = pIx->Reindex( &vpTag )) != XB_NO_ERROR ){
- iErrorStop = 170;
+ #ifdef XB_NDX_SUPPORT
+ if( iRc != XB_NO_ERROR && sIxType == "NDX" && bAssociate ){
+ xbInt16 iRc2;
+ if(( iRc2 = dbf->AssociateIndex( "NDX", sIxName, 1 )) != XB_NO_ERROR ){
+ iErrorStop = 180;
+ throw iRc2;
+ }
+ iErrorStop = 190;
+ throw iRc;
+ }
+ #endif // XB_NDX_SUPPORT
+
+ if( iRc != XB_NO_ERROR ){
+ iErrorStop = 200;
throw iRc;
}
#ifdef XB_LOCKING_SUPPORT
- if(( iRc = dbf->LockTable( XB_UNLOCK )) != XB_NO_ERROR ){
- iErrorStop = 180;
- throw iRc;
+ if( bTableLocked ){
+ if(( iRc = dbf->LockTable( XB_UNLOCK )) != XB_NO_ERROR ){
+ iErrorStop = 210;
+ throw iRc;
+ } else {
+ bTableLocked = xbFalse;
+ }
}
#endif // XB_LOCKING_SUPPORT
@@ -273,11 +286,10 @@ xbInt16 xbSql::SqlCreateIndex( const xbString &sCmdLine ){
sMsg.Sprintf( "xbSql::SqlCreateIndex() Exception Caught. Error Stop = [%d] rc = [%d] table = [%s]", iErrorStop, iRc, sTableName.Str() );
xbase->WriteLogMessage( sMsg.Str() );
xbase->WriteLogMessage( GetErrorMessage( iRc ));
-
}
-
#ifdef XB_LOCKING_SUPPORT
- dbf->LockTable( XB_UNLOCK );
+ if( bTableLocked && dbf )
+ dbf->LockTable( XB_UNLOCK );
#endif // XB_LOCKING_SUPPORT
return iRc;
diff --git a/src/sql/xbcrtbl.cpp b/src/sql/xbcrtbl.cpp
index dfb3403..e22b0b5 100755
--- a/src/sql/xbcrtbl.cpp
+++ b/src/sql/xbcrtbl.cpp
@@ -69,8 +69,8 @@ xbInt16 xbSql::SqlCreateTable( const xbString &sCmdLine ){
sTableName.ExtractElement( s, ' ', 3, 0 );
sTableName.Trim();
- // std::cout << "Tablename = [" << sTableName << "]\n";
-
+// std::cout << "Create table - Tablename = [" << sTableName.Str() << "]\n";
+// std::cout << "Cm line = [" << sCmdLine.Str() << "]\n";
// determine if it already exists
xbFile f( xbase );
@@ -160,17 +160,21 @@ xbInt16 xbSql::SqlCreateTable( const xbString &sCmdLine ){
if( iAttribCnt == 1 ){
sAttrib1 = sWork;
} else {
+
lCmPos = sWork.Pos( ',' );
if( lCmPos <= 0 ){
iErrorStop = 140;
iRc = XB_INVALID_FIELD_LEN;
throw iRc;
}
+
sAttrib1.Assign( sWork, 1, lCmPos - 1);
sAttrib1.Trim();
+
sWork.Ltrunc( lCmPos );
sAttrib2 = sWork;
sAttrib2.Trim();
+
}
s.Ltrunc( lPos );
}
@@ -239,13 +243,15 @@ xbInt16 xbSql::SqlCreateTable( const xbString &sCmdLine ){
#elif defined (XB_DBF3_SUPPORT)
dbf = new xbDbf3( xbase );
#endif
+
if(( iRc = dbf->CreateTable( sTableName, "", schema, 0, XB_MULTI_USER )) != XB_NO_ERROR ){
iErrorStop = 150;
throw iRc;
}
-
free( schema );
+
ll.Clear();
+
}
catch (xbInt16 iRc ){
xbString sMsg;
@@ -258,6 +264,7 @@ xbInt16 xbSql::SqlCreateTable( const xbString &sCmdLine ){
xbase->WriteLogMessage( GetErrorMessage( iRc ));
if( schema ) free( schema );
}
+
return iRc;
}
diff --git a/src/sql/xbcrtbl.save.cpp b/src/sql/xbcrtbl.save.cpp
deleted file mode 100755
index 445ad56..0000000
--- a/src/sql/xbcrtbl.save.cpp
+++ /dev/null
@@ -1,256 +0,0 @@
-/* xbcrtbl.cpp
-
-XBase64 Software Library
-
-Copyright (c) 1997,2003,2014,2019,2022 Gary A Kunkel
-
-The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.
-
-Email Contact:
-
- XDB-devel@lists.sourceforge.net
- XDB-users@lists.sourceforge.net
-
-*/
-
-#include "xbase.h"
-
-#ifdef XB_SQL_SUPPORT
-
-namespace xb{
-
-
-/***********************************************************************/
-xbInt16 xbSql::SqlCreateTable( const xbString &sCmdLine ){
-
-
- // std::cout << "CREATE TABLE " << sCmdLine << std::endl;
-
- // expected format:
- // CREATE TABLE tablename.dbf (Field1 CHAR(10), INTFLD1 INTEGER, ... )
-
- // supported field types
- //
- // SQL TYPE XBASE Field Type
- // -------- ----------------
- // SMALLINT NUMERIC(6,0)
- // INTEGER NUMERIC(11,0)
- // DECIMAL(x,y) NUMERIC(x+1,y)
- // NUMERIC(x,y) NUMERIC(x,y)
- // FLOAT(x,y) FLOAT(x,y)
- // CHAR(n) CHARACTER(n)
- // DATE DATE
- // VARCHAR MEMO
- // LOGICAL LOGICAL
-
- xbInt16 iRc = 0;
- xbInt16 iErrorStop = 0;
- xbString sTableName;
- xbString sFieldName;
- xbString sDataType;
- xbString sAttrib1;
- xbString sAttrib2;
- xbString sLlEntry;
- xbInt16 iType = 0;
- xbInt16 iAttribCnt; // number of attributes for a given data type
- xbString s;
- xbUInt32 lPos = 0;
- xbUInt32 lSpPos = 0; // space position
- xbUInt32 lCmPos = 0; // comma position
- xbUInt32 lLpPos = 0; // left paren position
- xbLinkList<xbString> ll;
- xbSchema *schema = NULL;
- xbString sMsg;
-
- try{
- // retrieve table name
- s.ExtractElement( sCmdLine, '(', 1, 0 );
- sTableName.ExtractElement( s, ' ', 3, 0 );
- sTableName.Trim();
-
- // std::cout << "Tablename = [" << sTableName << "]\n";
-
-
- // determine if it already exists
- xbFile f( xbase );
- f.SetFileName( sTableName );
- if( f.FileExists() ){
- iErrorStop = 100;
- iRc = XB_FILE_EXISTS;
- throw iRc;
- }
-
- // build out table structure with parms from the sql string
- lPos = sCmdLine.Pos( '(' );
- s = sCmdLine;
- s.Ltrunc( lPos );
- s.Trim();
-
- // remove the last byte, should be a )
- s.Remove( s.Len(), 1 );
-
- s.Trim();
- //std::cout << "s = [" << s << "]\n";
-
-
- xbBool bDone = xbFalse;
- xbInt16 iLoop = 0;
- while( !bDone && iLoop++ < 255 ){
- sFieldName.ExtractElement( s, ' ', 1 , 0 );
- lPos = sFieldName.Len();
- sFieldName.Trim();
- if( sFieldName.Len() > 10 )
- sFieldName.Mid( 1, 10 ); // shrink to 10 buytes if too big
-
- //std::cout << "field name=[" << sFieldName << "]\n";
- s.Ltrunc( lPos + 1 );
- s.Ltrim();
- //std::cout << "remainder after field name removed = [" << s << "]\n";
-
- // Data type is delimited with either a space, comma or left paren
- lPos = 9999999;
- lSpPos = s.Pos( ' ' );
- lCmPos = s.Pos( ',' );
- lLpPos = s.Pos( '(' );
- if( lSpPos != 0 ) lPos = lSpPos;
- if( lCmPos != 0 && lCmPos < lPos ) lPos = lCmPos;
- if( lLpPos != 0 && lLpPos < lPos ) lPos = lLpPos;
-
- //sMsg.Sprintf( "SpPos=[%d] CmPos=[%d] LpPos=[%d] lPos=[%d]", lSpPos, lCmPos, lLpPos, lPos );
- //std::cout << sMsg << "\n";
-
- sDataType.Assign( s, 1, lPos-1 );
-
- //std::cout << "DataType=[" << sDataType << "]\n";
- if( sDataType == "CHAR" )
- iAttribCnt = 1;
- else if( sDataType == "DECIMAL" || sDataType == "NUMERIC" || sDataType == "FLOAT" )
- iAttribCnt = 2;
- else if( sDataType == "SMALLINT" || sDataType == "INTEGER" || sDataType == "DATE" || sDataType == "VARCHAR" || sDataType == "LOGICAL" )
- iAttribCnt = 0;
- else{
- iErrorStop = 110;
- iRc = XB_INVALID_FIELD_TYPE;
- throw iRc;
- }
-
- sAttrib1 = "";
- sAttrib2 = "0";
-
- if( iAttribCnt == 0 ){
- s.Ltrunc( sDataType.Len());
-
- } else if( iAttribCnt > 0 ){
- lPos = s.Pos( '(' );
- if( lPos <= 0 ){
- iErrorStop = 110;
- iRc = XB_INVALID_FIELD_LEN;
- throw iRc;
- }
- s.Ltrunc( lPos );
- if( iAttribCnt == 1 )
- lPos = s.Pos( ')' );
- else
- lPos = s.Pos( ',' );
-
- sAttrib1.Assign( s, 1, lPos-1 );
- sAttrib1.Trim();
- s.Ltrunc( lPos );
-
- if( iAttribCnt > 1 ){
- lPos = s.Pos( ')' );
- sAttrib2.Assign( s, 1, lPos-1 );
- s.Ltrunc( lPos );
- }
- }
-
- s.Ltrim();
- s.ZapLeadingChar( ',' );
- s.Ltrim();
-
- if( sDataType == "CHAR" ){
- iType = XB_CHAR_FLD;
- } else if( sDataType == "DECIMAL" ){
- xbInt32 lVal = atol( sAttrib1.Str()) + 1;
- sAttrib1.Sprintf( "%d", lVal );
- iType = XB_NUMERIC_FLD;
- } else if( sDataType == "SMALLINT" ){
- sAttrib1 = "6";
- iType = XB_NUMERIC_FLD;
- } else if( sDataType == "INTEGER" ){
- sAttrib1 = "11";
- iType = XB_NUMERIC_FLD;
- } else if( sDataType == "NUMERIC" ){
- iType = XB_NUMERIC_FLD;
- } else if( sDataType == "FLOAT" ) {
- iType = XB_FLOAT_FLD;
- } else if( sDataType == "DATE" ){
- iType = XB_DATE_FLD;
- sAttrib1 = "8";
- } else if( sDataType == "VARCHAR" ){
- iType = XB_MEMO_FLD;
- sAttrib1 = "10";
- } else if( sDataType == "LOGICAL" ){
- iType = XB_LOGICAL_FLD;
- sAttrib1 = "1";
- }
- sLlEntry.Sprintf( "%s,%s,%c,%s,%s", sFieldName.Str(), sDataType.Str(), iType, sAttrib1.Str(), sAttrib2.Str());
- ll.InsertAtEnd( sLlEntry );
-
- if( s.Len() == 0 )
- bDone = xbTrue;
- }
-
- schema = (xbSchema *) calloc( ll.GetNodeCnt()+1, sizeof( xbSchema ));
- xbLinkListNode<xbString> * llN = ll.GetHeadNode();
- xbUInt32 ulCnt = ll.GetNodeCnt();
-
- char *pTrg;
- for( xbUInt32 i = 0; i < ulCnt; i++ ){
- s = llN->GetKey();
- sFieldName.ExtractElement( s, ',', 1 , 0 );
- pTrg = schema[i].cFieldName;
- for( xbUInt32 j = 0; j < sFieldName.Len(); j++ )
- *pTrg++ = sFieldName[j+1];
- sDataType.ExtractElement( s, ',', 3, 0 );
- schema[i].cType = sDataType[1];
- sAttrib1.ExtractElement( s, ',', 4, 0 );
- schema[i].iFieldLen = atoi( sAttrib1.Str());
- sAttrib2.ExtractElement( s, ',', 5, 0 );
- schema[i].iNoOfDecs = atoi( sAttrib2.Str());
- llN = llN->GetNextNode();
- }
-
- // create the table
- xbDbf * dbf = NULL;
- #ifdef XB_DBF4_SUPPORT
- dbf = new xbDbf4( xbase );
- #elif defined (XB_DBF3_SUPPORT)
- dbf = new xbDbf3( xbase );
- #endif
- if(( iRc = dbf->CreateTable( sTableName, "", schema, 0, XB_MULTI_USER )) != XB_NO_ERROR ){
- iErrorStop = 120;
- throw iRc;
- }
-
- free( schema );
- ll.Clear();
- }
- catch (xbInt16 iRc ){
- xbString sMsg;
- if( sFieldName.Len() > 0 )
- sMsg.Sprintf( "xbSql::SqlCreateTbl() Exception Caught. Error Stop = [%d] rc = [%d] table = [%s] field = [%s]", iErrorStop, iRc, sTableName.Str(), sFieldName.Str() );
- else
- sMsg.Sprintf( "xbSql::SqlCreateTbl() Exception Caught. Error Stop = [%d] rc = [%d] table = [%s]", iErrorStop, iRc, sTableName.Str() );
-
- xbase->WriteLogMessage( sMsg.Str() );
- xbase->WriteLogMessage( GetErrorMessage( iRc ));
- if( schema ) free( schema );
- }
- return iRc;
-}
-
-/***********************************************************************/
-} /* namespace */
-#endif /* XB_SQL_SUPPORT */
-
diff --git a/src/sql/xbdelete.cpp b/src/sql/xbdelete.cpp
index 82b4937..9bd4279 100755
--- a/src/sql/xbdelete.cpp
+++ b/src/sql/xbdelete.cpp
@@ -134,7 +134,7 @@ xbInt16 xbSql::SqlDelete( const xbString &sCmdLine ){
catch (xbInt16 iRc ){
xbString sMsg;
- sMsg.Sprintf( "xbSql::SqlDropTable() Exception Caught. Error Stop = [%d] rc = [%d] table = [%s]", iErrorStop, iRc, sTableName.Str() );
+ sMsg.Sprintf( "xbSql::SqlDelete() Exception Caught. Error Stop = [%d] rc = [%d] table = [%s]", iErrorStop, iRc, sTableName.Str() );
xbase->WriteLogMessage( sMsg.Str() );
xbase->WriteLogMessage( GetErrorMessage( iRc ));
}
diff --git a/src/sql/xbdrptbl.cpp b/src/sql/xbdrptbl.cpp
index b03ee8f..ee3e325 100755
--- a/src/sql/xbdrptbl.cpp
+++ b/src/sql/xbdrptbl.cpp
@@ -2,7 +2,7 @@
XBase64 Software Library
-Copyright (c) 1997,2003,2014,2022 Gary A Kunkel
+Copyright (c) 1997,2003,2014,2022,2023 Gary A Kunkel
The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.
@@ -86,8 +86,10 @@ xbInt16 xbSql::SqlDropTable( const xbString &sCmdLine ){
dbf = xbase->GetDbfPtr( fDbf.GetFqFileName());
if( !dbf ){
- dbf = xbase->Open( sTableName, iRc );
- if( iRc != XB_NO_ERROR ){
+
+ //dbf = xbase->Open( sTableName, iRc );
+ iRc = xbase->OpenHighestVersion( sTableName, "", &dbf );
+ if( iRc != XB_NO_ERROR ){
if( iRc == XB_FILE_NOT_FOUND && bIfExists ){
return XB_NO_ERROR;
} else {
@@ -106,6 +108,9 @@ xbInt16 xbSql::SqlDropTable( const xbString &sCmdLine ){
iErrorStop = 130;
throw iRc;
}
+
+ delete dbf;
+ // dbf = NULL;
}
catch (xbInt16 iRc ){