This file is indexed.

/usr/include/dballe/db/odbc/internals.h is in libdballe-dev 7.7-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
/*
 * db/odbc/internals - Implementation infrastructure for the ODBC DB connection
 *
 * Copyright (C) 2005--2014  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_ODBC_INTERNALS_H
#define DBALLE_DB_ODBC_INTERNALS_H

/** @file
 * @ingroup db
 *
 * Database functions and data structures used by the db module, but not
 * exported as official API.
 */

#include <dballe/db/db.h>
#include <dballe/db/sql.h>
#include <dballe/db/querybuf.h>
#include <sqltypes.h>
#include <list>

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

namespace dballe {
namespace db {
struct ODBCStatement;

// 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 db::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);
};

/// 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 ODBCConnection : public Connection
{
    /** ODBC database connection */
    SQLHDBC od_conn;
    /** True if the connection is open */
    bool connected;
    /// Bitfield of quirks we should be aware of when using ODBC
    unsigned server_quirks;

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

public:
    ODBCConnection();
    ODBCConnection(const ODBCConnection&) = delete;
    ODBCConnection(const ODBCConnection&&) = delete;
    ~ODBCConnection();

    ODBCConnection& operator=(const ODBCConnection&) = delete;

    void connect(const char* dsn, const char* user, const char* password);
    void connect_url(const char* url);
    // void connect_file(const std::string& fname);
    void connect_test();
    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);

    void exec(const std::string& query);

    std::unique_ptr<Transaction> transaction() override;
    std::unique_ptr<ODBCStatement> odbcstatement(const std::string& query);

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

    /**
     * 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) override;

    /**
     * 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) override;

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

    /**
     * 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();

    void add_datetime(Querybuf& qb, const Datetime& dt) const override;

protected:
    void init_after_connect();
};

/// ODBC statement
struct ODBCStatement
{
    const ODBCConnection& conn;
    SQLHSTMT stm = nullptr;
    /// If non-NULL, ignore all errors with this code
    const char* ignore_error = nullptr;
#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 = false;
#endif

    ODBCStatement(ODBCConnection& conn);
    ODBCStatement(const ODBCStatement&) = delete;
    ODBCStatement(const ODBCStatement&&) = delete;
    ~ODBCStatement();
    ODBCStatement& operator=(const ODBCStatement&) = delete;

    void bind_in(int idx, const int& val);
    void bind_in(int idx, const int& val, const SQLLEN& ind);
    void bind_in(int idx, const unsigned& val);
    void bind_in(int idx, const unsigned& val, const SQLLEN& ind);
    void bind_in(int idx, const unsigned short& val);
    void bind_in(int idx, const unsigned short& val, const SQLLEN& ind);
    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 std::string& val);
    void bind_in(int idx, const SQL_TIMESTAMP_STRUCT& val);

    void bind_out(int idx, int& val);
    void bind_out(int idx, int& val, SQLLEN& ind);
    void bind_out(int idx, unsigned& val);
    void bind_out(int idx, unsigned& val, SQLLEN& ind);
    void bind_out(int idx, unsigned short& val);
    void bind_out(int idx, unsigned short& val, SQLLEN& ind);
    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);
    void prepare(const std::string& query);

    /// @return SQLExecute's result
    int execute();

    /// @return SQLExecute's result
    int execute_and_close();
    /// @return SQLExecute's result
    int exec_direct_and_close(const char* query, int qlen);

    void execute_ignoring_results();

    /**
     * @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);
};

/// ODBC statement to read a sequence
struct Sequence : public ODBCStatement
{
    int out;

    Sequence(ODBCConnection& conn, const char* name);
    ~Sequence();

    /// Read the current value of the sequence
    const int& 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