summaryrefslogtreecommitdiff
path: root/src/tests/xb_test_sql.cpp
blob: 99a4f0b511b6b752302bfce5a76a2ec763260bf4 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* xb_test_sql.cpp

XBase64 Software Library

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.

Email Contact:

    xb64-devel@lists.sourceforge.net
    xb64-users@lists.sourceforge.net

*/

// This program tests the SQL functions

// usage:   xb_test_sql QUITE|NORMAL|VERBOSE



#include "xbase.h"

using namespace xb;

#include "tstfuncs.cpp"


int main( int argCnt, char **av )
{
  int iRc = 0;
  int iRc2 = 0;
  int po = 1;          /* print option */
                       /* 0 - QUIET    */
                       /* 1 - NORMAL   */
                       /* 2 - VERBOSE  */

  xbString sSql;

  if( argCnt > 1 ) {
    if( av[1][0] == 'Q' )
      po = 0;
    else if( av[1][0] == 'V' )
      po = 2;
  }

/*
  xbSchema MyAddressRecord[] = 
  {
    { "ADDRESS",     XB_CHAR_FLD,     30, 0 },
    { "CITY",        XB_CHAR_FLD,     30, 0 },
    { "STATE",       XB_CHAR_FLD,      2, 0 },
    { "ZIPCODE",     XB_NUMERIC_FLD,   9, 0 },
    { "NOTES",       XB_MEMO_FLD,     10, 0 },
    { "LASTUPDATE",  XB_DATE_FLD,      8, 1 },
    { "ACTIVE",      XB_LOGICAL_FLD,   1, 0 },
    { "",0,0,0 }
  };

  above structure below, depending on how table is created

  sSql = "CREATE TABLE Address.DBF ( ADDRESS CHAR(30), CITY CHAR(30), STATE CHAR(2), ZIPCODE NUMERIC(9,0), NOTES VARCHAR, LASTUPDATE DATE, ACTIVE LOGICAL )";
*/

  xbSchema MyZipRecord[] = 
  {
    { "ZIPCODE",     XB_NUMERIC_FLD,   9, 0 },
    { "CITY",        XB_CHAR_FLD,     30, 0 },
    { "STATE",       XB_CHAR_FLD,      2, 0 },
    { "",0,0,0 }
  };


  xbXBase x;
  #ifdef XB_LOGGING_SUPPORT
  x.SetLogDirectory( PROJECT_LOG_DIR );
  x.EnableMsgLogging();
  if( po ){
    std::cout << "Logfile is [" << x.GetLogFqFileName().Str() << "]" << std::endl;
  }
  xbString sMsg;
  sMsg.Sprintf( "Program [%s] initializing...", av[0] );
  x.WriteLogMessage( sMsg );
  #endif


  x.SetDataDirectory( PROJECT_DATA_DIR );
  InitTime();

  xbSql sql( &x );

  if( po > 0 )
    std::cout << "Default Data Directory is [" << x.GetDataDirectory().Str() << "]" << std::endl;


  xbDbf4 SqlDbf( &x );                // version 4 dbf file
  xbDbf4 SqlDbfZ( &x );                // version 4 dbf file





  sSql = "DROP TABLE IF EXISTS AddressR.DBF";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "DropTable()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );



  sSql = "DROP TABLE IF EXISTS Address.DBF";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "DropTable()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );



  sSql = "CREATE TABLE Address.DBF ( ADDRESS CHAR(30), CITY CHAR(30), STATE CHAR(2), ZIPCODE NUMERIC(9,0), NOTES VARCHAR, LASTUPDATE DATE, ACTIVE LOGICAL   )";

  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "CreateTable()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

/*
  non sql way to create a table
  iRc2 = SqlDbf.CreateTable( "Address.DBF", "Address", MyAddressRecord, XB_OVERLAY, XB_MULTI_USER );
  iRc += TestMethod( po, "CreateTable()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );
*/

//  return 0;

  #ifdef XB_MDX_SUPPORT
  sSql = "CREATE INDEX tag1 ON Address.DBF( CITY, STATE, DTOS( LASTUPDATE )) FILTER .NOT. DELETED()";
  // xbString sSql = "CREATE INDEX tag1 ON Address.DBF( CITY, STATE )";

  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqL CreateIndex()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );
  #endif // XB_MDX_SUPPORT

  sSql = "INSERT INTO Address (CITY, STATE, ZIPCODE, NOTES, LASTUPDATE, ACTIVE ) VALUES ( 'San Diego', 'CA', 92007, 'San Diego is a cool place', '1989-02-09', 'Y')";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqlInsert()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  sSql = "INSERT INTO Address (CITY, STATE, ZIPCODE, NOTES, LASTUPDATE, ACTIVE ) VALUES ( 'Dallas', 'TX', 76248, 'Dallas is hot in the summer', '1989-02-09', 'N')";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqlInsert()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  sSql = "ALTER TABLE Address.DBF RENAME TO AddressR.DBF";

  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqlAlterTable()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  sSql = "DELETE FROM AddressR.DBF WHERE CITY='San Diego'";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqlDelete()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  sSql = "UNDELETE FROM AddressR.DBF WHERE CITY='San Diego'";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqlUndelete()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  sSql = "DELETE FROM AddressR.DBF";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqlDelete()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );

  sSql = "UNDELETE FROM AddressR.DBF";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqlDelete()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );


  iRc2 = SqlDbfZ.CreateTable( "ZipCode.DBF", "", MyZipRecord, XB_OVERLAY, XB_MULTI_USER );
  iRc += TestMethod( po, "CreateTable()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );


//  sSql = "INSERT INTO ZipCode.DBF ( ZIPCODE, CITY, STATE ) VALUES ( 75087, 'Rockwall', 'TX' )";


//  std::cout << "---------------------------------------------------------\n";
//  std::cout << sSql.Str() << "\n";

  sSql = "INSERT INTO ZipCode ( CITY ) VALUES ( 'city' )";
  iRc2 = sql.ExecuteNonQuery( sSql );


  iRc += TestMethod( po, "SqlInsert()", (xbInt32) iRc2, XB_NO_ERROR );
  if( iRc2 )
    x.DisplayError( iRc2 );



//**************** work in progress

/*
  sSql = "DELETE FROM AddressR.DBF WHERE BAD='EXPRESSION'";
  iRc2 = sql.ExecuteNonQuery( sSql );
  iRc += TestMethod( po, "SqlDelete()", (xbInt32) iRc2, XB_INVALID_FIELD_NAME );
*/
//  if( iRc2 )
//    x.DisplayError( iRc2 );



  iRc += TestMethod( po, "Close()", SqlDbf.Close(), XB_NO_ERROR );
  iRc += TestMethod( po, "Close()", SqlDbfZ.Close(), XB_NO_ERROR );


//  return 0;

//  std::cout << "---------------------------------------------------------\n";

  xbStmt sqlQry1( &x );
//  sSql = "SELECT CITY, STATE, ZIPCODE FROM Address.DBF T LEFT JOIN LJ.DBF LJ WHERE CITY IS NOT NULL ORDER BY 2 GROUP BY STATE HAVING ZIPCODE .NOT. NULL";
//  sSql = "SELECT CITY, STATE, ZIPCODE FROM AddressR.DBF T WHERE CITY IS NOT NULL ORDER BY 2 GROUP BY STATE HAVING ZIPCODE .NOT. NULL";

//  sSql = "SELECT CITY, STATE, ZIPCODE FROM AddressR A LEFT JOIN ZipCode Z ON A.ZIPCODE = Z.ZIPCODE  WHERE CITY IS NOT NULL ORDER BY 2 GROUP BY STATE HAVING ZIPCODE .NOT. NULL";
//  sSql = "SELECT M.ID, M.LEFTFK0, L0.CFLD FROM MAIN0 M LEFT JOIN LEFT0 L0 ON M.LEFTFK0 = L0.LEFTFK0 WHERE M.ID IS NOT NULL";
  iRc += TestMethod( po, "Select()", sqlQry1.ExecuteQuery( sSql ), XB_NO_ERROR );

  sqlQry1.DumpStmtInternals();

  // sqlQry1.Test();


  x.DisplayTableList();


  if( po > 0 || iRc < 0 )
    fprintf( stdout, "Total Errors = %d\n", iRc * -1 );

#ifdef XB_LOGGING_SUPPORT
  sMsg.Sprintf( "Program [%s] terminating with [%d] errors...", av[0], iRc * -1 );
  x.WriteLogMessage( sMsg );
#endif

  return iRc;
}