Transactor.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/postgis/Transactor.h
22 
23  \brief A Transactor can be viewed as a connection to the data source for reading/writing things into it.
24 */
25 
26 #ifndef __TERRALIB_POSTGIS_INTERNAL_TRANSACTOR_H
27 #define __TERRALIB_POSTGIS_INTERNAL_TRANSACTOR_H
28 
29 // TerraLib
30 #include "../dataaccess/datasource/DataSourceTransactor.h"
31 #include "Config.h"
32 #include "DataSource.h"
33 
34 // STL
35 #include <memory>
36 #include <map>
37 #include <string>
38 
39 namespace te
40 {
41 // Forward declarations
42  namespace dt { class Property; }
43  namespace gm { class Envelope; class Geometry; }
44 
45  namespace pgis
46  {
47  // Forward declarations
48  class BatchExecutor;
49  class DataSet;
50  class Connection;
51  class ObjectIdSet;
52  class PreparedQuery;
53  class Query;
54 
55  /*!
56  \class Transactor
57 
58  \brief The transactor class for the PostGIS driver.
59 
60  \sa te::da::DataSourceTransactor, DataSource
61  */
63  {
64  public:
65 
66  /*!
67  \brief Constructor.
68 
69  \param ds The PostGIS data source associated to this transactor.
70  \param conn An available connection that will be released when the transactor is deleted
71  */
72  Transactor(DataSource* ds, Connection* conn);
73 
74  /*! \brief The destructor will automatically release the connection to the pool. */
75  ~Transactor();
76 
77  te::da::DataSource* getDataSource() const;
78 
79  /*!
80  \brief It returns the underlying connection.
81 
82  \return The underlying connection.
83 
84  \note PostGIS driver extended method.
85  */
86  Connection* getConnection() const;
87 
88  void begin();
89 
90  void commit();
91 
92  void rollBack();
93 
94  bool isInTransaction() const;
95 
96  std::auto_ptr<te::da::DataSet> getDataSet(const std::string& name,
98  bool isConnected = false,
99  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
100 
101  std::auto_ptr<te::da::DataSet> getDataSet(const std::string& name,
102  const std::string& propertyName,
103  const te::gm::Envelope* e,
106  bool isConnected = false,
107  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
108 
109  std::auto_ptr<te::da::DataSet> getDataSet(const std::string& name,
110  const std::string& propertyName,
111  const te::gm::Geometry* g,
114  bool isConnected = false,
115  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
116 
117  std::auto_ptr<te::da::DataSet> query(const te::da::Select& q,
119  bool connected = false,
120  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
121 
122 
123  std::auto_ptr<te::da::DataSet> query(const std::string& query,
125  bool connected = false,
126  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
127 
128  void execute(const te::da::Query& command);
129 
130  void execute(const std::string& command);
131 
132  std::auto_ptr<te::da::PreparedQuery> getPrepared(const std::string& qName = std::string(""));
133 
134  std::auto_ptr<te::da::BatchExecutor> getBatchExecutor();
135 
136  void cancel();
137 
138  boost::int64_t getLastGeneratedId();
139 
140  std::string escape(const std::string& value);
141 
142  std::vector<std::string> getDataSetNames();
143 
144  std::size_t getNumberOfDataSets();
145 
146  std::auto_ptr<te::da::DataSetType> getDataSetType(const std::string& name);
147 
148  boost::ptr_vector<te::dt::Property> getProperties(const std::string& datasetName);
149 
150  std::auto_ptr<te::dt::Property> getProperty(const std::string& datasetName, const std::string& name);
151 
152  std::auto_ptr<te::dt::Property> getProperty(const std::string& datasetName, std::size_t propertyPos);
153 
154  std::vector<std::string> getPropertyNames(const std::string& datasetName);
155 
156  std::size_t getNumberOfProperties(const std::string& datasetName);
157 
158  bool propertyExists(const std::string& datasetName, const std::string& name);
159 
160  void addProperty(const std::string& datasetName, te::dt::Property* p);
161 
162  void dropProperty(const std::string& datasetName, const std::string& name);
163 
164  void renameProperty(const std::string& datasetName,
165  const std::string& propertyName,
166  const std::string& newPropertyName);
167 
168 
169  void changePropertyDefinition(const std::string& datasetName, const std::string& propName, te::dt::Property* newProp);
170 
171 
172  std::auto_ptr<te::da::PrimaryKey> getPrimaryKey(const std::string& datasetName);
173 
174  bool primaryKeyExists(const std::string& datasetName, const std::string& name);
175 
176  void addPrimaryKey(const std::string& datasetName, te::da::PrimaryKey* pk);
177 
178  void dropPrimaryKey(const std::string& datasetName);
179 
180  std::auto_ptr<te::da::ForeignKey> getForeignKey(const std::string& datasetName, const std::string& name);
181 
182  std::vector<std::string> getForeignKeyNames(const std::string& datasetName);
183 
184  bool foreignKeyExists(const std::string& datasetName, const std::string& name);
185 
186  void addForeignKey(const std::string& datasetName, te::da::ForeignKey* fk);
187 
188  void dropForeignKey(const std::string& datasetName, const std::string& fkName);
189 
190  std::auto_ptr<te::da::UniqueKey> getUniqueKey(const std::string& datasetName, const std::string& name);
191 
192  std::vector<std::string> getUniqueKeyNames(const std::string& datasetName);
193 
194  bool uniqueKeyExists(const std::string& datasetName, const std::string& name);
195 
196  void addUniqueKey(const std::string& datasetName, te::da::UniqueKey* uk);
197 
198  void dropUniqueKey(const std::string& datasetName, const std::string& name);
199 
200  std::auto_ptr<te::da::CheckConstraint> getCheckConstraint(const std::string& datasetName, const std::string& name);
201 
202  std::vector<std::string> getCheckConstraintNames(const std::string& datasetName);
203 
204  bool checkConstraintExists(const std::string& datasetName, const std::string& name);
205 
206  void addCheckConstraint(const std::string& datasetName, te::da::CheckConstraint* cc);
207 
208  void dropCheckConstraint(const std::string& datasetName, const std::string& name);
209 
210  std::auto_ptr<te::da::Index> getIndex(const std::string& datasetName, const std::string& name);
211 
212  std::vector<std::string> getIndexNames(const std::string& datasetName);
213 
214  bool indexExists(const std::string& datasetName, const std::string& name);
215 
216  void addIndex(const std::string& datasetName, te::da::Index* idx,
217  const std::map<std::string, std::string>& options);
218 
219  void dropIndex(const std::string& datasetName, const std::string& idxName);
220 
221  std::auto_ptr<te::da::Sequence> getSequence(const std::string& name);
222 
223  std::vector<std::string> getSequenceNames();
224 
225  bool sequenceExists(const std::string& name);
226 
227  void addSequence(te::da::Sequence* sequence);
228 
229  void dropSequence(const std::string& name);
230 
231  std::auto_ptr<te::gm::Envelope> getExtent(const std::string& datasetName,
232  const std::string& propertyName);
233 
234  std::auto_ptr<te::gm::Envelope> getExtent(const std::string& datasetName,
235  std::size_t propertyPos);
236 
237  std::size_t getNumberOfItems(const std::string& datasetName);
238 
239  bool hasDataSets();
240 
241  bool dataSetExists(const std::string& name);
242 
243  void createDataSet(te::da::DataSetType* dt, const std::map<std::string, std::string>& options);
244 
245  void cloneDataSet(const std::string& name,
246  const std::string& cloneName,
247  const std::map<std::string, std::string>& options);
248 
249  void dropDataSet(const std::string& name);
250 
251  void renameDataSet(const std::string& name, const std::string& newName);
252 
253  void add(const std::string& datasetName,
254  te::da::DataSet* d,
255  const std::map<std::string, std::string>& options,
256  std::size_t limit = 0,
257  bool enableProgress = true);
258 
259  void remove(const std::string& datasetName, const te::da::ObjectIdSet* oids = 0);
260 
261  void update(const std::string& datasetName,
262  te::da::DataSet* dataset,
263  const std::vector<std::size_t>& properties,
264  const te::da::ObjectIdSet* oids,
265  const std::map<std::string, std::string>& options,
266  std::size_t limit = 0);
267 
268  void update(const std::string& datasetName,
269  te::da::DataSet* dataset,
270  const std::vector< std::set<int> >& properties,
271  const std::vector<size_t>& ids);
272 
273  void optimize(const std::map<std::string, std::string>& opInfo);
274 
275  /*!
276  \brief It will check in the database catalog the number that identifies the PostGIS Geometry type.
277 
278  \return The oid of the geometry type or 0 if it is not found.
279 
280  \exception Exception It may throws an exception.
281 
282  \note PostGIS driver extended method.
283  */
284  unsigned int getGeomTypeId();
285 
286  /*!
287  \brief It will check in the database catalog the number that identifies the PostGIS Raster type.
288 
289  \return The oid of the raster type or 0 if it is not found.
290 
291  \exception Exception It may throws an exception.
292 
293  \note PostGIS driver extended method.
294  */
295  unsigned int getRasterTypeId();
296 
297  /*!
298  \brief It retrieves some information about the database such as the default schema used when no one is provided.
299 
300  \param currentSchema A reference to output the current schema.
301 
302  \exception Exception It throws an exception if it was not possible to query the database.
303 
304  \note PostGIS driver extended method.
305  */
306  void getDatabaseInfo(std::string& currentSchema);
307 
308  /*!
309  \brief It loads information about a given geometry column.
310 
311  \param datasetName The name of the dataset containing the geometric property.
312  \param gp The geometric columns to load its information.
313 
314  \exception It throws an exception if it can not load the information.
315  */
316  void getGeometryInfo(const std::string& datasetName, te::gm::GeometryProperty* gp);
317 
318  /*!
319  \brief It loads information about a given raster column.
320 
321  \param datasetName The name of the dataset containing the geometric property.
322  \param rp The raster column to load its information.
323 
324  \exception It throws an exception if it can not load the information.
325  */
326  void getRasterInfo(const std::string& datasetName, te::rst::RasterProperty* rp);
327 
328  /*!
329  \brief It gets the full name of the given name including the schema name.
330 
331  \param name The dataset name.
332 
333  \return The dataset full name.
334  */
335  std::string getFullName(const std::string& name);
336 
337  protected:
338 
339  /*!
340  \brief It looks for the dataset id in the PostgreSQL system.
341 
342  \param datasetName Dataset name. If it has no schema prefix name, it will look the dataset id in the default schema.
343 
344  \exception Exception It throws an exception, if it was not possible to get the dataset id.
345 
346  \note PostGIS driver extended method.
347  */
348  unsigned int getDataSetId(const std::string& datasetName);
349 
350  /*!
351  \brief It looks for a dataset name with the given id in the PostgreSQL.
352 
353  \param id The dataset id.
354 
355  \return The dataset name.
356 
357  \exception Exception It throws an exception if it was not possible to retrieve the information.
358 
359  \note PostGIS driver extended method.
360  */
361  std::string getDataSetName(unsigned int id);
362 
363  /*!
364  \brief It gets the information about the given dataset.
365 
366  \param datasetName The dataset name.
367 
368  \return The information about the given dataset.
369 
370  \note Internally, a record set will be generated containing the following fields:
371  <ul>
372  <li>0 (int2): attribute number in the table (a.attnum), remember that attribute number is 1 based</li>
373  <li>1 (name): attribute name (a.attname)</li>
374  <li>2 (Oid): attribute type oid (t.oid)</li>
375  <li>3 (bool): 't' if attribute is NOT NULL, otherwise, its value is 'f' (a.attnotnull)</li>
376  <li>4 (text): type modifier information, like precision and scale (format_type(a.atttypid, a.atttypmod))</li>
377  <li>5 (bool): 't' if attribute is has a default value, otherwise, its value is 'f' (a.atthasdef)</li>
378  <li>6 (text): attribute default value if field number 5 is true (pg_get_expr(d.adbin, d.adrelid))</li>
379  <li>7 (int4): Number of dimensions, if the column is an array type; otherwise 0 (a.attndims)</li>
380  </ul>
381 
382  \exception Exception It throws an exception if it was not possible to get the information needed.
383 
384  \note PostGIS driver extended method.
385  */
386  std::auto_ptr<te::da::DataSet> getPropertiesInfo(const std::string& datasetName);
387 
388  /*!
389  \brief It sets the property id from the PostgreSQL system.
390 
391  \param p The property whose oid will be set from PostgreSQL system tables.
392 
393  \exception Exception It throws an exception if it was not possible to set the oid information.
394 
395  \note PostGIS driver extended method.
396  */
397  void getPropertyId(te::dt::Property* p);
398 
399  /*!
400  \brief It retrieves a property with the given id from the given dataset.
401 
402  \param pid The property id.
403  \param datasetName The dataset name.
404 
405  \return The property with the given id from the dataset.
406 
407  \note PostGIS driver extended method.
408  */
409  std::auto_ptr<te::dt::Property> getProperty(unsigned int pid, const std::string& datasetName);
410 
411  /*!
412  \brief It gets the dataset containing information about one of the constraints(primary, foreign or
413  unique keys, or the check constraints) of the given dataset. If the constraint type
414  is not specified, it gets all the constraints of the given dataset.
415 
416  \param datasetName The dataset name.
417  \param conType The type of constraint to be retrieved. If this value is '\0' all the types are retrieved.
418 
419  \return A recordset with the following fields:
420  <ul>
421  <li>0 (Oid): constraint oid (c.oid)</li>
422  <li>1 (name): constraint schema name (n.nspname)</li>
423  <li>2 (name): constraint name (c.conname)</li>
424  <li>3 (char): constraint type (c.contype), one of the following values: 'c', 'f', 'p', 'u'</li>
425  <li>4 (Oid): the referenced table (c.confrelid)</li>
426  <li>5 (char): OnUpdate action (c.confupdtype)</li>
427  <li>6 (char): OnDeletion action (c.confdeltype)</li>
428  <li>7 (char): ??? (c.confmatchtype)</li>
429  <li>8 (int2[]): array of attribute numbers (c.conkey), the list of keys in a foreign key, primary key or unique key</li>
430  <li>9 (int2[]): array of attribute numbers in the referenced table (c.confkey)</li>
431  <li>10 (text): constraint expression (pg_get_constraintdef(c.oid))</li>
432  </ul>
433 
434  \return The dataset containing information about the constraints of the given dataset.
435  */
436  std::auto_ptr<te::da::DataSet> getConstraints(const std::string& datasetName, char conType = '\0');
437 
438  /*!
439  \brief It gets the information about all the dataset constraints(primary, foreign and
440  unique keys, and the check constraints) and loads it on the given dataset schema.
441 
442  \param dt The dataset schema.
443  */
444  void getConstraints(te::da::DataSetType* dt);
445 
446  /*!
447  \brief It gets all the indexes of the given dataset and adds them to the dummy schema.
448 
449  \param dt The dataset schema.
450 
451  \return The dataset indexes.
452  */
453  void getIndexes(te::da::DataSetType* dt);
454 
455  /*!
456  \brief It gets information about all the sequences in the datasource.
457 
458  \exception It throws an exception if the sequences cannot be loaded.
459 
460  \return The list of sequences in the data source,
461 
462  \note Not thread-safe!
463  \note PostGIS driver extended method.
464  */
465  std::vector<te::da::Sequence*> getSequences();
466 
467  private:
468 
469  DataSource* m_ds; //!< The PostGIS data source associated to this transactor.
470  Connection* m_conn; //!< The connection used by this transactor.
471  const int m_fetchSize; //!< The size of cursor fetch.
472  bool m_isInTransaction; //!< Tells if there is a transaction in progress.
473  bool m_getConstraints; //!< Flag that defines if the method getConstraints must be called.
474  };
475 
476  typedef boost::shared_ptr<Transactor> TransactorPtr;
477 
478  } // end namespace da
479 } // end namespace te
480 
481 #endif // __TERRALIB_POSTGIS_INTERNAL_TRANSACTOR_H
Geometric property.
A class that models the description of a dataset.
Definition: DataSetType.h:72
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:127
It describes a sequence (a number generator).
Definition: Sequence.h:56
A class that implements a connection to a PostgreSQL database.
Definition: Connection.h:68
A class that describes a check constraint.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:119
bool m_getConstraints
Flag that defines if the method getConstraints must be called.
Definition: Transactor.h:473
It models a property definition.
Definition: Property.h:59
Raster property.
#define TEPGISEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:195
The PostGIS driver.
Definition: DataSource.h:54
The transactor class for the PostGIS driver.
Definition: Transactor.h:62
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
TraverseType
A dataset can be traversed in two ways:
Definition: Enums.h:53
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
Configuration flags for the PostGIS Driver Implementation of TerraLib.
URI C++ Library.
It models a foreign key constraint for a DataSetType.
Definition: ForeignKey.h:50
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:74
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
Implementation of the data source for the PostGIS driver.
boost::shared_ptr< Transactor > TransactorPtr
Definition: Transactor.h:476
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
bool m_isInTransaction
Tells if there is a transaction in progress.
Definition: Transactor.h:472
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
Connection * m_conn
The connection used by this transactor.
Definition: Transactor.h:470
const int m_fetchSize
The size of cursor fetch.
Definition: Transactor.h:471
A Query is independent from the data source language/dialect.
Definition: Query.h:46
It describes an index associated to a DataSetType.
Definition: Index.h:54
DataSource * m_ds
The PostGIS data source associated to this transactor.
Definition: Transactor.h:469