adding support for mutex from D flag
[csql.git] / include / SqlStatement.h
blob017c037a1f45fef3925ba9807e8f936b4eeebbff
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 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifndef SQLSTATEMENT_H
21 #define SQLSTATEMENT_H
22 #include <AbsSqlStatement.h>
23 #include <SqlConnection.h>
24 #include <SqlFactory.h>
25 #include "Statement.h"
26 //#include<CSql.h>
27 class Statement;
28 class ParsedData;
30 class DllExport SqlStatement: public AbsSqlStatement
32 public:
33 SqlStatement();
34 ~SqlStatement();
35 /** sets connection handle to be used for subsequent operations
36 * @param con SqlConnection*
38 void setConnection(AbsSqlConnection *con);
39 void setSqlConnection(SqlConnection *con);
41 /** compiles the sql statement. It calls the parser and tokenizes the statement
42 * into logical plan. This method sets the statement string which needs to be executed.
43 * free method needs to be called, if application wants to use the same handle to compile
44 * another sql statement.
45 * @param stmt sql statement string
46 * @returns DbRetVal
48 DbRetVal prepare(char *stmt);
50 /** Executes directly for the non parameterized statements **/
51 DbRetVal executeDirect(char *stmt);
53 char* getTableName();
55 /** executes the sql statement. For insert, update, delete queries execute performs the
56 * required operation on the table.
57 * For Select queries, application should call execute before they start fetching
58 * the values from the table.This starts scan on the table.
59 * @param rowsAffect number of rows affected by the sql statement
60 * @returns DbRetVal
62 DbRetVal execute(int &rowsAffect);
64 /**fetches the next tuple from the result of the execution of sql select query.
65 * execute should be called before calling this method. Application buffer should be
66 * binded to get the tuple values.
67 * @returns void* NULL if there is no tuple.
69 void* fetch();
70 void* fetch(DbRetVal &rv);
73 /**fetches the next tuple from the result of the execution of sql select query
74 * and prints it to stdout.
75 * execute should be called before calling this method.
76 * @returns void* NULL if there is no tuple.
78 void* fetchAndPrint(bool SQL);
81 /** binds application buffer to the specified parameter position in the sql statement.
82 * This method should be called for all the parameters in the sql statement.
83 * Parameters shall be specified for predicate for select, update, delete statements.
84 * Parameters shall be specified for field list value in SET of update statements.
85 * If value is not set for all parameters, execute will return error.
86 * <br/>
87 * @param pos position of the parameter in the statement
88 * @param val address of the application buffer. Memory should be allocated by
89 * the application before binding the buffer.
91 DbRetVal bindParam(int pos, void*);
93 /** binds application buffer to the specified field position of the projection list
94 * in the select query or for fields in the insert statement.
95 * This method should be called for select queries, insert, update statements.
96 * Before executing select queries, required fields must be binded first.
97 * Before executing insert statement, required fields must be binded first.
98 * Before executing update statement, required fields to be updated must be
99 * binded first.
100 * <br/>
101 * @param pos position in the projection list
102 * @param val address of the application buffer. Memory should be allocated by
103 * the application before binding the buffer.
105 DbRetVal bindField(int pos, void* val);
107 /** same as fetch, but does not populate bindFieldValues
108 * @returns address void*
110 void* next();
112 /**Closes the iterator and makes the statement ready for another execution
113 * @returns DbRetVal
115 DbRetVal close();
117 /** get FieldValue->value ptr after fetch is done.
118 * @returns address void*
120 void* getFieldValuePtr( int pos );
121 void* getFieldValuePtr( char *name );
123 /**Frees all the resources held for the sql statement. Needs to be called before calling prepare again on the same statement handle.
124 * @returns DbRetVal
126 DbRetVal free();
128 /**Retrieves the total number of projection fields in the statement
129 * @returns int no of projection fields
131 int noOfProjFields();
133 /** get Param value pointer after fetch is done.
134 * @returns address void*
136 void* getParamValuePtr( int pos );
138 /** get FieldValue->type
139 * @returns DataType
141 DataType getFieldType( int pos );
143 /** get FieldValue->length
144 * @returns int
146 int getFieldLength( int pos );
148 /** get FieldName
149 * @returns address char*
151 char* getFieldName ( int pos );
153 /**Retrieves the total number of parameters in the statement
154 * @returns int no of parameters
156 int noOfParamFields();
158 /**Retrieves the field info for the required projection field position in statement
159 * @param projPos int - projection field position
160 * @param info FieldInfo*& - OUT parameter
161 * @returns DbRetVal
163 DbRetVal getProjFldInfo(int projPos, FieldInfo *&info);
165 /**Retrieves the field info for the required parameter position in statement
166 * @param projPos int - parameter position
167 * @param info FieldInfo*& - OUT parameter
168 * @returns DbRetVal
170 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
172 /**Sets the value for the required parameter position in statement
173 * @param paramPos int - parameter position
174 * @param value short - value to be set
176 void setShortParam(int paramPos, short value);
177 /**Sets the value for the required parameter position in statement
178 * @param paramPos int - parameter position
179 * @param value int - value to be set
181 void setIntParam(int paramPos, int value);
182 /**Sets the value for the required parameter position in statement
183 * @param paramPos int - parameter position
184 * @param value long - value to be set
186 void setLongParam(int paramPos, long value);
187 /**Sets the value for the required parameter position in statement
188 * @param paramPos int - parameter position
189 * @param value long long - value to be set
191 void setLongLongParam(int paramPos, long long value);
192 /**Sets the value for the required parameter position in statement
193 * @param paramPos int - parameter position
194 * @param value ByteInt - value to be set
196 void setByteIntParam(int paramPos, ByteInt value);
197 /**Sets the value for the required parameter position in statement
198 * @param paramPos int - parameter position
199 * @param value float - value to be set
201 void setFloatParam(int paramPos, float value);
202 /**Sets the value for the required parameter position in statement
203 * @param paramPos int - parameter position
204 * @param value double - value to be set
206 void setDoubleParam(int paramPos, double value);
207 /**Sets the value for the required parameter position in statement
208 * @param paramPos int - parameter position
209 * @param value char* - value to be set
211 void setStringParam(int paramPos, char *value);
212 /**Sets the value for the required parameter position in statement
213 * @param paramPos int - parameter position
214 * @param value Date - value to be set
216 void setDateParam(int paramPos, Date value);
217 /**Sets the value for the required parameter position in statement
218 * @param paramPos int - parameter position
219 * @param value Time - value to be set
221 void setTimeParam(int paramPos, Time value);
222 /**Sets the value for the required parameter position in statement
223 * @param paramPos int - parameter position
224 * @param value TimeStamp - value to be set
226 void setTimeStampParam(int paramPos, TimeStamp value);
227 /**Sets the value for the required parameter position in statement
228 * @param paramPos int - parameter position
229 * @param value Binary - value to be set
231 void setBinaryParam(int paramPos, void *value, int length);
233 /**Returns whether the statement prepared is select statement
234 * @return bool true if it is select stmt, false otherwise
236 bool isSelect();
238 /**Returns whether the statement is prepared or not
239 * @return bool true if it is prepared, false otherwise
241 List getTableNameList();
242 List getFieldNameList(const char *tblName, DbRetVal &rv);
243 DbRetVal getFieldInfo(const char *tblName, const char *fldName, FieldInfo *&info);
244 bool isPrepared();
245 StatementType getStmtType() { return pData.getStmtType(); }
246 bool isFldNull(int pos);
247 bool isFldNull(char *name);
248 void setNull(int pos);
249 int getFldPos(char *name);
250 ResultSetPlan getResultSetPlan(){ return stmt->getResultSetPlan();}
251 List getAllTableNames(DbRetVal &ret);
252 List getAllUserNames(DbRetVal &ret);
253 int getNoOfPagesForTable(char *tblName);
254 DbRetVal loadRecords(char *tblName, void *buffer);
255 DbRetVal pasteRecords(char *tblName, void *buffer);
256 void setLoading(bool flag);
257 void getProjFieldType(int *data);
258 void setCachedStmt(bool flag){ isCachedStmt= flag; }
259 void flushCacheStmt();
260 long long getLastInsertedVal(DbRetVal &rv);
261 void setStmtString(char *ststr);
262 DbRetVal prepare();
263 DbRetVal prepareInt(char *ststr);
264 void resetStmtString();
265 static void setParamValues(AbsSqlStatement *sqlStmt, int parampos,
266 DataType type, int length, void *value);
267 static void *fillBindBuffer(TDBInfo tdbName, DataType type, void *&valBuf,
268 int length, int nRecords=1);
269 static void addToHashTable(int stmtID, AbsSqlStatement* sHdl,
270 void *stmtBuckets, char *stmtstr);
271 static void removeFromHashTable(int stmtID, void *stmtBuckets);
272 static AbsSqlStatement *getStmtFromHashTable(int stmtId, void *stmtBuckets);
273 static bool isStmtInHashTable(int stmtID, void *stmtBuckets);
274 static void freeAllStmtHandles(void *stmtBuckets);
275 static DbRetVal filterAndWriteStmtLogs(void *stmtBuckets);
276 static DbRetVal readAndPopulateStmts(AbsSqlConnection *conn, void *&stmtBuckets, bool list = false, bool interactive = false);
277 static DbRetVal iterateStmtLogs(AbsSqlConnection *conn, void *startAddr, int size, void *stmtBuckets, bool list=false, bool interactive = false);
278 void setDontCache(bool dntCache) { dontCache = dntCache; }
279 bool getDontCache() { return dontCache; }
280 private:
281 bool dontCache;
282 bool isMgmtStatement;
283 SqlConnection *sqlCon;
284 Statement *stmt;
285 ParsedData pData;
286 bool isPrepd;
287 bool isCachedStmt;
288 char *sqlStmtString;
289 friend class SqlFactory;
292 #endif