Revision 855d34f9

View differences:

include/DefaultQuotas.h
18 18
#define DEFAULT_QUOTAS_H_
19 19

  
20 20
#include "Quotas.h"
21
#include "SqlDB.h"
22
#include "ObjectSQL.h"
21 23

  
22
class DefaultQuotas : public Quotas
24
class DefaultQuotas : public Quotas, ObjectSQL
23 25
{
24 26
public:
25 27
    DefaultQuotas(
......
54 56
    string& to_xml(string& xml) const;
55 57

  
56 58
    /**
59
     *  Writes the quotas in the database.
60
     *    @param db pointer to the db
61
     *    @return 0 on success
62
     */
63
    int insert(SqlDB *db, string& error_str)
64
    {
65
        return insert_replace(db, false, error_str);
66
    };
67

  
68
    /**
69
     *  Writes/updates the quotas data fields in the database.
70
     *    @param db pointer to the db
71
     *    @return 0 on success
72
     */
73
    int update(SqlDB *db)
74
    {
75
        string error_str;
76
        return insert_replace(db, true, error_str);
77
    };
78

  
79
    /**
80
     *  Reads the Quotas from the database.
81
     *    @param db pointer to the db
82
     *    @return 0 on success
83
     */
84
    int select(SqlDB *db);
85

  
86
    /**
87
     *  Bootstraps the database table(s) associated to the default quotas
88
     *    @return 0 on success
89
     */
90
    static int bootstrap(SqlDB * db);
91

  
92
private:
93
    const char * root_elem;
94

  
95
    // *************************************************************************
96
    // DataBase implementation
97
    // *************************************************************************
98

  
99
    static const char * db_names;
100

  
101
    static const char * db_bootstrap;
102

  
103
    static const char * table;
104

  
105
    /**
106
     *  Execute an INSERT or REPLACE Sql query.
107
     *    @param db The SQL DB
108
     *    @param replace Execute an INSERT or a REPLACE
109
     *    @param error_str Returns the error reason, if any
110
     *    @return 0 one success
111
     */
112
    int insert_replace(SqlDB *db, bool replace, string& error_str);
113

  
114
    /**
115
     *  Callback function to unmarshall a PoolObjectSQL
116
     *    @param num the number of columns read from the DB
117
     *    @param names the column names
118
     *    @param vaues the column values
119
     *    @return 0 on success
120
     */
121
    int select_cb(void *nil, int num, char **values, char **names)
122
    {
123
        if ( (!values[0]) || (num != 1) )
124
        {
125
            return -1;
126
        }
127

  
128
        return from_xml(values[0]);
129
    };
130

  
131
    /**
57 132
     *  Builds quota object from an ObjectXML
58 133
     *    @param xml The xml-formatted string
59
     *    @return 0 if success
134
     *    @return 0 on success
60 135
     */
61 136
    int from_xml(const string& xml);
62 137

  
63
private:
64
    const char * root_elem;
138
    /**
139
     *  Removes the Quotas from the database.
140
     *    @param db pointer to the db
141
     *    @return 0 on success
142
     */
143
    int drop(SqlDB * db)
144
    {
145
        return -1;  // Not supported
146
    };
65 147
};
66 148

  
67 149
#endif /*DEFAULT_QUOTAS_H_*/
include/Nebula.h
320 320

  
321 321
    int set_default_user_quota(Template *tmpl, string& error)
322 322
    {
323
        return default_user_quota.set(tmpl, error);
323
        int rc;
324
        rc = default_user_quota.set(tmpl, error);
325

  
326
        if ( rc == 0 )
327
        {
328
            rc = default_user_quota.update(db);
329
        }
330

  
331
        return rc;
324 332
    };
325 333

  
326 334
    const DefaultQuotas& get_default_group_quota()
......
330 338

  
331 339
    int set_default_group_quota(Template *tmpl, string& error)
332 340
    {
333
        return default_group_quota.set(tmpl, error);
341
        int rc;
342
        rc = default_group_quota.set(tmpl, error);
343

  
344
        if ( rc == 0 )
345
        {
346
            rc = default_group_quota.update(db);
347
        }
348

  
349
        return rc;
334 350
    };
335 351

  
336 352
private:
src/nebula/Nebula.cc
245 245
            rc += ClusterPool::bootstrap(db);
246 246
            rc += DocumentPool::bootstrap(db);
247 247

  
248
            rc += DefaultQuotas::bootstrap(db);
249

  
248 250
            // Create the versioning table only if bootstrap went well
249 251
            if ( rc == 0 )
250 252
            {
......
340 342
        tpool  = new VMTemplatePool(db);
341 343

  
342 344
        dspool = new DatastorePool(db);
345

  
346
        default_user_quota.select(db);
347
        default_group_quota.select(db);
343 348
    }
344 349
    catch (exception&)
345 350
    {
......
681 686
{
682 687
    int             rc;
683 688
    ostringstream   oss;
689
    string          error;
684 690

  
685 691
    oss <<  "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, "
686 692
            "last_oid BIGINT UNSIGNED)";
......
700 706

  
701 707
    rc += db->exec(oss);
702 708

  
709
    rc += default_user_quota.insert(db, error);
710
    rc += default_group_quota.insert(db, error);
711

  
703 712
    return rc;
704 713
}
705 714

  
src/um/DefaultQuotas.cc
17 17
#include "DefaultQuotas.h"
18 18
#include "ObjectXML.h"
19 19

  
20
/* -------------------------------------------------------------------------- */
21
/* -------------------------------------------------------------------------- */
22

  
23
const char * DefaultQuotas::table    = "system_attributes";
24
const char * DefaultQuotas::db_names = "name, body";
25

  
26
/* -------------------------------------------------------------------------- */
27
/* -------------------------------------------------------------------------- */
28

  
20 29
string& DefaultQuotas::to_xml(string& xml) const
21 30
{
22 31
    ostringstream oss;
......
91 100

  
92 101
    return rc;
93 102
}
103

  
104
/* -------------------------------------------------------------------------- */
105
/* -------------------------------------------------------------------------- */
106

  
107
int DefaultQuotas::select(SqlDB *db)
108
{
109
    ostringstream oss;
110

  
111
    int rc;
112

  
113
    set_callback(
114
            static_cast<Callbackable::Callback>(&DefaultQuotas::select_cb));
115

  
116
    oss << "SELECT body FROM " << table << " WHERE name = '" << root_elem << "'";
117

  
118
    rc = db->exec(oss, this);
119

  
120
    unset_callback();
121

  
122
    if (rc != 0)
123
    {
124
        return -1;
125
    }
126

  
127
    return 0;
128
}
129

  
130
/* -------------------------------------------------------------------------- */
131
/* -------------------------------------------------------------------------- */
132

  
133
int DefaultQuotas::insert_replace(SqlDB *db, bool replace, string& error_str)
134
{
135
    ostringstream   oss;
136

  
137
    int    rc;
138
    string xml_body;
139

  
140
    char * sql_xml;
141

  
142
    sql_xml = db->escape_str(to_xml(xml_body).c_str());
143

  
144
    if ( sql_xml == 0 )
145
    {
146
        goto error_body;
147
    }
148
/*
149
    if ( validate_xml(sql_xml) != 0 )
150
    {
151
        goto error_xml;
152
    }
153
*/
154
    if ( replace )
155
    {
156
        oss << "REPLACE";
157
    }
158
    else
159
    {
160
        oss << "INSERT";
161
    }
162

  
163
    // Construct the SQL statement to Insert or Replace
164

  
165
    oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
166
        << "'" <<   root_elem            << "',"
167
        << "'" <<   sql_xml             << "')";
168

  
169
    rc = db->exec(oss);
170

  
171
    db->free_str(sql_xml);
172

  
173
    return rc;
174
/*
175
error_xml:
176
    db->free_str(sql_xml);
177

  
178
    error_str = "Error transforming the Quotas to XML.";
179

  
180
    goto error_common;
181
*/
182
error_body:
183
    goto error_generic;
184

  
185
error_generic:
186
    error_str = "Error inserting Quotas in DB.";
187
error_common:
188
    return -1;
189
}
190

  
191
/* -------------------------------------------------------------------------- */
192
/* -------------------------------------------------------------------------- */
193

  
194
int DefaultQuotas::bootstrap(SqlDB * db)
195
{
196
    ostringstream oss;
197

  
198
    oss << "CREATE TABLE IF NOT EXISTS system_attributes ("
199
            "name VARCHAR(128) PRIMARY KEY, body TEXT)";
200

  
201
    return db->exec(oss);
202
}

Also available in: Unified diff