stmt->close calls table->closeScan not close as earlier.
[csql.git] / include / SqlStatement.h
blobe5eacba8e3df4820c623546de80754bc4bee9a5a
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 SqlStatement: public AbsSqlStatement
32 public:
33 SqlStatement();
34 /** sets connection handle to be used for subsequent operations
35 * @param con SqlConnection*
37 void setConnection(AbsSqlConnection *con);
38 void setSqlConnection(SqlConnection *con);
40 /** compiles the sql statement. It calls the parser and tokenizes the statement
41 * into logical plan. This method sets the statement string which needs to be executed.
42 * free method needs to be called, if application wants to use the same handle to compile
43 * another sql statement.
44 * @param stmt sql statement string
45 * @returns DbRetVal
47 DbRetVal prepare(char *stmt);
49 char* getTableName();
51 /** executes the sql statement. For insert, update, delete queries execute performs the
52 * required operation on the table.
53 * For Select queries, application should call execute before they start fetching
54 * the values from the table.This starts scan on the table.
55 * @param rowsAffect number of rows affected by the sql statement
56 * @returns DbRetVal
58 DbRetVal execute(int &rowsAffect);
60 /**fetches the next tuple from the result of the execution of sql select query.
61 * execute should be called before calling this method. Application buffer should be
62 * binded to get the tuple values.
63 * @returns void* NULL if there is no tuple.
65 void* fetch();
66 void* fetch(DbRetVal &rv);
69 /**fetches the next tuple from the result of the execution of sql select query
70 * and prints it to stdout.
71 * execute should be called before calling this method.
72 * @returns void* NULL if there is no tuple.
74 void* fetchAndPrint(bool SQL);
77 /** binds application buffer to the specified parameter position in the sql statement.
78 * This method should be called for all the parameters in the sql statement.
79 * Parameters shall be specified for predicate for select, update, delete statements.
80 * Parameters shall be specified for field list value in SET of update statements.
81 * If value is not set for all parameters, execute will return error.
82 * <br/>
83 * @param pos position of the parameter in the statement
84 * @param val address of the application buffer. Memory should be allocated by
85 * the application before binding the buffer.
87 DbRetVal bindParam(int pos, void*);
89 /** binds application buffer to the specified field position of the projection list
90 * in the select query or for fields in the insert statement.
91 * This method should be called for select queries, insert, update statements.
92 * Before executing select queries, required fields must be binded first.
93 * Before executing insert statement, required fields must be binded first.
94 * Before executing update statement, required fields to be updated must be
95 * binded first.
96 * <br/>
97 * @param pos position in the projection list
98 * @param val address of the application buffer. Memory should be allocated by
99 * the application before binding the buffer.
101 DbRetVal bindField(int pos, void* val);
103 /** same as fetch, but does not populate bindFieldValues
104 * @returns address void*
106 void* next();
108 /**Closes the iterator and makes the statement ready for another execution
109 * @returns DbRetVal
111 DbRetVal close();
112 DbRetVal freeScan();
114 /** get FieldValue->value ptr after fetch is done.
115 * @returns address void*
117 void* getFieldValuePtr( int pos );
119 /**Frees all the resources held for the sql statement. Needs to be called before calling prepare again on the same statement handle.
120 * @returns DbRetVal
122 DbRetVal free();
124 /**Retrieves the total number of projection fields in the statement
125 * @returns int no of projection fields
127 int noOfProjFields();
129 /** get Param value pointer after fetch is done.
130 * @returns address void*
132 void* getParamValuePtr( int pos );
134 /** get FieldValue->type
135 * @returns DataType
137 DataType getFieldType( int pos );
139 /** get FieldValue->length
140 * @returns int
142 int getFieldLength( int pos );
144 /** get FieldName
145 * @returns address char*
147 char* getFieldName ( int pos );
149 /**Retrieves the total number of parameters in the statement
150 * @returns int no of parameters
152 int noOfParamFields();
154 /**Retrieves the field info for the required projection field position in statement
155 * @param projPos int - projection field position
156 * @param info FieldInfo*& - OUT parameter
157 * @returns DbRetVal
159 DbRetVal getProjFldInfo(int projPos, FieldInfo *&info);
161 /**Retrieves the field info for the required parameter position in statement
162 * @param projPos int - parameter position
163 * @param info FieldInfo*& - OUT parameter
164 * @returns DbRetVal
166 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
168 /**Sets the value for the required parameter position in statement
169 * @param paramPos int - parameter position
170 * @param value short - value to be set
172 void setShortParam(int paramPos, short value);
173 /**Sets the value for the required parameter position in statement
174 * @param paramPos int - parameter position
175 * @param value int - value to be set
177 void setIntParam(int paramPos, int value);
178 /**Sets the value for the required parameter position in statement
179 * @param paramPos int - parameter position
180 * @param value long - value to be set
182 void setLongParam(int paramPos, long value);
183 /**Sets the value for the required parameter position in statement
184 * @param paramPos int - parameter position
185 * @param value long long - value to be set
187 void setLongLongParam(int paramPos, long long value);
188 /**Sets the value for the required parameter position in statement
189 * @param paramPos int - parameter position
190 * @param value ByteInt - value to be set
192 void setByteIntParam(int paramPos, ByteInt value);
193 /**Sets the value for the required parameter position in statement
194 * @param paramPos int - parameter position
195 * @param value float - value to be set
197 void setFloatParam(int paramPos, float value);
198 /**Sets the value for the required parameter position in statement
199 * @param paramPos int - parameter position
200 * @param value double - value to be set
202 void setDoubleParam(int paramPos, double value);
203 /**Sets the value for the required parameter position in statement
204 * @param paramPos int - parameter position
205 * @param value char* - value to be set
207 void setStringParam(int paramPos, char *value);
208 /**Sets the value for the required parameter position in statement
209 * @param paramPos int - parameter position
210 * @param value Date - value to be set
212 void setDateParam(int paramPos, Date value);
213 /**Sets the value for the required parameter position in statement
214 * @param paramPos int - parameter position
215 * @param value Time - value to be set
217 void setTimeParam(int paramPos, Time value);
218 /**Sets the value for the required parameter position in statement
219 * @param paramPos int - parameter position
220 * @param value TimeStamp - value to be set
222 void setTimeStampParam(int paramPos, TimeStamp value);
223 /**Sets the value for the required parameter position in statement
224 * @param paramPos int - parameter position
225 * @param value Binary - value to be set
227 void setBinaryParam(int paramPos, void *value);
229 /**Returns whether the statement prepared is select statement
230 * @return bool true if it is select stmt, false otherwise
232 bool isSelect();
234 /**Returns whether the statement is prepared or not
235 * @return bool true if it is prepared, false otherwise
237 bool isPrepared();
238 StatementType getStmtType() { return pData.getStmtType(); }
239 bool isFldNull(int pos);
240 void setNull(int pos);
241 int getFldPos(char *name);
242 private:
243 SqlConnection *sqlCon;
244 Statement *stmt;
245 ParsedData pData;
246 bool isPrepd;
247 friend class SqlFactory;
250 #endif