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 ***************************************************************************/
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");
30 int main(int argc
, char **argv
)
32 char username
[IDENTIFIER_LENGTH
];
34 char password
[IDENTIFIER_LENGTH
];
37 char tablename
[IDENTIFIER_LENGTH
];
38 while ((c
= getopt(argc
, argv
, "u:p:t:?")) != EOF
)
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
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;";
65 SQLSMALLINT outstrlen
;
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
,
79 if (SQL_SUCCEEDED(retValue
)) {
80 printf("Connected to target database using dsn = %s\n", dsn
);
82 fprintf(stderr
, "Failed to connect to target database\n");
85 retValue
=SQLAllocHandle (SQL_HANDLE_STMT
, hdbc
, &hstmt
);
86 if (retValue
) exit(1);
88 sprintf(stmtBuf
, "SELECT * FROM %s;", tablename
);
89 SQLPrepare (hstmt
, (unsigned char *) stmtBuf
, SQL_NTS
);
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
;
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
);
116 fieldsize
= info
->length
;
119 //TODO::store valBuf with type in list
120 fieldsize
= sizeof(DATE_STRUCT
);
124 retValue
= SQLBindCol (hstmt
, fcount
++, AllDataType::convertToSQLType(info
->type
),
125 valBuf
, fieldsize
, NULL
);
126 if (retValue
) exit (2);
129 conn
.startTransaction();
130 retValue
= SQLExecute (hstmt
);
131 if (retValue
) exit (3);
134 retValue
= SQLFetch (hstmt
);
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
);
151 SQLFreeHandle (SQL_HANDLE_STMT
, hstmt
);
152 SQLDisconnect (hdbc
);
153 SQLFreeHandle (SQL_HANDLE_DBC
, hdbc
);
154 SQLFreeHandle (SQL_HANDLE_ENV
, henv
);