This file is indexed.

/usr/include/dballe/sql/postgresql.h is in libdballe-dev 7.21-1build1.

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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/** @file
 * PostgreSQL DB connector
 */
#ifndef DBALLE_SQL_POSTGRESQL_H
#define DBALLE_SQL_POSTGRESQL_H

#include <dballe/sql/sql.h>
#include <libpq-fe.h>
#include <arpa/inet.h>
#include <vector>

namespace dballe {
namespace sql {

/**
 * Report an PostgreSQL error
 */
struct error_postgresql : public error_db
{
    std::string msg;

    error_postgresql(PGconn* db, const std::string& msg);
    error_postgresql(PGresult* db, const std::string& msg);
    error_postgresql(const std::string& dbmsg, const std::string& msg);
    ~error_postgresql() throw () {}

    const char* what() const noexcept override { return msg.c_str(); }

    static void throwf(PGconn* db, const char* fmt, ...) WREPORT_THROWF_ATTRS(2, 3);
    static void throwf(PGresult* db, const char* fmt, ...) WREPORT_THROWF_ATTRS(2, 3);
};

namespace postgresql {

int64_t encode_datetime(const Datetime& arg);
int64_t encode_int64_t(int64_t arg);

/// Argument list for PQexecParams built at compile time
template<typename... ARGS> struct Params
{
    static const int count = sizeof...(ARGS);
    const char* args[sizeof...(ARGS)];
    int lengths[sizeof...(ARGS)];
    int formats[sizeof...(ARGS)];
    void* local[sizeof...(ARGS)];

    Params(const ARGS&... args)
    {
        _add(0, args...);
    }
    ~Params()
    {
        for (auto& i: local)
            free(i);
    }

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

protected:
    /// Terminating condition for compile-time arg expansion
    void _add(unsigned pos)
    {
    }

    /// Fill in the argument structures
    template<typename... REST>
    void _add(unsigned pos, std::nullptr_t arg, const REST&... rest)
    {
        local[pos] = nullptr;
        args[pos] = nullptr;
        lengths[pos] = 0;
        formats[pos] = 0;
        _add(pos + 1, rest...);
    }

    /// Fill in the argument structures
    template<typename... REST>
    void _add(unsigned pos, int32_t arg, const REST&... rest)
    {
        local[pos] = malloc(sizeof(int32_t));
        *(int32_t*)local[pos] = (int32_t)htonl((uint32_t)arg);
        args[pos] = (const char*)local[pos];
        lengths[pos] = sizeof(int32_t);
        formats[pos] = 1;
        _add(pos + 1, rest...);
    }

    /// Fill in the argument structures
    template<typename... REST>
    void _add(unsigned pos, uint64_t arg, const REST&... rest)
    {
        local[pos] = malloc(sizeof(int64_t));
        *(int64_t*)local[pos] = encode_int64_t(arg);
        args[pos] = (const char*)local[pos];
        lengths[pos] = sizeof(int64_t);
        formats[pos] = 1;
        _add(pos + 1, rest...);
    }

    /// Fill in the argument structures
    template<typename... REST>
    void _add(unsigned pos, const char* arg, const REST&... rest)
    {
        local[pos] = nullptr;
        args[pos] = arg;
        lengths[pos] = 0;
        formats[pos] = 0;
        _add(pos + 1, rest...);
    }

    /// Fill in the argument structures
    template<typename... REST>
    void _add(unsigned pos, const std::string& arg, const REST&... rest)
    {
        local[pos] = nullptr;
        args[pos] = arg.data();
        lengths[pos] = arg.size();
        formats[pos] = 0;
        _add(pos + 1, rest...);
    }

    /// Fill in the argument structures
    template<typename... REST>
    void _add(unsigned pos, const std::vector<uint8_t>& arg, const REST&... rest)
    {
        local[pos] = nullptr;
        args[pos] = (const char*)arg.data();
        lengths[pos] = arg.size();
        formats[pos] = 1;
        _add(pos + 1, rest...);
    }

    /// Fill in the argument structures
    template<typename... REST>
    void _add(unsigned pos, const Datetime& arg, const REST&... rest)
    {
        local[pos] = malloc(sizeof(int64_t));
        *(int64_t*)local[pos] = encode_datetime(arg);
        args[pos] = (const char*)local[pos];
        lengths[pos] = sizeof(int64_t);
        formats[pos] = 1;
        _add(pos + 1, rest...);
    }
};

/// Wrap a PGresult, taking care of its memory management
struct Result
{
    PGresult* res;

    Result() : res(nullptr) {}
    Result(PGresult* res) : res(res) {}
    ~Result() { PQclear(res); }

    /// Implement move
    Result(Result&& o) : res(o.res) { o.res = nullptr; }
    Result& operator=(Result&& o)
    {
        if (this == &o) return *this;
        PQclear(res);
        res = o.res;
        o.res = nullptr;
        return *this;
    }

    operator bool() const { return res != nullptr; }
    operator PGresult*() { return res; }
    operator const PGresult*() const { return res; }

    /// Check that the result successfully returned no data
    void expect_no_data(const std::string& query);

    /// Check that the result successfully returned some (possibly empty) data
    void expect_result(const std::string& query);

    /// Check that the result successfully returned one row of data
    void expect_one_row(const std::string& query);

    /// Check that the result was successful
    void expect_success(const std::string& query);

    /// Get the number of rows in the result
    unsigned rowcount() const { return PQntuples(res); }

    /// Check if a result value is null
    bool is_null(unsigned row, unsigned col) const
    {
        return PQgetisnull(res, row, col);
    }

    /// Return a result value, transmitted in binary as a byte (?)
    bool get_bool(unsigned row, unsigned col) const
    {
        char* val = PQgetvalue(res, row, col);
        return *val;
    }

    /// Return a result value, transmitted in binary as a 2 bit integer
    uint16_t get_int2(unsigned row, unsigned col) const
    {
        char* val = PQgetvalue(res, row, col);
        return ntohs(*(uint16_t*)val);
    }

    /// Return a result value, transmitted in binary as a 4 bit integer
    uint32_t get_int4(unsigned row, unsigned col) const
    {
        char* val = PQgetvalue(res, row, col);
        return ntohl(*(uint32_t*)val);
    }

    /// Return a result value, transmitted in binary as an 8 bit integer
    uint64_t get_int8(unsigned row, unsigned col) const;

    /// Return a result value, transmitted in binary as an 8 bit integer
    std::vector<uint8_t> get_bytea(unsigned row, unsigned col) const;

    /// Return a result value, transmitted as a string
    const char* get_string(unsigned row, unsigned col) const
    {
        return PQgetvalue(res, row, col);
    }

    /// Return a result value, transmitted as a timestamp without timezone
    Datetime get_timestamp(unsigned row, unsigned col) const;

    // Prevent copy
    Result(const Result&) = delete;
    Result& operator=(const Result&) = delete;
};

}


/// Database connection
class PostgreSQLConnection : public Connection
{
protected:
    /// Database connection
    PGconn* db = nullptr;

protected:
    void init_after_connect();

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

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

    operator PGconn*() { return db; }

    /**
     * Connect to PostgreSQL using a connection URI
     *
     * The syntax is described at:
     * http://www.postgresql.org/docs/9.4/static/libpq-connect.html#AEN41094
     */
    void open_url(const std::string& connection_string);
    void open_test();

    std::unique_ptr<Transaction> transaction() override;

    /// Precompile a query
    void prepare(const std::string& name, const std::string& query);

    postgresql::Result exec_unchecked(const char* query)
    {
        if (profile) ++profile_query_count;
        return PQexecParams(db, query, 0, nullptr, nullptr, nullptr, nullptr, 1);
    }

    postgresql::Result exec_unchecked(const std::string& query)
    {
        if (profile) ++profile_query_count;
        return PQexecParams(db, query.c_str(), 0, nullptr, nullptr, nullptr, nullptr, 1);
    }

    template<typename STRING>
    void exec_no_data(STRING query)
    {
        postgresql::Result res(exec_unchecked(query));
        res.expect_no_data(query);
    }

    template<typename STRING>
    postgresql::Result exec(STRING query)
    {
        postgresql::Result res(exec_unchecked(query));
        res.expect_result(query);
        return res;
    }

    template<typename STRING>
    postgresql::Result exec_one_row(STRING query)
    {
        postgresql::Result res(exec_unchecked(query));
        res.expect_one_row(query);
        return res;
    }

    template<typename ...ARGS>
    postgresql::Result exec_unchecked(const char* query, ARGS... args)
    {
        postgresql::Params<ARGS...> params(args...);
        if (profile) ++profile_query_count;
        return PQexecParams(db, query, params.count, nullptr, params.args, params.lengths, params.formats, 1);
    }

    template<typename ...ARGS>
    postgresql::Result exec_unchecked(const std::string& query, ARGS... args)
    {
        postgresql::Params<ARGS...> params(args...);
        if (profile) ++profile_query_count;
        return PQexecParams(db, query.c_str(), params.count, nullptr, params.args, params.lengths, params.formats, 1);
    }

    template<typename STRING, typename ...ARGS>
    void exec_no_data(STRING query, ARGS... args)
    {
        postgresql::Result res(exec_unchecked(query, args...));
        res.expect_no_data(query);
    }

    template<typename STRING, typename ...ARGS>
    postgresql::Result exec(STRING query, ARGS... args)
    {
        postgresql::Result res(exec_unchecked(query, args...));
        res.expect_result(query);
        return res;
    }

    template<typename STRING, typename ...ARGS>
    postgresql::Result exec_one_row(STRING query, ARGS... args)
    {
        postgresql::Result res(exec_unchecked(query, args...));
        res.expect_one_row(query);
        return res;
    }

    postgresql::Result exec_prepared_unchecked(const char* name)
    {
        if (profile) ++profile_query_count;
        return PQexecPrepared(db, name, 0, nullptr, nullptr, nullptr, 1);
    }

    postgresql::Result exec_prepared_unchecked(const std::string& name)
    {
        if (profile) ++profile_query_count;
        return PQexecPrepared(db, name.c_str(), 0, nullptr, nullptr, nullptr, 1);
    }

    template<typename STRING>
    void exec_prepared_no_data(STRING name)
    {
        postgresql::Result res(exec_prepared_unchecked(name));
        res.expect_no_data(name);
    }

    template<typename STRING>
    postgresql::Result exec_prepared(STRING name)
    {
        postgresql::Result res(exec_prepared_unchecked(name));
        res.expect_result(name);
        return res;
    }

    template<typename STRING>
    postgresql::Result exec_prepared_one_row(STRING name)
    {
        postgresql::Result res(exec_prepared_unchecked(name));
        res.expect_one_row(name);
        return res;
    }

    template<typename ...ARGS>
    postgresql::Result exec_prepared_unchecked(const char* name, ARGS... args)
    {
        postgresql::Params<ARGS...> params(args...);
        if (profile) ++profile_query_count;
        return PQexecPrepared(db, name, params.count, params.args, params.lengths, params.formats, 1);
    }

    template<typename ...ARGS>
    postgresql::Result exec_prepared_unchecked(const std::string& name, ARGS... args)
    {
        postgresql::Params<ARGS...> params(args...);
        if (profile) ++profile_query_count;
        return PQexecPrepared(db, name.c_str(), params.count, params.args, params.lengths, params.formats, 1);
    }

    template<typename STRING, typename ...ARGS>
    void exec_prepared_no_data(STRING name, ARGS... args)
    {
        postgresql::Result res(exec_prepared_unchecked(name, args...));
        res.expect_no_data(name);
    }

    template<typename STRING, typename ...ARGS>
    postgresql::Result exec_prepared(STRING name, ARGS... args)
    {
        postgresql::Result res(exec_prepared_unchecked(name, args...));
        res.expect_result(name);
        return res;
    }

    template<typename STRING, typename ...ARGS>
    postgresql::Result exec_prepared_one_row(STRING name, ARGS... args)
    {
        postgresql::Result res(exec_prepared_unchecked(name, args...));
        res.expect_one_row(name);
        return res;
    }

    /// Send a cancellation command to the server
    void cancel_running_query_nothrow() noexcept;

    /// Discard all input from an asynchronous request
    void discard_all_input_nothrow() noexcept;

    bool has_table(const std::string& name) override;
    std::string get_setting(const std::string& key) override;
    void set_setting(const std::string& key, const std::string& value) override;
    void drop_settings() override;
    void execute(const std::string& query) override;
    void explain(const std::string& query, FILE* out) override;

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

    /// Count the number of rows modified by the last query that was run
    int changes();

    /// Wrap PQexec
    void pqexec(const std::string& query);

    /**
     * Wrap PQexec but do not throw an exception in case of errors.
     *
     * This is useful to be called in destructors. Errors will be printed to
     * stderr.
     */
    void pqexec_nothrow(const std::string& query) noexcept;

    /// Retrieve query results in single row mode
    void run_single_row_mode(const std::string& query_desc, std::function<void(const postgresql::Result&)> dest);

    /// Escape the string as a literal value and append it to qb
    void append_escaped(Querybuf& qb, const char* str);

    /// Escape the string as a literal value and append it to qb
    void append_escaped(Querybuf& qb, const std::string& str);

    /// Escape the buffer as bytea literal and append it to qb
    void append_escaped(Querybuf& qb, const std::vector<uint8_t>& buf);
};

}
}
#endif