Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
storm-ptr committed Jan 10, 2021
1 parent ad41737 commit 6e1cd73
Show file tree
Hide file tree
Showing 38 changed files with 204 additions and 172 deletions.
1 change: 0 additions & 1 deletion blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <bark/detail/utility.hpp>
#include <iomanip>
#include <sstream>
#include <vector>

namespace bark {
Expand Down
9 changes: 6 additions & 3 deletions db/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ namespace bark::db {
struct command {
virtual ~command() = default;

/// Returns the options for constructing @ref sql_builder
virtual sql_syntax syntax() = 0;
/// Option for constructing @ref sql_builder
virtual sql_quoted_identifier quoted_identifier() = 0;

/// Option for constructing @ref sql_builder
virtual sql_parameter_marker parameter_marker() = 0;

/// Executes the SQL in @ref sql_builder
virtual void exec(const sql_builder&) = 0;
Expand All @@ -37,7 +40,7 @@ struct command {

inline sql_builder builder(command& cmd)
{
return sql_builder{cmd.syntax()};
return sql_builder{cmd.quoted_identifier(), cmd.parameter_marker()};
}

inline void exec(command& cmd, const sql_builder& bld)
Expand Down
4 changes: 2 additions & 2 deletions db/detail/db2_dialect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ struct db2_dialect : dialect {
<< " ORDER BY is_primary DESC, indschema, indname, colseq";
}

meta::decoder_t geom_decoder() override
sql_decoder geom_decoder() override
{
return [](sql_builder& bld, std::string_view col_nm) {
bld << "db2gse.ST_AsBinary(" << id(col_nm) << ") AS " << id(col_nm);
};
}

meta::encoder_t geom_encoder(std::string_view type, int srid) override
sql_encoder geom_encoder(std::string_view type, int srid) override
{
return [type = std::string{type}, srid](sql_builder& bld, variant_t v) {
bld << "db2gse." << type << "(CAST(" << param{v}
Expand Down
4 changes: 2 additions & 2 deletions db/detail/ddl_guide.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <bark/proj/epsg.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <initializer_list>
#include <sstream>

namespace bark::db {

Expand All @@ -21,7 +20,8 @@ class ddl_guide {
std::pair<qualified_name, std::string> script(meta::table tbl)
{
tbl.name = id(as_mixin().cached_schema(), tbl.name.back());
sql_builder bld{embeded_params(as_mixin().make_command()->syntax())};
auto bld = sql_builder{as_mixin().make_command()->quoted_identifier(),
nullptr};
create_table_sql(bld, tbl);
add_geometry_columns_sql(bld, tbl);
create_indexes_sql(bld, tbl);
Expand Down
4 changes: 2 additions & 2 deletions db/detail/dialect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ struct dialect {

virtual std::string type_name(meta::column_type) = 0;

virtual meta::decoder_t geom_decoder() = 0;
virtual sql_decoder geom_decoder() = 0;

virtual meta::encoder_t geom_encoder(std::string_view type, int srid) = 0;
virtual sql_encoder geom_encoder(std::string_view type, int srid) = 0;
};

using dialect_holder = std::unique_ptr<dialect>;
Expand Down
4 changes: 2 additions & 2 deletions db/detail/mssql_dialect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ struct mssql_dialect : dialect {
<< ") ORDER BY is_primary_key DESC, name, key_ordinal";
}

meta::decoder_t geom_decoder() override
sql_decoder geom_decoder() override
{
return [](sql_builder& bld, std::string_view col_nm) {
bld << id(col_nm) << ".STAsBinary() AS " << id(col_nm);
};
}

meta::encoder_t geom_encoder(std::string_view type, int srid) override
sql_encoder geom_encoder(std::string_view type, int srid) override
{
return [type = std::string{type}, srid](sql_builder& bld, variant_t v) {
bld << type << "::STGeomFromWKB(" << param{v} << ", " << srid
Expand Down
4 changes: 2 additions & 2 deletions db/detail/mysql_dialect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ struct mysql_dialect : dialect {
<< ") ORDER BY is_primary DESC, index_name, seq_in_index";
}

meta::decoder_t geom_decoder() override
sql_decoder geom_decoder() override
{
return [](sql_builder& bld, std::string_view col_nm) {
bld << "ST_AsBinary(" << id(col_nm) << ", "
<< param{"axis-order=long-lat"} << ") AS " << id(col_nm);
};
}

meta::encoder_t geom_encoder(std::string_view, int srid) override
sql_encoder geom_encoder(std::string_view, int srid) override
{
return [srid](sql_builder& bld, variant_t val) {
bld << "ST_GeomFromWKB(" << param{val} << ", " << srid << ", "
Expand Down
4 changes: 2 additions & 2 deletions db/detail/mysql_old_dialect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ struct mysql_old_dialect : mysql_dialect {
<< ") AND LOWER(column_name) = LOWER(" << param{col} << "))";
}

meta::decoder_t geom_decoder() override { return st_as_binary(); }
sql_decoder geom_decoder() override { return st_as_binary(); }

meta::encoder_t geom_encoder(std::string_view, int srid) override
sql_encoder geom_encoder(std::string_view, int srid) override
{
return st_geom_from_wkb(srid);
}
Expand Down
4 changes: 2 additions & 2 deletions db/detail/postgres_dialect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ ORDER BY pri DESC, scm, nm, col
)";
}

meta::decoder_t geom_decoder() override { return st_as_binary(); }
sql_decoder geom_decoder() override { return st_as_binary(); }

meta::encoder_t geom_encoder(std::string_view type, int srid) override
sql_encoder geom_encoder(std::string_view type, int srid) override
{
if (type == "geography")
return [](sql_builder& bld, variant_t v) {
Expand Down
4 changes: 2 additions & 2 deletions db/detail/sqlite_dialect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ ORDER BY is_primary DESC, index_name, seqno
)";
}

meta::decoder_t geom_decoder() override { return st_as_binary(); }
sql_decoder geom_decoder() override { return st_as_binary(); }

meta::encoder_t geom_encoder(std::string_view, int srid) override
sql_encoder geom_encoder(std::string_view, int srid) override
{
return st_geom_from_wkb(srid);
}
Expand Down
22 changes: 8 additions & 14 deletions db/detail/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
#include <bark/db/sql_builder.hpp>
#include <bark/geometry/as_binary.hpp>
#include <initializer_list>
#include <sstream>

namespace bark::db {

template <class ColumnNames>
auto index_name(std::string_view tbl, const ColumnNames& cols)
qualified_name index_name(std::string_view tbl, const ColumnNames& cols)
{
std::ostringstream os;
os << "idx_" << tbl << "_" << list{cols, "_"};
return id(os.str());
return id(concat("idx_", tbl, "_", list{cols, "_"}));
}

inline auto index_name(const qualified_name& col_nm)
inline qualified_name index_name(const qualified_name& col_nm)
{
return index_name(col_nm.at(-2), std::initializer_list{col_nm.back()});
auto& col = col_nm.back();
auto& tbl = col_nm.at(-2);
return index_name(tbl, std::initializer_list{col});
}

/// OpenGIS Document 99-049
Expand Down Expand Up @@ -67,12 +66,6 @@ inline meta::column_type iso_type(std::string_view type, int scale)
return meta::column_type::Invalid;
}

inline sql_syntax embeded_params(sql_syntax syntax)
{
syntax.parameter_marker = nullptr;
return syntax;
}

inline void ogc_projections_sql(sql_builder& bld)
{
bld << "SELECT srid, (CASE LOWER(auth_name) WHEN " << param{"epsg"}
Expand All @@ -90,10 +83,11 @@ inline void iso_layers_sql(sql_builder& bld,

inline void iso_columns_sql(sql_builder& bld, const qualified_name& tbl_nm)
{
auto& tbl = tbl_nm.back();
auto& scm = tbl_nm.at(-2);
bld << "SELECT column_name, LOWER(data_type), numeric_scale FROM "
"information_schema.columns WHERE table_schema = "
<< param{scm} << " AND table_name = " << param{tbl_nm.back()}
<< param{scm} << " AND table_name = " << param{tbl}
<< " ORDER BY ordinal_position";
}

Expand Down
7 changes: 6 additions & 1 deletion db/gdal/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ class command : public db::command, private transaction<gdal::command> {
{
}

sql_syntax syntax() override { return embeded_params({}); }
sql_quoted_identifier quoted_identifier() override
{
return [](auto id) { return concat('"', id, '"'); };
}

sql_parameter_marker parameter_marker() override { return nullptr; }

void exec(const sql_builder& bld) override
{
Expand Down
4 changes: 2 additions & 2 deletions db/gdal/detail/bind_column.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ inline column_holder bind_column(OGRFieldType ogr_type)
case OFTBinary:
return std::make_unique<column_blob>();
default:
throw std::runtime_error("unsupported OGR type: " +
std::to_string(ogr_type));
throw std::runtime_error(
concat("unsupported OGR type: ", ogr_type));
}
}

Expand Down
2 changes: 1 addition & 1 deletion db/gdal/detail/dataset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class dataset {

auto drv = GDALGetDriverByName("PNG");
check(!!drv);
auto file = "/vsimem/" + std::to_string(++file_id_) + ".png";
auto file = concat("/vsimem/", ++file_id_, ".png");
{
dataset_holder cp(
GDALCreateCopy(drv, file.c_str(), ds_.get(), false, 0, 0, 0));
Expand Down
4 changes: 2 additions & 2 deletions db/gdal/detail/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class layer {
case OFTBinary:
return meta::column_type::Blob;
default:
throw std::runtime_error("unsupported OGR type: " +
std::to_string(ogr_type));
throw std::runtime_error(
concat("unsupported OGR type: ", ogr_type));
}
}

Expand Down
10 changes: 2 additions & 8 deletions db/meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,18 @@ enum class index_type { Invalid, Primary, Secondary };

enum class layer_type { Invalid, Geometry, Raster };

/// Converts the well-known binary representation of the geometry
using decoder_t = std::function<void(sql_builder&, std::string_view name)>;

/// Creates a geometry instance from a well-known binary representation
using encoder_t = std::function<void(sql_builder&, variant_t var)>;

/// Describes column
struct column {
std::string name;
column_type type = column_type::Invalid;
std::string projection; ///< PROJ.4 string for the spatial reference system
geometry::box_rtree tiles; ///< balanced data grid

decoder_t decoder
sql_decoder decoder

= [](sql_builder& bld, std::string_view name) { bld << id(name); };

encoder_t encoder
sql_encoder encoder

= [](sql_builder& bld, variant_t var) { bld << param{var}; };
};
Expand Down
13 changes: 7 additions & 6 deletions db/mysql/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ class command : public db::command, private transaction<mysql::command> {
check(con_, !mysql_set_character_set(con_.get(), "utf8"));
}

sql_syntax syntax() override
sql_quoted_identifier quoted_identifier() override
{
sql_syntax res{};
res.delimited_identifier = [](const auto& id) {
return '`' + id + '`';
};
return res;
return [](auto id) { return concat('`', id, '`'); };
}

sql_parameter_marker parameter_marker() override
{
return [](auto) { return "?"; };
}

void exec(const sql_builder& bld) override
Expand Down
3 changes: 1 addition & 2 deletions db/mysql/detail/bind_column.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ inline column_holder bind_column(enum_field_types type,
case MYSQL_TYPE_YEAR:
return std::make_unique<column_arr<std::string_view>>(bnd);
default:
throw std::runtime_error("unsupported MySQL type: " +
std::to_string(type));
throw std::runtime_error(concat("unsupported MySQL type: ", type));
}
}

Expand Down
13 changes: 7 additions & 6 deletions db/odbc/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ class command : public db::command {
SQL_DRIVER_NOPROMPT));
}

sql_syntax syntax() override
sql_quoted_identifier quoted_identifier() override
{
sql_syntax res{};
auto quote = get_info(dbc_, SQL_IDENTIFIER_QUOTE_CHAR);
res.delimited_identifier = [=](const auto& id) {
return quote + id + quote;
};
return res;
return [quote](auto id) { return concat(quote, id, quote); };
}

sql_parameter_marker parameter_marker() override
{
return [](auto) { return "?"; };
}

void exec(const sql_builder& bld) override
Expand Down
2 changes: 1 addition & 1 deletion db/odbc/detail/bind_column.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ inline column_holder bind_column(const stmt_holder& stmt, SQLUSMALLINT col)
case SQL_DB2_BLOB:
return std::make_unique<column_blob>();
}
throw std::runtime_error("invalid ODBC type: " + std::to_string(type));
throw std::runtime_error(concat("unsupported ODBC type: ", type));
}

} // namespace bark::db::odbc
Expand Down
38 changes: 23 additions & 15 deletions db/postgres/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <bark/db/postgres/detail/bind_column.hpp>
#include <bark/db/postgres/detail/bind_param.hpp>
#include <bark/db/postgres/detail/utility.hpp>
#include <sstream>
#include <stdexcept>

namespace bark::db::postgres {
Expand All @@ -24,25 +23,34 @@ class command : public db::command, private transaction<postgres::command> {
std::string_view pwd)
{
using namespace std::chrono;
std::ostringstream con;
con << "host='" << host << "' port='" << port << "' dbname='" << db
<< "' user='" << usr << "' password='" << pwd
<< "' connect_timeout=" << duration_cast<seconds>(DbTimeout).count()
<< " options='-c statement_timeout="
<< duration_cast<milliseconds>(DbTimeout).count()
<< "' client_encoding='UTF8'";
con_.reset(PQconnectdb((char*)con.str().c_str()));
auto con = concat("host='",
host,
"' port='",
port,
"' dbname='",
db,
"' user='",
usr,
"' password='",
pwd,
"' connect_timeout=",
duration_cast<seconds>(DbTimeout).count(),
" options='-c statement_timeout=",
duration_cast<milliseconds>(DbTimeout).count(),
"' client_encoding='UTF8'");
con_.reset(PQconnectdb((char*)con.c_str()));
if (!con_ || PQstatus(con_.get()) != CONNECTION_OK)
throw std::runtime_error("Postgres error");
}

sql_syntax syntax() override
sql_quoted_identifier quoted_identifier() override
{
sql_syntax res{};
res.parameter_marker = [](auto order) {
return '$' + std::to_string(order + 1);
};
return res;
return [](auto id) { return concat('"', id, '"'); };
}

sql_parameter_marker parameter_marker() override
{
return [](auto number) { return concat('$', number + 1); };
}

void exec(const sql_builder& bld) override
Expand Down
Loading

0 comments on commit 6e1cd73

Please sign in to comment.