*** empty log message ***
[csql.git] / test / soakTest / csqlInsert.c
blobea5ec3b0e4eca4ef24bd90e322fd5febccd61d5d
1 // This case inserts records in csql every 200 ms based on the random numbers it finds.
3 #include <SqlFactory.h>
4 #include <time.h>
5 #define TS 1000
6 //TS -> micro secs to sleep between transaction
8 int main(int argc, char **argv)
10 DbRetVal rv = OK;
11 if (argv[1] == NULL) {
12 fprintf(stderr, "Usage: csqlInsert <o/p file>\n");
13 return 1;
15 FILE *fp = fopen(argv[1], "a");
16 struct timeval timeout;
17 struct timeval timeStamp;
18 char msgBuf[25];
19 AbsSqlConnection *con = SqlFactory::createConnection(CSql);
20 AbsSqlConnection *adCon = SqlFactory::createConnection(CSqlAdapter);
21 rv = con->connect("root","manager");
22 if(rv!=OK)return 1;
23 rv = adCon->connect("root", "manager");
24 if(rv!=OK)return 1;
25 AbsSqlStatement *stmt = SqlFactory::createStatement(CSql);
26 AbsSqlStatement *adStmt = SqlFactory::createStatement(CSqlAdapter);
27 stmt->setConnection(con);
28 adStmt->setConnection(adCon);
29 char statement[200];
30 int rows = 0;
31 // insert into table
32 strcpy(statement,"INSERT INTO soakTable VALUES(1234,?,654323,'LAKSHYA_CSQL');");
33 rv = stmt->prepare(statement);
34 if(rv!=OK) { delete stmt; delete con; return 4; }
35 rv = adStmt->prepare(statement);
36 if(rv!=OK) { delete stmt; delete con; return 4; }
37 int rnd = 0;
38 int i = 0;
39 while (1) {
40 if (rv == OK) {
41 timeout.tv_sec = 0;
42 timeout.tv_usec = TS;
43 os::select(0, 0, 0, 0, &timeout);
44 srand(time(0));
46 /*if (i == 0) rnd = rand() % 16645635;
47 else if (i == 1) rnd = (rand() / 3) % 16645635;
48 else if (i == 2) rnd = (rand() / 7) % 16645635;
49 else if (i == 3) rnd = (rand() / 11) % 16645635;
50 else if (i == 4) { i = 0; rnd = (rand() / 13) % 16645635; }
52 i++;
53 rv = con->beginTrans();
54 if (rv != OK) continue;
55 //stmt->setIntParam(1, rnd);
56 stmt->setIntParam(1, i);
57 rv = stmt->execute(rows);
58 if (rv == ErrUnique) {
59 printf("Unique Key Violation Error\n");
60 con->rollback(); continue;
61 } else if (rv != OK) {
62 printError(rv, "Insert failed with ret val %d", rv);
63 con->rollback(); continue;
65 else con->commit();
66 rv = adCon->beginTrans();
67 //adStmt->setIntParam(1,rnd);
68 adStmt->setIntParam(1,i);
69 rv = adStmt->execute(rows);
70 if (rv != OK) {
71 printError(rv, "Adapter:execute failed with rv = %d");
72 adCon->rollback();
73 continue;
75 else adCon->commit();
76 if (i% 10000 == 0) {
77 os::gettimeofday(&timeStamp);
78 struct tm *tempTm = os::localtime(&timeStamp.tv_sec);
79 strftime(msgBuf, 25, "%d/%m/%Y %H:%M:%S", tempTm);
80 fprintf(fp, "Inserted PK: %8d %s\n", rnd, msgBuf);
81 fflush(fp);
84 fclose(fp);
85 adStmt->free(); stmt->free();
86 adCon->disconnect(); con->disconnect();
87 delete adStmt; delete adCon;
88 delete stmt; delete con;