SQLiteConnection.h 2.03 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>
Lukas Appelhans's avatar
Lukas Appelhans committed
30
#include <QSharedDataPointer>
31

32 33 34 35 36 37 38
/*
 * Stores the result of a query, so that the user can access it later.
 */
class SQLiteResource
{
    typedef QVariant Value;
    typedef QVariantMap Row;
Lukas Appelhans's avatar
Astyle  
Lukas Appelhans committed
39
public:
40
    SQLiteResource(QList<Row>);
Lukas Appelhans's avatar
Lukas Appelhans committed
41
    SQLiteResource(const SQLiteResource &other);
42 43
    ~SQLiteResource();
    
44 45 46
    QVariant getDataAt(int, QString const&);
    QList<QVariant> getColumn(QString const&);
    int getRowsCount();
Lukas Appelhans's avatar
Lukas Appelhans committed
47
    SQLiteResource &operator=(const SQLiteResource &other);
Lukas Appelhans's avatar
Astyle  
Lukas Appelhans committed
48 49
    
private:
50
    class Private;
Lukas Appelhans's avatar
Lukas Appelhans committed
51
    QSharedDataPointer<Private> d;
52 53
};

54 55 56
/*
 * Public interface for SQLite3 main functions and utilities
 */
57
class SQLiteConnection
58
{
59 60 61
    typedef QVariant Value;
    typedef QVariantMap Row;

Lukas Appelhans's avatar
Astyle  
Lukas Appelhans committed
62
public:
63
    SQLiteConnection(QString const&, bool);
64
    ~SQLiteConnection();
65

Lukas Appelhans's avatar
Lukas Appelhans committed
66
    SQLiteResource query(const QString &q);
67 68
    qint64 getLastRowId();

69 70 71
    void bind(QString const&, int);
    void bind(QString const&, QString);
    void bind(QString const&, QByteArray);
72
    
Lukas Appelhans's avatar
Astyle  
Lukas Appelhans committed
73 74 75 76
private:
    // Disable copy constructor and default constructor
    SQLiteConnection(SQLiteConnection const&);
    
Lukas Appelhans's avatar
Lukas Appelhans committed
77 78
    class Private;
    Private *d;
Lukas Appelhans's avatar
Astyle  
Lukas Appelhans committed
79 80
};

81 82
class SQLiteException : public std::exception
{
Lukas Appelhans's avatar
Astyle  
Lukas Appelhans committed
83
public:
84
    SQLiteException(const char *m) : message(m) {}
85

Lukas Appelhans's avatar
Astyle  
Lukas Appelhans committed
86 87 88 89 90 91
    virtual const char *what() const throw() {
        return message;
    }
    
private:
    const char *message;
92
};
93 94

#endif