This file is indexed.

/usr/include/dballe/db/internals.h is in libdballe-dev 6.8-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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * db/internals - Internal support infrastructure for the DB
 *
 * Copyright (C) 2005--2013  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

// Bit values for server/driver quirks
#define DBA_DB_QUIRK_NO_ROWCOUNT_IN_DIAG (1 << 0)

/* Define this to enable referential integrity */
#undef USE_REF_INT

namespace dballe {
namespace db {
struct Statement;

/** 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;
    /// Bitfield of quirks we should be aware of when using ODBC
    unsigned server_quirks;

protected:
    /** Precompiled LAST_INSERT_ID (or equivalent) SQL statement */
    db::Statement* stm_last_insert_id;
    /** ID of the last autogenerated primary key */
    DBALLE_SQL_C_SINT_TYPE m_last_insert_id;

public:
    Connection();
    ~Connection();

    void connect(const char* dsn, const char* user, const char* password);
    void connect_file(const std::string& fname);
    void driver_connect(const char* config);
    std::string driver_name();
    std::string driver_version();
    void get_info(SQLUSMALLINT info_type, SQLINTEGER& res);
    void set_autocommit(bool val);

    /// Commit a transaction
    void commit();

    /// Rollback a transaction
    void rollback();

    /// Check if the database contains a table
    bool has_table(const std::string& name);

    /**
     * Get a value from the settings table.
     *
     * Returns the empty string if the table does not exist.
     */
    std::string get_setting(const std::string& key);

    /**
     * Set a value in the settings table.
     *
     * The table is created if it does not exist.
     */
    void set_setting(const std::string& key, const std::string& value);

    /// Drop the settings table
    void drop_settings();

	/**
	 * Delete a table in the database if it exists, otherwise do nothing.
	 */
	void drop_table_if_exists(const char* name);

	/**
	 * Delete a sequence in the database if it exists, otherwise do nothing.
	 */
	void drop_sequence_if_exists(const char* name);

    /**
     * Return LAST_INSERT_ID or LAST_INSER_ROWID or whatever is appropriate for
     * the current database, if supported.
     *
     * If not supported, an exception is thrown.
     */
    int get_last_insert_id();

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
{
    const 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 bind_out(int idx, SQL_TIMESTAMP_STRUCT& val, SQLLEN& ind);

    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();
    void close_cursor_if_needed();
    /// Row count for select operations
    size_t select_rowcount();
    /// Row count for insert, delete and other non-select operations
    size_t rowcount();

    void set_cursor_forward_only();
    void set_cursor_static();

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&);
};

static inline bool operator!=(const SQL_TIMESTAMP_STRUCT& a, const SQL_TIMESTAMP_STRUCT& b)
{
    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;
}

std::ostream& operator<<(std::ostream& o, const SQL_TIMESTAMP_STRUCT& t);

static inline SQL_TIMESTAMP_STRUCT make_sql_timestamp(int year, int month, int day, int hour, int minute, int second)
{
    SQL_TIMESTAMP_STRUCT res;
    res.year = year;
    res.month = month;
    res.day = day;
    res.hour = hour;
    res.minute = minute;
    res.second = second;
    res.fraction = 0;
    return res;
}


} // namespace db
} // namespace dballe

/* vim:set ts=4 sw=4: */
#endif