unittest/postgis/Utils.cpp
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/unittest/postgis/Utils.cpp
22 
23  \brief A Utils functions for postgis unit test.
24 
25  */
26 
27 // TerraLib
31 
32 #include "Utils.h"
33 
34 // STL
35 #include <iostream>
36 
37 std::unique_ptr<te::da::DataSource> GetPostGISConnection()
38 {
39  // let's give the minimal server connection information needed to connect to
40  // the database server
41  std::string user, password, host, port, path, query;
42  std::string strURI = "pgsql://"; // The base of the URI
43  host = "localhost";
44  port = "5432";
45  user = "postgres";
46  password = "postgres";
47  path = "TerraLib5";
48  query = "PG_CLIENT_ENCODING=" +
50  query += "&PG_CONNECT_TIMEOUT=4";
51 
52  strURI += user + ":";
53  strURI += password + "@";
54  strURI += host + ":";
55  strURI += port + "/";
56  strURI += path + "?";
57  strURI += query;
58 
59  // create a data source using the data source factory
60  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("POSTGIS", strURI);
61 
62  try
63  {
64  ds->open();
65  }
66  catch(const std::exception& e)
67  {
68  std::cout << "Datasource " << host << "/" << path
69  << " can not be used!\nMake sure to have the correct connection "
70  "parameters\n";
71  std::cout << "Error: " << e.what() << std::endl;
72  ds.reset();
73  return ds;
74  }
75  catch(...)
76  {
77  std::cout << "Datasource " << host << "/" << path
78  << " can not be used!\nMake sure to have the correct connection "
79  "parameters\n";
80  ds.reset();
81  return ds;
82  }
83 
84  std::cout << "Using datasource " << host << "/" << path << std::endl;
85  ds->close();
86  return ds;
87 }
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
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...
std::unique_ptr< te::da::DataSource > GetPostGISConnection()
It returns the union of a geometry vector.
static std::string getEncodingName(EncodingType et)
Retrive a string from a given character encoding type enum.
A factory for data sources.