Skip to content

Commit

Permalink
more soci cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
hobu committed Oct 1, 2012
1 parent 2029dad commit cee9766
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 74 deletions.
4 changes: 3 additions & 1 deletion include/pdal/drivers/soci/Reader.hpp
Expand Up @@ -39,6 +39,7 @@
#include <pdal/ReaderIterator.hpp>
#include <pdal/GDALUtils.hpp>

#include <pdal/drivers/soci/common.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>

Expand Down Expand Up @@ -93,7 +94,8 @@ class PDAL_DLL Reader : public pdal::Reader
Reader(const Reader&); // not implemented
//


Database_Type m_database_type;

// Connection m_connection;
// Statement m_initialQueryStatement;
// QueryType m_querytype;
Expand Down
54 changes: 2 additions & 52 deletions include/pdal/drivers/soci/Writer.hpp
Expand Up @@ -35,16 +35,9 @@
#ifndef INCLUDED_DRIVERS_SOCI_WRITER_HPP
#define INCLUDED_DRIVERS_SOCI_WRITER_HPP

#include <pdal/Writer.hpp>

#ifdef PDAL_HAVE_SOCI
#include <boost-optional.h>
#include <boost-tuple.h>
#include <boost-fusion.h>
#include <boost-gregorian-date.h>
#include <soci/soci.h>
#include <soci/postgresql/soci-postgresql.h>
#endif
#include <pdal/Writer.hpp>
#include <pdal/drivers/soci/common.hpp>

pdal::Writer* createSociWriter(pdal::Stage& prevStage, const pdal::Options& options);

Expand All @@ -64,50 +57,7 @@ PDAL_DLL void PDALRegister_writer_soci(void* factory);
PDAL_C_END
#endif

enum Database_Type
{
Database_Postgresql,
Database_Oracle,
Database_Unknown = 128
};

// class PDAL_DLL Table
// {
// public:
//
//
// Table( std::string const& name,
// ::soci::session* session);
//
// inline std::string const getName() const { return m_name; }
//
// virtual void create() = 0;
// virtual void destroy() = 0;
// virtual bool exists() = 0;
//
// private:
// std::string m_name;
// ::soci::session* m_session;
// };
//
// class PDAL_DLL Postgresql : public Table
// {
// public:
// Postgresql( std::string const& name,
// ::soci::session* session);
//
// virtual void create() {};
// virtual void destroy() {};
// virtual bool exists() { return true; }
// };

class soci_driver_error : public pdal_error
{
public:
soci_driver_error(std::string const& msg)
: pdal_error(msg)
{}
};

class PDAL_DLL Writer : public pdal::Writer
{
Expand Down
86 changes: 86 additions & 0 deletions include/pdal/drivers/soci/common.hpp
@@ -0,0 +1,86 @@
/******************************************************************************
* Copyright (c) 2012, Howard Butler, hobu.inc@gmail.com
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/

#ifndef INCLUDED_DRIVER_SOCI_COMMON_HPP
#define INCLUDED_DRIVER_SOCI_COMMON_HPP

#ifdef PDAL_HAVE_SOCI
#include <boost-optional.h>
#include <boost-tuple.h>
#include <boost-fusion.h>
#include <boost-gregorian-date.h>
#include <soci/soci.h>
#include <soci/postgresql/soci-postgresql.h>
#endif

#include <pdal/pdal_error.hpp>

namespace pdal
{
namespace drivers
{
namespace soci
{

class soci_driver_error : public pdal_error
{
public:
soci_driver_error(std::string const& msg)
: pdal_error(msg)
{}
};


enum Database_Type
{
DATABASE_POSTGRESQL,
DATABASE_ORACLE,
Database_Unknown = 128
};

enum QueryType
{
QUERY_SDO_PC = 0,
QUERY_SDO_PC_BLK_TYPE,
QUERY_SDO_BLK_PC_VIEW,
QUERY_UNKNOWN = 512
};


}
}
} // namespace pdal::driver::soci


#endif
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -399,6 +399,7 @@ set(PDAL_SOCI_HEADERS ${PDAL_HEADERS_DIR}/${PDAL_SOCI_PATH})
set(PDAL_SOCI_SRC ${PROJECT_SOURCE_DIR}/src/${PDAL_SOCI_PATH})

set(PDAL_DRIVERS_SOCI_HPP
${PDAL_SOCI_HEADERS}/common.hpp
${PDAL_SOCI_HEADERS}/Reader.hpp
${PDAL_SOCI_HEADERS}/Writer.hpp
)
Expand Down
42 changes: 21 additions & 21 deletions src/drivers/soci/Writer.cpp
Expand Up @@ -76,7 +76,7 @@ namespace soci
Writer::Writer(Stage& prevStage, const Options& options)
: pdal::Writer(prevStage, options)
, m_session(0)
, m_type(Database_Postgresql)
, m_type(DATABASE_POSTGRESQL)
, m_doCreateIndex(false)
, m_bounds(Bounds<double>())
, m_sdo_pc_is_initialized(false)
Expand Down Expand Up @@ -105,9 +105,9 @@ void Writer::initialize()

std::string connection_type = getOptions().getValueOrDefault<std::string>("type", "postgresql");
if (boost::iequals(connection_type, "oracle"))
m_type = Database_Oracle;
m_type = DATABASE_ORACLE;
else if (boost::iequals(connection_type, "postgresql"))
m_type = Database_Postgresql;
m_type = DATABASE_POSTGRESQL;
else
m_type = Database_Unknown;

Expand All @@ -120,7 +120,7 @@ void Writer::initialize()

try
{
if (m_type == Database_Postgresql)
if (m_type == DATABASE_POSTGRESQL)
m_session = new ::soci::session(::soci::postgresql, connection);

log()->get(logDEBUG) << "Connected to database" << std::endl;
Expand Down Expand Up @@ -206,9 +206,9 @@ bool Writer::CheckTableExists(std::string const& name)

std::ostringstream oss;

if (m_type == Database_Oracle)
if (m_type == DATABASE_ORACLE)
oss << "select table_name from user_tables";
else if (m_type == Database_Postgresql)
else if (m_type == DATABASE_POSTGRESQL)
oss << "SELECT tablename FROM pg_tables";

log()->get(logDEBUG) << "checking for " << name << " existence ... " << std::endl;
Expand Down Expand Up @@ -236,15 +236,15 @@ void Writer::CreateBlockTable(std::string const& name, boost::uint32_t srid)
{
std::ostringstream oss;

if (m_type == Database_Oracle)
if (m_type == DATABASE_ORACLE)
{
// We just create a new block table as a copy of
// the SDO_PC_BLK_TYPE
oss << "CREATE TABLE " << name << " AS SELECT * FROM MDSYS.SDO_PC_BLK_TABLE";
m_session->once << oss.str();
oss.str("");
}
else if (m_type == Database_Postgresql)
else if (m_type == DATABASE_POSTGRESQL)
{
std::string cloud_column = getOptions().getValueOrDefault<std::string>("cloud_column", "id");
std::string cloud_table = getOptions().getValueOrThrow<std::string>("cloud_table");
Expand Down Expand Up @@ -285,7 +285,7 @@ void Writer::DeleteBlockTable(std::string const& cloud_table_name,
oss.str("");

// Drop the table's dependencies
if (m_type == Database_Oracle)
if (m_type == DATABASE_ORACLE)
{
// These need to be uppercase to satisfy the PLSQL function
oss << "declare\n"
Expand All @@ -300,7 +300,7 @@ void Writer::DeleteBlockTable(std::string const& cloud_table_name,
}

// Go drop the table
if (m_type == Database_Oracle)
if (m_type == DATABASE_ORACLE)
{
// We need to clean up the geometry column before dropping the table
// Oracle upper cases the table name when inserting it in the
Expand All @@ -313,7 +313,7 @@ void Writer::DeleteBlockTable(std::string const& cloud_table_name,
m_session->once << oss.str();
oss.str("");

} else if (m_type == Database_Postgresql)
} else if (m_type == DATABASE_POSTGRESQL)
{
// We need to clean up the geometry column before dropping the table
oss << "SELECT DropGeometryColumn('" << boost::to_lower_copy(block_table_name) << "', 'extent')";
Expand All @@ -333,7 +333,7 @@ void Writer::CreateCloudTable(std::string const& name, boost::uint32_t srid)
std::ostringstream oss;


if (m_type == Database_Postgresql)
if (m_type == DATABASE_POSTGRESQL)
{
oss << "CREATE SEQUENCE " << boost::to_lower_copy(name)<<"_id_seq";
m_session->once << oss.str();
Expand Down Expand Up @@ -373,7 +373,7 @@ void Writer::DeleteCloudTable(std::string const& cloud_table_name,
oss.str("");

// Go drop the table
if (m_type == Database_Oracle)
if (m_type == DATABASE_ORACLE)
{

try
Expand Down Expand Up @@ -402,7 +402,7 @@ void Writer::DeleteCloudTable(std::string const& cloud_table_name,
}
oss.str("");

} else if (m_type == Database_Postgresql)
} else if (m_type == DATABASE_POSTGRESQL)
{
// We need to clean up the geometry column before dropping the table

Expand Down Expand Up @@ -455,7 +455,7 @@ void Writer::CreateIndexes( std::string const& table_name,
std::string index_name = index_name_ss.str().substr(0,29);

// Spatial indexes
if (m_type == Database_Oracle)
if (m_type == DATABASE_ORACLE)
{
oss << "CREATE INDEX "<< index_name << " on "
<< table_name << "(" << spatial_column_name
Expand All @@ -466,7 +466,7 @@ void Writer::CreateIndexes( std::string const& table_name,
}
m_session->once << oss.str();
oss.str("");
} else if (m_type == Database_Postgresql)
} else if (m_type == DATABASE_POSTGRESQL)
{
oss << "CREATE INDEX "<< index_name << " on "
<< boost::to_lower_copy(table_name) << " USING GIST ("<< boost::to_lower_copy(spatial_column_name) << ")";
Expand All @@ -486,7 +486,7 @@ void Writer::CreateIndexes( std::string const& table_name,

oss << "ALTER TABLE "<< table_name << " ADD CONSTRAINT "<< index_name <<
" PRIMARY KEY ("<<boost::to_lower_copy(cloud_column) <<", BLK_ID)";
if (m_type == Database_Oracle)
if (m_type == DATABASE_ORACLE)
{
oss <<" ENABLE VALIDATE";
}
Expand Down Expand Up @@ -654,11 +654,11 @@ void Writer::writeEnd(boost::uint64_t /*actualNumPointsWritten*/)
bool is3d = getOptions().getValueOrDefault<bool>("is3d", false);

CreateIndexes(block_table_name, "extent", is3d);
if (m_type == Database_Postgresql)
if (m_type == DATABASE_POSTGRESQL)
CreateIndexes(cloud_table_name, "extent", is3d, false);
}

if (m_type == Database_Oracle)
if (m_type == DATABASE_ORACLE)
{
std::string block_table_name = getOptions().getValueOrThrow<std::string>("block_table");
boost::uint32_t srid = getOptions().getValueOrThrow<boost::uint32_t>("srid");
Expand Down Expand Up @@ -706,7 +706,7 @@ void Writer::CreateCloud(Schema const& buffer_schema)

}

if (m_type == Database_Postgresql)
if (m_type == DATABASE_POSTGRESQL)
{
// strk: create table tabref( ref regclass );
// [11:32am] strk: insert into tabref values ('geometry_columns');
Expand Down Expand Up @@ -802,7 +802,7 @@ bool Writer::WriteBlock(PointBuffer const& buffer)
boost::int32_t blk_id = buffer.getField<boost::int32_t>(blockDim, 0);
boost::int32_t obj_id = getOptions().getValueOrThrow<boost::int32_t>("pc_id");
boost::int64_t num_points = static_cast<boost::int64_t>(buffer.getNumPoints());
if (m_type == Database_Postgresql)
if (m_type == DATABASE_POSTGRESQL)
{
std::string cloud_column = getOptions().getValueOrDefault<std::string>("cloud_column", "id");
bool is3d = getOptions().getValueOrDefault<bool>("is3d", false);
Expand Down

0 comments on commit cee9766

Please sign in to comment.