Removing dependency for Cache module in MMDB build
[csql.git] / src / sql / CopyTblStatement.cxx
blob043258077d95791aba9bb5bf74a82c80c5abd180
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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 <os.h>
17 #include <Statement.h>
18 #include <TableImpl.h>
19 #include <OrderTableImpl.h>
21 CopyTblStatement::CopyTblStatement()
23 innerSelStmt = NULL;
24 totalFields = 0;
25 bindFieldValues= NULL;
28 CopyTblStatement::~CopyTblStatement()
30 if (table) { table->close(); table = NULL; }
31 SelStatement *stmt= (SelStatement *)innerSelStmt;
32 stmt->close();
33 if(totalFields){
34 for(int i=0;i<totalFields;i++)
36 free(bindFieldValues[i]);
38 free(bindFieldValues); bindFieldValues = NULL;
40 totalFields =0;
41 delete innerSelStmt;
44 DbRetVal CopyTblStatement::resolve()
46 DbRetVal rv = OK;
47 strcpy(tblName,parsedData->getPKTableName());
48 SelStatement *sStmt = new SelStatement();
49 innerSelStmt = ( Statement *) sStmt;
50 innerSelStmt->setDbMgr(dbMgr);
51 innerSelStmt->setParsedData(parsedData);
52 innerSelStmt->resolve();
53 if(parsedData->getCreateTbl())
54 rv = resolveForCreate();
55 else
56 rv = resolveForInsert();
57 return rv;
60 DbRetVal CopyTblStatement::resolveForInsert()
62 table = dbMgr->openTable(tblName);
63 if (table == NULL)
65 printError(ErrNotExists, "Unable to open the table:Table not exists");
66 return ErrNotExists;
68 table->setLoading(true);
69 //bind field
70 List fNameList = table->getFieldNameList();
71 ListIterator fNameIter = fNameList.getIterator();
72 FieldInfo *info = new FieldInfo();
73 int fcount = 1; void *valBuf; int fieldsize=0;
74 Identifier *elem = NULL;
75 totalFields = fNameList.size();
76 bindFieldValues = (void**) malloc( totalFields * sizeof(char*));
77 memset(bindFieldValues, 0, totalFields * sizeof(char*));
78 SelStatement *selStmt = (SelStatement*) innerSelStmt;
79 while (fNameIter.hasElement())
81 elem = (Identifier*) fNameIter.nextElement();
82 table->getFieldInfo((const char*)elem->name, info);
83 //TODO :: Should free
84 valBuf = AllDataType::alloc(info->type, info->length);
85 table->bindFld(elem->name, valBuf);
86 fieldsize=0;
87 selStmt->setBindField(fcount, valBuf);
88 bindFieldValues[fcount-1] = valBuf;
89 fcount++;
91 delete info;
92 fNameIter.reset();
93 while (fNameIter.hasElement())
95 delete (Identifier*) fNameIter.nextElement();
97 fNameList.reset();
98 return OK;
101 DbRetVal CopyTblStatement::resolveForCreate()
103 DbRetVal rv = OK;
104 int nop = 0;
105 SelStatement *stmt = (SelStatement*) innerSelStmt;
106 nop = stmt->noOfProjFields();
107 FieldInfo *field = new FieldInfo();
108 TableDef tabDef;
109 char name[IDENTIFIER_LENGTH];
110 for(int i=0; i<nop; i++)
112 rv = stmt->getProjFldInfo(i+1,field);
113 Table::getFieldNameAlone(field->fldName, name);
114 tabDef.addField(name,field->type, field->length, field->defaultValueBuf, field->isNull);
116 delete field;
117 rv = dbMgr->createTable(tblName, tabDef);
118 if(rv!=OK){
119 return rv;
121 rv = resolveForInsert();
122 return rv;
125 DbRetVal CopyTblStatement::execute(int &rowsAffected)
127 DbRetVal rv = OK;
128 SelStatement *selStmt = (SelStatement*) innerSelStmt;
129 selStmt->execute(rowsAffected);
130 while(selStmt->fetch())
132 rv = table->insertTuple();
133 if(rv!=OK) break;
135 if( (rv!=OK) && ( !parsedData->getCreateTbl()))
137 dbMgr->dropTable(tblName);
139 return rv;
142 DbRetVal CopyTblStatement::setShortParam(int paramNo, short value)
144 SelStatement *stmt = (SelStatement*) innerSelStmt;
145 return stmt->setShortParam(paramNo,value);
148 DbRetVal CopyTblStatement::setIntParam(int paramNo, int value)
150 SelStatement *stmt = (SelStatement*) innerSelStmt;
151 return stmt->setIntParam(paramNo,value);
154 DbRetVal CopyTblStatement::setLongParam(int paramNo, long value)
156 SelStatement *stmt = (SelStatement*) innerSelStmt;
157 return stmt->setLongParam(paramNo,value);
160 DbRetVal CopyTblStatement::setLongLongParam(int paramNo, long long value)
162 SelStatement *stmt = (SelStatement*) innerSelStmt;
163 return stmt->setLongLongParam(paramNo,value);
166 DbRetVal CopyTblStatement::setByteIntParam(int paramNo, ByteInt value)
168 SelStatement *stmt = (SelStatement*) innerSelStmt;
169 return stmt->setByteIntParam(paramNo,value);
172 DbRetVal CopyTblStatement::setFloatParam(int paramNo, float value)
174 SelStatement *stmt = (SelStatement*) innerSelStmt;
175 return stmt->setFloatParam(paramNo,value);
178 DbRetVal CopyTblStatement::setDoubleParam(int paramNo, double value)
180 SelStatement *stmt = (SelStatement*) innerSelStmt;
181 return stmt->setDoubleParam(paramNo,value);
184 DbRetVal CopyTblStatement::setStringParam(int paramNo, char *value)
186 SelStatement *stmt = (SelStatement*) innerSelStmt;
187 return stmt->setStringParam(paramNo,value);
190 DbRetVal CopyTblStatement::setDateParam(int paramNo, Date value)
192 SelStatement *stmt = (SelStatement*) innerSelStmt;
193 return stmt->setDateParam(paramNo,value);
196 DbRetVal CopyTblStatement::setTimeParam(int paramNo, Time value)
198 SelStatement *stmt = (SelStatement*) innerSelStmt;
199 return stmt->setTimeParam(paramNo,value);
202 DbRetVal CopyTblStatement::setTimeStampParam(int paramNo, TimeStamp value)
204 SelStatement *stmt = (SelStatement*) innerSelStmt;
205 return stmt->setTimeStampParam(paramNo,value);
208 DbRetVal CopyTblStatement::setBinaryParam(int paramNo, void *value, int length)
210 SelStatement *stmt = (SelStatement*) innerSelStmt;
211 return stmt->setBinaryParam(paramNo,value,length);
214 void* CopyTblStatement::getParamValuePtr(int pos)
216 SelStatement *stmt = (SelStatement*) innerSelStmt;
217 return stmt->getParamValuePtr(pos);
220 int CopyTblStatement::getFldPos(char *name)
222 return 0;
225 DbRetVal CopyTblStatement::getParamFldInfo(int paramPos, FieldInfo *&info)
227 DbRetVal rv = OK;
228 SelStatement *stmt = (SelStatement*) innerSelStmt;
229 rv = stmt->getParamFldInfo(paramPos, info);
230 return rv;
232 DbRetVal CopyTblStatement::setParam(int paramNo, void *value)
234 DbRetVal rv = OK;
235 SelStatement *stmt = (SelStatement*) innerSelStmt;
236 rv = stmt->setParam(paramNo,value);
237 return rv;