1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
17 #include <CacheTableLoader.h>
21 printf("Usage: cachetable [-u username] [-p passwd] -t tablename [-r]\n");
22 printf(" tablename -> tablename residing in target database which needs to be cached.\n");
23 printf(" username -> username to connect with csql.\n");
24 printf(" passwd -> password for the above username to connect with csql.\n");
25 printf(" r -> recover all cached tables from the target database.\n");
30 void addToCacheTableFile(char *tablename
)
33 printf("CACHE_TABLE_FILE is %s\n", Conf::config
.getCacheTableFile());
34 fp
= fopen(Conf::config
.getCacheTableFile(),"a");
36 printError(ErrSysInit
, "Invalid path/filename in CACHE_TABLE_FILE.\nTable will not be"
37 "recovered in case of crash");
41 fprintf(fp
, "%s\n", tablename
);
44 int main(int argc
, char **argv
)
46 char username
[IDENTIFIER_LENGTH
];
48 char password
[IDENTIFIER_LENGTH
];
51 char tablename
[IDENTIFIER_LENGTH
];
52 while ((c
= getopt(argc
, argv
, "u:p:t:r?")) != EOF
)
56 case 'u' : { strcpy(username
, argv
[optind
- 1]); opt
=10; break; }
57 case 'p' : { strcpy(password
, argv
[optind
- 1]); opt
=10; break; }
58 case 't' : { strcpy(tablename
, argv
[optind
- 1]); opt
= 2; break; }
59 case '?' : { opt
= 10; break; } //print help
60 case 'r' : { opt
= 3; break; } //recover all the tables
71 //printf("%s %s \n", username, password);
72 if (username
[0] == '\0' )
74 strcpy(username
, "root");
75 strcpy(password
, "manager");
78 CacheTableLoader cacheLoader
;
81 cacheLoader
.setTable(tablename
);
82 rv
= cacheLoader
.load(username
, password
);
83 if (rv
!= OK
) exit (1);
84 rv
= cacheLoader
.addToCacheTableFile();
85 if (rv
!= OK
) exit (2);
88 rv
= cacheLoader
.recoverAllCachedTables(username
, password
);
89 if (rv
!= OK
) exit (1);
94 DbRetVal rv = conn.open(username, password);
95 if (rv != OK) return 1;
96 DatabaseManager *dbMgr = (DatabaseManager*) conn.getDatabaseManager();
97 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
99 Conf::config.getDSN();
100 //char* dsn = (char *) "DSN=myodbc3;";
102 sprintf(dsn, "DSN=%s;", Conf::config.getDSN());
103 printf("DSN VALUE FROM CONFIG=%s\n", dsn);
104 SQLCHAR outstr[1024];
105 SQLSMALLINT outstrlen;
111 retValue = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
112 if (retValue) exit(1);
113 SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
114 retValue = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
115 if (retValue) exit(1);
116 retValue = SQLDriverConnect(hdbc, NULL, (SQLCHAR*)dsn, SQL_NTS,
117 outstr, sizeof(outstr), &outstrlen,
118 SQL_DRIVER_NOPROMPT);
119 if (SQL_SUCCEEDED(retValue)) {
120 printf("Connected to target database using dsn = %s\n", dsn);
122 fprintf(stderr, "Failed to connect to target database\n");
127 retValue=SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
128 if (retValue) exit(1);
130 sprintf(stmtBuf, "SELECT * FROM %s;", tablename);
131 retValue = SQLPrepare (hstmt, (unsigned char *) stmtBuf, SQL_NTS);
132 if (retValue) exit(1);
134 retValue = SQLNumResultCols (hstmt, &totalFields);
135 if (retValue) exit(1);
136 printf("ODBC Retrieved total columns = %d\n", totalFields);
139 UCHAR szColName[IDENTIFIER_LENGTH];
148 icol = 1; cbColNameMax = IDENTIFIER_LENGTH;
149 while (icol <= totalFields) {
150 retValue = SQLDescribeCol(hstmt, icol, szColName, cbColNameMax,
151 &pcbColName, &pfSqlType, &pcbColDef,
152 &pibScale, &pfNullable);
153 if (retValue ) exit (2);
155 printf("Describe Column %s %d %d %d\n", szColName, pcbColName, pfSqlType, pcbColDef);
157 tabDef.addField((char*) szColName, AllDataType::convertFromSQLType(pfSqlType), pcbColDef);
159 rv = dbMgr->createTable(tablename, tabDef);
160 if (rv != OK) { printf("Table creation failed in csql for %s\n", tablename);
161 conn.close(); SQLDisconnect(hdbc); exit (1); }
163 Table *table = dbMgr->openTable(tablename);
164 if (table == NULL){ printf("Table creation failed in csql for %s\n", tablename);
165 conn.close(); SQLDisconnect(hdbc); exit (1); }
166 List fNameList = table->getFieldNameList();
167 ListIterator fNameIter = fNameList.getIterator();
168 FieldInfo *info = new FieldInfo();
169 int fcount =1; void *valBuf; int fieldsize=0;
170 Identifier *elem = NULL;
173 TIMESTAMP_STRUCT sTimestamp;
175 while (fNameIter.hasElement()) {
176 elem = (Identifier*) fNameIter.nextElement();
177 table->getFieldInfo((const char*)elem->name, info);
178 valBuf = AllDataType::alloc(info->type, info->length);
179 table->bindFld(elem->name, valBuf);
184 fieldsize = info->length;
187 //TODO::store valBuf with type in list
188 fieldsize = sizeof(DATE_STRUCT);
192 retValue = SQLBindCol (hstmt, fcount++, AllDataType::convertToSQLType(info->type),
193 valBuf, fieldsize, NULL);
194 if (retValue) exit (12);
197 conn.startTransaction();
198 retValue = SQLExecute (hstmt);
199 if (retValue) exit (13);
202 retValue = SQLFetch (hstmt);
204 //TODO::convert odbc to csql for date, time, timestamp
206 //ACCESSING MEMBERS of above structures...
207 // sDate.year, sDate.month, sDate.day,
208 // sTime.hour, sTime.minute, sTime.second,
209 // sTimestamp.year, sTimestamp.month, sTimestamp.day,
210 //sTimestamp.hour, sTimestamp.minute, sTimestamp.second, sTimestamp.fraction
213 table->insertTuple();
214 printf("Inserted tuple\n");
216 SQLCloseCursor (hstmt);
218 //append the tablename in the cache.table
219 addToCacheTableFile(tablename);
221 SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
222 SQLDisconnect (hdbc);
223 SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
224 SQLFreeHandle (SQL_HANDLE_ENV, henv);