using tree index
[csql.git] / include / AbsSqlStatement.h
blobb0a56fe730d9967fb0d41e3d451ef88e2e3148ac
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 ABSSQLSTATEMENT_H
21 #define ABSSQLSTATEMENT_H
22 //#include <SqlConnection.h>
23 //#include "Statement.h"
24 //#include<CSql.h>
25 #include<AbsSqlConnection.h>
26 //#include <SqlStatement.h>
27 class Statement;
28 class ParsedData;
30 /**
31 * @class AbsSqlStatement
33 * @brief Handle to the sql statement.
34 * It is used to execute queries and return the values from the database<br>
35 * Sql Statement is fed to the prepare method first and then it should be executed. <br/>
36 * Functionality: <br/>
37 * 1.Input values for insert statement <br/>
38 * 2.Iterator for retrieving rows from the table <br/>
39 * 3.Parameter support for performance.<br/>
40 * <br/>
43 class AbsSqlStatement
45 protected:
46 AbsSqlStatement(){}
47 AbsSqlStatement *innerStmt;
48 AbsSqlConnection *con;
49 public:
50 void setInnerStatement(AbsSqlStatement *stmt) { innerStmt = stmt; }
51 /** sets connection handle to be used for subsequent operations
52 * @param con SqlConnection*
54 virtual void setConnection(AbsSqlConnection *conn) { con = conn; }
56 /** compiles the sql statement. It calls the parser and tokenizes the statement
57 * into logical plan. This method sets the statement string which needs to be executed.
58 * free method needs to be called, if application wants to use the same handle to compile
59 * another sql statement.
60 * @param stmt sql statement string
61 * @returns DbRetVal
63 virtual DbRetVal prepare(char *stmt) = 0;
65 /** Retrieves the tablename of the prepared statement
66 * Used internally to get the tablename of the non select DML stmts
67 * @returns char* tablename
69 virtual char* getTableName(){ return NULL; }
71 /** executes the sql statement. For insert, update, delete queries execute performs the
72 * required operation on the table.
73 * For Select queries, application should call execute before they start fetching
74 * the values from the table.This starts scan on the table.
75 * @param rowsAffect number of rows affected by the sql statement
76 * @returns DbRetVal
78 virtual DbRetVal execute(int &rowsAffect) =0;
80 /**fetches the next tuple from the result of the execution of sql select query.
81 * execute should be called before calling this method. Application buffer should be
82 * binded to get the tuple values.
83 * @returns void* NULL if there is no tuple.
85 virtual void* fetch() = 0;
88 /**fetches the next tuple from the result of the execution of sql select query
89 * and prints it to stdout.
90 * execute should be called before calling this method.
91 * @returns void* NULL if there is no tuple.
93 virtual void* fetchAndPrint(bool SQL) = 0;
96 /** binds application buffer to the specified parameter position in the sql statement.
97 * This method should be called for all the parameters in the sql statement.
98 * Parameters shall be specified for predicate for select, update, delete statements.
99 * Parameters shall be specified for field list value in SET of update statements.
100 * If value is not set for all parameters, execute will return error.
101 * <br/>
102 * @param pos position of the parameter in the statement
103 * @param val address of the application buffer. Memory should be allocated by
104 * the application before binding the buffer.
106 virtual DbRetVal bindParam(int pos, void*) = 0;
108 /** binds application buffer to the specified field position of the projection list
109 * in the select query or for fields in the insert statement.
110 * This method should be called for select queries, insert, update statements.
111 * Before executing select queries, required fields must be binded first.
112 * Before executing insert statement, required fields must be binded first.
113 * Before executing update statement, required fields to be updated must be
114 * binded first.
115 * <br/>
116 * @param pos position in the projection list
117 * @param val address of the application buffer. Memory should be allocated by
118 * the application before binding the buffer.
120 virtual DbRetVal bindField(int pos, void* val) = 0;
122 /** same as fetch, but does not populate bindFieldValues
123 * @returns address void*
125 virtual void* next() = 0;
127 /**Closes the iterator and makes the statement ready for another execution
128 * @returns DbRetVal
130 virtual DbRetVal close() = 0;
132 /** get FieldValue->value ptr after fetch is done.
133 * @returns address void*
135 virtual void* getFieldValuePtr( int pos ) = 0;
137 /**Frees all the resources held for the sql statement. Needs to be called before calling prepare again on the same statement handle.
138 * @returns DbRetVal
140 virtual DbRetVal free() = 0;
142 /**Retrieves the total number of projection fields in the statement
143 * @returns int no of projection fields
145 virtual int noOfProjFields() = 0;
147 /**Retrieves the total number of parameters in the statement
148 * @returns int no of parameters
150 virtual int noOfParamFields() = 0;
152 /**Retrieves the field info for the required projection field position in statement
153 * @param projPos int - projection field position
154 * @param info FieldInfo*& - OUT parameter
155 * @returns DbRetVal
157 virtual DbRetVal getProjFldInfo(int projPos, FieldInfo *&info) = 0;
159 /**Retrieves the field info for the required parameter position in statement
160 * @param projPos int - parameter position
161 * @param info FieldInfo*& - OUT parameter
162 * @returns DbRetVal
164 virtual DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info) = 0;
166 /**Sets the value for the required parameter position in statement
167 * @param paramPos int - parameter position
168 * @param value short - value to be set
170 virtual void setShortParam(int paramPos, short value) = 0;
171 /**Sets the value for the required parameter position in statement
172 * @param paramPos int - parameter position
173 * @param value int - value to be set
175 virtual void setIntParam(int paramPos, int value) = 0;
176 /**Sets the value for the required parameter position in statement
177 * @param paramPos int - parameter position
178 * @param value long - value to be set
180 virtual void setLongParam(int paramPos, long value) =0;
181 /**Sets the value for the required parameter position in statement
182 * @param paramPos int - parameter position
183 * @param value long long - value to be set
185 virtual void setLongLongParam(int paramPos, long long value) =0;
186 /**Sets the value for the required parameter position in statement
187 * @param paramPos int - parameter position
188 * @param value ByteInt - value to be set
190 virtual void setByteIntParam(int paramPos, ByteInt value) = 0;
191 /**Sets the value for the required parameter position in statement
192 * @param paramPos int - parameter position
193 * @param value float - value to be set
195 virtual void setFloatParam(int paramPos, float value) = 0;
196 /**Sets the value for the required parameter position in statement
197 * @param paramPos int - parameter position
198 * @param value double - value to be set
200 virtual void setDoubleParam(int paramPos, double value) = 0;
201 /**Sets the value for the required parameter position in statement
202 * @param paramPos int - parameter position
203 * @param value char* - value to be set
205 virtual void setStringParam(int paramPos, char *value) = 0;
206 /**Sets the value for the required parameter position in statement
207 * @param paramPos int - parameter position
208 * @param value Date - value to be set
210 virtual void setDateParam(int paramPos, Date value) = 0;
211 /**Sets the value for the required parameter position in statement
212 * @param paramPos int - parameter position
213 * @param value Time - value to be set
215 virtual void setTimeParam(int paramPos, Time value) = 0;
216 /**Sets the value for the required parameter position in statement
217 * @param paramPos int - parameter position
218 * @param value TimeStamp - value to be set
220 virtual void setTimeStampParam(int paramPos, TimeStamp value) = 0;
221 /**Sets the value for the required parameter position in statement
222 * @param paramPos int - parameter position
223 * @param value Binary - value to be set
225 virtual void setBinaryParam(int paramPos, void *value) = 0;
227 /**Returns whether the statement prepared is select statement
228 * @return bool true if it is select stmt, false otherwise
230 virtual bool isSelect() = 0;
232 virtual ~AbsSqlStatement(){}
235 //used to store the binded field values and parameters from derived clases of
236 //AbsSqlStatement class
237 class BindSqlField
239 public:
240 DataType type;
241 int length;
242 void *value;
243 void *targetvalue;
244 BindSqlField(){ value = NULL; targetvalue = NULL; }
247 class BindSqlProjectField
249 public:
250 char fName[IDENTIFIER_LENGTH];
251 DataType type;
252 int length;
253 void *value;
254 void *targetvalue;
255 BindSqlProjectField(){ value = NULL; targetvalue = NULL; }
258 #endif