Though select fails, it return the tuple. This is because curTuple_ is not set in...
[csql.git] / include / SqlStatement.h
blob4dbe489711f277d37704db38fb0557054daa42e9
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 <SqlConnection.h>
23 #include "Statement.h"
24 //#include<CSql.h>
25 //class Statement;
26 class ParsedData;
28 /**
29 * @class SqlStatement
31 * @brief Handle to the sql statement.
32 * It is used to execute queries and return the values from the database<br>
33 * Sql Statement is fed to the prepare method first and then it should be executed. <br/>
34 * Functionality: <br/>
35 * 1.Input values for insert statement <br/>
36 * 2.Iterator for retrieving rows from the table <br/>
37 * 3.Parameter support for performance.<br/>
38 * <br/>
39 * @author Prabakaran Thirumalai
41 class SqlStatement
43 public:
44 /** Default constructor
46 SqlStatement();
47 /** sets connection handle to be used for subsequent operations
48 * @param con SqlConnection*
50 void setConnection(SqlConnection *con);
52 /** compiles the sql statement. It calls the parser and tokenizes the statement
53 * into logical plan. This method sets the statement string which needs to be executed.
54 * free method needs to be called, if application wants to use the same handle to compile
55 * another sql statement.
56 * @param stmt sql statement string
57 * @returns DbRetVal
59 DbRetVal prepare(char *stmt);
61 /** executes the sql statement. For insert, update, delete queries execute performs the
62 * required operation on the table.
63 * For Select queries, application should call execute before they start fetching
64 * the values from the table.This starts scan on the table.
65 * @param rowsAffect number of rows affected by the sql statement
66 * @returns DbRetVal
68 DbRetVal execute(int &rowsAffect);
70 /**fetches the next tuple from the result of the execution of sql select query.
71 * execute should be called before calling this method. Application buffer should be
72 * binded to get the tuple values.
73 * @returns void* NULL if there is no tuple.
75 void* fetch();
78 /**fetches the next tuple from the result of the execution of sql select query
79 * and prints it to stdout.
80 * execute should be called before calling this method.
81 * @returns void* NULL if there is no tuple.
83 void* fetchAndPrint();
86 /** binds application buffer to the specified parameter position in the sql statement.
87 * This method should be called for all the parameters in the sql statement.
88 * Parameters shall be specified for predicate for select, update, delete statements.
89 * Parameters shall be specified for field list value in SET of update statements.
90 * If value is not set for all parameters, execute will return error.
91 * <br/>
92 * @param pos position of the parameter in the statement
93 * @param val address of the application buffer. Memory should be allocated by
94 * the application before binding the buffer.
96 DbRetVal bindParam(int pos, void*);
98 /** binds application buffer to the specified field position of the projection list
99 * in the select query or for fields in the insert statement.
100 * This method should be called for select queries, insert, update statements.
101 * Before executing select queries, required fields must be binded first.
102 * Before executing insert statement, required fields must be binded first.
103 * Before executing update statement, required fields to be updated must be
104 * binded first.
105 * <br/>
106 * @param pos position in the projection list
107 * @param val address of the application buffer. Memory should be allocated by
108 * the application before binding the buffer.
110 DbRetVal bindField(int pos, void* val);
112 /**Frees all the resources held for the sql statement. Needs to be called before calling prepare again on the same statement handle.
113 * @returns DbRetVal
115 DbRetVal free();
117 /**Retrieves the total number of projection fields in the statement
118 * @returns int no of projection fields
120 int noOfProjFields();
122 /**Retrieves the total number of parameters in the statement
123 * @returns int no of parameters
125 int noOfParamFields();
127 /**Retrieves the field info for the required projection field position in statement
128 * @param projPos int - projection field position
129 * @param info FieldInfo*& - OUT parameter
130 * @returns DbRetVal
132 DbRetVal getProjFldInfo(int projPos, FieldInfo *&info);
134 /**Retrieves the field info for the required parameter position in statement
135 * @param projPos int - parameter position
136 * @param info FieldInfo*& - OUT parameter
137 * @returns DbRetVal
139 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
141 /**Sets the value for the required parameter position in statement
142 * @param paramPos int - parameter position
143 * @param value short - value to be set
145 void setShortParam(int paramPos, short value);
146 /**Sets the value for the required parameter position in statement
147 * @param paramPos int - parameter position
148 * @param value int - value to be set
150 void setIntParam(int paramPos, int value);
151 /**Sets the value for the required parameter position in statement
152 * @param paramPos int - parameter position
153 * @param value long - value to be set
155 void setLongParam(int paramPos, long value);
156 /**Sets the value for the required parameter position in statement
157 * @param paramPos int - parameter position
158 * @param value long long - value to be set
160 void setLongLongParam(int paramPos, long long value);
161 /**Sets the value for the required parameter position in statement
162 * @param paramPos int - parameter position
163 * @param value ByteInt - value to be set
165 void setByteIntParam(int paramPos, ByteInt value);
166 /**Sets the value for the required parameter position in statement
167 * @param paramPos int - parameter position
168 * @param value float - value to be set
170 void setFloatParam(int paramPos, float value);
171 /**Sets the value for the required parameter position in statement
172 * @param paramPos int - parameter position
173 * @param value double - value to be set
175 void setDoubleParam(int paramPos, double value);
176 /**Sets the value for the required parameter position in statement
177 * @param paramPos int - parameter position
178 * @param value char* - value to be set
180 void setStringParam(int paramPos, char *value);
181 /**Sets the value for the required parameter position in statement
182 * @param paramPos int - parameter position
183 * @param value Date - value to be set
185 void setDateParam(int paramPos, Date value);
186 /**Sets the value for the required parameter position in statement
187 * @param paramPos int - parameter position
188 * @param value Time - value to be set
190 void setTimeParam(int paramPos, Time value);
191 /**Sets the value for the required parameter position in statement
192 * @param paramPos int - parameter position
193 * @param value TimeStamp - value to be set
195 void setTimeStampParam(int paramPos, TimeStamp value);
197 /**Returns whether the statement prepared is select statement
198 * @return bool true if it is select stmt, false otherwise
200 bool isSelect();
202 private:
203 SqlConnection *con;
204 Statement *stmt;
205 ParsedData pData;
209 #endif