-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample5_describe.c
More file actions
281 lines (219 loc) · 6.64 KB
/
sample5_describe.c
File metadata and controls
281 lines (219 loc) · 6.64 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
/******************************************************************************
* 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);
int connectDB();
void disconnectDB();
int executeDirectSQL(const char *aSQL, int aErrIgnore);
int createTable();
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);
}
}
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 createTable()
{
int sRC;
sRC = executeDirectSQL("DROP TABLE CLI_SAMPLE", 1);
if( sRC != RC_SUCCESS )
{
return RC_FAILURE;
}
sRC = executeDirectSQL("CREATE TABLE CLI_SAMPLE(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;
}
int main()
{
const char *sSQL = "SELECT * FROM CLI_SAMPLE";
SQLHSTMT sStmt = SQL_NULL_HSTMT;
SQLRETURN sRC = SQL_ERROR;
SQLCHAR sColName[32];
SQLSMALLINT sColNameLen;
SQLSMALLINT sColType;
SQLULEN sColSize;
SQLSMALLINT sDecimalDigits;
SQLSMALLINT sNullable;
SQLSMALLINT sColumnCount;
int i;
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;
}
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, "SQLPrepare Error");
goto error;
}
if( SQLNumResultCols(sStmt, &sColumnCount) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLNumResultsCols Error");
goto error;
}
printf("----------------------------------------------------------------\n");
printf("%32s%16s%10s\n","Name","Type","Length");
printf("----------------------------------------------------------------\n");
for(i = 1; i <= sColumnCount; i++)
{
sRC = SQLDescribeCol(sStmt,
(SQLUSMALLINT)(i),
(SQLCHAR *)sColName,
(SQLSMALLINT)sizeof(sColName),
(SQLSMALLINT *)&sColNameLen,
(SQLSMALLINT *)&sColType,
(SQLULEN *)&sColSize,
(SQLSMALLINT *)&sDecimalDigits,
(SQLSMALLINT *)&sNullable);
CHECK_STMT_RESULT(sRC, sStmt, "SQLDescribeCol Error");
printf("%32s%16d%10d\n",sColName, sColType, (int)sColSize);
}
printf("----------------------------------------------------------------\n");
if( SQLFreeStmt(sStmt, SQL_DROP) != SQL_SUCCESS )
{
printError(gEnv, gCon, sStmt, "SQLFreeStmt Error");
goto error;
}
sStmt = SQL_NULL_HSTMT;
disconnectDB();
return RC_SUCCESS;
error:
if( sStmt != SQL_NULL_HSTMT )
{
SQLFreeStmt(sStmt, SQL_DROP);
sStmt = SQL_NULL_HSTMT;
}
if( gCon != SQL_NULL_HDBC )
{
disconnectDB();
}
return RC_FAILURE;
}