|
|
| version 1.45, 2019/10/25 12:27:44 | version 1.47, 2021/11/03 14:51:51 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser PgSQL driver. | Parser PgSQL driver. |
| Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2019 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| Line 99 struct Connection { | Line 99 struct Connection { |
| PGconn *conn; | PGconn *conn; |
| const char* client_charset; | const char* client_charset; |
| bool autocommit; | bool autocommit; |
| bool with_default_transactions; | |
| bool standard_conforming_strings; | bool standard_conforming_strings; |
| }; | }; |
| Line 136 public: | Line 135 public: |
| datestyle=value& // 'SET DATESTYLE=value' available values are: ISO|SQL|Postgres|European|US|German [default=ISO] | datestyle=value& // 'SET DATESTYLE=value' available values are: ISO|SQL|Postgres|European|US|German [default=ISO] |
| autocommit=0& // 1 -- each statement is commited automatically, only when with_default_transaction enabled | autocommit=0& // 1 -- each statement is commited automatically, only when with_default_transaction enabled |
| standard_conforming_strings=1& // 0 -- escape \ char that could be needed for old servers | standard_conforming_strings=1& // 0 -- escape \ char that could be needed for old servers |
| with_default_transaction=0 // 1 -- wrap connection into BEGIN TRAN/COMMIT/ROLLBACK | |
| */ | */ |
| void connect( | void connect( |
| char* url, | char* url, |
| Line 159 public: | Line 157 public: |
| *connection_ref=&connection; | *connection_ref=&connection; |
| connection.services=&services; | connection.services=&services; |
| connection.client_charset=0; | connection.client_charset=0; |
| connection.autocommit=false; | connection.autocommit=true; |
| connection.with_default_transactions=false; | |
| connection.standard_conforming_strings=true; | connection.standard_conforming_strings=true; |
| connection.conn=PQsetdbLogin( | connection.conn=PQsetdbLogin( |
| Line 185 public: | Line 182 public: |
| } else if(strcasecmp(key, "datestyle")==0){ | } else if(strcasecmp(key, "datestyle")==0){ |
| datestyle=value; | datestyle=value; |
| } else if(strcasecmp(key, "autocommit")==0){ | } else if(strcasecmp(key, "autocommit")==0){ |
| if(atoi(value)==1){ | |
| if(!connection.with_default_transactions) | |
| services._throw("autocommit can be used only with_default_transaction enabled"); | |
| connection.autocommit=true; | |
| } | |
| } else if(strcmp(key, "with_default_transaction")==0){ | |
| if(atoi(value)==1) | |
| connection.with_default_transactions=true; | |
| } else if(strcmp(key, "WithoutDefaultTransaction")==0){ | |
| if(atoi(value)==0) | if(atoi(value)==0) |
| connection.with_default_transactions=true; | connection.autocommit=false; |
| } else if(strcasecmp(key, "standard_conforming_strings")==0){ | } else if(strcasecmp(key, "standard_conforming_strings")==0){ |
| if(atoi(value)==0) | if(atoi(value)==0) |
| connection.standard_conforming_strings=false; | connection.standard_conforming_strings=false; |
| Line 221 public: | Line 209 public: |
| _execute_cmd(connection, statement); | _execute_cmd(connection, statement); |
| } | } |
| _transaction_begin(connection); | if(!connection.autocommit) |
| _execute_cmd(connection, "set AUTOCOMMIT off"); | |
| } | } |
| void disconnect(void *aconnection){ | void disconnect(void *aconnection){ |
| Line 232 public: | Line 221 public: |
| void commit(void *aconnection){ | void commit(void *aconnection){ |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| _transaction_commit(connection); | if(!connection.autocommit) |
| _transaction_begin(connection); | _execute_cmd(connection, "COMMIT"); |
| } | } |
| void rollback(void *aconnection){ | void rollback(void *aconnection){ |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| _transaction_rollback(connection); | if(!connection.autocommit) |
| _transaction_begin(connection); | _execute_cmd(connection, "ROLLBACK"); |
| } | } |
| bool ping(void *aconnection) { | bool ping(void *aconnection) { |
| Line 355 public: | Line 344 public: |
| break; | break; |
| case PGRES_COMMAND_OK: // empty result: insert|delete|update|... | case PGRES_COMMAND_OK: // empty result: insert|delete|update|... |
| PQclear(res); | PQclear(res); |
| if(connection.autocommit) | |
| commit(aconnection); | |
| return; | return; |
| case PGRES_TUPLES_OK: | case PGRES_TUPLES_OK: |
| break; | break; |
| default: | default: |
| PQclear_throwPQerror; | PQclear_throwPQerror; |
| break; | break; |
| Line 483 cleanup: | Line 470 cleanup: |
| PQclear(res); | PQclear(res); |
| if(failed) | if(failed) |
| services._throw(sql_error); | services._throw(sql_error); |
| if(connection.autocommit) | |
| commit(aconnection); | |
| } | } |
| private: | private: |
| Line 521 private: | Line 505 private: |
| } | } |
| } | } |
| void _transaction_begin(Connection& connection){ | |
| _execute_transactions_cmd(connection, "BEGIN"); | |
| } | |
| void _transaction_commit(Connection& connection){ | |
| _execute_transactions_cmd(connection, "COMMIT"); | |
| } | |
| void _transaction_rollback(Connection& connection){ | |
| _execute_transactions_cmd(connection, "ROLLBACK"); | |
| } | |
| void _execute_transactions_cmd(const Connection& connection, const char *query){ | |
| if(connection.with_default_transactions) // without ?with_default_transaction=1 user must execute BEGIN/COMMIT/ROLLBACK by himself | |
| _execute_cmd(connection, query); | |
| } | |
| // executes a query and throw away the result. | // executes a query and throw away the result. |
| void _execute_cmd(const Connection& connection, const char *query){ | void _execute_cmd(const Connection& connection, const char *query){ |
| if(PGresult *res=PQexec(connection.conn, query)) | if(PGresult *res=PQexec(connection.conn, query)) |