1 /***************************************************************************
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
5 ***************************************************************************/
7 #ifndef ABSSQLSTATEMENT_H
8 #define ABSSQLSTATEMENT_H
9 #include<AbsSqlConnection.h>
28 CompactTableStatement
,
37 * @class AbsSqlStatement
39 * @brief Handle to the sql statement.
40 * It is used to execute queries and return the values from the database<br>
41 * Sql Statement is fed to the prepare method first and then it should be executed. <br/>
42 * Functionality: <br/>
43 * 1.Input values for insert statement <br/>
44 * 2.Iterator for retrieving rows from the table <br/>
45 * 3.Parameter support for performance.<br/>
53 AbsSqlStatement
*innerStmt
;
54 AbsSqlConnection
*con
;
56 void setInnerStatement(AbsSqlStatement
*stmt
) { innerStmt
= stmt
; }
57 AbsSqlStatement
*getInnerStatement() { return innerStmt
; }
58 /** sets connection handle to be used for subsequent operations
59 * @param con SqlConnection*
61 virtual void setConnection(AbsSqlConnection
*conn
) { con
= conn
; }
63 /** compiles the sql statement. It calls the parser and tokenizes the statement
64 * into logical plan. This method sets the statement string which needs to be executed.
65 * free method needs to be called, if application wants to use the same handle to compile
66 * another sql statement.
67 * @param stmt sql statement string
70 virtual DbRetVal
prepare(char *stmt
) = 0;
72 /** Executes directly for non parameterized statements **/
73 virtual DbRetVal
executeDirect(char *stmt
) = 0;
75 /** Retrieves the tablename of the prepared statement
76 * Used internally to get the tablename of the non select DML stmts
77 * @returns char* tablename
79 virtual char* getTableName()=0;
81 /** executes the sql statement. For insert, update, delete queries execute performs the
82 * required operation on the table.
83 * For Select queries, application should call execute before they start fetching
84 * the values from the table.This starts scan on the table.
85 * @param rowsAffect number of rows affected by the sql statement
88 virtual DbRetVal
execute(int &rowsAffect
)=0;
90 /**fetches the next tuple from the result of the execution of sql select query.
91 * execute should be called before calling this method. Application buffer should be
92 * binded to get the tuple values.
93 * @returns void* NULL if there is no tuple.
95 virtual void* fetch() = 0;
96 virtual void* fetch(DbRetVal
&rv
) = 0;
98 /**fetches the next tuple from the result of the execution of sql select query
99 * and prints it to stdout.
100 * execute should be called before calling this method.
101 * @returns void* NULL if there is no tuple.
103 virtual void* fetchAndPrint(bool SQL
) = 0;
106 /** binds application buffer to the specified parameter position in the sql statement.
107 * This method should be called for all the parameters in the sql statement.
108 * Parameters shall be specified for predicate for select, update, delete statements.
109 * Parameters shall be specified for field list value in SET of update statements.
110 * If value is not set for all parameters, execute will return error.
112 * @param pos position of the parameter in the statement
113 * @param val address of the application buffer. Memory should be allocated by
114 * the application before binding the buffer.
116 virtual DbRetVal
bindParam(int pos
, void*) = 0;
118 /** binds application buffer to the specified field position of the projection list
119 * in the select query or for fields in the insert statement.
120 * This method should be called for select queries, insert, update statements.
121 * Before executing select queries, required fields must be binded first.
122 * Before executing insert statement, required fields must be binded first.
123 * Before executing update statement, required fields to be updated must be
126 * @param pos position in the projection list
127 * @param val address of the application buffer. Memory should be allocated by
128 * the application before binding the buffer.
130 virtual DbRetVal
bindField(int pos
, void* val
) = 0;
132 /** same as fetch, but does not populate bindFieldValues
133 * @returns address void*
135 virtual void* next() = 0;
137 /**Closes the iterator and makes the statement ready for another execution
140 virtual DbRetVal
close() = 0;
142 /** get FieldValue->value ptr after fetch is done.
143 * @returns address void*
145 virtual void* getFieldValuePtr( int pos
) = 0;
146 virtual void* getFieldValuePtr( char *name
) = 0;
148 /**Frees all the resources held for the sql statement. Needs to be called before calling prepare again on the same statement handle.
151 virtual DbRetVal
free() = 0;
153 /**Retrieves the total number of projection fields in the statement
154 * @returns int no of projection fields
156 virtual int noOfProjFields() = 0;
158 /**Retrieves the total number of parameters in the statement
159 * @returns int no of parameters
161 virtual int noOfParamFields() = 0;
163 /**Retrieves the field info for the required projection field position in statement
164 * @param projPos int - projection field position
165 * @param info FieldInfo*& - OUT parameter
168 virtual DbRetVal
getProjFldInfo(int projPos
, FieldInfo
*&info
) = 0;
170 /**Retrieves the field info for the required parameter position in statement
171 * @param projPos int - parameter position
172 * @param info FieldInfo*& - OUT parameter
175 virtual DbRetVal
getParamFldInfo(int paramPos
, FieldInfo
*&info
) = 0;
177 /**Sets the value for the required parameter position in statement
178 * @param paramPos int - parameter position
179 * @param value short - value to be set
181 virtual void setShortParam(int paramPos
, short value
) = 0;
182 /**Sets the value for the required parameter position in statement
183 * @param paramPos int - parameter position
184 * @param value int - value to be set
186 virtual void setIntParam(int paramPos
, int value
) = 0;
187 /**Sets the value for the required parameter position in statement
188 * @param paramPos int - parameter position
189 * @param value long - value to be set
191 virtual void setLongParam(int paramPos
, long value
) =0;
192 /**Sets the value for the required parameter position in statement
193 * @param paramPos int - parameter position
194 * @param value long long - value to be set
196 virtual void setLongLongParam(int paramPos
, long long value
) =0;
197 /**Sets the value for the required parameter position in statement
198 * @param paramPos int - parameter position
199 * @param value ByteInt - value to be set
201 virtual void setByteIntParam(int paramPos
, ByteInt value
) = 0;
202 /**Sets the value for the required parameter position in statement
203 * @param paramPos int - parameter position
204 * @param value float - value to be set
206 virtual void setFloatParam(int paramPos
, float value
) = 0;
207 /**Sets the value for the required parameter position in statement
208 * @param paramPos int - parameter position
209 * @param value double - value to be set
211 virtual void setDoubleParam(int paramPos
, double value
) = 0;
212 /**Sets the value for the required parameter position in statement
213 * @param paramPos int - parameter position
214 * @param value char* - value to be set
216 virtual void setStringParam(int paramPos
, char *value
) = 0;
217 /**Sets the value for the required parameter position in statement
218 * @param paramPos int - parameter position
219 * @param value Date - value to be set
221 virtual void setDateParam(int paramPos
, Date value
) = 0;
222 /**Sets the value for the required parameter position in statement
223 * @param paramPos int - parameter position
224 * @param value Time - value to be set
226 virtual void setTimeParam(int paramPos
, Time value
) = 0;
227 /**Sets the value for the required parameter position in statement
228 * @param paramPos int - parameter position
229 * @param value TimeStamp - value to be set
231 virtual void setTimeStampParam(int paramPos
, TimeStamp value
) = 0;
232 /**Sets the value for the required parameter position in statement
233 * @param paramPos int - parameter position
234 * @param value Binary - value to be set
236 virtual void setBinaryParam(int paramPos
, void *value
, int length
) = 0;
238 /**Returns whether the statement prepared is select statement
239 * @return bool true if it is select stmt, false otherwise
241 virtual List
getTableNameList(){ List dummy
; return dummy
;}
242 virtual int getNoOfPagesForTable(char *tblName
)=0;
243 virtual DbRetVal
loadRecords(char *tblName
, void *buf
)=0;
244 virtual ResultSetPlan
getResultSetPlan()=0;
245 virtual bool isSelect() = 0;
246 virtual bool isFldNull(int pos
)=0;
247 virtual bool isFldNull(char *name
)=0;
248 virtual void setNull(int pos
)=0;
249 virtual int getFldPos(char *name
)=0;
250 virtual List
getAllTableNames(DbRetVal
&ret
)=0;
251 virtual List
getAllUserNames(DbRetVal
&ret
)=0;
252 virtual StatementType
getStmtType()=0;
253 virtual void getProjFieldType( int *data
)=0;
254 virtual ~AbsSqlStatement()
256 if (innerStmt
) { delete innerStmt
; innerStmt
= NULL
; }
260 //used to store the binded field values and parameters from derived clases of
261 //AbsSqlStatement class
265 char fName
[IDENTIFIER_LENGTH
];
269 char defaultValueBuf
[DEFAULT_VALUE_BUF_LENGTH
];
276 BindSqlField(){ value
= NULL
; targetvalue
= NULL
; }
279 class BindSqlProjectField
282 char fName
[IDENTIFIER_LENGTH
];
286 char defaultValueBuf
[DEFAULT_VALUE_BUF_LENGTH
];
295 BindSqlProjectField()
297 value
= NULL
; targetvalue
= NULL
; isFreed
= false; aType
= AGG_UNKNOWN
;
311 AbsSqlStatement
*stmt
;