27 #include "../core/filesystem/FileSystem.h" 28 #include "../core/translator/Translator.h" 29 #include "../dataaccess/datasource/DataSourceCapabilities.h" 30 #include "../dataaccess/query/SQLDialect.h" 31 #include "../dataaccess/Exception.h" 41 #include <boost/algorithm/string/case_conv.hpp> 42 #include <boost/filesystem.hpp> 43 #include <boost/format.hpp> 60 ret = sqlite3_close(
m_db);
61 assert(ret == SQLITE_OK);
113 flags = SQLITE_OPEN_READONLY;
115 std::string fileName;
118 std::map<std::string, std::string>::const_iterator it =
m_pImpl->
m_connInfo.find(
"SQLITE_FILE");
121 fileName = it->second;
127 std::map<std::string, std::string>::const_iterator it =
m_pImpl->
m_connInfo.find(
"SQLITE_VFS");
133 int ret = sqlite3_open_v2(fileName.c_str(), &(
m_pImpl->
m_db), flags, vfs.empty() ? 0 : vfs.c_str());
137 boost::format errmsg(TR_COMMON(
"Could not open the SQLite data source due to the following error: %1%."));
142 errmsg = errmsg %
"unknown error";
148 throw te::da::Exception(errmsg.str());
151 std::auto_ptr<te::da::DataSourceTransactor> transactor(
getTransactor());
153 std::auto_ptr<te::da::DataSet> result(transactor->query(
"SELECT CheckSpatialMetaData()"));
155 if(!result->moveNext())
156 throw te::da::Exception(TR_COMMON(
"Could not check if SQLite database has spatial metadata tables due to unexpected error!"));
158 int spatialMetaTableType = result->getInt32(0);
160 if(spatialMetaTableType == 0)
161 transactor->execute(
"SELECT InitSpatialMetadata(1)");
173 boost::format errmsg(TR_COMMON(
"Could not close the SQLite data source due to the following error: %1%."));
178 errmsg = errmsg %
"unknown error";
180 throw te::da::Exception(errmsg.str());
221 throw te::da::Exception(TR_COMMON(
"The informed SQLite database already exists!"));
224 std::map<std::string, std::string>::const_iterator it = dsInfo.find(
"SQLITE_CREATE_INTERMEDIATE_DIR");
226 if((it != dsInfo.end()) && (boost::to_upper_copy(it->second) ==
"TRUE"))
228 it = dsInfo.find(
"SQLITE_FILE");
230 if(it == dsInfo.end())
231 throw te::da::Exception(TR_COMMON(
"You must at least provide a database file name if you want to create its intermediate directories!"));
233 boost::filesystem::path fileName(it->second);
235 if(fileName.has_parent_path() &&
238 throw te::da::Exception(TR_COMMON(
"Could not create directory for SQLite database!"));
242 std::map<std::string, std::string> info = dsInfo;
244 info[
"SQLITE_OPEN_READWRITE"] =
"true";
245 info[
"SQLITE_OPEN_CREATE"] =
"true";
255 throw te::da::Exception(TR_COMMON(
"The informed SQLite database doesn't exist!"));
257 if((dsInfo.find(
"SQLITE_OPEN_URI") != dsInfo.end()) &&
258 (boost::to_upper_copy(
m_pImpl->
m_connInfo.find(
"SQLITE_OPEN_URI")->second) ==
"TRUE"))
259 throw te::da::Exception(TR_COMMON(
"Sorry! We don't support removing SQLite database through an URI!"));
261 std::string fileName;
264 std::map<std::string, std::string>::const_iterator it = dsInfo.find(
"SQLITE_FILE");
266 if(it != dsInfo.end())
267 fileName = it->second;
269 throw te::da::Exception(TR_COMMON(
"To remove an SQLite database you must inform its file name!"));
277 std::string fileName;
279 std::map<std::string, std::string>::const_iterator it = dsInfo.find(
"SQLITE_FILE");
281 if(it == dsInfo.end())
282 throw te::da::Exception(TR_COMMON(
"In order to check the existence of a SQLite database you must inform its file name"));
289 return std::vector<std::string>();
294 std::vector<std::string> encodings;
295 encodings.push_back(
"UTF-8");
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
void DataSourceTransactor()
~DataSource()
Virtual destructor.
te::da::SQLDialect * dialect
void drop(const std::map< std::string, std::string > &dsInfo)
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
static bool createDirectories(const std::string &path)
Creates a directory for any element of path that does not exist.
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)
void create(const std::map< std::string, std::string > &dsInfo)
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 the set of parameters used to set up the access channel to the underlying repository...
std::map< std::string, std::string > m_connInfo
static void set(te::da::DataSourceCapabilities *capabilities)
static bool remove(const std::string &path)
Removes a file or directory from a given path in UTF-8.
te::da::DataSourceCapabilities capabilities
void setConnectionInfo(const std::map< std::string, std::string > &connInfo)
void open()
It opens the data source and makes it ready for using.
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.
static te::da::DataSourceCapabilities * sm_capabilities
bool exists(const std::map< std::string, std::string > &dsInfo)
bool Exists(const std::map< std::string, std::string > &dbInfo)
static te::da::SQLDialect * sm_dialect
void close()
It closes the data source and clears all the resources used by its internal communication channel...
#define TE_SQLITE_DRIVER_IDENTIFIER
The SQLite driver identifier string.
const std::map< std::string, std::string > & getConnectionInfo() const
std::vector< std::string > getEncodings(const std::map< std::string, std::string > &dsInfo)
int GetConnectionFlags(const std::map< std::string, std::string > &connInfo)