@@ -37,56 +37,51 @@ char const *pg_conn_t::error_msg() const noexcept
3737 return PQerrorMessage (m_conn.get ());
3838}
3939
40- pg_result_t pg_conn_t::query (ExecStatusType expect, char const *sql) const
40+ void pg_conn_t::set_config (char const *setting, char const *value) const
41+ {
42+ // Update pg_settings instead of using SET because it does not yield
43+ // errors on older versions of PostgreSQL where the settings are not
44+ // implemented.
45+ exec (" UPDATE pg_settings SET setting = '{}' WHERE name = '{}'" _format (
46+ value, setting));
47+ }
48+
49+ pg_result_t pg_conn_t::exec (char const *sql) const
4150{
4251 assert (m_conn);
4352
4453 log_sql (" {}" , sql);
4554 pg_result_t res{PQexec (m_conn.get (), sql)};
46- if (res.status () != expect ) {
55+ if (res.status () != PGRES_COMMAND_OK && res. status () != PGRES_TUPLES_OK ) {
4756 throw std::runtime_error{" Database error: {}" _format (error_msg ())};
4857 }
4958 return res;
5059}
5160
52- pg_result_t pg_conn_t::query (ExecStatusType expect,
53- std::string const &sql) const
54- {
55- return query (expect, sql.c_str ());
56- }
57-
58- void pg_conn_t::set_config (char const *setting, char const *value) const
61+ pg_result_t pg_conn_t::exec (std::string const &sql) const
5962{
60- // Update pg_settings instead of using SET because it does not yield
61- // errors on older versions of PostgreSQL where the settings are not
62- // implemented.
63- auto const sql =
64- " UPDATE pg_settings SET setting = '{}' WHERE name = '{}'" _format (
65- value, setting);
66- query (PGRES_TUPLES_OK, sql);
63+ return exec (sql.c_str ());
6764}
6865
69- void pg_conn_t::exec (char const *sql) const
66+ void pg_conn_t::copy_start (char const *sql) const
7067{
71- if (sql && sql[0 ] != ' \0 ' ) {
72- query (PGRES_COMMAND_OK, sql);
73- }
74- }
68+ assert (m_conn);
7569
76- void pg_conn_t::exec (std::string const &sql) const
77- {
78- if (!sql.empty ()) {
79- query (PGRES_COMMAND_OK, sql.c_str ());
70+ log_sql (" {}" , sql);
71+ pg_result_t const res{PQexec (m_conn.get (), sql)};
72+ if (res.status () != PGRES_COPY_IN) {
73+ throw std::runtime_error{
74+ " Database error on COPY: {}" _format (error_msg ())};
8075 }
8176}
8277
83- void pg_conn_t::copy_data (std::string const &sql ,
78+ void pg_conn_t::copy_send (std::string const &data ,
8479 std::string const &context) const
8580{
8681 assert (m_conn);
8782
88- log_sql_data (" Copy data to '{}':\n {}" , context, sql );
89- int const r = PQputCopyData (m_conn.get (), sql .c_str (), (int )sql .size ());
83+ log_sql_data (" Copy data to '{}':\n {}" , context, data );
84+ int const r = PQputCopyData (m_conn.get (), data .c_str (), (int )data .size ());
9085
9186 switch (r) {
9287 case 0 : // need to wait for write ready
@@ -101,17 +96,17 @@ void pg_conn_t::copy_data(std::string const &sql,
10196 break ;
10297 }
10398
104- if (sql .size () < 1100 ) {
105- log_error (" Data: {}" , sql );
99+ if (data .size () < 1100 ) {
100+ log_error (" Data: {}" , data );
106101 } else {
107- log_error (" Data: {}\n ...\n {}" , std::string (sql , 0 , 500 ),
108- std::string (sql, sql .size () - 500 ));
102+ log_error (" Data: {}\n ...\n {}" , std::string (data , 0 , 500 ),
103+ std::string (data, data .size () - 500 ));
109104 }
110105
111106 throw std::runtime_error{" COPYing data to Postgresql." };
112107}
113108
114- void pg_conn_t::end_copy (std::string const &context) const
109+ void pg_conn_t::copy_end (std::string const &context) const
115110{
116111 assert (m_conn);
117112
0 commit comments