SQLiteConnection.h 2.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* <SQLite3 C++ interface: header file>

   Copyright (C) 2011 Lisa "shainer" Vitolo

   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, or (at your option) any later version.
*/

#ifndef _SQLITE_CONNECTION_H
#define _SQLITE_CONNECTION_H

/*
 * If there's a God of programming somewhere,
 * please forgive these sinners, for they have lost the way
 */
#include <sqlite3.h>

/*
 * Instead, these are rather good people indeed
 */
#include <QString>
24 25 26 27
#include <QMap>
#include <QList>
#include <QVariant>
#include <QByteArray>
28

29
#include <exception>
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * Stores the result of a query, so that the user can access it later.
 */
class SQLiteResource
{
    typedef QVariant Value;
    typedef QVariantMap Row;
  
    QList<Row> table;
  
  public:
    SQLiteResource(QList<Row>);
    QVariant getDataAt(int, QString const&);
    QList<QVariant> getColumn(QString const&);
    int getRowsCount();
};

48 49 50
/*
 * Public interface for SQLite3 main functions and utilities
 */
51
class SQLiteConnection
52
{
53 54
    typedef QVariant Value;
    typedef QVariantMap Row;
55
  
56
    sqlite3 *dbHandle;
57
    sqlite3_stmt *statement;
58
    QList<Row> rows;
59 60
    QMap<QString, int> intBindings;
    QMap<QString, QString> stringBindings;
61

62
    void checkForErrors(int);
63
    Row getRowFromStatement(sqlite3_stmt*);
64 65 66 67 68

    // Disable copy constructor and default constructor
    SQLiteConnection();
    SQLiteConnection(SQLiteConnection const&);

69
  public:
70
    SQLiteConnection(QString const&, bool);
71
    ~SQLiteConnection();
72

73
    SQLiteResource query(QString);
74 75
    qint64 getLastRowId();

76 77 78
    void bind(QString const&, int);
    void bind(QString const&, QString);
    void bind(QString const&, QByteArray);
79
};
80
    
81 82 83 84
class SQLiteException : public std::exception
{
  private:
    const char *message;
85

86
  public:
87
    SQLiteException(const char *m) : message(m) {}
88

89 90 91 92 93
  virtual const char *what() const throw()
  {
    return message;
  }
};
94 95

#endif