Statistics
| Branch: | Tag: | Revision:

one / include / PoolObjectSQL.h @ dd32809e

History | View | Annotate | Download (8.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 POOL_OBJECT_SQL_H_
18
#define POOL_OBJECT_SQL_H_
19

    
20
#include "ObjectSQL.h"
21
#include "ObjectXML.h"
22
#include "Template.h"
23
#include <pthread.h>
24
#include <string.h>
25

    
26
using namespace std;
27

    
28
/**
29
 * PoolObject class. Provides a SQL backend interface for Pool components. Each
30
 * object is identified with and unique OID
31
 *
32
 * Note: The PoolObject provides a synchronization mechanism (mutex). This
33
 * implementation assumes that the mutex IS LOCKED when the class destructor
34
 * is called.
35
 */
36
class PoolObjectSQL : public ObjectSQL, public ObjectXML
37
{
38
public:
39

    
40
    PoolObjectSQL(int id, const string& _name, int _uid,const char *_table)
41
            :ObjectSQL(),ObjectXML(),oid(id),name(_name),uid(_uid),
42
             valid(true),obj_template(0),table(_table)
43
    {
44
        pthread_mutex_init(&mutex,0);
45
    };
46

    
47
    virtual ~PoolObjectSQL()
48
    {
49
        pthread_mutex_unlock(&mutex);
50

    
51
        pthread_mutex_destroy(&mutex);
52
    };
53

    
54
    /* --------------------------------------------------------------------- */
55

    
56
    int get_oid() const
57
    {
58
        return oid;
59
    };
60

    
61
    const string& get_name() const
62
    {
63
        return name;
64
    };
65

    
66
    int get_uid()
67
    {
68
        return uid;
69
    };
70

    
71
    /* --------------------------------------------------------------------- */
72

    
73
    /**
74
     *  Check if the object is valid
75
     *    @return true if object is valid
76
     */
77
    const bool& isValid() const
78
    {
79
       return valid;
80
    };
81

    
82
    /**
83
     *  Set the object valid flag
84
     *  @param _valid new valid flag
85
     */
86
    void set_valid(const bool _valid)
87
    {
88
        valid = _valid;
89
    };
90

    
91
    /**
92
     *  Function to lock the object
93
     */
94
    void lock()
95
    {
96
        pthread_mutex_lock(&mutex);
97
    };
98

    
99
    /**
100
     *  Function to unlock the object
101
     */
102
    void unlock()
103
    {
104
        pthread_mutex_unlock(&mutex);
105
    };
106

    
107
    /**
108
     * Function to print the object into a string in XML format
109
     *  @param xml the resulting XML string
110
     *  @return a reference to the generated string
111
     */
112
    virtual string& to_xml(string& xml) const = 0;
113

    
114
    /**
115
     *  Rebuilds the object from an xml formatted string
116
     *    @param xml_str The xml-formatted string
117
     *
118
     *    @return 0 on success, -1 otherwise
119
     */
120
    virtual int from_xml(const string &xml_str) = 0;
121

    
122
    // ------------------------------------------------------------------------
123
    // Template
124
    // ------------------------------------------------------------------------
125

    
126
    /**
127
     *  Gets the values of a template attribute
128
     *    @param name of the attribute
129
     *    @param values of the attribute
130
     *    @return the number of values
131
     */
132
    int get_template_attribute(
133
        string& name,
134
        vector<const Attribute*>& values) const
135
    {
136
        return obj_template->get(name,values);
137
    };
138

    
139
    /**
140
     *  Gets the values of a template attribute
141
     *    @param name of the attribute
142
     *    @param values of the attribute
143
     *    @return the number of values
144
     */
145
    int get_template_attribute(
146
        const char *name,
147
        vector<const Attribute*>& values) const
148
    {
149
        string str=name;
150
        return obj_template->get(str,values);
151
    };
152

    
153
    /**
154
     *  Gets a string based attribute (single)
155
     *    @param name of the attribute
156
     *    @param value of the attribute (a string), will be "" if not defined or
157
     *    not a single attribute
158
     */
159
    void get_template_attribute(
160
        const char *    name,
161
        string&         value) const
162
    {
163
        string str=name;
164
        obj_template->get(str,value);
165
    }
166

    
167
    /**
168
     *  Gets an int based attribute (single)
169
     *    @param name of the attribute
170
     *    @param value of the attribute (an int), will be 0 if not defined or
171
     *    not a single attribute
172
     */
173
    void get_template_attribute(
174
        const char *    name,
175
        int&            value) const
176
    {
177
        string str=name;
178
        obj_template->get(str,value);
179
    }
180

    
181
    /**
182
     *  Adds a new attribute to the template (replacing it if
183
     *  already defined), the object's mutex SHOULD be locked
184
     *    @param name of the new attribute
185
     *    @param value of the new attribute
186
     *    @return 0 on success
187
     */
188
    int replace_template_attribute(
189
        const string& name,
190
        const string& value)
191
    {
192
        SingleAttribute * sattr = new SingleAttribute(name,value);
193

    
194
        obj_template->erase(sattr->name());
195

    
196
        obj_template->set(sattr);
197

    
198
        return 0;
199
    }
200

    
201
    /**
202
     *  Generates a XML string for the template of the Object
203
     *    @param xml the string to store the XML description.
204
     */
205
    void template_to_xml(string &xml) const
206
    {
207
        obj_template->to_xml(xml);
208
    }
209

    
210
    /**
211
     *  Removes an Image attribute
212
     *    @param name of the attribute
213
     */
214
    int remove_template_attribute(const string& name)
215
    {
216
        return obj_template->erase(name);
217
    }
218

    
219
    /**
220
     *  Sets an error message for the VM in the template
221
     *    @param message
222
     *    @return 0 on success
223
     */
224
    void set_template_error_message(const string& message);
225

    
226
protected:
227

    
228
    /**
229
     *  Callback function to unmarshall a PoolObjectSQL
230
     *    @param num the number of columns read from the DB
231
     *    @param names the column names
232
     *    @param vaues the column values
233
     *    @return 0 on success
234
     */
235
    int select_cb(void *nil, int num, char **values, char **names)
236
    {
237
        if ( (!values[0]) || (num != 1) )
238
        {
239
            return -1;
240
        }
241

    
242
        return from_xml(values[0]);
243
    };
244

    
245
    /**
246
     *  Reads the PoolObjectSQL (identified by its OID) from the database.
247
     *    @param db pointer to the db
248
     *    @return 0 on success
249
     */
250
    virtual int select(SqlDB *db);
251

    
252
    /**
253
     *  Reads the PoolObjectSQL (identified by its OID) from the database.
254
     *    @param db pointer to the db
255
     *    @return 0 on success
256
     */
257
    virtual int select(SqlDB *db, const string& _name, int _uid);
258

    
259
    /**
260
     *  Drops object from the database
261
     *    @param db pointer to the db
262
     *    @return 0 on success
263
     */
264
    virtual int drop(SqlDB *db);
265

    
266
    /**
267
     *  Function to output a pool object into a stream in XML format
268
     *    @param oss the output stream
269
     *    @param num the number of columns read from the DB
270
     *    @param names the column names
271
     *    @param vaues the column values
272
     *    @return 0 on success
273
     */
274
    static int dump(ostringstream& oss, int num, char **values, char **names)
275
    {
276
        if ( (!values[0]) || (num != 1) )
277
        {
278
            return -1;
279
        }
280

    
281
        oss << values[0];
282
        return 0;
283
    };
284

    
285
    /**
286
     *  The object's unique ID
287
     */
288
    int     oid;
289

    
290
    /**
291
     *  The object's name
292
     */
293
    string  name;
294

    
295
    /**
296
     *  Object's owner, set it to -1 if owner is not used
297
     */
298
    int     uid;
299

    
300
    /**
301
     *  The contents of this object are valid
302
     */
303
    bool    valid;
304

    
305
    /**
306
     *  Template for this object, will be allocated if needed
307
     */
308
    Template * obj_template;
309

    
310
private:
311

    
312
    /**
313
     *  The PoolSQL, friend to easily manipulate its Objects
314
     */
315
    friend class PoolSQL;
316

    
317
    /**
318
     * The mutex for the PoolObject. This implementation assumes that the mutex
319
     * IS LOCKED when the class destructor is called.
320
     */
321
    pthread_mutex_t mutex;
322

    
323
    /**
324
     *  Pointer to the SQL table for the PoolObjectSQL
325
     */
326
    const char * table;
327

    
328
    /**
329
     *  Name for the error messages attribute
330
     */
331
    static const char * error_attribute_name;
332
};
333

    
334
#endif /*POOL_OBJECT_SQL_H_*/