allocator fixes
[csql.git] / include / AbsSqlStatement.h
blobeafb5e8ac44578dceae94c213e0e926a24dad3c6
1 /***************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #ifndef ABSSQLSTATEMENT_H
8 #define ABSSQLSTATEMENT_H
9 #include<AbsSqlConnection.h>
10 #include<Info.h>
11 #include<DataType.h>
12 class Statement;
13 class ParsedData;
14 #ifndef STMT_TYPE
15 #define STMT_TYPE
16 enum StatementType
18 UnknownStatement,
19 SelectStatement,
20 InsertStatement,
21 UpdateStatement,
22 DeleteStatement,
23 CreateTableStatement,
24 DropTableStatement,
25 CreateIndexStatement,
26 DropIndexStatement,
27 CacheTableStatement,
28 CompactTableStatement,
29 CopyTableStatement,
30 MetaStatement,
31 UserStatement,
32 MgmtStatement
34 #endif
37 /**
38 * @class AbsSqlStatement
40 * @brief Handle to the sql statement.
41 * It is used to execute queries and return the values from the database<br>
42 * Sql Statement is fed to the prepare method first and then it should be executed. <br/>
43 * Functionality: <br/>
44 * 1.Input values for insert statement <br/>
45 * 2.Iterator for retrieving rows from the table <br/>
46 * 3.Parameter support for performance.<br/>
47 * <br/>
50 class AbsSqlStatement
52 protected:
53 AbsSqlStatement(){}
54 AbsSqlStatement *innerStmt;
55 AbsSqlConnection *con;
56 public:
57 void setInnerStatement(AbsSqlStatement *stmt) { innerStmt = stmt; }
58 AbsSqlStatement *getInnerStatement() { return innerStmt; }
59 /** sets connection handle to be used for subsequent operations
60 * @param con SqlConnection*
62 virtual void setConnection(AbsSqlConnection *conn) { con = conn; }
64 /** compiles the sql statement. It calls the parser and tokenizes the statement
65 * into logical plan. This method sets the statement string which needs to be executed.
66 * free method needs to be called, if application wants to use the same handle to compile
67 * another sql statement.
68 * @param stmt sql statement string
69 * @returns DbRetVal
71 virtual DbRetVal prepare(char *stmt) = 0;
73 /** Executes directly for non parameterized statements **/
74 virtual DbRetVal executeDirect(char *stmt) = 0;
76 /** Retrieves the tablename of the prepared statement
77 * Used internally to get the tablename of the non select DML stmts
78 * @returns char* tablename
80 virtual char* getTableName()=0;
82 /** executes the sql statement. For insert, update, delete queries execute performs the
83 * required operation on the table.
84 * For Select queries, application should call execute before they start fetching
85 * the values from the table.This starts scan on the table.
86 * @param rowsAffect number of rows affected by the sql statement
87 * @returns DbRetVal
89 virtual DbRetVal execute(int &rowsAffect)=0;
91 /**fetches the next tuple from the result of the execution of sql select query.
92 * execute should be called before calling this method. Application buffer should be
93 * binded to get the tuple values.
94 * @returns void* NULL if there is no tuple.
96 virtual void* fetch() = 0;
97 virtual void* fetch(DbRetVal &rv) = 0;
99 /**fetches the next tuple from the result of the execution of sql select query
100 * and prints it to stdout.
101 * execute should be called before calling this method.
102 * @returns void* NULL if there is no tuple.
104 virtual void* fetchAndPrint(bool SQL) = 0;
107 /** binds application buffer to the specified parameter position in the sql statement.
108 * This method should be called for all the parameters in the sql statement.
109 * Parameters shall be specified for predicate for select, update, delete statements.
110 * Parameters shall be specified for field list value in SET of update statements.
111 * If value is not set for all parameters, execute will return error.
112 * <br/>
113 * @param pos position of the parameter in the statement
114 * @param val address of the application buffer. Memory should be allocated by
115 * the application before binding the buffer.
117 virtual DbRetVal bindParam(int pos, void*) = 0;
119 /** binds application buffer to the specified field position of the projection list
120 * in the select query or for fields in the insert statement.
121 * This method should be called for select queries, insert, update statements.
122 * Before executing select queries, required fields must be binded first.
123 * Before executing insert statement, required fields must be binded first.
124 * Before executing update statement, required fields to be updated must be
125 * binded first.
126 * <br/>
127 * @param pos position in the projection list
128 * @param val address of the application buffer. Memory should be allocated by
129 * the application before binding the buffer.
131 virtual DbRetVal bindField(int pos, void* val) = 0;
133 /** same as fetch, but does not populate bindFieldValues
134 * @returns address void*
136 virtual void* next() = 0;
138 /**Closes the iterator and makes the statement ready for another execution
139 * @returns DbRetVal
141 virtual DbRetVal close() = 0;
143 /** get FieldValue->value ptr after fetch is done.
144 * @returns address void*
146 virtual void* getFieldValuePtr( int pos ) = 0;
147 virtual void* getFieldValuePtr( char *name ) = 0;
149 /**Frees all the resources held for the sql statement. Needs to be called before calling prepare again on the same statement handle.
150 * @returns DbRetVal
152 virtual DbRetVal free() = 0;
154 /**Retrieves the total number of projection fields in the statement
155 * @returns int no of projection fields
157 virtual int noOfProjFields() = 0;
159 /**Retrieves the total number of parameters in the statement
160 * @returns int no of parameters
162 virtual int noOfParamFields() = 0;
164 /**Retrieves the field info for the required projection field position in statement
165 * @param projPos int - projection field position
166 * @param info FieldInfo*& - OUT parameter
167 * @returns DbRetVal
169 virtual DbRetVal getProjFldInfo(int projPos, FieldInfo *&info) = 0;
171 /**Retrieves the field info for the required parameter position in statement
172 * @param projPos int - parameter position
173 * @param info FieldInfo*& - OUT parameter
174 * @returns DbRetVal
176 virtual DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info) = 0;
178 /**Sets the value for the required parameter position in statement
179 * @param paramPos int - parameter position
180 * @param value short - value to be set
182 virtual void setShortParam(int paramPos, short value) = 0;
183 /**Sets the value for the required parameter position in statement
184 * @param paramPos int - parameter position
185 * @param value int - value to be set
187 virtual void setIntParam(int paramPos, int value) = 0;
188 /**Sets the value for the required parameter position in statement
189 * @param paramPos int - parameter position
190 * @param value long - value to be set
192 virtual void setLongParam(int paramPos, long value) =0;
193 /**Sets the value for the required parameter position in statement
194 * @param paramPos int - parameter position
195 * @param value long long - value to be set
197 virtual void setLongLongParam(int paramPos, long long value) =0;
198 /**Sets the value for the required parameter position in statement
199 * @param paramPos int - parameter position
200 * @param value ByteInt - value to be set
202 virtual void setByteIntParam(int paramPos, ByteInt value) = 0;
203 /**Sets the value for the required parameter position in statement
204 * @param paramPos int - parameter position
205 * @param value float - value to be set
207 virtual void setFloatParam(int paramPos, float value) = 0;
208 /**Sets the value for the required parameter position in statement
209 * @param paramPos int - parameter position
210 * @param value double - value to be set
212 virtual void setDoubleParam(int paramPos, double value) = 0;
213 /**Sets the value for the required parameter position in statement
214 * @param paramPos int - parameter position
215 * @param value char* - value to be set
217 virtual void setStringParam(int paramPos, char *value) = 0;
218 /**Sets the value for the required parameter position in statement
219 * @param paramPos int - parameter position
220 * @param value Date - value to be set
222 virtual void setDateParam(int paramPos, Date value) = 0;
223 /**Sets the value for the required parameter position in statement
224 * @param paramPos int - parameter position
225 * @param value Time - value to be set
227 virtual void setTimeParam(int paramPos, Time value) = 0;
228 /**Sets the value for the required parameter position in statement
229 * @param paramPos int - parameter position
230 * @param value TimeStamp - value to be set
232 virtual void setTimeStampParam(int paramPos, TimeStamp value) = 0;
233 /**Sets the value for the required parameter position in statement
234 * @param paramPos int - parameter position
235 * @param value Binary - value to be set
237 virtual void setBinaryParam(int paramPos, void *value, int length) = 0;
239 /**Returns whether the statement prepared is select statement
240 * @return bool true if it is select stmt, false otherwise
242 virtual List getTableNameList(){ List dummy; return dummy;}
243 virtual int getNoOfPagesForTable(char *tblName)=0;
244 virtual DbRetVal loadRecords(char *tblName, void *buf)=0;
245 virtual ResultSetPlan getResultSetPlan()=0;
246 virtual bool isSelect() = 0;
247 virtual bool isFldNull(int pos)=0;
248 virtual bool isFldNull(char *name)=0;
249 virtual void setNull(int pos)=0;
250 virtual int getFldPos(char *name)=0;
251 virtual List getAllTableNames(DbRetVal &ret)=0;
252 virtual List getAllUserNames(DbRetVal &ret)=0;
253 virtual StatementType getStmtType()=0;
254 virtual void getProjFieldType( int *data )=0;
255 virtual long long getLastInsertedVal(DbRetVal &ret)=0;
256 virtual ~AbsSqlStatement()
258 if (innerStmt) { delete innerStmt; innerStmt = NULL; }
262 //used to store the binded field values and parameters from derived clases of
263 //AbsSqlStatement class
264 class BindSqlField
266 public:
267 char fName[IDENTIFIER_LENGTH];
268 DataType type;
269 int length;
270 int offset;
271 char defaultValueBuf[DEFAULT_VALUE_BUF_LENGTH];
272 bool isNull;
273 bool isPrimary;
274 bool isDefault;
275 bool isUnique;
276 void *value;
277 void *targetvalue;
278 BindSqlField(){ value = NULL; targetvalue = NULL; }
281 class BindSqlProjectField
283 public:
284 char fName[IDENTIFIER_LENGTH];
285 DataType type;
286 int length;
287 int offset;
288 char defaultValueBuf[DEFAULT_VALUE_BUF_LENGTH];
289 AggType aType;
290 bool isNull;
291 bool isPrimary;
292 bool isDefault;
293 bool isUnique;
294 bool isFreed;
295 void *value;
296 void *targetvalue;
297 void *jdbcBindValue; //For DATE ,TIME, TIMESTAMP
298 BindSqlProjectField()
300 value = NULL; targetvalue = NULL; jdbcBindValue = NULL; isFreed = false; aType = AGG_UNKNOWN;
304 class StmtBucket
306 public:
307 List bucketList;
310 class StmtNode
312 public:
313 int stmtId;
314 AbsSqlStatement *stmt;
315 char stmtstr[1024];
317 #endif