QueryInsertExample.cpp
Go to the documentation of this file.
1 // Examples
2 #include "DataAccessExamples.h"
3 
4 // TerraLib
5 
6 // STL
7 #include <iostream>
8 #include <exception>
9 
10 std::unique_ptr<te::da::DataSource> GetPostGISConnection();
11 
13 {
14 // Create a simple DataSetType.
15  std::string outName = "exampleInsert";
16  std::unique_ptr<te::da::DataSetType> dsType(new te::da::DataSetType(outName));
17 
18  // Primary key
19  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(outName + "_id", te::dt::INT32_TYPE);
20  pkProperty->setAutoNumber(true);
21  dsType->add(pkProperty);
22 
23  te::da::PrimaryKey* pk = new te::da::PrimaryKey(outName + "_pk", dsType.get());
24  pk->add(pkProperty);
25  dsType->setPrimaryKey(pk);
26 
28  dsType->add(region);
29 
30  te::gm::GeometryProperty* geomProp = new te::gm::GeometryProperty("geom");
32  dsType->add(geomProp);
33 
34  try
35  {
36  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
37  if (!ds.get())
38  return;
39 
40  ds->open();
41 
42 // get fields from output datasettype
43  te::da::Fields* outFields = new te::da::Fields;
44  std::vector<te::dt::Property*> outputProps = dsType->getProperties();
45 
46  for (std::size_t i = 0; i < outputProps.size(); ++i)
47  {
48  te::da::Field* field = new te::da::Field(outputProps[i]->getName());
49  outFields->push_back(field);
50  }
51 
52 // get a transactor to interact to the data source
53  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
54 
55  std::map<std::string, std::string> options;
56  ds->createDataSet(dsType.get(), options);
57 
58  te::da::DataSetName* dsName = new te::da::DataSetName(outName);
59 
60  te::da::Fields* fields = new te::da::Fields;
61 
62  te::da::Field* f_gid = new te::da::Field("gid");
63  fields->push_back(f_gid);
64 
65  te::da::Field* f_deno = new te::da::Field("deno");
66  fields->push_back(f_deno);
67 
68  te::da::Field* f_geom = new te::da::Field("the_geom");
69  fields->push_back(f_geom);
70 
71  te::da::From* from = new te::da::From;
72  te::da::FromItem* fromItem = new te::da::DataSetName("distritos");
73  from->push_back(fromItem);
74 
75  te::da::Select* select = new te::da::Select(fields, from);
76 
77  te::da::Insert insert(dsName, outFields, select);
78 
79  ds->execute(insert);
80 
81 // At the end, let's release transactor and close data source
82  delete transactor.release();
83  ds->close();
84  }
85  catch(const std::exception& e)
86  {
87  std::cout << std::endl << "An exception has occurred in QueryInsertExample: " << e.what() << std::endl;
88  }
89  catch(...)
90  {
91  std::cout << std::endl << "An unexpected exception has occurred in QueryInsertExample!" << std::endl;
92  }
93 }
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
Geometric property.
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
void setGeometryType(GeomType t)
It sets the geometry subtype.
An atomic property like an integer or double.
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
A class that models the description of a dataset.
Definition: DataSetType.h:72
static te::dt::Date ds(2010, 01, 01)
The Insert object can add the return of a select object.
Definition: Insert.h:50
void QueryInsertExample()
Quering Insert clause.
Examples on how to access/manipulate DataSources in TerraLib.
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
std::unique_ptr< te::da::DataSource > GetPostGISConnection()
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52