22 #ifndef DBALLE_DB_INTERNALS_H
23 #define DBALLE_DB_INTERNALS_H
34 #include <wreport/error.h>
41 #define DBA_USE_TRANSACTIONS
44 #define DBA_DB_QUIRK_NO_ROWCOUNT_IN_DIAG (1 << 0)
60 #define TRACE(...) fprintf(stderr, __VA_ARGS__)
61 #define IFTRACE if (1)
64 #define TRACE(...) do { } while (0)
66 #define IFTRACE if (0)
86 error_odbc(SQLSMALLINT handleType, SQLHANDLE handle,
const std::string& msg);
89 wreport::ErrorCode code()
const throw () {
return wreport::WR_ERR_ODBC; }
91 virtual const char* what()
const throw () {
return msg.c_str(); }
93 static void throwf(SQLSMALLINT handleType, SQLHANDLE handle,
const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
145 void connect(
const char* dsn,
const char* user,
const char* password);
146 void connect_file(
const std::string& fname);
147 void driver_connect(
const char* config);
148 std::string driver_name();
149 std::string driver_version();
150 void get_info(SQLUSMALLINT info_type, SQLINTEGER& res);
151 void set_autocommit(
bool val);
174 void set_setting(
const std::string& key,
const std::string& value);
198 void init_after_connect();
215 void commit() { conn.
commit(); fired =
true; }
216 void rollback() { conn.
rollback(); fired =
true; }
226 #ifdef DEBUG_WARN_OPEN_TRANSACTIONS
227 std::string debug_query;
229 bool debug_reached_completion;
235 void bind_in(
int idx,
const DBALLE_SQL_C_SINT_TYPE& val);
236 void bind_in(
int idx,
const DBALLE_SQL_C_SINT_TYPE& val,
const SQLLEN& ind);
237 void bind_in(
int idx,
const DBALLE_SQL_C_UINT_TYPE& val);
238 void bind_in(
int idx,
const DBALLE_SQL_C_UINT_TYPE& val,
const SQLLEN& ind);
239 void bind_in(
int idx,
const unsigned short& val);
240 void bind_in(
int idx,
const char* val);
241 void bind_in(
int idx,
const char* val,
const SQLLEN& ind);
242 void bind_in(
int idx,
const SQL_TIMESTAMP_STRUCT& val);
244 void bind_out(
int idx, DBALLE_SQL_C_SINT_TYPE& val);
245 void bind_out(
int idx, DBALLE_SQL_C_SINT_TYPE& val, SQLLEN& ind);
246 void bind_out(
int idx, DBALLE_SQL_C_UINT_TYPE& val);
247 void bind_out(
int idx, DBALLE_SQL_C_UINT_TYPE& val, SQLLEN& ind);
248 void bind_out(
int idx,
unsigned short& val);
249 void bind_out(
int idx,
char* val, SQLLEN buflen);
250 void bind_out(
int idx,
char* val, SQLLEN buflen, SQLLEN& ind);
251 void bind_out(
int idx, SQL_TIMESTAMP_STRUCT& val);
252 void bind_out(
int idx, SQL_TIMESTAMP_STRUCT& val, SQLLEN& ind);
254 void prepare(
const char* query);
255 void prepare(
const char* query,
int qlen);
277 bool fetch_expecting_one();
279 void close_cursor_if_needed();
285 void set_cursor_forward_only();
286 void set_cursor_static();
289 bool error_is_ignored();
290 bool is_error(
int sqlres);
301 DBALLE_SQL_C_SINT_TYPE out;
307 const DBALLE_SQL_C_SINT_TYPE&
read();
315 static inline bool operator!=(
const SQL_TIMESTAMP_STRUCT& a,
const SQL_TIMESTAMP_STRUCT& b)
317 return a.year != b.year || a.month != b.month || a.day != b.day || a.hour != b.hour || a.minute != b.minute || a.second != b.second || a.fraction != b.fraction;
320 std::ostream& operator<<(std::ostream& o,
const SQL_TIMESTAMP_STRUCT& t);
322 static inline SQL_TIMESTAMP_STRUCT make_sql_timestamp(
int year,
int month,
int day,
int hour,
int minute,
int second)
324 SQL_TIMESTAMP_STRUCT res;
enum ServerType server_type
Type of SQL server we are connected to.
Definition: internals.h:131
Report an ODBC error, using informations from the ODBC diagnostic record.
Definition: internals.h:78
const DBALLE_SQL_C_SINT_TYPE & read()
Read the current value of the sequence.
error_odbc(SQLSMALLINT handleType, SQLHANDLE handle, const std::string &msg)
Copy informations from the ODBC diagnostic record to the dba error report.
bool connected
True if the connection is open.
Definition: internals.h:129
void rollback()
Rollback a transaction.
Database connection.
Definition: internals.h:124
db::Statement * stm_last_insert_id
Precompiled LAST_INSERT_ID (or equivalent) SQL statement.
Definition: internals.h:137
The ODBC specification is imperfect with regards to integer sizes on 64bit platforms, and different ODBC drivers are currently interpreting it differently.
RAII transaction.
Definition: internals.h:207
int exec_direct_and_close(const char *query)
const char * ignore_error
If non-NULL, ignore all errors with this code.
Definition: internals.h:225
bool has_table(const std::string &name)
Check if the database contains a table.
void commit()
Commit a transaction.
Implementation of an efficient string buffer for composing database queries.
void drop_settings()
Drop the settings table.
ODBC environment.
Definition: internals.h:108
DBALLE_SQL_C_SINT_TYPE m_last_insert_id
ID of the last autogenerated primary key.
Definition: internals.h:139
ODBC statement.
Definition: internals.h:220
void set_setting(const std::string &key, const std::string &value)
Set a value in the settings table.
SQLHDBC od_conn
ODBC database connection.
Definition: internals.h:127
int get_last_insert_id()
Return LAST_INSERT_ID or LAST_INSER_ROWID or whatever is appropriate for the current database...
unsigned server_quirks
Bitfield of quirks we should be aware of when using ODBC.
Definition: internals.h:133
ODBC statement to read a sequence.
Definition: internals.h:299
void drop_sequence_if_exists(const char *name)
Delete a sequence in the database if it exists, otherwise do nothing.
size_t select_rowcount()
Row count for select operations.
std::string get_setting(const std::string &key)
Get a value from the settings table.
size_t rowcount()
Row count for insert, delete and other non-select operations.
void drop_table_if_exists(const char *name)
Delete a table in the database if it exists, otherwise do nothing.
int exec_direct(const char *query)