Statistics
| Branch: | Tag: | Revision:

one / src / group / Group.cc @ ae53d437

History | View | Annotate | Download (5.11 KB)

1
/* ------------------------------------------------------------------------ */
2
/* Copyright 2002-2010, 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
#include <limits.h>
18
#include <string.h>
19

    
20
#include <iostream>
21
#include <sstream>
22

    
23
#include "Group.h"
24

    
25

    
26
const char * Group::table = "group_pool";
27

    
28
const char * Group::db_names = "oid, name, body, uid";
29

    
30
const char * Group::db_bootstrap = "CREATE TABLE IF NOT EXISTS group_pool ("
31
    "oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, "
32
    "UNIQUE(name))";
33

    
34

    
35
/* ************************************************************************ */
36
/* Group :: Constructor/Destructor                                        */
37
/* ************************************************************************ */
38

    
39
Group::Group(int id, int uid, const string& name):
40
    PoolObjectSQL(id,name,uid,table){};
41

    
42
Group::~Group(){};
43

    
44
/* ************************************************************************ */
45
/* Group :: Database Access Functions                                     */
46
/* ************************************************************************ */
47

    
48
/* ------------------------------------------------------------------------ */
49
/* ------------------------------------------------------------------------ */
50

    
51
int Group::insert(SqlDB *db, string& error_str)
52
{
53
    int rc;
54

    
55
    rc = insert_replace(db, false);
56

    
57
    if ( rc != 0 )
58
    {
59
        error_str = "Error inserting Group in DB.";
60
    }
61

    
62
    return rc;
63
}
64

    
65
/* ------------------------------------------------------------------------ */
66
/* ------------------------------------------------------------------------ */
67

    
68
int Group::update(SqlDB *db)
69
{
70
    int    rc;
71

    
72
    rc = insert_replace(db, true);
73

    
74
    return rc;
75
}
76

    
77
/* ------------------------------------------------------------------------ */
78
/* ------------------------------------------------------------------------ */
79

    
80
int Group::insert_replace(SqlDB *db, bool replace)
81
{
82
    ostringstream   oss;
83

    
84
    int    rc;
85
    string xml_body;
86

    
87
    char * sql_name;
88
    char * sql_xml;
89

    
90
   // Update the Group
91

    
92
    sql_name = db->escape_str(name.c_str());
93

    
94
    if ( sql_name == 0 )
95
    {
96
        goto error_name;
97
    }
98

    
99
    sql_xml = db->escape_str(to_xml(xml_body).c_str());
100

    
101
    if ( sql_xml == 0 )
102
    {
103
        goto error_body;
104
    }
105

    
106
    if(replace)
107
    {
108
        oss << "REPLACE";
109
    }
110
    else
111
    {
112
        oss << "INSERT";
113
    }
114

    
115
    // Construct the SQL statement to Insert or Replace
116

    
117
    oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
118
        <<          oid                 << ","
119
        << "'" <<   sql_name            << "',"
120
        << "'" <<   sql_xml             << "',"
121
        <<          uid                 << ")";
122

    
123
    rc = db->exec(oss);
124

    
125
    db->free_str(sql_name);
126
    db->free_str(sql_xml);
127

    
128
    return rc;
129

    
130
error_body:
131
    db->free_str(sql_name);
132
error_name:
133
    return -1;
134
}
135

    
136
/* ************************************************************************ */
137
/* Group :: Misc                                                          */
138
/* ************************************************************************ */
139

    
140
ostream& operator<<(ostream& os, Group& group)
141
{
142
    string group_str;
143

    
144
    os << group.to_xml(group_str);
145

    
146
    return os;
147
}
148

    
149
/* ------------------------------------------------------------------------ */
150
/* ------------------------------------------------------------------------ */
151

    
152
string& Group::to_xml(string& xml) const
153
{
154
    ostringstream   oss;
155

    
156
    oss <<
157
    "<GROUP>"  <<
158
        "<ID>"   << oid  << "</ID>"   <<
159
        "<UID>"  << uid  << "</UID>"  <<
160
        "<NAME>" << name << "</NAME>" <<
161
    "</GROUP>";
162

    
163
    xml = oss.str();
164

    
165
    return xml;
166
}
167

    
168
/* ------------------------------------------------------------------------ */
169
/* ------------------------------------------------------------------------ */
170

    
171
int Group::from_xml(const string& xml)
172
{
173
    int rc = 0;
174

    
175
    // Initialize the internal XML object
176
    update_from_str(xml);
177

    
178
    // Get class base attributes
179
    rc += xpath(oid, "/GROUP/ID",   -1);
180
    rc += xpath(uid, "/GROUP/UID",  -1);
181
    rc += xpath(name,"/GROUP/NAME", "not_found");
182

    
183
    if (rc != 0)
184
    {
185
        return -1;
186
    }
187

    
188
    return 0;
189
}