summaryrefslogtreecommitdiff
path: root/src/utils/xb_execsql.cpp
blob: d47459372e8daf3138c81a16d182a92d20afdb59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*  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 <xbase.h>

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;
}