Statistics
| Branch: | Tag: | Revision:

one / include / MySqlDB.h @ 7aacd86c

History | View | Annotate | Download (3.63 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             */
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 MYSQL_DB_H_
18
#define MYSQL_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
#include "SqlDB.h"
30
#include "ObjectSQL.h"
31

    
32

    
33
using namespace std;
34

    
35
#ifdef MYSQL_DB
36

    
37
#include <mysql.h>
38

    
39
/**
40
 * SqliteDB class. Provides a wrapper to the mysql database interface.
41
 */
42
class MySqlDB : public SqlDB
43
{
44
public:
45

    
46
    MySqlDB(const string& _server,
47
            int           _port,
48
            const string& _user,
49
            const string& _password,
50
            const string& _database);
51

    
52
    ~MySqlDB();
53

    
54
    /**
55
     *  Wraps the mysql_query function call
56
     *    @param cmd the SQL command
57
     *    @param obj Callbackable obj to call if the query succeeds
58
     *    @return 0 on success
59
     */
60
    int exec(ostringstream& cmd, Callbackable* obj=0);
61

    
62
    /**
63
     *  This function returns a legal SQL string that can be used in an SQL
64
     *  statement. The string is encoded to an escaped SQL string, taking into
65
     *  account the current character set of the connection.
66
     *    @param str the string to be escaped
67
     *    @return a valid SQL string or NULL in case of failure
68
     */
69
    char * escape_str(const string& str);
70

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

    
77
private:
78

    
79
    /**
80
     * The MySql connection handler
81
     */
82
    MYSQL *             db;
83

    
84
    /**
85
     *  MySQL Connection parameters
86
     */
87
    string              server;
88

    
89
    int                 port;
90

    
91
    string              user;
92

    
93
    string              password;
94

    
95
    string              database;
96

    
97
    /**
98
     *  Fine-grain mutex for DB access
99
     */
100
    pthread_mutex_t     mutex;
101

    
102
    /**
103
     *  Function to lock the DB
104
     */
105
    void lock()
106
    {
107
        pthread_mutex_lock(&mutex);
108
    };
109

    
110
    /**
111
     *  Function to unlock the DB
112
     */
113
    void unlock()
114
    {
115
        pthread_mutex_unlock(&mutex);
116
    };
117
};
118
#else
119
//CLass stub
120
class MySqlDB : public SqlDB
121
{
122
public:
123

    
124
    MySqlDB(
125
            string server,
126
            int    port,
127
            string user,
128
            string password,
129
            string database)
130
    {
131
        throw runtime_error("Aborting oned, MySQL support not compiled!");
132
    };
133

    
134
    ~MySqlDB(){};
135

    
136
    int exec(ostringstream& cmd, Callbackable* obj=0){return -1;};
137

    
138
    char * escape_str(const string& str){return 0;};
139

    
140
    void free_str(char * str){};
141
};
142
#endif
143

    
144
#endif /*MYSQL_DB_H_*/