|
void | close () |
| Close the statement and frees anything has been allocated. More...
|
|
void | customFillBuffer (char *buf, unsigned int &chunksize, bool firstTime) |
| Fills a buffer with a result-set. More...
|
|
void | execute (bool StoreResult=false) |
| Execute the prepared statement. More...
|
|
vector< string > | ExecutionDetails (string pre="") |
| Returns a vector of strings containing details of query execution. More...
|
|
bool | gotRecordSet () |
|
long long int | last_id () |
| Returns the last value generated for an AUTO_INCREMENT column. More...
|
|
Data & | lookup (string field, string table, string where="") |
| Returns the current record set. More...
|
|
unsigned int | nAffectedRows () |
| Returns number of affected records by the last execute() call. More...
|
|
Query & | operator= (const Query &) |
| Declared to avoid using of default assignment operator. More...
|
|
Record & | param () |
| Returns a reference to a Record object containing all the input parameters. More...
|
|
void | prepare (string SQL="") |
| Prepare the SQL statement. More...
|
|
void | prepare_with_parameters (int op, string fields, string table, string where="", int nrec=1) |
| Prepare a query with input parameters. More...
|
|
void | prepare_with_parameters (int op, char **fields, int nfields, string table, string where="", int nrec=1) |
| Wrapper around prepare_with_parameters(int, string, string, string). More...
|
|
| Query (const Query &) |
| Declared to avoid using of default copy constructor. More...
|
|
| Query (DBConn *lconn, bool call_newDBConn=false) |
| Constructor. More...
|
|
void | query (string SQL, bool StoreResult=false) |
| Wrapper aropund prepare() and execute(). More...
|
|
void | readTableList () |
| Reads the list of tables in the current database. More...
|
|
void | Result2Ascii (string fn) |
| Writes a table into an ASCII file using the customFillBuffer() method. More...
|
|
vector< string > | simpleQuery (string SQL, unsigned int &nrows, unsigned int &nfields) |
| Execute a query and return result in a vector of strings. More...
|
|
vector< string > | simpleQuery (string SQL) |
| Wrapper for simpleQuery(string, unsigned int&, unsigned int&). More...
|
|
vector< string > | tableInfo (string tbl) |
| Retrieve information about a table. More...
|
|
| ~Query () |
|
bool | alwaysSameStructure () |
|
void | dump (string fn) |
|
bool | eof () |
|
void | insert (Record *rec) |
| Insert a new record in the set. More...
|
|
void | insert (Record &rec) |
| Copy a new record in the set. More...
|
|
bool | know_nRows () |
|
Record & | metarec () |
|
int | nFields () |
| Returns number of fields. More...
|
|
unsigned int | nRows () |
|
RecordSet & | operator= (const RecordSet &) |
| Declared to avoid using of default assignment operator. More...
|
|
unsigned int | pos () |
|
Record * | prepRecToSend () |
|
Record & | rec () |
|
| RecordSet (const RecordSet &) |
| Declared to avoid using of default copy constructor. More...
|
|
| RecordSet () |
| Build an empty record set. More...
|
|
void | setFieldMap (string s="") |
| Set up a field map for each record in the recordset. More...
|
|
bool | setFirst () |
| Set the record pointer to the first position. More...
|
|
bool | setLast () |
| Set the record pointer to the last position. More...
|
|
bool | setNext () |
| Set the record pointer to the next position. More...
|
|
bool | setPos (unsigned int i) |
| Set the record pointer to the i-th position. More...
|
|
bool | setPrev () |
| Set the record pointer to the previous position in the set. More...
|
|
bool | setWhere (int i, string equalTo) |
| Set the record pointer to the position which satisfies a string condition. More...
|
|
bool | setWhere (int field, int equalTo) |
| Set the record pointer to the position which satisfies an integer condition. More...
|
|
virtual | ~RecordSet () |
| Destructor. More...
|
|
Execute queries on the database.
You can execute two types of query with this class:
The result-set (if any is present) will be returned as a whole inside a vector<string> object in the first case, as a DBRecordSet object in the second (see recordSet() method). With prepared statement you can also prepare a parametric query, then execute it any number of time changing just the parameters.
Definition at line 5544 of file mcs.hh.
void mcs::Query::execute |
( |
bool |
StoreResult = false | ) |
|
Execute the prepared statement.
This will thrown Event exceptions if the statement hasn't be prepared yet (see prepare()) or if an error during execution occur.
If the prepared statement is a SELECT query the server can execute it in two ways:
- read all the record set at once, then send all data to the client;
- read just the first record and send it to the client, the remnant records will be read when needed;
You choose which method use by the "StoreResult" parameter, if it is true the first method will be used, the second otherwise.
In both cases the Query class behave almost the same, except that in the first case the RecordSet::know_nRows() method will return true (and RecordSet::nRows() will return the number of records), in the second case will return false.
- Parameters
-
StoreResult | If true storeResult() will automatically be called. |
- Exceptions
-
ERROR | MSG_STMT_NOT_YET_PREPARED; |
ERROR | MSG_MYSQL_ERROR; |
ERROR | MSG_BIND_FAILED. |
Definition at line 332 of file Db.cc.
Data & mcs::Query::lookup |
( |
string |
field, |
|
|
string |
table, |
|
|
string |
where = "" |
|
) |
| |
Returns the current record set.
Return the value of a field on a record satisfying a where condition.
This method execute a lookup on the table "table", for a record that satisfy the condition "where". Then if this record is found it returns the value of the field "field" as a Data reference, otherwise it throws an exception.
- Parameters
-
field | The field whose value is to be returned. |
table | The table to be used for search. |
where | The condition that should be satisfied by the record. |
- Returns
- Reference to a Data object containing the value of "field".
- Exceptions
-
Definition at line 311 of file Db.cc.
void mcs::Query::prepare |
( |
string |
SQL = "" | ) |
|
Prepare the SQL statement.
Before executing a prepared statement you must prepare it. Any call to this method will destroy all parameter binding, result-set and prepared statement previously present.
To prepare a query with input parameters you should call the method prepare_with_parameters() instead of this.
After this method has executed you can use the recordSet() method to get access to the recordset which already contain all fields, but no data.
- Parameters
-
SQL | The query to be prepared. |
- Exceptions
-
ERROR | MSG_EMPTY_STRING; |
ERROR | MSG_OUT_OF_MEMORY; |
ERROR | MSG_MYSQL_ERROR; |
ERROR | MSG_TYPE_NOT_HANDLED. |
Definition at line 221 of file Db.cc.
void mcs::Query::prepare_with_parameters |
( |
int |
op, |
|
|
string |
fields, |
|
|
string |
table, |
|
|
string |
where = "" , |
|
|
int |
nrec = 1 |
|
) |
| |
Prepare a query with input parameters.
Due to the fact that the mysql_stmt_param_metadata() function is not yet implemented, not all the queries with input parameters can be prepared, but only those queries which performs simple insert or update operations on one table. See the following code for an example:
qry->prepare_with_parameters(MCS_PAB_INSERT, "C I F D", "MYTABLE");
qry->param()[0] = "hello";
qry->param()[1] = 12;
qry->param()[2] = 3.14156;
qry->param()[3] = "2006-03-03 12:00:00";
qry->execute();
The first parameter identify which kind of query you want to execute: MCS_PAB_INSERT ==> insert queries; MCS_PAB_REPLACE ==> replace queries; MCS_PAB_UPDATE ==> update queries;
- Parameters
-
op | One of MCS_PAB_INSERT, MCS_PAB_REPLACE or MCS_PAB_UPDATE; |
fields | List of fields to insert or update, separated by blanks; |
table | Table name; |
where | Where clause, used only if op = MCS_PAB_UPDATE. |
Definition at line 846 of file Db.cc.
vector< string > mcs::Query::simpleQuery |
( |
string |
SQL, |
|
|
unsigned int & |
nrows, |
|
|
unsigned int & |
nfields |
|
) |
| |
Execute a query and return result in a vector of strings.
This executes the query using mysql_query, no placeholders can be present in the query. Then it stores the result on a vector<string> object which contains the entire result-set. An example to print all the contents of a table follows:
DBConn conn();
conn.connect("user, "pass", "dbname");
Query qry(&conn, false);
int nrecs, nfields, i, j;
vector<string> res = qry.simpleQuery("SELECT * FROM MyTable",
nrecs, nfields);
for (i=0; i<nrecs; i++) {
for (j=0; j<nfields; j++)
cout << res[i*nfields + j];
cout << endl;
}
Remember that all the result-set will be retrieved, so it is better to specify a LIMIT clause on the query.
- Parameters
-
SQL | The query to execute. |
nrows | Reference to an integer that will contain the number of rows in the result-set. |
nfields | Reference to an integer that will contain the number of fields in the result-set. |
- Returns
- A vector<string> containing all the result set as returned by printResultSet().
- Exceptions
-
Definition at line 533 of file Db.cc.