diff options
Diffstat (limited to 'src/examples')
-rwxr-xr-x | src/examples/xb_ex_expression.cpp | 230 | ||||
-rwxr-xr-x | src/examples/xb_ex_string.cpp | 73 | ||||
-rwxr-xr-x | src/examples/xb_ex_v4_create_dbf.cpp | 42 | ||||
-rwxr-xr-x | src/examples/xb_ex_v4_upd_dbf.cpp | 173 |
4 files changed, 382 insertions, 136 deletions
diff --git a/src/examples/xb_ex_expression.cpp b/src/examples/xb_ex_expression.cpp new file mode 100755 index 0000000..dc6d7e6 --- /dev/null +++ b/src/examples/xb_ex_expression.cpp @@ -0,0 +1,230 @@ +/* xb_ex_expression.cpp + +XBase64 Software Library + +Copyright (c) 1997,2003,2014,2022 Gary A Kunkel + +The xb64 software library is covered under +the terms of the GPL Version 3, 2007 license. + +Email Contact: + + xb64-devel@lists.sourceforge.net + xb64-users@lists.sourceforge.net + + + This example program demonstrates expression usage + +*/ + + +#include <xbase.h> + +using namespace xb; + + + + xbSchema MyV4Record[] = + { + { "CFLD1", XB_CHAR_FLD, 20, 0 }, + { "CFLD2", XB_CHAR_FLD, 10, 0 }, + { "NFLD1", XB_NUMERIC_FLD, 5, 0 }, + { "DATE1", XB_DATE_FLD, 8, 0 }, + { "",0,0,0 } + }; + +//********************************************************************************************************************************* +void PrintResult( xbString *sExpression, xbExp *exp ); +void PrintResult( xbString *sExpression, xbExp *exp ){ + + // Determine the expression return type + char cExpType = exp->GetReturnType(); + + // Process the expression results, dependent on return type + if( cExpType == XB_EXP_NUMERIC ){ + xbDouble dResult; + exp->GetNumericResult( dResult ); + std::cout << "Numeric result from expression [" << sExpression->Str() << "] is [" << dResult << "]" << std::endl; + + } else if( cExpType == XB_EXP_DATE ){ + xbDate dt; + exp->GetDateResult( dt ); + std::cout << "Date result from expression [" << sExpression->Str() << "] is [" << dt.Str() << "]" << std::endl; + + } else if( cExpType == XB_EXP_LOGICAL ){ + xbBool bResult; + exp->GetBoolResult( bResult ); + std::cout << "Bool result from expression [" << sExpression->Str() << "] is [" << (bResult ? " True" : "False") << "]" << std::endl; + + } else if( cExpType == XB_EXP_CHAR ){ + xbString sResult; + exp->GetStringResult( sResult ); + std::cout << "Char result from expression [" << sExpression->Str() << "] is [" << sResult.Str() << "]" << std::endl; + } + +} + +//********************************************************************************************************************************* +int main( int ac, char ** av ){ + + xbInt16 iRc = 0; + xbInt16 iErrorStop = 0; + xbIx *pIx = NULL; + void *pTag = NULL; + + xbXBase x; + xbDbf *MyFile = new xbDbf4( &x ); + + try{ + + if(( iRc = MyFile->CreateTable( "EXPEXAMPL.DBF", "TestMdxX2", MyV4Record, XB_OVERLAY, XB_MULTI_USER )) != XB_NO_ERROR ){ + iErrorStop = 100; + throw iRc; + } + /* + CreateTag( const xbString &sIxType, const xbString &sName, const xbString &sKey, const xbString &sFilter, + xbInt16 iDescending, xbInt16 iUnique, xbInt16 iOverLay, xbIx **xbIxOut, void **vpTagOut ); + */ + + // the following index definition has two expressions + // 1) CFLD1+CFLD2 -- concat two char fields into an index key + // 2) .NOT. DELETED() -- don't include any deleted records in the index + if(( iRc = MyFile->CreateTag( "MDX", "TAG1", "CFLD1+CFLD2", ".NOT. DELETED()", 0, 0, XB_OVERLAY, &pIx, &pTag )) != XB_NO_ERROR ){ + iErrorStop = 110; + throw iRc; + } + + // add a record to the table + if(( iRc = MyFile->BlankRecord()) != XB_NO_ERROR ){ + iErrorStop = 120; + throw iRc; + } + + if(( iRc = MyFile->PutField( "CFLD1", "Some text" )) != XB_NO_ERROR ){ + iErrorStop = 130; + throw iRc; + } + + if(( iRc = MyFile->PutField( "CFLD2", "Other text" )) != XB_NO_ERROR ){ + iErrorStop = 140; + throw iRc; + } + + if(( iRc = MyFile->PutLongField( "NFLD1", 1000 )) != XB_NO_ERROR ){ + iErrorStop = 150; + throw iRc; + } + + xbDate dt; + dt.Set( "19890209" ); + if(( iRc = MyFile->PutDateField( "DATE1", dt )) != XB_NO_ERROR ){ + iErrorStop = 160; + throw iRc; + } + + if(( iRc = MyFile->AppendRecord()) != XB_NO_ERROR ){ + iErrorStop = 170; + throw iRc; + } + + if(( iRc = MyFile->Commit()) != XB_NO_ERROR ){ + iErrorStop = 180; + throw iRc; + } + + + // To use the XBase64 expression processing logic + // 1) Parse an expression with the xbExp::ParseExpression() method + // 2) Process the parsed expression with the xbExp::ProcessExpression() method + // 3) If needed, determine the expression return type with the xbExp::GetReturnType() method + // 4) Use the appriate methid to retrieve the expression value: + // xbExp::GetNumericResult() + // xbExp::GetDateResult() + // xbExp::GetLogicalResult() + // xbExp::GetStringResult() + + + // The expression only needs to be parsed once. The ProcessExpression() method can be used + // zero, one or many times after it is initially parsed. + + // see docs/html/xbc5.html for expression documentation + // see example below + + + // Numeric expression example + xbString sExpression = "NFLD1 * (2 + RECNO())"; + xbExp exp( &x ); + // Parse the expression + if(( iRc = exp.ParseExpression( MyFile, sExpression )) != XB_NO_ERROR ){ + iErrorStop = 190; + throw iRc; + } + // Process the parsed expression + if(( iRc = exp.ProcessExpression()) != XB_NO_ERROR ){ + iErrorStop = 200; + return -1; + } + PrintResult( &sExpression, &exp ); + + + // String expression example + sExpression = "CFLD1+CFLD2+'{'+DTOS(DATE1)+'}'"; + xbExp exp2( &x ); + if(( iRc = exp2.ParseExpression( MyFile, sExpression )) != XB_NO_ERROR ){ + iErrorStop = 210; + throw iRc; + } + // Process the parsed expression + if(( iRc = exp2.ProcessExpression()) != XB_NO_ERROR ){ + iErrorStop = 220; + return -1; + } + PrintResult( &sExpression, &exp2 ); + + // Date example + sExpression = "DATE() + 6"; + xbExp exp3( &x ); + if(( iRc = exp3.ParseExpression( MyFile, sExpression )) != XB_NO_ERROR ){ + iErrorStop = 230; + throw iRc; + } + // Process the parsed expression + if(( iRc = exp3.ProcessExpression()) != XB_NO_ERROR ){ + iErrorStop = 240; + return -1; + } + PrintResult( &sExpression, &exp3 ); + + // Logic example + sExpression = "NFLD1 = 5"; + xbExp exp4( &x ); + if(( iRc = exp4.ParseExpression( MyFile, sExpression )) != XB_NO_ERROR ){ + iErrorStop = 250; + throw iRc; + } + // Process the parsed expression + if(( iRc = exp4.ProcessExpression()) != XB_NO_ERROR ){ + iErrorStop = 260; + return -1; + } + PrintResult( &sExpression, &exp4 ); + + + + // Cleanup + MyFile->DeleteTable(); + delete MyFile; + + + } catch (xbInt16 iRc ){ + + std::cout << "Error in program xb_ex_expression at location " << iErrorStop << std::endl; + std::cout << x.GetErrorMessage( iRc ) << std::endl; + + } + + + + + return iRc; +} diff --git a/src/examples/xb_ex_string.cpp b/src/examples/xb_ex_string.cpp index 3d582a1..30fd74e 100755 --- a/src/examples/xb_ex_string.cpp +++ b/src/examples/xb_ex_string.cpp @@ -65,15 +65,18 @@ int main() // trim methods s3 = " abc "; s3.Ltrim(); + #ifdef XB_DEBUG_SUPPORT s3.Dump( "LTrim test" ); - + #else + std::cout << s3.Str() << std::endl; + #endif s3 = " abc "; s3.Rtrim(); - s3.Dump( "RTrim test" ); + std::cout << "RTrim test - " << s3.Str() << std::endl; s3.Trim(); - s3.Dump( "Trim test" ); + std::cout << "Trim test - " << s3.Str() << std::endl; printf( "s3 Len = [%d]\n", s3.Len() ); // Concatenation tests - I @@ -88,29 +91,24 @@ int main() s1 -= "Concatenation test1 part 2 "; s1 -= 'X'; s1 -= s2; - s1.Dump( "Concatenation test 2 " ); - + std::cout << "Concatenation test 2 - " << s1.Str() << std::endl; // Concatenation tests - III s1 = "s1data "; s2 = "s2data "; s3 = s1 - s2; - s3.Dump( "Concatenation test 3a" ); + std::cout << "Concatenation test 3a - " << s3.Str() << std::endl; s3 = s1 + s2; - s3.Dump( "Concatenation test 3b" ); + std::cout << "Concatenation test 3b - " << s3.Str() << std::endl; s3 = s1 + " char * data "; - s3.Dump( "Concatenation test 3c" ); - - // s3 = " char * data " + s2; - // s3.Dump( "Concatenation test 3d" ); + std::cout << "Concatenation test 3c - " << s3.Str() << std::endl; s3 = s1 + 'Z'; - s3.Dump( "Concatenation test 3e" ); + std::cout << "Concatenation test 3d - " << s3.Str() << std::endl; -// s3 = 'A' + s1; s3 = 'A'; std::cout << s3.Str() << std::endl; @@ -118,14 +116,8 @@ int main() std::cout << s3.Str() << std::endl; - - s3 = 'A' + s1; - - - return 0; - - // s3.Dump( "Concatenation test 3f" ); - + s3 = 'A' + s1; + std::cout << std::endl << "== operator tests" << std::endl; if( s1 == s2 ) std::cout << s1.Str() << " == " << s2.Str() << std::endl; @@ -145,8 +137,9 @@ int main() std::cout << std::endl << "!= operator tests" << std::endl; s2 = "abc123"; - s1.Dump( "s1" ); - s2.Dump( "s2" ); + std::cout << "s1 - " << s1.Str() << std::endl; + std::cout << "s2 - " << s2.Str() << std::endl; + if( s1 != s2 ) std::cout << s1.Str() << " != " << s2.Str() << std::endl; else @@ -284,7 +277,7 @@ int main() std::cout << "s2 = " << s2.Str() << std::endl; s1 = s2.Copy(); - s1.Dump( "s1.Copy() results" ); + std::cout << "s1.Copy() results" << s1.Str() << std::endl; s1 = "0x35"; char hexChar; @@ -299,7 +292,7 @@ int main() std::cout << "CvtHexString [" << s1.Str() << "] converts to [" << s2.Str() << "]" << std::endl; s1.ExtractElement( "aaaa|bbbb|cccc|dddd", '|', 2, 0 ); - s1.Dump( "ExtractElement() " ); + std::cout << "ExtractElement() " << s1.Str() << std::endl; s1 = "123"; s2 = "ABC"; @@ -317,50 +310,52 @@ int main() std::cout << s1.Str() << " s1.Pos('G') = " << s1.Pos( 'G' ) << std::endl; std::cout << s1.Str() << " s1.Pos(\"JKL\") = " << s1.Pos( "JKL" ) << std::endl; - s1.Dump( "Remove( 3, 5 ) before " ); + std::cout << "Remove( 3, 5 ) before " << s1.Str() << std::endl; std::cout << s1.Str() << " s1.Remove( 3, 5 ) = [" << s1.Remove( 3, 5 ).Str() << "]" << std::endl; - s1.Dump( "Remove( 3, 5 ) after " ); + std::cout << "Remove( 3, 5 ) after " << s1.Str() << std::endl; s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::cout << "s1.Remove( 20, 10 ) = [" << s1.Remove( 20, 10 ).Str() << "]" << std::endl; - s1.Dump( "Remove( 20, 10 ) " ); + + std::cout << "Remove( 20, 10 ) - " << s1.Str() << std::endl; + s1.Sprintf( "%d", 12345 ); - s1.Dump( "Sprintf( %d, 12345 ) " ); + std::cout << "Sprintf( %d, 12345 ) " << s1.Str() << std::endl; s1.SetNum( (long) 123456 ); std::cout << "s1.SetNum( 123456 ) = " << s1.Str() << std::endl; s1.Set( "Yet another way to set a string value" ); - s1.Dump( "Set" ); + std::cout << "Set - " << s1.Str() << std::endl; s1 = "ABCABCABZ"; - s1.Dump( "SwapChars( 'A', '9' ) before" ); + std::cout << "SwapChars( 'A', '9' ) before - " << s1.Str() << std::endl; s1.SwapChars( 'A', '9' ); - s1.Dump( "SwapChars( 'A', '9' ) after"); + std::cout << "SwapChars( 'A', '9' ) after - " << s1.Str() << std::endl; s1.ToLowerCase(); - s1.Dump( "ToLowerCase" ); + std::cout << "ToLowerCase - " << s1.Str() << std::endl; s1.ToUpperCase(); - s1.Dump( "ToUpperCase" ); + std::cout << "ToUpperCase - " << s1.Str() << std::endl; s1.ZapChar( '9' ); - s1.Dump( "ZapChar( '9' )" ); + std::cout << "ZapChar( '9' )" << s1.Str() << std::endl; s1.ZapLeadingChar( 'B' ); - s1.Dump( "ZapLeadingChar( 'B' )" ); + std::cout << "ZapLeadingChar( 'B' )" << s1.Str() << std::endl; s1.ZapTrailingChar( 'Z' ); - s1.Dump( "ZapLeadingChar( 'Z' )" ); + std::cout << "ZapLeadingChar( 'Z' ) - " << s1.Str() << std::endl; s1 = "123"; s1.PadLeft( '0', 9 ); - s1.Dump( "s1.PadLeft('0', 9 ) "); + std::cout << "s1.PadLeft('0', 9 ) - " << s1.Str() << std::endl; s1 = "abc"; s1.PadRight( 'Z', 9 ); - s1.Dump( "s1.PadRight('Z', 9 ) "); + std::cout << "s1.PadRight('Z', 9 ) " << s1.Str() << std::endl; xbString sNullString; if( sNullString.IsNull()) diff --git a/src/examples/xb_ex_v4_create_dbf.cpp b/src/examples/xb_ex_v4_create_dbf.cpp index a2aff74..6169213 100755 --- a/src/examples/xb_ex_v4_create_dbf.cpp +++ b/src/examples/xb_ex_v4_create_dbf.cpp @@ -13,6 +13,11 @@ Email Contact: This example demonstrates the creation of a Version IV file and and indices + Creates three files in folder "PROJECT_DATA_DIR" + Address.DBF - Table with all the data + Address.DBT - Memo (variable lenght char field) data + Address.MDX - File with index data + */ #include <xbase.h> @@ -24,18 +29,27 @@ int main() #ifdef XB_DBF4_SUPPORT - xbSchema MyRecord[] = + xbSchema MyAddressBookRecord[] = { - { "FIRSTNAME", XB_CHAR_FLD, 15, 0 }, { "LASTNAME", XB_CHAR_FLD, 20, 0 }, + { "FIRSTNAME", XB_CHAR_FLD, 15, 0 }, + { "COMPANY", XB_CHAR_FLD, 20, 0 }, + { "ADDRESS", XB_CHAR_FLD, 35, 0 }, + { "CITY", XB_CHAR_FLD, 30, 0 }, + { "STATECD", XB_CHAR_FLD, 2, 0 }, + { "ZIPCD", XB_CHAR_FLD, 10, 0 }, + { "BIRTHDATE", XB_DATE_FLD, 8, 0 }, - { "AMOUNT", XB_NUMERIC_FLD, 9, 2 }, - { "RETIRED?", XB_LOGICAL_FLD, 1, 0 }, - { "ZIPCODE", XB_NUMERIC_FLD, 5, 0 }, - { "NUMFLD1", XB_FLOAT_FLD, 12, 2 }, - { "NUMFLD2", XB_FLOAT_FLD, 14, 2 }, + + { "AMOUNT1", XB_NUMERIC_FLD, 9, 2 }, + { "AMOUNT2", XB_FLOAT_FLD, 12, 2 }, + + { "FRIEND?", XB_LOGICAL_FLD, 1, 0 }, + { "FAMILY?", XB_LOGICAL_FLD, 1, 0 }, + { "BUSASSOC?", XB_LOGICAL_FLD, 1, 0 }, + #ifdef XB_MEMO_SUPPORT - { "MEMO1", XB_MEMO_FLD, 10, 0 }, + { "NOTES", XB_MEMO_FLD, 10, 0 }, #endif { "",0,0,0 } }; @@ -46,11 +60,15 @@ int main() xbInt16 iRc; xbDbf * MyDbfFile; + + #ifdef XB_MDX_SUPPORT xbIx *pIx; void *pTag; + #endif // XB_MDX_SUPPORT + MyDbfFile = new xbDbf4( &x ); - if(( iRc = MyDbfFile->CreateTable( "MyV4Table1", "MyV4TableAlias", MyRecord, XB_OVERLAY, XB_MULTI_USER )) != XB_NO_ERROR ) + if(( iRc = MyDbfFile->CreateTable( "Address.DBF", "Address", MyAddressBookRecord, XB_OVERLAY, XB_MULTI_USER )) != XB_NO_ERROR ) x.DisplayError( iRc ); else { @@ -64,11 +82,11 @@ int main() */ // std::cout << "Creating three index tags\n"; - if(( iRc = MyDbfFile->CreateTag( "MDX", "NAME_TAG", "LASTNAME+FIRSTNAME", ".NOT. DELETED()", 0, 0, XB_OVERLAY, &pIx, &pTag )) != XB_NO_ERROR ) + if(( iRc = MyDbfFile->CreateTag( "MDX", "NAME", "LASTNAME+FIRSTNAME", ".NOT. DELETED()", 0, 0, XB_OVERLAY, &pIx, &pTag )) != XB_NO_ERROR ) x.DisplayError( iRc ); - if(( iRc = MyDbfFile->CreateTag( "MDX", "BDDATE_TAG", "BIRTHDATE", "", 0, 0, XB_OVERLAY, &pIx, &pTag )) != XB_NO_ERROR ) + if(( iRc = MyDbfFile->CreateTag( "MDX", "BDDATE", "BIRTHDATE", ".NOT. DELETED()", 0, 0, XB_OVERLAY, &pIx, &pTag )) != XB_NO_ERROR ) x.DisplayError( iRc ); - if(( iRc = MyDbfFile->CreateTag( "MDX", "ZIP_TAG", "ZIPCODE", "", 0, 0, XB_OVERLAY, &pIx, &pTag )) != XB_NO_ERROR ) + if(( iRc = MyDbfFile->CreateTag( "MDX", "COMPANY", "COMPANY+LASTNAME+FIRSTNAME", ".NOT. DELETED()", 0, 0, XB_OVERLAY, &pIx, &pTag )) != XB_NO_ERROR ) x.DisplayError( iRc ); #endif // XB_MDX_SUPPORT diff --git a/src/examples/xb_ex_v4_upd_dbf.cpp b/src/examples/xb_ex_v4_upd_dbf.cpp index 6f68c73..96b6bdf 100755 --- a/src/examples/xb_ex_v4_upd_dbf.cpp +++ b/src/examples/xb_ex_v4_upd_dbf.cpp @@ -11,7 +11,7 @@ Email Contact: XDB-devel@lists.sourceforge.net XDB-users@lists.sourceforge.net - This example demonstrates how to open the DBase III table created by xb_ex_v4_create_dbf + This example demonstrates how to open the DBase IV table created by xb_ex_v4_create_dbf and apply various updates to the table. */ @@ -20,27 +20,11 @@ Email Contact: using namespace xb; -#undef XB_MEMO_FIELDS - int main() { #ifdef XB_DBF4_SUPPORT - xbInt16 fld_FIRSTNAME; - xbInt16 fld_LASTNAME; - xbInt16 fld_BIRTHDATE; - xbInt16 fld_AMOUNT; - xbInt16 fld_RETIRED; - xbInt16 fld_ZIPCODE; - xbInt16 fld_NUMFLD1; - xbInt16 fld_NUMFLD2; - - - #ifdef XB_MEMO_FIELDS - xbInt16 fld_MEMO1; - #endif - /* define the classes */ xbXBase x; /* initialize xbase */ @@ -48,37 +32,30 @@ int main() x.EnableMsgLogging(); x.WriteLogMessage( "Program [xb_ex_v4_upd_dbf] initializing..." ); - - xbDbf *MyTable = new xbDbf4( &x ); /* class for table */ - -#ifdef XB_INDEX_NDX - xbNdx MyIndex1( &MyTable ); /* class for index 1 */ -#endif + xbDbf *MyTable = new xbDbf4( &x ); /* class for DBase V4 table */ xbInt16 iRc = 0; xbInt16 iErrorStop = 0; try{ - if(( iRc = MyTable->Open( "MyV4Table1.DBF" )) != XB_NO_ERROR ){ - iErrorStop = 1; + if(( iRc = MyTable->Open( "Address.DBF" )) != XB_NO_ERROR ){ + iErrorStop = 100; throw iRc; } /* get the field numbers for all the fields in the table */ - - fld_FIRSTNAME = MyTable->GetFieldNo( "FIRSTNAME" ); - fld_LASTNAME = MyTable->GetFieldNo( "LASTNAME" ); - fld_BIRTHDATE = MyTable->GetFieldNo( "BIRTHDATE" ); - fld_AMOUNT = MyTable->GetFieldNo( "AMOUNT" ); - fld_RETIRED = MyTable->GetFieldNo( "RETIRED?" ); - fld_ZIPCODE = MyTable->GetFieldNo( "ZIPCODE" ); - fld_NUMFLD1 = MyTable->GetFieldNo( "NUMFLD1" ); - fld_NUMFLD2 = MyTable->GetFieldNo( "NUMFLD2" ); + xbInt16 iFld_FIRSTNAME = MyTable->GetFieldNo( "FIRSTNAME" ); + xbInt16 iFld_LASTNAME = MyTable->GetFieldNo( "LASTNAME" ); + xbInt16 iFld_BIRTHDATE = MyTable->GetFieldNo( "BIRTHDATE" ); + xbInt16 iFld_AMOUNT1 = MyTable->GetFieldNo( "AMOUNT1" ); + xbInt16 iFld_FRIEND = MyTable->GetFieldNo( "FRIEND?" ); + xbInt16 iFld_ZIPCD = MyTable->GetFieldNo( "ZIPCD" ); + xbInt16 iFld_AMOUNT2 = MyTable->GetFieldNo( "AMOUNT2" ); #ifdef XB_MEMO_FIELDS - fld_MEMO1 = MyTable->GetFieldNo( "MEMO1" ); + zbInt16 iFld_MEMO1 = MyTable->GetFieldNo( "MEMO1" ); #endif @@ -88,187 +65,213 @@ int main() // Blank the record buffer if(( iRc = MyTable->BlankRecord()) != XB_NO_ERROR ){ - iErrorStop = 7; + iErrorStop = 110; throw iRc; } // put field examples - using field numbers - if(( iRc = MyTable->PutField( fld_LASTNAME, "JONES" )) != XB_NO_ERROR ){ - iErrorStop = 8; + if(( iRc = MyTable->PutField( iFld_LASTNAME, "JONES" )) != XB_NO_ERROR ){ + iErrorStop = 120; throw iRc; } + // could also reference by field name (see below) but referencing by number + // is a little bit faster because it doesn't need to look up the number for the field name + // Alternative--> if(( iRc = MyTable->PutField( "LASTNAME", "JONES" )) != XB_NO_ERROR ){ - if(( iRc = MyTable->PutField( fld_FIRSTNAME, "JERRY" )) != XB_NO_ERROR ){ - iErrorStop = 9; + if(( iRc = MyTable->PutField( iFld_FIRSTNAME, "JERRY" )) != XB_NO_ERROR ){ + iErrorStop = 130; throw iRc; } - if(( iRc = MyTable->PutField( fld_AMOUNT, "12.35" )) != XB_NO_ERROR ){ - iErrorStop = 10; + if(( iRc = MyTable->PutField( iFld_AMOUNT1, "12.35" )) != XB_NO_ERROR ){ + iErrorStop = 140; throw iRc; } - if(( iRc = MyTable->PutLogicalField( fld_RETIRED, "Y" )) != XB_NO_ERROR ){ - iErrorStop = 11; + if(( iRc = MyTable->PutLogicalField( iFld_FRIEND, "Y" )) != XB_NO_ERROR ){ + iErrorStop = 150; throw iRc; } - if(( iRc = MyTable->PutField( fld_BIRTHDATE, "19880209" )) != XB_NO_ERROR ){ - iErrorStop = 12; + if(( iRc = MyTable->PutField( iFld_BIRTHDATE, "19880209" )) != XB_NO_ERROR ){ + iErrorStop = 160; throw iRc; } - if(( iRc = MyTable->PutLongField( fld_ZIPCODE, 12345 )) != XB_NO_ERROR ){ - iErrorStop = 13; + if(( iRc = MyTable->PutLongField( iFld_ZIPCD, 12345 )) != XB_NO_ERROR ){ + iErrorStop = 170; throw iRc; } // Append the first record if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){ - iErrorStop = 15; + iErrorStop = 180; + throw iRc; + } + + // Commit the updates + if(( iRc = MyTable->Commit()) != XB_NO_ERROR ){ + iErrorStop = 190; throw iRc; } // Blank the record buffer if(( iRc = MyTable->BlankRecord()) != XB_NO_ERROR ){ - iErrorStop = 16; + iErrorStop = 200; throw iRc; } // put field to the record buffer using field name (slightly less efficient than using field numbers) if(( iRc = MyTable->PutField( "LASTNAME", "FUCKPUTIN" )) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 210; throw iRc; } if(( iRc = MyTable->PutField( "FIRSTNAME", "ALBERT" )) != XB_NO_ERROR ){ - iErrorStop = 21; + iErrorStop = 220; throw iRc; } - if(( iRc = MyTable->PutField( "AMOUNT", "987.55" )) != XB_NO_ERROR ){ - iErrorStop = 22; + if(( iRc = MyTable->PutDoubleField( "AMOUNT1", (xbDouble) 987.55 )) != XB_NO_ERROR ){ + iErrorStop = 230; throw iRc; } - if(( iRc = MyTable->PutLogicalField( "RETIRED?", "N" )) != XB_NO_ERROR ){ - iErrorStop = 23; + if(( iRc = MyTable->PutLogicalField( "FRIEND?", "N" )) != XB_NO_ERROR ){ + iErrorStop = 240; throw iRc; } - if(( iRc = MyTable->PutLongField( "ZIPCODE", 44256 )) != XB_NO_ERROR ){ - iErrorStop = 24; + if(( iRc = MyTable->PutLongField( "ZIPCD", 44256 )) != XB_NO_ERROR ){ + iErrorStop = 250; throw iRc; } xbFloat f = (xbFloat) 12345.67; std::cout << f << std::endl; - if(( iRc = MyTable->PutFloatField( fld_NUMFLD1, f )) != XB_NO_ERROR ){ - iErrorStop = 13; + if(( iRc = MyTable->PutFloatField( iFld_AMOUNT2, f )) != XB_NO_ERROR ){ + iErrorStop = 260; throw iRc; } xbDouble d = 76543.21; - if(( iRc = MyTable->PutDoubleField( fld_NUMFLD2, d )) != XB_NO_ERROR ){ - iErrorStop = 14; + if(( iRc = MyTable->PutDoubleField( iFld_AMOUNT1, d )) != XB_NO_ERROR ){ + iErrorStop = 270; throw iRc; } // Append the second record if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){ - iErrorStop = 25; + iErrorStop = 280; + throw iRc; + } + + // Commit the updates + if(( iRc = MyTable->Commit()) != XB_NO_ERROR ){ + iErrorStop = 290; throw iRc; } + // get a field with a field number xbString FirstName; - if(( iRc = MyTable->GetField( fld_FIRSTNAME, FirstName )) < 0 ){ - iErrorStop = 30; + if(( iRc = MyTable->GetField( iFld_FIRSTNAME, FirstName )) < 0 ){ + iErrorStop = 300; throw iRc; } std::cout << "First Name is [" << FirstName.Str() << "]" << std::endl; xbString LastName; if(( iRc = MyTable->GetField( "LASTNAME", LastName )) < 0 ){ - iErrorStop = 31; + iErrorStop = 310; throw iRc; } std::cout << "Last Name is [" << LastName.Str() << "]" << std::endl; xbInt16 iNoOfDecimals; - if(( iRc = MyTable->GetFieldDecimal( "AMOUNT", iNoOfDecimals )) != XB_NO_ERROR ){ - iErrorStop = 32; + if(( iRc = MyTable->GetFieldDecimal( "AMOUNT2", iNoOfDecimals )) != XB_NO_ERROR ){ + iErrorStop = 320; throw iRc; } std::cout << "There are " << iNoOfDecimals << " decimals in the AMOUNT field" << std::endl; xbString FieldName; if(( iRc = MyTable->GetFieldName( 4, FieldName )) != XB_NO_ERROR ){ - iErrorStop = 36; + iErrorStop = 330; throw iRc; } std::cout << "Field #4 name is " << FieldName.Str() << std::endl; - xbString sRetired; - if(( iRc = MyTable->GetLogicalField( "RETIRED?", sRetired )) < 0 ){ - iErrorStop = 38; + xbString sFriend; + if(( iRc = MyTable->GetLogicalField( "FRIEND?", sFriend )) < 0 ){ + iErrorStop = 340; throw iRc; } - std::cout << "Switch value = [" << sRetired.Str() << "]" << std::endl; + std::cout << "Switch value = [" << sFriend.Str() << "]" << std::endl; xbInt32 lZip; if(( iRc = MyTable->GetLongField( "ZIPCODE", lZip )) < 0 ){ - iErrorStop = 39; + iErrorStop = 350; throw iRc; } std::cout << "Long value = [" << lZip << "]" << std::endl; - if(( iRc = MyTable->GetFloatField( fld_NUMFLD1, f )) < 0 ){ - iErrorStop = 41; + if(( iRc = MyTable->GetFloatField( iFld_AMOUNT2, f )) < 0 ){ + iErrorStop = 360; throw iRc; } printf( "Field NUMFLD1 %8.2f\n", f ); - if(( iRc = MyTable->GetDoubleField( fld_NUMFLD2, d )) < 0 ){ - iErrorStop = 42; + if(( iRc = MyTable->GetDoubleField( iFld_AMOUNT1, d )) < 0 ){ + iErrorStop = 370; throw iRc; } printf( "Field NUMFLD2 %8.2f\n", d ); - // Initialize the record buffer in preparation for another record if(( iRc = MyTable->BlankRecord()) != XB_NO_ERROR ){ - iErrorStop = 44; + iErrorStop = 380; throw iRc; } // Append another record (it will be blank) if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){ - iErrorStop = 45; + iErrorStop = 390; throw iRc; }; // mark current record for deletion if(( iRc = MyTable->DeleteRecord()) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 400; throw iRc; }; // save current record if(( iRc = MyTable->PutRecord()) != XB_NO_ERROR ){ - iErrorStop = 51; + iErrorStop = 410; throw iRc; } if(( iRc = MyTable->Commit()) != XB_NO_ERROR ){ - iErrorStop = 52; + iErrorStop = 420; throw iRc; } + + // example code to loop through the table + for( xbUInt32 ulTuple = 1; ulTuple <= MyTable->GetRecordCount(); ulTuple++ ){ + if(( iRc = MyTable->GetRecord( ulTuple ) != XB_NO_ERROR )){ + iErrorStop = 430; + throw iRc; + } + // do something with the record here + std::cout << "Tuple = " << MyTable->GetCurRecNo() << std::endl; + } + /* Close database and associated indexes */ if(( iRc = MyTable->Close()) != XB_NO_ERROR ){ - iErrorStop = 53; + iErrorStop = 440; throw iRc; } |