/usr/include/dballe/db/internals.h is in libdballe-dev 5.10-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | /*
* db/internals - Internal support infrastructure for the DB
*
* Copyright (C) 2005--2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Enrico Zini <enrico@enricozini.com>
*/
#ifndef DBALLE_DB_INTERNALS_H
#define DBALLE_DB_INTERNALS_H
/** @file
* @ingroup db
*
* Database functions and data structures used by the db module, but not
* exported as official API.
*/
#include <dballe/db/querybuf.h>
#include <dballe/db/odbcworkarounds.h>
#include <wreport/error.h>
#include <sqltypes.h>
/*
* Define to true to enable the use of transactions during writes
*/
#define DBA_USE_TRANSACTIONS
/* Define this to enable referential integrity */
#undef USE_REF_INT
namespace dballe {
namespace db {
/** Trace macros internally used for debugging
* @{
*/
// #define TRACE_DB
#ifdef TRACE_DB
#define TRACE(...) fprintf(stderr, __VA_ARGS__)
#define IFTRACE if (1)
#else
/** Ouput a trace message */
#define TRACE(...) do { } while (0)
/** Prefix a block of code to compile only if trace is enabled */
#define IFTRACE if (0)
#endif
/** @} */
// Define this to get warnings when a Statement is closed but its data have not
// all been read yet
// #define DEBUG_WARN_OPEN_TRANSACTIONS
/**
* Report an ODBC error, using informations from the ODBC diagnostic record
*/
struct error_odbc : public wreport::error
{
std::string msg;
/**
* Copy informations from the ODBC diagnostic record to the dba error
* report
*/
error_odbc(SQLSMALLINT handleType, SQLHANDLE handle, const std::string& msg);
~error_odbc() throw () {}
wreport::ErrorCode code() const throw () { return wreport::WR_ERR_ODBC; }
virtual const char* what() const throw () { return msg.c_str(); }
static void throwf(SQLSMALLINT handleType, SQLHANDLE handle, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4);
};
/**
* Supported SQL servers.
*/
enum ServerType
{
MYSQL,
SQLITE,
ORACLE,
POSTGRES,
};
/// ODBC environment
struct Environment
{
SQLHENV od_env;
Environment();
~Environment();
static Environment& get();
private:
// disallow copy
Environment(const Environment&);
Environment& operator=(const Environment&);
};
/// Database connection
struct Connection
{
/** ODBC database connection */
SQLHDBC od_conn;
/** True if the connection is open */
bool connected;
/** Type of SQL server we are connected to */
enum ServerType server_type;
Connection();
~Connection();
void connect(const char* dsn, const char* user, const char* password);
void driver_connect(const char* config);
std::string driver_name();
void set_autocommit(bool val);
/// Commit a transaction
void commit();
/// Rollback a transaction
void rollback();
protected:
void init_after_connect();
private:
// disallow copy
Connection(const Connection&);
Connection& operator=(const Connection&);
};
/// RAII transaction
struct Transaction
{
Connection& conn;
bool fired;
Transaction(Connection& conn) : conn(conn), fired(false) {}
~Transaction() { if (!fired) rollback(); }
void commit() { conn.commit(); fired = true; }
void rollback() { conn.rollback(); fired = true; }
};
/// ODBC statement
struct Statement
{
//Connection& conn;
SQLHSTMT stm;
/// If non-NULL, ignore all errors with this code
const char* ignore_error;
#ifdef DEBUG_WARN_OPEN_TRANSACTIONS
/// Debugging aids: dump query to stderr if destructor is called before fetch returned SQL_NO_DATA
std::string debug_query;
bool debug_reached_completion;
#endif
Statement(Connection& conn);
~Statement();
void bind_in(int idx, const DBALLE_SQL_C_SINT_TYPE& val);
void bind_in(int idx, const DBALLE_SQL_C_SINT_TYPE& val, const SQLLEN& ind);
void bind_in(int idx, const DBALLE_SQL_C_UINT_TYPE& val);
void bind_in(int idx, const DBALLE_SQL_C_UINT_TYPE& val, const SQLLEN& ind);
void bind_in(int idx, const unsigned short& val);
void bind_in(int idx, const char* val);
void bind_in(int idx, const char* val, const SQLLEN& ind);
void bind_in(int idx, const SQL_TIMESTAMP_STRUCT& val);
void bind_out(int idx, DBALLE_SQL_C_SINT_TYPE& val);
void bind_out(int idx, DBALLE_SQL_C_SINT_TYPE& val, SQLLEN& ind);
void bind_out(int idx, DBALLE_SQL_C_UINT_TYPE& val);
void bind_out(int idx, DBALLE_SQL_C_UINT_TYPE& val, SQLLEN& ind);
void bind_out(int idx, unsigned short& val);
void bind_out(int idx, char* val, SQLLEN buflen);
void bind_out(int idx, char* val, SQLLEN buflen, SQLLEN& ind);
void bind_out(int idx, SQL_TIMESTAMP_STRUCT& val);
void prepare(const char* query);
void prepare(const char* query, int qlen);
/// @return SQLExecute's result
int execute();
/// @return SQLExecute's result
int exec_direct(const char* query);
/// @return SQLExecute's result
int exec_direct(const char* query, int qlen);
/// @return SQLExecute's result
int execute_and_close();
/// @return SQLExecute's result
int exec_direct_and_close(const char* query);
/// @return SQLExecute's result
int exec_direct_and_close(const char* query, int qlen);
/**
* @return the number of columns in the result set (or 0 if the statement
* did not return columns)
*/
int columns_count();
bool fetch();
bool fetch_expecting_one();
void close_cursor();
size_t rowcount();
void set_cursor_forward_only();
protected:
bool error_is_ignored();
bool is_error(int sqlres);
private:
// disallow copy
Statement(const Statement&);
Statement& operator=(const Statement&);
};
/// ODBC statement to read a sequence
struct Sequence : public Statement
{
DBALLE_SQL_C_SINT_TYPE out;
Sequence(Connection& conn, const char* name);
~Sequence();
/// Read the current value of the sequence
const DBALLE_SQL_C_SINT_TYPE& read();
private:
// disallow copy
Sequence(const Sequence&);
Sequence& operator=(const Sequence&);
};
/// Return the default repinfo file pathname
const char* default_repinfo_file();
} // namespace db
} // namespace dballe
/* vim:set ts=4 sw=4: */
#endif
|