-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample2_insert.c
More file actions
460 lines (367 loc) · 11.8 KB
/
sample2_insert.c
File metadata and controls
460 lines (367 loc) · 11.8 KB
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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/******************************************************************************
* Copyright of this product 2013-2023,
* InfiniFlux Corporation(or Inc.) or its subsidiaries.
* All Rights reserved.
******************************************************************************/
/* $Id:$ */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <windows.h>
#include <machbase_sqlcli.h>
#define MACHBASE_PORT_NO 5656
#define RC_SUCCESS 0
#define RC_FAILURE -1
#define CHECK_STMT_RESULT(aRC, aSTMT, aMsg) \
if( sRC != SQL_SUCCESS ) \
{ \
printError(gEnv, gCon, aSTMT, aMsg); \
goto error; \
}
SQLHENV gEnv;
SQLHDBC gCon;
void printError(SQLHENV aEnv, SQLHDBC aCon, SQLHSTMT aStmt, char *aMsg);
void printColumn(char *aCol, SQLLEN aLen, char *aFormat, ...);
int connectDB();
void disconnectDB();
int executeDirectSQL(const char *aSQL, int aErrIgnore);
int prepareExecuteSQL(const char *aSQL);
int createTable();
void directInsert();
int selectTable();
void printError(SQLHENV aEnv, SQLHDBC aCon, SQLHSTMT aStmt, char *aMsg)
{
SQLINTEGER sNativeError;
SQLCHAR sErrorMsg[SQL_MAX_MESSAGE_LENGTH + 1];
SQLCHAR sSqlState[SQL_SQLSTATE_SIZE + 1];
SQLSMALLINT sMsgLength;
if( aMsg != NULL )
{
printf("%s\n", aMsg);
}
if( SQLError(aEnv, aCon, aStmt, sSqlState, &sNativeError,
sErrorMsg, SQL_MAX_MESSAGE_LENGTH, &sMsgLength) == SQL_SUCCESS )
{
printf("SQLSTATE-[%s], Machbase-[%ld][%s]\n", sSqlState, sNativeError, sErrorMsg);
}
}
void printColumn(char *aCol, SQLLEN aLen, char *aFormat, ...)
{
fprintf(stdout, "%s : ", aCol);
if( aLen == SQL_NULL_DATA )
{
fprintf(stdout, "NULL");
}
else
{
va_list ap;
va_start(ap, aFormat);
vfprintf(stdout, aFormat, ap);
va_end(ap);
}
}
int connectDB()
{
char sConnStr[1024];
if( SQLAllocEnv(&gEnv) != SQL_SUCCESS )
{
printf("SQLAllocEnv error\n");
return RC_FAILURE;
}
if( SQLAllocConnect(gEnv, &gCon) != SQL_SUCCESS )
{
printf("SQLAllocConnect error\n");
SQLFreeEnv(gEnv);
gEnv = SQL_NULL_HENV;
return RC_FAILURE;
}
sprintf_s(sConnStr, sizeof(sConnStr), "DSN=127.0.0.1;UID=SYS;PWD=MANAGER;CONNTYPE=1;PORT_NO=%d", MACHBASE_PORT_NO);
if( SQLDriverConnect( gCon, NULL,
(SQLCHAR *)sConnStr,
SQL_NTS,
NULL, 0, NULL,
SQL_DRIVER_NOPROMPT ) != SQL_SUCCESS
)
{
printError(gEnv, gCon, NULL, "SQLDriverConnect error");
SQLFreeConnect(gCon);
gCon = SQL_NULL_HDBC;
SQLFreeEnv(gEnv);
gEnv = SQL_NULL_HENV;
return RC_FAILURE;
}
return RC_SUCCESS;
}
void disconnectDB()
{
if( SQLDisconnect(gCon) != SQL_SUCCESS )
{
printError(gEnv, gCon, NULL, "SQLDisconnect error");
}
SQLFreeConnect(gCon);
gCon = SQL_NULL_HDBC;
SQLFreeEnv(gEnv);
gEnv = SQL_NULL_HENV;
}
int executeDirectSQL(const char *aSQL, int aErrIgnore)
{
SQLHSTMT sStmt = SQL_NULL_HSTMT;
if( SQLAllocStmt(gCon, &sStmt) != SQL_SUCCESS )
{
if( aErrIgnore == 0 )
{
printError(gEnv, gCon, sStmt, "SQLAllocStmt Error");
return RC_FAILURE;
}
}
if( SQLExecDirect(sStmt, (SQLCHAR *)aSQL, SQL_NTS) != SQL_SUCCESS )
{
if( aErrIgnore == 0 )
{
printError(gEnv, gCon, sStmt, "SQLExecDirect Error");
SQLFreeStmt(sStmt,SQL_DROP);
sStmt = SQL_NULL_HSTMT;
return RC_FAILURE;
}
}
if( SQLFreeStmt(sStmt, SQL_DROP) != SQL_SUCCESS )
{
if (aErrIgnore == 0)
{
printError(gEnv, gCon, sStmt, "SQLFreeStmt Error");
sStmt = SQL_NULL_HSTMT;
return RC_FAILURE;
}
}
sStmt = SQL_NULL_HSTMT;
return RC_SUCCESS;
}
int prepareExecuteSQL(const char *aSQL)
{
SQLHSTMT sStmt = SQL_NULL_HSTMT;
if( SQLAllocStmt(gCon, &sStmt) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLAllocStmt Error");
goto error;
}
if( SQLPrepare(sStmt, (SQLCHAR *)aSQL, SQL_NTS) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLPrepare Error");
goto error;
}
if( SQLExecute(sStmt) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLExecute Error");
goto error;
}
if( SQLFreeStmt(sStmt, SQL_DROP) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLFreeStmt Error");
goto error;
}
sStmt = SQL_NULL_HSTMT;
return RC_SUCCESS;
error:
if( sStmt != SQL_NULL_HSTMT )
{
SQLFreeStmt(sStmt, SQL_DROP);
sStmt = SQL_NULL_HSTMT;
}
return RC_FAILURE;
}
int createTable()
{
int sRC;
sRC = executeDirectSQL("DROP TABLE CLI_SAMPLE1", 1);
if( sRC != RC_SUCCESS )
{
return RC_FAILURE;
}
sRC = executeDirectSQL("CREATE TABLE CLI_SAMPLE1(seq short, score integer, total long, percentage float, ratio double, id varchar(10), srcip ipv4, dstip ipv6, reg_date datetime, textlog text, image binary)", 0);
if( sRC != RC_SUCCESS )
{
return RC_FAILURE;
}
return RC_SUCCESS;
}
void directInsert()
{
int i;
char query[2 * 1024];
short sSeq;
int sScore;
long sTotal;
float sPercentage;
double sRatio;
char sId [11];
char sSrcIP [16];
char sDstIP [40];
char sRegDate [40];
char sLog [1024];
char sImage [1024];
for(i=1; i<=10; i++)
{
sSeq = i;
sScore = i+i;
sTotal = (sSeq + sScore) * 100;
sPercentage = (float)sScore/sTotal;
sRatio = (double)sSeq/sTotal;
sprintf_s(sId, sizeof(sId), "id-%d", i);
sprintf_s(sSrcIP, sizeof(sSrcIP), "192.168.0.%d", i);
sprintf_s(sDstIP, sizeof(sDstIP), "2001:0DB8:0000:0000:0000:0000:1428:%04d", i);
sprintf_s(sRegDate, sizeof(sRegDate), "2015-03-31 15:26:%02d", i);
sprintf_s(sLog, sizeof(sLog), "text log-%d", i);
sprintf_s(sImage, sizeof(sImage), "binary image-%d", i);
memset(query, 0x00, sizeof(query));
if( i == 10 )
{
sprintf_s(query, sizeof(query), "INSERT INTO CLI_SAMPLE1 VALUES(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)");
}
else
{
sprintf_s(query, sizeof(query), "INSERT INTO CLI_SAMPLE1 VALUES(%d, %d, %ld, %f, %f, '%s', '%s', '%s',TO_DATE('%s','YYYY-MM-DD HH24:MI:SS'),'%s','%s')", sSeq, sScore, sTotal, sPercentage, sRatio, sId, sSrcIP, sDstIP, sRegDate, sLog, sImage);
}
if( prepareExecuteSQL(query) == RC_SUCCESS )
{
printf("%d record inserted\n", i);
}
else
{
printf("%d record failed\n", i);
}
}
}
int selectTable()
{
const char *sSQL = "SELECT seq, score, total, percentage, ratio, id, srcip, dstip, reg_date, textlog, image FROM CLI_SAMPLE1";
SQLHSTMT sStmt = SQL_NULL_HSTMT;
SQLRETURN sRC = SQL_ERROR;
int i = 0;
SQLLEN sSeqLen = 0;
SQLLEN sScoreLen = 0;
SQLLEN sTotalLen = 0;
SQLLEN sPctLen = 0;
SQLLEN sRatioLen = 0;
SQLLEN sIdLen = 0;
SQLLEN sSrcIPLen = 0;
SQLLEN sDstIPLen = 0;
SQLLEN sRegDateLen = 0;
SQLLEN sLogLen = 0;
SQLLEN sImageLen = 0;
short sSeq;
int sScore;
long sTotal;
float sPercentage;
double sRatio;
char sId[11];
char sSrcIP[16];
char sDstIP[40];
SQL_TIMESTAMP_STRUCT sRegDate;
char sLog[1024];
char sImage[1024];
if( SQLAllocStmt(gCon, &sStmt) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLAllocStmt Error");
goto error;
}
if( SQLPrepare(sStmt, (SQLCHAR *)sSQL, SQL_NTS) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQPrepare Error");
goto error;
}
if( SQLExecute(sStmt) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLExecute Error");
goto error;
}
sRC = SQLBindCol(sStmt, 1, SQL_C_SHORT, &sSeq, 0, &sSeqLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 1 Error");
sRC = SQLBindCol(sStmt, 2, SQL_C_LONG, &sScore, 0, &sScoreLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 2 Error");
sRC = SQLBindCol(sStmt, 3, SQL_C_BIGINT, &sTotal, 0, &sTotalLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 3 Error");
sRC = SQLBindCol(sStmt, 4, SQL_C_FLOAT, &sPercentage, 0, &sPctLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 4 Error");
sRC = SQLBindCol(sStmt, 5, SQL_C_DOUBLE, &sRatio, 0, &sRatioLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 5 Error");
sRC = SQLBindCol(sStmt, 6, SQL_C_CHAR, sId, sizeof(sId), &sIdLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 6 Error");
sRC = SQLBindCol(sStmt, 7, SQL_C_CHAR, sSrcIP, sizeof(sSrcIP), &sSrcIPLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 7 Error");
sRC = SQLBindCol(sStmt, 8, SQL_C_CHAR, sDstIP, sizeof(sDstIP), &sDstIPLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 8 Error");
sRC = SQLBindCol(sStmt, 9, SQL_C_TYPE_TIMESTAMP, &sRegDate, 0, &sRegDateLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 9 Error");
sRC = SQLBindCol(sStmt, 10, SQL_C_CHAR, sLog, sizeof(sLog), &sLogLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 10 Error");
sRC = SQLBindCol(sStmt, 11, SQL_C_CHAR, sImage, sizeof(sImage), &sImageLen);
CHECK_STMT_RESULT(sRC, sStmt, "SQLBindCol 11 Error");
while( SQLFetch(sStmt) == SQL_SUCCESS )
{
printf("===== %d ========\n", i++);
printColumn("seq", sSeqLen, "%d", sSeq);
printColumn(", score", sScoreLen, "%d", sScore);
printColumn(", total", sTotalLen, "%ld", sTotal);
printColumn(", percentage", sPctLen, "%.2f", sPercentage);
printColumn(", ratio", sRatioLen, "%g", sRatio);
printColumn(", id", sIdLen, "%s", sId);
printColumn(", srcip", sSrcIPLen, "%s", sSrcIP);
printColumn(", dstip", sDstIPLen, "%s", sDstIP);
printColumn(", regdate", sRegDateLen, "%d-%02d-%02d %02d:%02d:%02d",
sRegDate.year, sRegDate.month, sRegDate.day,
sRegDate.hour, sRegDate.minute, sRegDate.second);
printColumn(", log", sLogLen, "%s", sLog);
printColumn(", image", sImageLen, "%s", sImage);
printf("\n");
}
if( SQLFreeStmt(sStmt, SQL_DROP) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLFreeStmt Error");
goto error;
}
sStmt = SQL_NULL_HSTMT;
return RC_SUCCESS;
error:
if( sStmt != SQL_NULL_HSTMT )
{
SQLFreeStmt(sStmt, SQL_DROP);
sStmt = SQL_NULL_HSTMT;
}
return RC_FAILURE;
}
int main()
{
if( connectDB() == RC_SUCCESS )
{
printf("connectDB success.\n");
}
else
{
printf("connectDB failure.\n");
goto error;
}
if( createTable() == RC_SUCCESS )
{
printf("createTable success.\n");
}
else
{
printf("createTable failure.\n");
goto error;
}
directInsert();
if( selectTable() != RC_SUCCESS )
{
printf("selectTable failure.\n");
goto error;
}
disconnectDB();
return RC_SUCCESS;
error:
if( gCon != SQL_NULL_HDBC )
{
disconnectDB();
}
return RC_FAILURE;
}