-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsybase.cpp
455 lines (390 loc) · 12.3 KB
/
sybase.cpp
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
/*
* sybase.cpp
*
*/
#include "Edb.hh"
#define LOG(fmt,...) ESystem::out->printfln(fmt, ##__VA_ARGS__)
#define HOST "192.168.2.61"
#define PORT "5000"
#define DATABASE "master"
#define USERNAME "sa"
#define PASSWORD "password"
static void test_db_connect() {
EConnection conn;
//1.
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
ON_FINALLY_NOTHROW(
conn.close();
) {
EDatabaseMetaData* meta = conn.getMetaData();
LOG("name=%s", meta->getDatabaseProductName().c_str());
LOG("version=%s", meta->getDatabaseProductVersion().c_str());
}}
//2.
conn.connect("edbc:SYBASE://" HOST ":" PORT "/" DATABASE "?connectTimeout=10", USERNAME, PASSWORD);
ON_FINALLY_NOTHROW(
conn.close();
) {
EDatabaseMetaData* meta = conn.getMetaData();
LOG("name=%s", meta->getDatabaseProductName().c_str());
LOG("version=%s", meta->getDatabaseProductVersion().c_str());
}}
}
static void test_db_execute() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
ECommonStatement stmt(&conn);
EResultSet* rs;
//0.
try {
stmt.setSql("DROP TABLE sybase001").execute();
} catch (...) {
}
stmt.setSql("CREATE TABLE sybase001 ("
"a int NULL ,"
"b tinyint NULL ,"
"c binary (50) NULL ,"
"d bit NOT NULL ,"
"e text NULL,"
"f datetime NULL ,"
"g float NULL ,"
"h money NULL ,"
"i nchar (255) NULL ,"
"j timestamp NULL ,"
"k smallint NULL ,"
"l image NULL ,"
"m numeric(15,5) NULL)").execute();
//1.
stmt.clear();
char *zw = "0123456789中文";
stmt.setSql("insert into sybase001 (d,c,a,i,f,h,g,m,e) values(1,?,?,?,?,?,?,?,?)")
.bindBytes((byte*)"0x12345000", 8)
.bindInt(4)
.bindString("中文")
.bindString("2017-07-08")
.bindNumeric("8888.88")
.bindDouble(5.4433)
.bindNumeric("1.9998")
.bind(DB_dtCLob, zw, strlen(zw));
for (int i=0; i<100; i++) {
stmt.execute();
}
//2.
stmt.clear();
stmt.setSql("select * from sybase001").execute();
rs = stmt.getResultSet();
EResultSetMetaData* rsm = rs->getMetaData();
LOG(rsm->toString().c_str());
while (rs->next()) {
for (int i=1; i<=rs->getMetaData()->getColumnCount(); i++) {
LOG("%s:%s", rs->getMetaData()->getColumnLabel(i).c_str(), rs->isNull(i) ? "is null" : rs->getString(i).c_str());
}
}
rs->close();
//3.
stmt.clear();
stmt.setFetchSize(2);
stmt.setSql("select * from sybase001 where a=? or i=?")
.bindInt(4)
.bindString("1")
.execute();
rs = stmt.getResultSet();
while (rs != null) {
while (rs->next()) {
for (int i=1; i<=rs->getMetaData()->getColumnCount(); i++) {
LOG("%s:%s", rs->getMetaData()->getColumnLabel(i).c_str(), rs->isNull(i) ? "is null" : rs->getString(i).c_str());
}
}
rs = stmt.getResultSet(true);
}
if (rs != null) rs->close();
conn.close();
}
static void test_db_update() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
EUpdateStatement stmt(&conn);
stmt.setFailedResume(true);
stmt.setSql("insert into sybase001 (d,a,i,f) values (1, -2, '666', '2015-5-5')"); //1
stmt.addBatch("insert into sybase001 (d,a,i,f) values (?, ?, ?, ?)"); //2
stmt.bindBool(true);
stmt.bindInt(-1);
stmt.bindString("777");
stmt.bindString("2016-6-6");
stmt.addBatch("insert into sybase001 (d,a,i,f) values (0, ?, ?, ?)"); //3
stmt.bindInt(-3);
stmt.bindString("333");
stmt.bindString("2016-6-6");
for (int i = 0; i < 99; i++) {
stmt.addBatch();
if (i == 0) { //模拟错误
stmt.bindInt(i);
} else {
stmt.bindInt(i);
stmt.bindString("999");
stmt.bindString("2016-6-6");
}
}
stmt.addBatch("insert into sybase001 (d,a,i,f) values (0, -2, '666', '2015-5-5')"); //103
stmt.addBatch("insert into sybase001 (d,a,i,f) values (0, -2, '666', '2015-5-5')"); //104
stmt.execute();
LOG("failures=%d", stmt.getFailures());
LOG("first failed=%d", stmt.getFirstFailed());
LOG("sql 1 affected=%d", stmt.getUpdateCount(1));
LOG("sql 3 affected=%d", stmt.getUpdateCount(3));
LOG("sql 3 errmsg=%s", stmt.getUpdateMessage(3).c_str());
LOG("sql 4 affected=%d", stmt.getUpdateCount(4));
LOG("sql 4 errmsg=%s", stmt.getUpdateMessage(4).c_str());
LOG("sql 5 affected=%d", stmt.getUpdateCount(5));
LOG("sql 6 affected=%d", stmt.getUpdateCount(6));
LOG("sql 103 affected=%d", stmt.getUpdateCount(103));
LOG("sql 104 affected=%d", stmt.getUpdateCount(104));
conn.close();
}
static void test_db_commit() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
ECommonStatement stmt(&conn);
stmt.setSql("update sybase001 set i='xxxxxxxxxx' where a=1");
conn.setAutoCommit(false);
stmt.execute();
// conn.commit();
conn.rollback();
// conn.setAutoCommit(true);
stmt.execute();
conn.commit();
conn.close();
}
static void test_db_rollback() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
ECommonStatement stmt(&conn);
try {
stmt.setSql("drop table sybase002").execute();
} catch (...) {
}
stmt.setSql("create table sybase002 (a int)").execute();
conn.setAutoCommit(false);
ESavepoint sp1 = conn.setSavepoint();
stmt.setSql("insert into sybase002 values (1)").execute();
// conn.rollback();
// conn.setAutoCommit(true);
ESavepoint sp2 = conn.setSavepoint();
stmt.setSql("insert into sybase002 values (2)").execute();
conn.rollback(sp2);
conn.commit();
conn.close();
}
static void test_large_object() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
conn.setAutoCommit(false);
llong oid = conn.createLargeObject();
LOG("create oid=%ld", oid);
char *s = "12345678905567894598765679";
// EByteArrayInputStream bais(s, strlen(s));
EByteArrayOutputStream baos;
for (int i = 0; i< 10000000; i++) {
baos.write(s);
}
EByteArrayInputStream bais(baos.data(), baos.size());
llong written = conn.writeLargeObject(oid, &bais);
LOG("written=%ld", written);
EFileOutputStream fos("/tmp/lob.txt");
llong reading = conn.readLargeObject(oid, &fos);
LOG("reading=%ld", reading);
conn.commit();
conn.close();
}
static void test_create_table() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
EUpdateStatement stmt(&conn);
stmt.setFailedResume(true);
stmt.addBatch("dump tran master with truncate_only");
stmt.addBatch("drop table sybase001");
stmt.addBatch("CREATE TABLE sybase001 ("
"a int NULL ,"
"b tinyint NULL ,"
"c binary (50) NULL ,"
"d bit NOT NULL ,"
"e text NULL,"
"f datetime NULL ,"
"g float NULL ,"
"h money NULL ,"
"i nchar (255) NULL ,"
"j timestamp NULL ,"
"k smallint NULL ,"
"l image NULL ,"
"m numeric(15,5) NULL ,"
"n bigint NULL)");
stmt.execute();
conn.close();
}
static void test_sql_insert() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
//large lob
ECommonStatement st(&conn);
st.setSql("insert into sybase001 (d, e, l) values (1, ?, ?)");
EFileInputStream fis1("/tmp/a.csv");
EFileInputStream fis2("/tmp/a.csv");
st.bindAsciiStream(&fis1);
st.bindBinaryStream(&fis2);
st.execute();
//other
EUpdateStatement stmt(&conn);
stmt.addBatch("insert into sybase001 (a,b,c,d,e,f,g,h,i,j,k,l,m) "
"values (100000,20,0x616161616161,1,'中午的太阳火辣辣','2005-01-09 23:12:59',21212.4343,3333.01,"
"'舞起来',null,522,0x61616161616122,334343.2123)");
stmt.addBatch("insert into sybase001 (d,e,g) values (1,?,?)");
es_buffer_t *buffer = eso_buffer_make(100, 0);
for (int i = 0; i < 100; i++) {
eso_buffer_append(buffer, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa11aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
50);
}
stmt.bind(DB_dtCLob, (char*)buffer->data, buffer->len);
eso_buffer_free(&buffer);
stmt.bindFloat(5.44454);
stmt.addBatch("insert into sybase001 (a,b,c,d,e,f,g,i) values (?,?,?,0,?,?,?,?)");
stmt.bindInt(55555);
stmt.bindShort(12);
stmt.bindBytes((byte *)"\1\2\3\4\0\9\8", 7);
stmt.bindString("计费地热 热");
stmt.bindDateTime("2005-01-09 23:12:59");
stmt.bindFloat(5.44454);
stmt.bindString("2222031");
stmt.setFailedResume(true);
stmt.execute();
LOG("err=%s, first failed sql index=%d, failures=%d", stmt.getErrorMessage().c_str(), stmt.getFirstFailed(), stmt.getFailures());
for (int i=1; i <= stmt.getSqlCount(); i++) {
LOG("sql#%d affected=%d, errmsg=%s", i, stmt.getUpdateCount(i), stmt.getUpdateMessage(i).c_str());
}
conn.close();
}
static void test_sql_query() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
ECommonStatement stmt(&conn);
stmt.setSql("select * from sybase001").execute();
EResultSet* rs = stmt.getResultSet();
while (rs->next()) {
for (int i=1; i<=rs->getMetaData()->getColumnCount(); i++) {
LOG("%s:%s", rs->getMetaData()->getColumnLabel(i).c_str(), rs->isNull(i) ? "is null" : rs->getString(i).c_str());
}
}
conn.close();
}
static void test_sql_update() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
EUpdateStatement stmt(&conn);
//1.
stmt.setSql("update sybase001 set n=?,k=?,a=?,d=?,g=?,m=?");
stmt.bindLong(ELLong::MAX_VALUE);
stmt.bindShort(EShort::MAX_VALUE);
stmt.bindInt(EInteger::MAX_VALUE);
stmt.bindBool(1);
stmt.bindFloat(8.8843);
stmt.bindNumeric("55555.5454");
stmt.execute();
//2. text: <=64k
stmt.addBatch("update sybase001 set e=?");
es_buffer_t *buffer = eso_buffer_make(100, 0);
for (int i = 0; i < 1000; i++) {
eso_buffer_append(buffer, "a中午的太aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa11aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
50);
}
stmt.bind(DB_dtCLob, (char*)buffer->data, buffer->len);
eso_buffer_free(&buffer);
//3. image: <=64k
stmt.addBatch("update sybase001 set l=?");
buffer = eso_buffer_make(100, 0);
for (int i = 0; i < 10; i++) {
eso_buffer_append(buffer, "\0\1\2\aaaaaaaaa中午的太aaaaaaaaaaaaaaaaaaaaaaaaaaa11aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
50);
}
stmt.bind(DB_dtBLob, (char*)buffer->data, buffer->len);
eso_buffer_free(&buffer);
stmt.execute();
LOG("sql 1 effected = %d", stmt.getUpdateCount(1));
LOG("sql 2 effected = %d", stmt.getUpdateCount(2));
LOG("sql 3 effected = %d", stmt.getUpdateCount(3));
conn.close();
}
static void test_sql_func() {
EConnection conn;
conn.connect(DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
//create
{
EUpdateStatement stmt(&conn);
stmt.setFailedResume(true);
stmt.addBatch("drop procedure sybase001func");
stmt.addBatch("create procedure sybase001func "
"@a int "
"as select * from sybase001 where a=@a "
"select a,b,c,d,e from sybase001");
stmt.execute();
}
//select
{
ECommonStatement stmt(&conn);
#if 0 //您不能使用绑定参数调用存储过程!
stmt.setSql("exec sybase001func @a=?");
stmt.bindInt(4);
#else
stmt.setSql("exec sybase001func @a=4");
#endif
stmt.execute();
EResultSet* rs = stmt.getResultSet();
while (rs != null) {
while (rs->next()) {
for (int i=1; i<=rs->getMetaData()->getColumnCount(); i++) {
LOG("%s:%s", rs->getMetaData()->getColumnLabel(i).c_str(), rs->isNull(i) ? "is null" : rs->getString(i).c_str());
}
}
rs = stmt.getResultSet(true);
}
if (rs != null) rs->close();
}
conn.close();
}
static void test_connect_pool() {
EConnectionPool pool(10, NULL, DATABASE, HOST, atoi(PORT), USERNAME, PASSWORD);
EArrayList<sp<EThread> > threads;
for (int i = 0; i < 20; i++) {
sp<EThread> ths = EThread::executeX([&]() {
sp<EConnection> conn;
for (int j = 0; j < 1000; j++) {
conn = pool.getConnection();
ES_ASSERT (conn != null);
sp<ECommonStatement> stmt = conn->createCommonStatement();
stmt->setSql("update sybase002 set a=1").execute();
EThread::sleep(100);
conn->close();
}
});
threads.add(ths);
}
for (int i=0; i<threads.size(); i++) {
threads[i]->join();
}
pool.close();
}
//=============================================================================
void test_db_sybase(void)
{
EConnection::setDefaultDBType("SYBASE");
// test_db_connect();
// test_db_update();
// test_db_commit();
// test_db_rollback();
// test_large_object();
// test_create_table();
// test_sql_insert();
// test_sql_query();
test_sql_query();
// test_sql_update();
// test_sql_func();
// test_connect_pool();
}