aboutsummaryrefslogtreecommitdiff
path: root/include/sqlitepp.h
blob: db932d2c506c9f5dae8cdf482a91e9216471e574 (plain)
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
/*
 * (C) 2014 Robin Krahl
 * MIT license -- http://opensource.org/licenses/MIT
 */

#ifndef __SQLITEPP_H
#define __SQLITEPP_H

#include <memory>
#include <string>

#include <boost/noncopyable.hpp>
#include <sqlite3.h>

namespace sqlitepp {
    class Openable {
    public:
        const bool isOpen() const;

    protected:
        Openable(const bool open, const std::string & name);

        void requireOpen() const;
        void setOpen(const bool open);

    private:
        bool m_open;
        const std::string & m_name;
    };

    class DatabaseError : public std::runtime_error {
    public:
        DatabaseError(const int errorCode);
        DatabaseError(const int errorCode, const std::string & errorMessage);

        const int errorCode() const;
    private:
        const int m_errorCode;

        static const std::string getErrorMessage(const int errorCode, const std::string & errorMessage);
    };

    class Statement;

    class Database : private boost::noncopyable, public Openable {
        friend class Statement;
    public:
        Database();
        Database(const std::string & file);
        ~Database();

        void close();
        void execute(const std::string & sql);
        void open(const std::string & file);
        std::shared_ptr<Statement> prepare(const std::string & sql);

    private:
        sqlite3 * m_handle;
    };

    class Statement : private boost::noncopyable, public Openable {
    public:
        Statement(Database & database, const std::string & statement);
        ~Statement();

        void bindDouble(const int index, const double value);
        void bindDouble(const std::string & name, const double value);
        void bindInt(const int index, const int value);
        void bindInt(const std::string & name, const int value);
        void bindString(const int index, const std::string & value);
        void bindString(const std::string & name, const std::string & value);
        const bool canRead() const;
        const int columnCount() const;
        const double readDouble(const int column) const;
        const int readInt(const int column) const;
        const std::string readString(const int column) const;
        const bool step();
        void finalize();
        const bool reset();

    private:
        sqlite3_stmt * m_handle;
        bool m_canRead;

        int getParameterIndex(const std::string & name) const;
        void handleBindResult(const int index, const int result) const;
        void requireCanRead() const;
    };
}

#endif