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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# File 'lib/cqgen.rb', line 126
def cqgen(dsn, uid, pwd, name, sql, parmtypes=nil, parmnames=nil)
dsn=$cqgen_dsn if dsn.nil?
uid=$cqgen_uid if uid.nil?
pwd=$cqgen_pwd if pwd.nil?
begin
conn=ODBC::connect(dsn, uid, pwd)
$cqgen_dsn=dsn
$cqgen_uid=uid
$cqgen_pwd=pwd
rescue Exception => err
$stderr.puts("connect for #{name} failed:\n")
$stderr.puts("\t#{err}\n")
return
end
sql = sql.strip
sql = sql.gsub(/[\r\n]+/, " ")
if sql.downcase =~ /where/
sql0=sql + " and 1=0"
else
sql0=sql + " where 1=0"
end
begin
stmt=conn.prepare(sql0)
if stmt.ncols < 1
stmt.execute
end
rescue Exception => err
$stderr.puts("SQL error for #{name}:\n")
$stderr.puts("\t#{err}\n")
return
end
if stmt.ncols > 0
fhead="SQLRETURN\n" + name + "_init(SQLHDBC hdbc, SQLHSTMT *hstmtRet"
fhead2="SQLRETURN\n" + name + "(SQLHSTMT hstmt"
flvars2="{\n SQLRETURN ret;\n INTERNAL_STMT *istmt;\n"
fbody2="\n istmt = (INTERNAL_STMT *) hstmt;\n"
fhead3="SQLRETURN\n" + name + "_deinit(SQLHSTMT hstmt)\n"
flvars3="{\n INTERNAL_STMT *istmt;\n"
fbody3="\n istmt = (INTERNAL_STMT *) hstmt;\n"
fbody3+=" if (istmt != NULL) {\n"
fbody3+="\tSQLFreeStmt(istmt->hstmt, SQL_DROP);\n"
fbody3+="\tfree(istmt);\n"
fbody3+=" }\n return SQL_SUCCESS;\n}\n\n"
else
fhead="SQLRETURN\n" + name + "(SQLHDBC hdbc"
fhead2=""
flvars2=""
fbody2=""
fhead3=""
flvars3=""
fbody3=""
end
flvars="{\n SQLRETURN ret;\n"
if stmt.ncols < 1
flvars+=" SQLHSTMT hstmt;\n"
end
fbody=""
flvarst="{\n SQLRETURN ret;\n"
fheadt="SQLRETURN\n" + name + "_test(SQLHDBC hdbc"
fbodyt=""
if stmt.ncols > 0
flvars+=" INTERNAL_STMT *istmt;\n SQLHSTMT hstmt;\n"
fbody+=" *hstmtRet = hstmt = SQL_NULL_HSTMT;\n"
fbody+=" ret = SQLAllocStmt(hdbc, &hstmt);\n"
fbody+=" if (!SQL_SUCCEEDED(ret)) {\n\treturn ret;\n }\n"
fbody+=" istmt = malloc(sizeof (INTERNAL_STMT));\n"
fbody+=" if (istmt == NULL) {\n\tSQLFreeStmt(hstmt, SQL_DROP);\n"
fbody+="\treturn SQL_ERROR;\n }\n"
fbody+=" istmt->hstmt = hstmt;\n"
fbody+=" istmt->result = NULL;\n"
fbody+=" *hstmtRet = (SQLHSTMT) istmt;\n"
flvarst+=" SQLHSTMT hstmt;\n"
fbodyt+=" ret = #{name}_init(hdbc, &hstmt"
else
fbodyt+=" ret = #{name}(hdbc"
end
pnum=1
stmt.parameters.each do |par|
if !parmtypes.nil?
ptype=parmtypes[pnum-1]
end
if ptype.nil?
ptype = par.type
end
if !parmnames.nil?
pname=parmnames[pnum-1]
end
if pname.nil?
pname="par#{pnum}"
end
case ptype
when ODBC::SQL_INTEGER
pdef=", SQLINTEGER *#{pname}"
fbody+=" #{pname}_len = sizeof (SQLINTEGER);\n";
fbody+=" ret = SQLBindParameter(hstmt, #{pnum}, SQL_PARAM_INPUT,"
fbody+=" SQL_C_LONG, SQL_INTEGER, #{par.precision}, #{par.scale},"
when ODBC::SQL_SMALLINT
pdef=", SQLSMALLINT *#{pname}"
fbody+=" #{pname}_len = sizeof (SQLSMALLINT);\n";
fbody+=" ret = SQLBindParameter(hstmt, #{pnum}, SQL_PARAM_INPUT,"
fbody+=" SQL_C_SHORT, SQL_SMALLINT, #{par.precision}, #{par.scale},"
when ODBC::SQL_FLOAT
pdef=", SQLDOUBLE *#{pname}"
fbody+=" #{pname}_len = sizeof (SQLDOUBLE);\n";
fbody+=" ret = SQLBindParameter(hstmt, #{pnum}, SQL_PARAM_INPUT,"
fbody+=" SQL_C_DOUBLE, SQL_FLOAT, #{par.precision}, #{par.scale},"
when ODBC::SQL_DOUBLE
pdef=", SQLDOUBLE *#{pname}"
fbody+=" #{pname}_len = sizeof (SQLDOUBLE);\n";
fbody+=" ret = SQLBindParameter(hstmt, #{pnum}, SQL_PARAM_INPUT,"
fbody+=" SQL_C_DOUBLE, SQL_DOUBLE, #{par.precision}, #{par.scale},"
else
pdef=", SQLCHAR *#{pname}"
fbody+=" #{pname}_len = #{pname} ? strlen(#{pname}) : SQL_NULL_DATA;\n";
fbody+=" ret = SQLBindParameter(hstmt, #{pnum}, SQL_PARAM_INPUT,"
fbody+=" SQL_C_CHAR, SQL_CHAR, #{par.precision}, #{par.scale},"
end
fbody+=" #{pname}, #{pname}_len, &#{pname}_len);\n"
fhead+=pdef
fheadt+=pdef
fbodyt+=", #{pname}"
flvars+=" SQLINTEGER #{pname}_len;\n"
if stmt.ncols > 0
fbody+=" if (!SQL_SUCCEEDED(ret)) {\n\treturn ret;\n }\n"
else
fbody+=" if (!SQL_SUCCEEDED(ret)) {\n\tgoto done;\n }\n"
end
pnum+=1
end
fheadt+=")\n"
if stmt.ncols < 1
fbodyt+= ", &rowCount"
end
fbodyt+=");\n"
if stmt.ncols > 0
fbodyt+=" if (!SQL_SUCCEEDED(ret)) {\n"
fbodyt+="\t#{name}_deinit(hstmt);\n\treturn ret;\n }\n"
end
struc=""
istruc=""
if stmt.ncols > 0
if pnum<=1
$cqgen_simple_fns["#{name}_test"]=1
end
fbody+=" ret = SQLPrepare(hstmt, \"#{sql}\", SQL_NTS);\n"
fbody+=" if (!SQL_SUCCEEDED(ret)) {\n\treturn ret;\n }\n"
fbody+=" ret = SQLExecute(hstmt);\n"
fbody+=" return ret;\n}\n\n"
struc+="typedef struct {\n"
fbody2+=" hstmt = istmt->hstmt;\n"
fbody2+=" if (result == istmt->result) {\n"
fbody2+="\tgoto doexec;\n }\n"
fbody2+=" istmt->result = NULL;\n"
fbodyt+=" do {\n"
fbodyt+="\tret = #{name}(hstmt, &result);\n"
fbodyt+="\tif (!SQL_SUCCEEDED(ret)) {\n\t break;\n\t}\n"
cols = stmt.columns(1)
coltable = Hash.new
cols.each do |col|
coltable[col.table => 1]
end
cnum=1
cols.each do |col|
if coltable.size > 1
cname=col.table+"_"+col.name
else
cname=col.name
end
case col.type
when ODBC::SQL_INTEGER
istruc+=" SQLLEN #{cname}_len;\n"
istruc+=" SQLINTEGER #{cname};\n"
fbody2+=" ret = SQLBindCol(hstmt, #{cnum}, SQL_C_LONG,"
fbody2+=" &result->#{cname}, sizeof (SQL_C_LONG),"
fbody2+=" &result->#{cname}_len);\n"
when ODBC::SQL_SMALLINT
istruc+=" SQLLEN #{cname}_len;\n"
istruc+=" SQLSMALLINT #{cname};\n"
fbody2+=" ret = SQLBindCol(hstmt, #{cnum}, SQL_C_SMALLINT,"
fbody2+=" &result->#{cname}, sizeof (SQL_C_SMALLINT),"
fbody2+=" &result->#{cname}_len);\n"
when ODBC::SQL_FLOAT
istruc+=" SQLLEN #{cname}_len;\n"
istruc+=" SQLDOUBLE #{cname};\n"
fbody2+=" ret = SQLBindCol(hstmt, #{cnum}, SQL_C_DOUBLE,"
fbody2+=" &result->#{cname}, sizeof (SQLDOUBLE),"
fbody2+=" &result->#{cname}_len);\n"
when ODBC::SQL_DOUBLE
istruc+=" SQLLEN #{cname}_len;\n"
istruc+=" SQLDOUBLE #{cname};\n"
fbody2+=" ret = SQLBindCol(hstmt, #{cnum}, SQL_C_DOUBLE,"
fbody2+=" &result->#{cname}, sizeof (SQL_C_DOUBLE),"
fbody2+=" &result->#{cname}_len);\n"
else
istruc+=" SQLLEN #{cname}_len;\n"
istruc+=" SQLCHAR #{cname}[#{col.length}];\n"
fbody2+=" ret = SQLBindCol(hstmt, #{cnum}, SQL_C_CHAR,"
fbody2+=" result->#{cname}, #{col.length},"
fbody2+=" &result->#{cname}_len);\n"
end
fbody2+=" if (!SQL_SUCCEEDED(ret)) {\n\treturn ret;\n }\n"
cnum+=1;
end
sname=$cqgen_stypes[istruc]
if sname.nil?
sname="RESULT_#{name}"
$cqgen_stypes[istruc]=sname
addstruct=true
else
addstruct=false
end
struc+="#{istruc}} #{sname};\n\n"
flvarst+=" #{sname} result;\n\n"
if addstruct
fhead2+=", #{sname} *result"
else
fbody2=""
flvars2=""
fbody3=""
flvars3=""
fname=sname.gsub(/^RESULT_/, "")
fhead2="#define\t\t#{name}\t#{fname}\n"
fhead3="#define\t\t#{name}_deinit\t#{fname}_deinit\n"
end
cnum=1
cols.each do |col|
if coltable.size > 1
cname=col.table+"_"+col.name
else
cname=col.name
end
case col.type
when ODBC::SQL_INTEGER
fmt='%ld'
when ODBC::SQL_SMALLINT
fmt='%d'
when ODBC::SQL_FLOAT
fmt='%g'
when ODBC::SQL_DOUBLE
fmt='%g'
else
fmt='%s'
end
fbodyt+="\tif (result.#{cname}_len != SQL_NULL_DATA) {\n"
fbodyt+="\t printf(\"#{sname}.#{cname}=#{fmt}\\n\",\n"
fbodyt+="\t\t result.#{cname});\n"
fbodyt+="\t} else {\n"
fbodyt+="\t printf(\"#{sname}.#{cname}=NULL\\n\");\n"
fbodyt+="\t}\n"
cnum+=1;
end
fbodyt+=" } while (SQL_SUCCEEDED(ret));\n"
fbodyt+=" #{name}_deinit(hstmt);\n"
else
fhead+=", SQLINTEGER *rowCountRet"
fbody=" if (!SQL_SUCCEEDED(ret)) {\n\tgoto done;\n }\n" + fbody
fbody=" ret = SQLAllocStmt(hdbc, &hstmt);\n" + fbody
fbody=" *rowCountRet = 0;\n" + fbody
fbody+=" ret = SQLExecDirect(hstmt, "
fbody+='"'+sql+'"'
fbody+=", SQL_NTS);\n"
fbody+=" if (!SQL_SUCCEEDED(ret)) {\n\tgoto done;\n }\n"
fbody+=" ret = SQLRowCount(hstmt, rowCountRet);\ndone:\n"
fbody+=" SQLFreeStmt(hstmt, SQL_DROP);\n return ret;\n}\n\n"
flvarst+=" SQLINTEGER rowCount;\n\n"
fbodyt+=" printf(\"#{name}_test rowCount=%ld\\n\", rowCount);\n"
end
if fbodyt.length > 0
fbodyt+=" return ret;\n"
fbodyt+="}\n"
end
fhead+=")\n"
if fbody2.length > 0
fhead2+=")\n"
fbody2+="doexec:\n"
fbody2+=" ret = SQLFetch(hstmt);\n"
fbody2+=" if (!SQL_SUCCEEDED(ret)) {\n\treturn ret;\n }\n"
fbody2+=" istmt->result = result;\n"
fbody2+=" return ret;\n}\n\n"
end
flvars+="\n"
stmt.drop
conn.disconnect
sql = sql.gsub(/[ \t]+/, " ")
sql = sql.gsub(/\/\*/, "")
sql = sql.gsub(/\*\//, "")
$cqgen_h1+="/* #{sql} */\n" if addstruct
$cqgen_h1+=struc if addstruct
$cqgen_h2+="/* #{sql} */\n"
tmp=fhead.gsub(/SQLRETURN\n/, "SQLRETURN\t");
$cqgen_h2+=tmp.gsub(/\)\n/, ");\n")
if fhead2.length > 0
tmp=fhead2.gsub(/SQLRETURN\n/, "SQLRETURN\t");
$cqgen_h2+=tmp.gsub(/\)\n/, ");\n")
end
if fhead3.length > 0
tmp=fhead3.gsub(/SQLRETURN\n/, "SQLRETURN\t");
$cqgen_h2+=tmp.gsub(/\)\n/, ");\n")
end
$cqgen_h2+="\n"
$cqgen_c+=fhead
$cqgen_c+=flvars
$cqgen_c+=fbody
$cqgen_c+=fhead2 unless fhead2 =~ /^#define/
$cqgen_c+=flvars2
$cqgen_c+=fbody2
$cqgen_c+=fhead3 unless fhead3 =~ /^#define/
$cqgen_c+=flvars3
$cqgen_c+=fbody3
$cqgen_ct1+="#if defined(TEST_#{name}) || defined(TEST_cqgen_all)\n"
$cqgen_ct1+=fheadt
$cqgen_ct1+=flvarst
$cqgen_ct1+=fbodyt
$cqgen_ct1+="#endif\n\n"
end
|