Adding cachetable tool
[csql.git] / src / tools / cachetable.cxx
blobdc6c353f0ebf5e920da901af3031c6ca74e7638d
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include <CSql.h>
17 #include <sql.h>
18 #include <sqlext.h>
20 void printUsage()
22 printf("Usage: cachetable [-u username] [-p passwd] -t tablename \n");
23 printf(" tablename -> tablename residing in target database which needs to be cached.\n");
24 printf(" username -> username to connect with csql.\n");
25 printf(" passwd -> password for the above username to connect with csql.\n");
26 return;
30 int main(int argc, char **argv)
32 char username[IDENTIFIER_LENGTH];
33 username [0] = '\0';
34 char password[IDENTIFIER_LENGTH];
35 password [0] = '\0';
36 int c = 0, opt = 10;
37 char tablename[IDENTIFIER_LENGTH];
38 while ((c = getopt(argc, argv, "u:p:t:?")) != EOF)
40 switch (c)
42 case 'u' : { strcpy(username, argv[optind - 1]); opt=10; break; }
43 case 'p' : { strcpy(password, argv[optind - 1]); opt=10; break; }
44 case 't' : { strcpy(tablename, argv[optind - 1]); opt = 2; break; }
45 case '?' : { opt = 10; break; } //print help
46 default: opt=10;
49 }//while options
50 if (opt == 10) {
51 printUsage();
52 return 0;
55 //printf("%s %s \n", username, password);
56 if (username[0] == '\0' )
58 strcpy(username, "root");
59 strcpy(password, "manager");
61 //TODO::Read dsn from the cache.conf file
63 char* dsn = (char *) "DSN=myodbc3;";
64 SQLCHAR outstr[1024];
65 SQLSMALLINT outstrlen;
67 int retValue =0;
68 SQLHENV henv;
69 SQLHDBC hdbc;
70 SQLHSTMT hstmt;
71 retValue = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
72 if (retValue) exit(1);
73 SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
74 retValue = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
75 if (retValue) exit(1);
76 retValue = SQLDriverConnect(hdbc, NULL, (SQLCHAR*)dsn, SQL_NTS,
77 outstr, sizeof(outstr), &outstrlen,
78 SQL_DRIVER_NOPROMPT);
79 if (SQL_SUCCEEDED(retValue)) {
80 printf("Connected to target database using dsn = %s\n", dsn);
81 } else {
82 fprintf(stderr, "Failed to connect to target database\n");
83 exit (2);
85 retValue=SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
86 if (retValue) exit(1);
87 char stmtBuf[1024];
88 sprintf(stmtBuf, "SELECT * FROM %s;", tablename);
89 SQLPrepare (hstmt, (unsigned char *) stmtBuf, SQL_NTS);
91 Connection conn;
92 DbRetVal rv = conn.open(username, password);
93 if (rv != OK) return 1;
94 DatabaseManager *dbMgr = (DatabaseManager*) conn.getDatabaseManager();
95 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
96 Table *table = dbMgr->openTable(tablename);
97 if (table == NULL) exit (2);
98 List fNameList = table->getFieldNameList();
99 ListIterator fNameIter = fNameList.getIterator();
100 FieldInfo *info = new FieldInfo();
101 int fcount =1; void *valBuf; int fieldsize=0;
102 Identifier *elem = NULL;
103 DATE_STRUCT sDate;
104 TIME_STRUCT sTime;
105 TIMESTAMP_STRUCT sTimestamp;
107 while (fNameIter.hasElement()) {
108 elem = (Identifier*) fNameIter.nextElement();
109 table->getFieldInfo((const char*)elem->name, info);
110 valBuf = AllDataType::alloc(info->type, info->length);
111 table->bindFld(elem->name, valBuf);
112 fieldsize=0;
113 switch( info->type)
115 case typeString:
116 fieldsize = info->length;
117 break;
118 case typeDate:
119 //TODO::store valBuf with type in list
120 fieldsize = sizeof(DATE_STRUCT);
121 valBuf = &sDate;
122 break;
124 retValue = SQLBindCol (hstmt, fcount++, AllDataType::convertToSQLType(info->type),
125 valBuf, fieldsize, NULL);
126 if (retValue) exit (2);
128 delete info;
129 conn.startTransaction();
130 retValue = SQLExecute (hstmt);
131 if (retValue) exit (3);
132 while(true)
134 retValue = SQLFetch (hstmt);
135 if (retValue) break;
136 //TODO::convert odbc to csql for date, time, timestamp
138 ACCESSING MEMBERS of above structures...
139 sDate.year, sDate.month, sDate.day,
140 sTime.hour, sTime.minute, sTime.second,
141 sTimestamp.year, sTimestamp.month, sTimestamp.day,
142 sTimestamp.hour, sTimestamp.minute, sTimestamp.second, sTimestamp.fraction
145 table->insertTuple();
146 printf("Inserted tuple\n");
148 SQLCloseCursor (hstmt);
149 conn.commit();
150 conn.close();
151 SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
152 SQLDisconnect (hdbc);
153 SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
154 SQLFreeHandle (SQL_HANDLE_ENV, henv);
155 return 0;