Statistics
| Branch: | Tag: | Revision:

one / include / SqliteDB.h @ 5a66cefb

History | View | Annotate | Download (3.55 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs        */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#ifndef SQLITE_DB_H_
18
#define SQLITE_DB_H_
19

    
20
#include <string>
21
#include <sstream>
22
#include <stdexcept>
23

    
24
#include <sys/time.h>
25
#include <sys/types.h>
26
#include <unistd.h>
27

    
28
#include "NebulaLog.h"
29

    
30
#include "SqlDB.h"
31
#include "ObjectSQL.h"
32

    
33
using namespace std;
34

    
35
#ifdef SQLITE_DB
36

    
37
#include <sqlite3.h>
38

    
39
/**
40
 * SqliteDB class. Provides a wrapper to the sqlite3 database interface. It also
41
 * provides "global" synchronization mechanism to use it in a multithread
42
 * environment.
43
 */
44
class SqliteDB : public SqlDB
45
{
46
public:
47

    
48
    SqliteDB(string& db_name);
49

    
50
    ~SqliteDB();
51

    
52
    /**
53
     *  Wraps the sqlite3_exec function call, and locks the DB mutex.
54
     *    @param sql_cmd the SQL command
55
     *    @param callbak function to execute on each data returned, watch the
56
     *    mutex you block in the callback.
57
     *    @param arg to pass to the callback function
58
     *    @return 0 on success
59
     */
60
    int exec(ostringstream& cmd, Callbackable* obj=0, bool quiet=false);
61

    
62
    /**
63
     *  This function returns a legal SQL string that can be used in an SQL
64
     *  statement.
65
     *    @param str the string to be escaped
66
     *    @return a valid SQL string or NULL in case of failure
67
     */
68
    char * escape_str(const string& str);
69

    
70
    /**
71
     *  Frees a previously scaped string
72
     *    @param str pointer to the str
73
     */
74
    void free_str(char * str);
75

    
76
    /**
77
     * Returns true if the syntax INSERT VALUES (data), (data), (data)
78
     * is supported
79
     *
80
     * @return true if supported
81
     */
82
    bool multiple_values_support();
83

    
84
private:
85
    /**
86
     *  Fine-grain mutex for DB access
87
     */
88
    pthread_mutex_t     mutex;
89

    
90
    /**
91
     *  Pointer to the database.
92
     */
93
    sqlite3 *           db;
94

    
95
    /**
96
     *  Function to lock the DB
97
     */
98
    void lock()
99
    {
100
        pthread_mutex_lock(&mutex);
101
    };
102

    
103
    /**
104
     *  Function to unlock the DB
105
     */
106
    void unlock()
107
    {
108
        pthread_mutex_unlock(&mutex);
109
    };
110
};
111
#else
112
//CLass stub
113
class SqliteDB : public SqlDB
114
{
115
public:
116

    
117
    SqliteDB(string& db_name)
118
    {
119
        throw runtime_error("Aborting oned, Sqlite support not compiled!");
120
    };
121

    
122
    ~SqliteDB(){};
123

    
124
    int exec(ostringstream& cmd, Callbackable* obj=0, bool quiet=false){return -1;};
125

    
126
    char * escape_str(const string& str){return 0;};
127

    
128
    void free_str(char * str){};
129

    
130
    bool multiple_values_support(){return true;};
131
};
132
#endif
133

    
134
#endif /*SQLITE_DB_H_*/