*** empty log message ***
[csql.git] / include / SqlStatement.h
blobaf1e1cce77b926d044862063da2eb8ab1a2188f8
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();
113 /** get FieldValue->value ptr after fetch is done.
114 * @returns address void*
116 void* getFieldValuePtr( int pos );
118 /**Frees all the resources held for the sql statement. Needs to be called before calling prepare again on the same statement handle.
119 * @returns DbRetVal
121 DbRetVal free();
123 /**Retrieves the total number of projection fields in the statement
124 * @returns int no of projection fields
126 int noOfProjFields();
128 /** get Param value pointer after fetch is done.
129 * @returns address void*
131 void* getParamValuePtr( int pos );
133 /** get FieldValue->type
134 * @returns DataType
136 DataType getFieldType( int pos );
138 /** get FieldValue->length
139 * @returns int
141 int getFieldLength( int pos );
143 /** get FieldName
144 * @returns address char*
146 char* getFieldName ( int pos );
148 /**Retrieves the total number of parameters in the statement
149 * @returns int no of parameters
151 int noOfParamFields();
153 /**Retrieves the field info for the required projection field position in statement
154 * @param projPos int - projection field position
155 * @param info FieldInfo*& - OUT parameter
156 * @returns DbRetVal
158 DbRetVal getProjFldInfo(int projPos, FieldInfo *&info);
160 /**Retrieves the field info for the required parameter position in statement
161 * @param projPos int - parameter position
162 * @param info FieldInfo*& - OUT parameter
163 * @returns DbRetVal
165 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
167 /**Sets the value for the required parameter position in statement
168 * @param paramPos int - parameter position
169 * @param value short - value to be set
171 void setShortParam(int paramPos, short value);
172 /**Sets the value for the required parameter position in statement
173 * @param paramPos int - parameter position
174 * @param value int - value to be set
176 void setIntParam(int paramPos, int value);
177 /**Sets the value for the required parameter position in statement
178 * @param paramPos int - parameter position
179 * @param value long - value to be set
181 void setLongParam(int paramPos, long value);
182 /**Sets the value for the required parameter position in statement
183 * @param paramPos int - parameter position
184 * @param value long long - value to be set
186 void setLongLongParam(int paramPos, long long value);
187 /**Sets the value for the required parameter position in statement
188 * @param paramPos int - parameter position
189 * @param value ByteInt - value to be set
191 void setByteIntParam(int paramPos, ByteInt value);
192 /**Sets the value for the required parameter position in statement
193 * @param paramPos int - parameter position
194 * @param value float - value to be set
196 void setFloatParam(int paramPos, float value);
197 /**Sets the value for the required parameter position in statement
198 * @param paramPos int - parameter position
199 * @param value double - value to be set
201 void setDoubleParam(int paramPos, double value);
202 /**Sets the value for the required parameter position in statement
203 * @param paramPos int - parameter position
204 * @param value char* - value to be set
206 void setStringParam(int paramPos, char *value);
207 /**Sets the value for the required parameter position in statement
208 * @param paramPos int - parameter position
209 * @param value Date - value to be set
211 void setDateParam(int paramPos, Date value);
212 /**Sets the value for the required parameter position in statement
213 * @param paramPos int - parameter position
214 * @param value Time - value to be set
216 void setTimeParam(int paramPos, Time value);
217 /**Sets the value for the required parameter position in statement
218 * @param paramPos int - parameter position
219 * @param value TimeStamp - value to be set
221 void setTimeStampParam(int paramPos, TimeStamp value);
223 /**Returns whether the statement prepared is select statement
224 * @return bool true if it is select stmt, false otherwise
226 bool isSelect();
228 /**Returns whether the statement is prepared or not
229 * @return bool true if it is prepared, false otherwise
231 bool isPrepared();
233 private:
234 SqlConnection *sqlCon;
235 Statement *stmt;
236 ParsedData pData;
237 bool isPrepd;
238 friend class SqlFactory;
241 #endif