1313#include " changesetwriter.h"
1414#include " postgresutils.h"
1515#include " sqliteutils.h"
16+ #include " geodiffcontext.hpp"
1617
1718#include < algorithm>
1819#include < iostream>
@@ -62,14 +63,19 @@ class PostgresTransaction
6263};
6364
6465
65- static void logApplyConflict ( const std::string &type, const ChangesetEntry &entry )
66+ static void logApplyConflict ( Context *context, const std::string &type, const ChangesetEntry &entry )
6667{
67- Logger::instance ().warn ( " CONFLICT: " + type + " :\n " + changesetEntryToJSON ( entry ) );
68+ context-> logger ().warn ( " CONFLICT: " + type + " :\n " + changesetEntryToJSON ( entry ) );
6869}
6970
7071// ///
7172
7273
74+ PostgresDriver::PostgresDriver ( Context *context )
75+ : Driver( context )
76+ {
77+ }
78+
7379PostgresDriver::~PostgresDriver ()
7480{
7581 close ();
@@ -339,7 +345,7 @@ TableSchema PostgresDriver::tableSchema( const std::string &tableName, bool useM
339345 col.setGeometry ( geomTypeName, srsId, hasM, hasZ );
340346 }
341347
342- col.type = columnType ( type, Driver::POSTGRESDRIVERNAME, col.isGeometry );
348+ col.type = columnType ( context ()-> logger (), type, Driver::POSTGRESDRIVERNAME, col.isGeometry );
343349 col.isNotNull = ( res.value ( i, 3 ) == " t" );
344350 col.isAutoIncrement = ( res.value ( i, 4 ) == " t" );
345351
@@ -483,7 +489,7 @@ static bool isColumnGeometry( const TableColumnInfo &col )
483489 return col.isGeometry ;
484490}
485491
486- static Value resultToValue ( const PostgresResult &res, int r, size_t i, const TableColumnInfo &col )
492+ static Value resultToValue ( Context *context, const PostgresResult &res, int r, size_t i, const TableColumnInfo &col )
487493{
488494 Value v;
489495 if ( res.isNull ( r, i ) )
@@ -525,7 +531,7 @@ static Value resultToValue( const PostgresResult &res, int r, size_t i, const Ta
525531 std::string binString = hex2bin ( valueStr.substr ( 2 ) ); // chop \x prefix
526532
527533 // 2. create binary header
528- std::string binHead = createGpkgHeader ( binString, col );
534+ std::string binHead = createGpkgHeader ( context-> logger (), binString, col );
529535
530536 // 3. copy header and body
531537 std::string gpb ( binHead.size () + binString.size (), 0 );
@@ -586,7 +592,7 @@ static std::string valueToSql( const Value &v, const TableColumnInfo &col )
586592}
587593
588594
589- static void handleInserted ( const std::string &schemaNameBase, const std::string &schemaNameModified, const std::string &tableName, const TableSchema &tbl, bool reverse, PGconn *conn, ChangesetWriter &writer, bool &first )
595+ static void handleInserted ( Context *context, const std::string &schemaNameBase, const std::string &schemaNameModified, const std::string &tableName, const TableSchema &tbl, bool reverse, PGconn *conn, ChangesetWriter &writer, bool &first )
590596{
591597 std::string sqlInserted = sqlFindInserted ( schemaNameBase, schemaNameModified, tableName, tbl, reverse );
592598 PostgresResult res ( execSql ( conn, sqlInserted ) );
@@ -607,7 +613,7 @@ static void handleInserted( const std::string &schemaNameBase, const std::string
607613 size_t numColumns = tbl.columns .size ();
608614 for ( size_t i = 0 ; i < numColumns; ++i )
609615 {
610- Value v ( resultToValue ( res, r, i, tbl.columns [i] ) );
616+ Value v ( resultToValue ( context, res, r, i, tbl.columns [i] ) );
611617 if ( reverse )
612618 e.oldValues .push_back ( v );
613619 else
@@ -619,7 +625,7 @@ static void handleInserted( const std::string &schemaNameBase, const std::string
619625}
620626
621627
622- static void handleUpdated ( const std::string &schemaNameBase, const std::string &schemaNameModified, const std::string &tableName, const TableSchema &tbl, PGconn *conn, ChangesetWriter &writer, bool &first )
628+ static void handleUpdated ( Context *context, const std::string &schemaNameBase, const std::string &schemaNameModified, const std::string &tableName, const TableSchema &tbl, PGconn *conn, ChangesetWriter &writer, bool &first )
623629{
624630 std::string sqlModified = sqlFindModified ( schemaNameBase, schemaNameModified, tableName, tbl );
625631 PostgresResult res ( execSql ( conn, sqlModified ) );
@@ -651,8 +657,8 @@ static void handleUpdated( const std::string &schemaNameBase, const std::string
651657 size_t numColumns = tbl.columns .size ();
652658 for ( size_t i = 0 ; i < numColumns; ++i )
653659 {
654- Value v1 ( resultToValue ( res, r, i + numColumns, tbl.columns [i] ) );
655- Value v2 ( resultToValue ( res, r, i, tbl.columns [i] ) );
660+ Value v1 ( resultToValue ( context, res, r, i + numColumns, tbl.columns [i] ) );
661+ Value v2 ( resultToValue ( context, res, r, i, tbl.columns [i] ) );
656662 bool pkey = tbl.columns [i].isPrimaryKey ;
657663 bool updated = v1 != v2;
658664 e.oldValues .push_back ( ( pkey || updated ) ? v1 : Value () );
@@ -696,9 +702,9 @@ void PostgresDriver::createChangeset( ChangesetWriter &writer )
696702
697703 bool first = true ;
698704
699- handleInserted ( mBaseSchema , mModifiedSchema , tableName, tbl, false , mConn , writer, first ); // INSERT
700- handleInserted ( mBaseSchema , mModifiedSchema , tableName, tbl, true , mConn , writer, first ); // DELETE
701- handleUpdated ( mBaseSchema , mModifiedSchema , tableName, tbl, mConn , writer, first ); // UPDATE
705+ handleInserted ( context (), mBaseSchema , mModifiedSchema , tableName, tbl, false , mConn , writer, first ); // INSERT
706+ handleInserted ( context (), mBaseSchema , mModifiedSchema , tableName, tbl, true , mConn , writer, first ); // DELETE
707+ handleUpdated ( context (), mBaseSchema , mModifiedSchema , tableName, tbl, mConn , writer, first ); // UPDATE
702708 }
703709}
704710
@@ -844,9 +850,9 @@ void PostgresDriver::applyChangeset( ChangesetReader &reader )
844850 PostgresResult res ( execSql ( mConn , sql ) );
845851 if ( res.status () != PGRES_COMMAND_OK )
846852 {
847- logApplyConflict ( " insert_failed" , entry );
853+ logApplyConflict ( context (), " insert_failed" , entry );
848854 ++conflictCount;
849- Logger::instance ().warn ( " Failure doing INSERT: " + res.statusErrorMessage () );
855+ context ()-> logger ().warn ( " Failure doing INSERT: " + res.statusErrorMessage () );
850856 }
851857 if ( res.affectedRows () != " 1" )
852858 {
@@ -868,15 +874,15 @@ void PostgresDriver::applyChangeset( ChangesetReader &reader )
868874 PostgresResult res ( execSql ( mConn , sql ) );
869875 if ( res.status () != PGRES_COMMAND_OK )
870876 {
871- logApplyConflict ( " update_failed" , entry );
877+ logApplyConflict ( context (), " update_failed" , entry );
872878 ++conflictCount;
873- Logger::instance ().warn ( " Failure doing UPDATE: " + res.statusErrorMessage () );
879+ context ()-> logger ().warn ( " Failure doing UPDATE: " + res.statusErrorMessage () );
874880 }
875881 if ( res.affectedRows () != " 1" )
876882 {
877- logApplyConflict ( " update_nothing" , entry );
883+ logApplyConflict ( context (), " update_nothing" , entry );
878884 ++conflictCount;
879- Logger::instance ().warn ( " Wrong number of affected rows! Expected 1, got: " + res.affectedRows () + " \n SQL: " + sql );
885+ context ()-> logger ().warn ( " Wrong number of affected rows! Expected 1, got: " + res.affectedRows () + " \n SQL: " + sql );
880886 }
881887 }
882888 else if ( entry.op == ChangesetEntry::OpDelete )
@@ -885,22 +891,22 @@ void PostgresDriver::applyChangeset( ChangesetReader &reader )
885891 PostgresResult res ( execSql ( mConn , sql ) );
886892 if ( res.status () != PGRES_COMMAND_OK )
887893 {
888- logApplyConflict ( " delete_failed" , entry );
894+ logApplyConflict ( context (), " delete_failed" , entry );
889895 ++conflictCount;
890- Logger::instance ().warn ( " Failure doing DELETE: " + res.statusErrorMessage () );
896+ context ()-> logger ().warn ( " Failure doing DELETE: " + res.statusErrorMessage () );
891897 }
892898 if ( res.affectedRows () != " 1" )
893899 {
894- logApplyConflict ( " delete_nothing" , entry );
895- Logger::instance ().warn ( " Wrong number of affected rows! Expected 1, got: " + res.affectedRows () );
900+ logApplyConflict ( context (), " delete_nothing" , entry );
901+ context ()-> logger ().warn ( " Wrong number of affected rows! Expected 1, got: " + res.affectedRows () );
896902 }
897903 }
898904 else
899905 throw GeoDiffException ( " Unexpected operation" );
900906 }
901907
902908 // at the end, update any SEQUENCE objects if needed
903- for ( const std::pair<std::string, int64_t > &it : autoIncrementTablesToFix )
909+ for ( const auto &it : autoIncrementTablesToFix )
904910 updateSequenceObject ( tableNameToSequenceName[it.first ], it.second );
905911
906912 if ( !conflictCount )
@@ -947,7 +953,7 @@ void PostgresDriver::updateSequenceObject( const std::string &seqName, int64_t m
947953
948954 if ( currValue < maxValue )
949955 {
950- Logger::instance ().info ( " Updating sequence " + seqName + " from " + std::to_string ( currValue ) + " to " + std::to_string ( maxValue ) );
956+ context ()-> logger ().info ( " Updating sequence " + seqName + " from " + std::to_string ( currValue ) + " to " + std::to_string ( maxValue ) );
951957
952958 std::string sql = " SELECT setval(" + quotedString ( seqName ) + " , " + std::to_string ( maxValue ) + " )" ;
953959 PostgresResult resSetVal ( execSql ( mConn , sql ) );
@@ -1025,7 +1031,7 @@ void PostgresDriver::dumpData( ChangesetWriter &writer, bool useModified )
10251031 size_t numColumns = tbl.columns .size ();
10261032 for ( size_t i = 0 ; i < numColumns; ++i )
10271033 {
1028- e.newValues .push_back ( Value ( resultToValue ( res, r, i, tbl.columns [i] ) ) );
1034+ e.newValues .push_back ( Value ( resultToValue ( context (), res, r, i, tbl.columns [i] ) ) );
10291035 }
10301036 writer.writeEntry ( e );
10311037 }
0 commit comments