Revision 855d34f9 src/um/DefaultQuotas.cc

View differences:

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