27 #include "../common/Translator.h"
28 #include "../dataaccess/datasource/DataSourceCapabilities.h"
29 #include "../dataaccess/query/SQLDialect.h"
30 #include "../dataaccess/Exception.h"
40 #include <boost/algorithm/string/case_conv.hpp>
41 #include <boost/filesystem.hpp>
42 #include <boost/format.hpp>
59 ret = sqlite3_close(
m_db);
60 assert(ret == SQLITE_OK);
91 return m_pImpl->m_connInfo;
96 m_pImpl->m_connInfo = connInfo;
112 flags = SQLITE_OPEN_READONLY;
114 std::string fileName;
117 std::map<std::string, std::string>::const_iterator it = m_pImpl->m_connInfo.find(
"SQLITE_FILE");
119 if(it != m_pImpl->m_connInfo.end())
120 fileName = it->second;
126 std::map<std::string, std::string>::const_iterator it = m_pImpl->m_connInfo.find(
"SQLITE_VFS");
128 if(it != m_pImpl->m_connInfo.end())
132 int ret = sqlite3_open_v2(fileName.c_str(), &(m_pImpl->m_db), flags, vfs.empty() ? 0 : vfs.c_str());
136 boost::format errmsg(TR_COMMON(
"Could not open the SQLite data source due to the following error: %1%."));
139 errmsg = errmsg % sqlite3_errmsg(m_pImpl->m_db);
141 errmsg = errmsg %
"unknown error";
143 sqlite3_close(m_pImpl->m_db);
147 throw te::da::Exception(errmsg.str());
150 std::auto_ptr<te::da::DataSourceTransactor> transactor(getTransactor());
152 std::auto_ptr<te::da::DataSet> result(transactor->query(
"SELECT CheckSpatialMetaData()"));
154 if(!result->moveNext())
155 throw te::da::Exception(TR_COMMON(
"Could not check if SQLite database has spatial metadata tables due to unexpected error!"));
157 int spatialMetaTableType = result->getInt32(0);
159 if(spatialMetaTableType == 0)
160 transactor->execute(
"SELECT InitSpatialMetadata(1)");
165 if(m_pImpl->m_db == 0)
168 int ret = sqlite3_close(m_pImpl->m_db);
172 boost::format errmsg(TR_COMMON(
"Could not close the SQLite data source due to the following error: %1%."));
175 errmsg = errmsg % sqlite3_errmsg(m_pImpl->m_db);
177 errmsg = errmsg %
"unknown error";
179 throw te::da::Exception(errmsg.str());
187 return m_pImpl->m_db != 0;
192 return m_pImpl->m_db != 0;
197 return *Impl::sm_capabilities;
202 return Impl::sm_dialect;
207 delete Impl::sm_capabilities;
213 delete Impl::sm_dialect;
220 throw te::da::Exception(TR_COMMON(
"The informed SQLite database already exists!"));
223 std::map<std::string, std::string>::const_iterator it = dsInfo.find(
"SQLITE_CREATE_INTERMEDIATE_DIR");
225 if((it != dsInfo.end()) && (boost::to_upper_copy(it->second) ==
"TRUE"))
227 it = dsInfo.find(
"SQLITE_FILE");
229 if(it == dsInfo.end())
230 throw te::da::Exception(TR_COMMON(
"You must at least provide a database file name if you want to create its intermediate directories!"));
232 boost::filesystem::path fileName(it->second);
234 if(fileName.has_parent_path() &&
235 !boost::filesystem::exists(fileName.parent_path()) &&
236 !boost::filesystem::create_directories(fileName.parent_path()))
237 throw te::da::Exception(TR_COMMON(
"Could not create directory for SQLite database!"));
241 std::map<std::string, std::string> info = dsInfo;
243 info[
"SQLITE_OPEN_READWRITE"] =
"true";
244 info[
"SQLITE_OPEN_CREATE"] =
"true";
246 setConnectionInfo(info);
254 throw te::da::Exception(TR_COMMON(
"The informed SQLite database doesn't exist!"));
256 if((dsInfo.find(
"SQLITE_OPEN_URI") != dsInfo.end()) &&
257 (boost::to_upper_copy(m_pImpl->m_connInfo.find(
"SQLITE_OPEN_URI")->second) ==
"TRUE"))
258 throw te::da::Exception(TR_COMMON(
"Sorry! We don't support removing SQLite database through an URI!"));
260 std::string fileName;
263 std::map<std::string, std::string>::const_iterator it = dsInfo.find(
"SQLITE_FILE");
265 if(it != dsInfo.end())
266 fileName = it->second;
268 throw te::da::Exception(TR_COMMON(
"To remove an SQLite database you must inform its file name!"));
271 boost::filesystem::remove(fileName);
276 std::string fileName;
278 std::map<std::string, std::string>::const_iterator it = dsInfo.find(
"SQLITE_FILE");
280 if(it == dsInfo.end())
281 throw te::da::Exception(TR_COMMON(
"In order to check the existence of a SQLite database you must inform its file name"));
283 return boost::filesystem::exists(it->second);
288 return std::vector<std::string>();
293 std::vector<std::string> encodings;
294 encodings.push_back(
"UTF-8");
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
Configuration flags for the SQLite Data Access driver.
~DataSource()
Virtual destructor.
te::da::SQLDialect * dialect
void drop(const std::map< std::string, std::string > &dsInfo)
It removes the data source with the connection information from a driver.
bool isValid() const
It checks if the data source is valid (available for using).
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
It represents the SQL query dialect accepted by a given data source.
std::vector< std::string > getDataSourceNames(const std::map< std::string, std::string > &dsInfo)
It gets the data source names available in a driver.
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
std::auto_ptr< te::da::DataSourceTransactor > getTransactor()
It returns an object that can execute transactions in the context of a data source.
static te::da::SQLDialect * sm_dialect
static void set(te::da::DataSourceCapabilities *capabilities)
te::da::DataSourceCapabilities capabilities
void setConnectionInfo(const std::map< std::string, std::string > &connInfo)
It sets the connection information to be used when connecting to the data source. ...
Utility functions for the TerraLib SQLite Data Access driver.
void open()
It opens the data source and makes it ready for using.
static te::da::DataSourceCapabilities * sm_capabilities
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Implements the DataSource class for the SQLite Data Access Driver.
An implementation of DataSourceTransactor class for the TerraLib SQLite Data Access Driver...
std::map< std::string, std::string > m_connInfo
bool Exists(const std::map< std::string, std::string > &dbInfo)
#define TE_SQLITE_DRIVER_IDENTIFIER
The SQLite driver identifier string.
void close()
It closes the data source and clears all the resources used by its internal communication channel...
const std::map< std::string, std::string > & getConnectionInfo() const
It returns the set of parameters used to set up the access channel to the underlying repository...
std::vector< std::string > getEncodings(const std::map< std::string, std::string > &dsInfo)
It gets the encodings for the data source.
int GetConnectionFlags(const std::map< std::string, std::string > &connInfo)