27 #include "../common/Globals.h"
28 #include "../common/Translator.h"
29 #include "../dataaccess/dataset/DataSet.h"
30 #include "../dataaccess/dataset/DataSetType.h"
31 #include "../dataaccess/query/Query.h"
32 #include "../datatype/ByteArray.h"
33 #include "../datatype/DateTime.h"
34 #include "../geometry/Geometry.h"
35 #include "../memory/DataSet.h"
48 #include <boost/date_time.hpp>
49 #include <boost/format.hpp>
61 int retval = SQLITE_OK;
67 retval = sqlite3_bind_null(stmt, i + 1);
69 if(retval != SQLITE_OK)
75 switch(propertyDataType)
78 retval = sqlite3_bind_int(stmt, i + 1, d->
getChar(propertyPos));
82 retval = sqlite3_bind_int(stmt, i + 1, d->
getUChar(propertyPos));
86 retval = sqlite3_bind_int(stmt, i + 1, d->
getInt16(propertyPos));
90 retval = sqlite3_bind_int(stmt, i + 1, d->
getInt32(propertyPos));
94 retval = sqlite3_bind_int64(stmt, i + 1, d->
getInt64(propertyPos));
102 retval = sqlite3_bind_double(stmt, i + 1, static_cast<double>(d->
getFloat(propertyPos)));
106 retval = sqlite3_bind_double(stmt, i + 1, d->
getDouble(propertyPos));
111 std::string value = d->
getNumeric(propertyPos);
112 retval = sqlite3_bind_text(stmt, i + 1, value.c_str(), value.size(), SQLITE_TRANSIENT);
118 std::string value = d->
getString(propertyPos);
119 retval = sqlite3_bind_text(stmt, i + 1, value.c_str(), value.size(), SQLITE_TRANSIENT);
133 std::auto_ptr<te::gm::Geometry> g(d->
getGeometry(propertyPos));
137 unsigned char* ewkb =
new unsigned char[ewkbSize];
141 retval = sqlite3_bind_blob(stmt, i + 1, ewkb, ewkbSize, SQLITE_TRANSIENT);
149 std::auto_ptr<te::dt::DateTime> dt(d->
getDateTime(propertyPos));
151 std::string sdate = dt->toString();
153 retval = sqlite3_bind_text(stmt, i + 1, sdate.c_str(), sdate.size(), SQLITE_TRANSIENT);
172 if(retval != SQLITE_OK)
188 sqlite3_finalize(m_stmt);
198 assert(m_t && m_t->getDataSource() && m_t->getDataSource()->getDialect());
201 SQLVisitor visitor(*(m_t->getDataSource()->getDialect()), sql);
209 sqlite3_finalize(m_stmt);
213 int ret = sqlite3_prepare_v2(m_db, query.c_str(), -1, &m_stmt, 0);
218 sqlite3_finalize(m_stmt);
222 throw te::common::Exception((boost::format(
TR_COMMON(
"Could not excute the given query due to the following error: %1%.")) % sqlite3_errmsg(m_db)).str());
228 int retval = sqlite3_step(m_stmt);
230 if(retval != SQLITE_DONE)
232 boost::format errmsg(
TR_COMMON(
"Could not execute prepared query due to the following error: %1%."));
234 const char* litemsg = sqlite3_errmsg(m_db);
237 errmsg = errmsg % litemsg;
242 retval = sqlite3_reset(m_stmt);
244 if(retval != SQLITE_OK)
246 boost::format errmsg(
TR_COMMON(
"Could not reset the prepared query to its initial state due to the following error: %1%."));
248 const char* litemsg = sqlite3_errmsg(m_db);
251 errmsg = errmsg % litemsg;
305 int retval = sqlite3_bind_int(m_stmt, i + 1, value);
307 if(retval != SQLITE_OK)
313 int retval = sqlite3_bind_int(m_stmt, i + 1, value);
315 if(retval != SQLITE_OK)
321 int retval = sqlite3_bind_int(m_stmt, i + 1, value);
323 if(retval != SQLITE_OK)
329 int retval = sqlite3_bind_int(m_stmt, i + 1, value);
331 if(retval != SQLITE_OK)
337 int retval = sqlite3_bind_int64(m_stmt, i + 1, value);
339 if(retval != SQLITE_OK)
347 if(retval != SQLITE_OK)
353 int retval = sqlite3_bind_double(m_stmt, i + 1, static_cast<double>(value));
355 if(retval != SQLITE_OK)
361 int retval = sqlite3_bind_double(m_stmt, i + 1, value);
363 if(retval != SQLITE_OK)
369 int retval = sqlite3_bind_text(m_stmt, i + 1, value.c_str(), value.size(), SQLITE_TRANSIENT);
371 if(retval != SQLITE_OK)
377 int retval = sqlite3_bind_text(m_stmt, i + 1, value.c_str(), value.size(), SQLITE_TRANSIENT);
379 if(retval != SQLITE_OK)
385 int retval = sqlite3_bind_blob(m_stmt, i + 1, value.
getData(), value.
bytesUsed(), SQLITE_TRANSIENT);
387 if(retval != SQLITE_OK)
395 unsigned char* ewkb =
new unsigned char[ewkbSize];
399 int retval = sqlite3_bind_blob(m_stmt, i + 1, ewkb, ewkbSize, SQLITE_TRANSIENT);
403 if(retval != SQLITE_OK)
414 std::string sdate = value.
toString();
416 int retval = sqlite3_bind_text(m_stmt, i + 1, sdate.c_str(), sdate.size(), SQLITE_TRANSIENT);
418 if(retval != SQLITE_OK)
440 sqlite3_finalize(m_stmt);
444 sqlite3_finalize(m_stmt);
448 int ret = sqlite3_prepare_v2(m_db, query.c_str(), -1, &m_stmt, 0);
453 sqlite3_finalize(m_stmt);
457 throw te::common::Exception((boost::format(
TR_COMMON(
"Could not excute the given query due to the following error: %1%.")) % sqlite3_errmsg(m_db)).str());
465 const std::size_t nparams = propertiesPos.size();
467 for(std::size_t i = 0; i != nparams; ++i)
468 BindValue(m_stmt, d, i + offset, propertiesPos[i]);
474 const std::size_t nparams = propertiesPos.size();
476 for(std::size_t i = 0; i != nparams; ++i)
477 BindValue(m_stmt, d, i, propertiesPos[i]);
484 for(std::size_t i = 0; i != nparams; ++i)
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
te::da::DataSet * query(te::common::TraverseType travType=te::common::FORWARDONLY, te::common::AccessPolicy rwRole=te::common::RAccess)
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
An implementation of DataSourceTransactor class for the TerraLib SQLite Data Access Driver...
A visitor for building an SQL statement using SQLite dialect.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
void prepare(const te::da::Query &query, const std::vector< te::dt::Property * > ¶mTypes)
It prepares the query that may be used for commands that are used mutiple times (select, insert, update and delete).
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
virtual bool getBool(std::size_t i) const =0
Method for retrieving a boolean attribute value.
virtual std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const =0
Method for retrieving a date and time attribute value.
void bind(int i, char value)
Binds the i-th parameter of the query to a value.
Implements the DataSource class for the SQLite Data Access Driver.
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
virtual unsigned char getUChar(std::size_t i) const =0
Method for retrieving an unsigned character attribute value (1 byte long).
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
virtual std::string toString() const =0
It returns the data value in a string notation.
std::string getName() const
It returns the prepared query name.
virtual std::string getNumeric(std::size_t i) const =0
Method for retrieving a numeric attribute value.
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
te::da::DataSourceTransactor * getTransactor() const
It returns a pointer to the underlying data source transactor.
A visitor for building an SQL statement using SQLite dialect.
AccessPolicy
Supported data access policies (can be used as bitfield).
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
A class that helps to determine the size of a SpatiaLite geometry.
PreparedQuery()
Constructor.
static const MachineByteOrder sm_machineByteOrder
A flag that indicates the machine byte order (Big Endian or Little Endian).
A Query is independent from the data source language/dialect.
static std::size_t getEWKBSize(const te::gm::Geometry *g)
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
void BindValue(sqlite3_stmt *stmt, te::da::DataSet *d, std::size_t i, std::size_t propertyPos)
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
virtual char getChar(std::size_t i) const =0
Method for retrieving a signed character attribute value (1 byte long).
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
std::size_t bytesUsed() const
It returns the number of used bytes in the internal buffer.
#define TE_SQLITE_BOOL_FALSE
A flag that indicates a false value (boolean).
TraverseType
A dataset can be traversed in two ways:
#define TR_COMMON(message)
It marks a string in order to get translated. This is the mark used in the Common module of TerraLib...
A base class for values that can be retrieved from the data access module.
A class that implements a prepared query for the TerraLib SQLite Data Access Driver.
Implementation of a forward-only dataset for the TerraLib SQLite Data Access driver.
A class that serializes a geometry to the SpatiaLite EWKB format.
void bindNumeric(int i, const std::string &value)
Binds the i-th parameter of the query to a value.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
An abstract class for raster data strucutures.
char * getData() const
It returns the data array.
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
A class for representing binary data.
~PreparedQuery()
Virtual destructor.
A dataset is the unit of information manipulated by the data access module of TerraLib.
struct sqlite3_stmt sqlite3_stmt
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
#define TE_SQLITE_BOOL_TRUE
A flag that indicates a false value (boolean).