/* xb_execsql.cpp XBase64 Software Library Copyright (c) 1997,2003,2014 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 */ #include using namespace xb; xbInt16 GetNextSqlCmd( xbFile &f, xbString &sCmd ); xbInt16 GetNextSqlCmd( xbFile &f, xbString &sCmd ) { sCmd = ""; xbString sLine; xbInt16 iRc = XB_NO_ERROR; xbUInt32 lPos = 0; xbBool bDone = xbFalse; while( !bDone ){ if(( iRc = f.xbFgets( 256, sLine )) != XB_NO_ERROR ){ bDone = xbTrue; } else { // don't need CR/LF chars sLine.ZapChar( 0x0a ); sLine.ZapChar( 0x0d ); // if comment, zap out everything to the right of the hash lPos = sLine.Pos( '#' ); if( lPos > 0 ) sLine.Left( lPos - 1); if( sLine.Pos( ';' ) > 0 ){ bDone = xbTrue; sLine.ZapChar( ';' ); } } sCmd += sLine; } return iRc; } int main(int ac,char** av) { if (ac <= 1) { std::cout << "Usage: xb_execsql filename..." << std::endl; return 1; } xbXBase x; x.EnableMsgLogging(); xbSql sql( &x ); xbFile f( sql.GetXbasePtr() ); xbInt16 iRc = XB_NO_ERROR; xbString sFileName; xbString sSqlLine; sFileName = av[1]; if(( iRc = f.xbFopen( "r", sFileName, XB_SINGLE_USER )) != XB_NO_ERROR ){ xbString sMsg; sMsg.Sprintf( "Error opening [%s]\n", sFileName.Str() ); std::cout << sMsg.Str(); sql.GetXbasePtr()->DisplayError( iRc ); return 1; } while( iRc == XB_NO_ERROR ){ iRc = GetNextSqlCmd( f, sSqlLine ); if( iRc == XB_NO_ERROR ){ sSqlLine.Trim(); std::cout << "Processing line [" << sSqlLine.Str() << "]\n"; iRc = sql.ExecuteNonQuery( sSqlLine ); if( iRc != XB_NO_ERROR ) x.DisplayError( iRc ); } } f.xbFclose(); return 0; }