27 #include "../common/StringUtils.h" 28 #include "../core/translator/Translator.h" 29 #include "../core/uri/URI.h" 30 #include "../core/uri/Utils.h" 31 #include "../dataaccess/dataset/CheckConstraint.h" 32 #include "../dataaccess/dataset/DataSet.h" 33 #include "../dataaccess/dataset/ForeignKey.h" 34 #include "../dataaccess/dataset/Index.h" 35 #include "../dataaccess/dataset/PrimaryKey.h" 36 #include "../dataaccess/dataset/Sequence.h" 37 #include "../dataaccess/dataset/UniqueKey.h" 38 #include "../dataaccess/datasource/ScopedTransaction.h" 39 #include "../dataaccess/query/Select.h" 40 #include "../dataaccess/query/SQLDialect.h" 41 #include "../dataaccess/utils/Utils.h" 42 #include "../datatype/Array.h" 43 #include "../datatype/Property.h" 44 #include "../datatype/SimpleData.h" 45 #include "../geometry/GeometryProperty.h" 46 #include "../geometry/Utils.h" 47 #include "../raster/Grid.h" 48 #include "../raster/BandProperty.h" 49 #include "../raster/RasterProperty.h" 50 #include "../geometry/Geometry.h" 68 #include <boost/format.hpp> 69 #include <boost/lexical_cast.hpp> 138 std::unique_ptr<te::da::DataSourceTransactor> t =
getTransactor();
197 std::unique_ptr<DataSource> dsPGIS(
new DataSource(connInfo));
200 std::map<std::string, std::string> kvp =
te::core::Expand(dsPGIS->getConnectionInfo().query());
201 std::map<std::string, std::string>::const_iterator it = kvp.begin();
202 std::map<std::string, std::string>::const_iterator itend = kvp.end();
207 std::string sql =
"CREATE DATABASE ";
209 it = kvp.find(
"PG_NEWDB_NAME");
210 if (it != itend && !it->second.empty())
213 sql +=
"\"" + it->second +
"\"";
216 throw Exception(
TE_TR(
"The database could not be created due the missing parameter: PG_NEWDB_NAME!"));
218 it = kvp.find(
"PG_NEWDB_TEMPLATE");
219 if(it != itend && !it->second.empty())
220 sql +=
" TEMPLATE = " + it->second;
222 it = kvp.find(
"PG_NEWDB_OWNER");
223 if (it != itend && !it->second.empty())
224 sql +=
" OWNER = " + it->second;
228 it = kvp.find(
"PG_NEWDB_TABLESPACE");
229 if (it != itend && !it->second.empty())
230 sql +=
" TABLESPACE = " + it->second;
232 it = kvp.find(
"PG_NEWDB_CONN_LIMIT");
233 if (it != itend && !it->second.empty())
234 sql +=
" CONNECTION LIMIT = " + it->second;
236 dsPGIS->execute(sql);
240 std::vector<std::string> tokens;
243 std::string connInfoCheck = tokens[0] + dbName;
245 std::unique_ptr<DataSource> dsPGISCheck(
new DataSource(connInfoCheck));
248 std::unique_ptr<te::da::DataSet> result(dsPGISCheck->query(
"SELECT extname, extversion FROM pg_extension WHERE extname = 'postgis'"));
250 if (!result->moveNext())
253 dsPGISCheck->execute(
"CREATE EXTENSION postgis");
255 dsPGISCheck->close();
262 std::unique_ptr<DataSource>
ds(
new DataSource(connInfo));
267 std::string sql =
"DROP DATABASE ";
268 std::map<std::string, std::string> kvp =
te::core::Expand(ds->getConnectionInfo().query());
269 std::map<std::string, std::string>::const_iterator it = kvp.begin();
270 std::map<std::string, std::string>::const_iterator itend = kvp.end();
272 it = kvp.find(
"PG_DB_TO_DROP");
273 if (it == itend || it->second.empty())
274 throw Exception(
TE_TR(
"Could not drop the database due the missing parameter: PG_DB_TO_DROP!"));
276 if((it->second ==
"postgres") || (it->second ==
"template_postgis"))
277 throw Exception(
TE_TR(
"The database postgres or template_postgis is not allowed to be dropped!"));
289 std::unique_ptr<DataSource>
ds(
new DataSource(connInfo));
292 std::map<std::string, std::string> kvp =
te::core::Expand(ds->getConnectionInfo().query());
293 std::map<std::string, std::string>::const_iterator it = kvp.begin();
294 std::map<std::string, std::string>::const_iterator itend = kvp.end();
296 it = kvp.find(
"PG_CHECK_DB_EXISTENCE");
298 if (it == itend || it->second.empty())
299 throw Exception(
TE_TR(
"Could not check the PostgreSQL database existence due the missing parameter : PG_CHECK_DB_EXISTENCE!"));
301 std::string sql(
"SELECT * FROM pg_database WHERE datname = '");
305 std::unique_ptr<te::da::DataSet> database(ds->query(sql));
309 return database->moveNext();
315 std::unique_ptr<DataSource>
ds(
new DataSource(connInfo));
318 std::string sql(
"SELECT datname FROM pg_database");
320 std::unique_ptr<te::da::DataSet> dataset(ds->query(sql));
322 std::vector<std::string> dataSourceNames;
324 while(dataset->moveNext())
325 dataSourceNames.push_back(dataset->getString(0));
329 return dataSourceNames;
void getDatabaseInfo(std::string ¤tSchema)
It retrieves some information about the database such as the default schema used when no one is provi...
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
void drop(const std::string &connInfo)
It removes the data source with the connection information from a driver.
ConnectionPool * m_pool
The connection pool.
bool isValid() const
It checks if all the connections in the pool are valid (the communication channel is ok)...
A visitor for building an SQL statement using PostGIS dialect.
A class that control the use of connection to a PostgreSQL database.
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
~DataSource()
Virtual destructor.
bool isInitialized() const
It returns true if the connection pool is initialized, otherwise it returns false.
Base exception class for plugin module.
void open()
It opens the connection(s) to the PostgreSQL database server.
static te::da::DataSourceCapabilities * sm_capabilities
The query dialect supported by PostGIS driver.
std::vector< std::string > getDataSourceNames(const std::string &connInfo)
It gets the data source names available in a driver.
It represents the SQL query dialect accepted by a given data source.
unsigned int getRasterTypeId() const
It returns the type id associated to the PostGIS Raster type.
const std::string & getCurrentSchema() const
It returns the current schema associated to the database connection, or NULL, if none is set...
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
A class that implements a connection to a PostgreSQL database.
static te::dt::Date ds(2010, 01, 01)
A Transactor can be viewed as a connection to the data source for reading/writing things into it...
#define TE_TR(message)
It marks a string in order to get translated.
void closeConnection(Connection *conn)
void setTimeAsInteger(bool timeIsInteger)
static te::da::SQLDialect * sm_queryDialect
The query dialect supported by PostGIS driver.
void release(Connection *conn)
It brings the informed connection back to the pool.
The transactor class for the PostGIS driver.
#define PGIS_DRIVER_IDENTIFIER
The PostGIS driver identifier string.
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
An exception class for the PostGIS driver.
static std::string getEncodingName(EncodingType et)
Retrive a string from a given character encoding type enum.
unsigned int m_rasterTypeOid
PostGIS Raster type OID.
void initialize()
It initializes the connections to be managed by the pool.
DataSource(const std::string &connInfo)
te::pgis::Connection * getConnection()
bool isValid() const
It checks if the data source is valid (available for using).
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
void create(const std::string &connInfo)
It creates a new data source.
A class that implements a connection to a PostgreSQL database.
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
A class for representing an Uniform Resource Identifier (URI).
void close()
It closes the data source and clears all the resources used by its internal communication channel...
A class that control the use of the connection to a PostgreSQL database.
ConnectionPool * getConnPool() const
It returns a pointer to the internal connection pool.
Connection * getConnection(int id=-1)
It returns a connection from the pool.
bool m_timeIsInteger
It indicates if the postgis stores, internally, time and timestamp as an integer. ...
This file contains utility functions used to manipulate data from a URI.
int m_id
The connection Id.
std::string m_currentSchema
The default schema used when no one is provided.
TECOREEXPORT std::map< std::string, std::string > Expand(const std::string &query_str)
Split a query string into its components.
An static class with global definitions.
This class implements a connection pool for the PostGIS driver.
unsigned int getGeomTypeId()
It will check in the database catalog the number that identifies the PostGIS Geometry type...
bool exists(const std::string &connInfo)
Check the existence of a data source in a driver.
unsigned int getRasterTypeId()
It will check in the database catalog the number that identifies the PostGIS Raster type...
A dataset is the unit of information manipulated by the data access module of TerraLib.
A class that implements a connection pool for PostGIS.
A class that implements a prepared query for PostgreSQL data access driver.
Implementation of the data source for the PostGIS driver.
void finalize()
It closes all connections and clears all resources managed by the pool.
unsigned int getGeomTypeId() const
It returns the type id associated to the PostGIS Geometry type.
std::unique_ptr< te::da::DataSourceTransactor > getTransactor()
It returns the set of parameters used to set up the access channel to the underlying repository...
unsigned int m_geomTypeOid
PostGIS Geometry type OID.