Statistics
| Branch: | Tag: | Revision:

one / src / pool / PoolObjectSQL.cc @ c7625b48

History | View | Annotate | Download (5.14 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 "PoolObjectSQL.h"
18
#include "SSLTools.h"
19

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

    
23
string& PoolObjectSQL::to_xml64(string &xml64)
24
{
25
    string *str64;
26
    
27
    to_xml(xml64);
28

    
29
    str64 = SSLTools::base64_encode(xml64);
30
   
31
    xml64 = *str64;
32

    
33
    delete str64;
34

    
35
    return xml64;
36
}
37

    
38
/* -------------------------------------------------------------------------- */
39
/* -------------------------------------------------------------------------- */
40

    
41
int PoolObjectSQL::select(SqlDB *db)
42
{
43
    ostringstream   oss;
44
    int             rc;
45
    int             boid;
46

    
47
    set_callback(
48
            static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
49

    
50
    oss << "SELECT body FROM " << table << " WHERE oid = " << oid;
51

    
52
    boid = oid;
53
    oid  = -1;
54

    
55
    rc = db->exec(oss, this);
56

    
57
    unset_callback();
58

    
59
    if ((rc != 0) || (oid != boid ))
60
    {
61
        return -1;
62
    }
63

    
64
    return 0;
65
}
66

    
67
/* -------------------------------------------------------------------------- */
68
/* -------------------------------------------------------------------------- */
69

    
70
int PoolObjectSQL::select(SqlDB *db, const string& _name, int _uid)
71
{
72
    ostringstream oss;
73

    
74
    int rc;
75
    char * sql_name;
76

    
77
    sql_name = db->escape_str(_name.c_str());
78

    
79
    if ( sql_name == 0 )
80
    {
81
        return -1;
82
    }
83

    
84
    set_callback(
85
            static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
86

    
87
    oss << "SELECT body FROM " << table << " WHERE name = '" <<_name << "'";
88

    
89
    if ( _uid != -1 )
90
    {
91
        oss << " AND uid = " << _uid;
92
    }
93

    
94
    name  = "";
95
    uid   = -1;
96

    
97
    rc = db->exec(oss, this);
98

    
99
    unset_callback();
100

    
101
    db->free_str(sql_name);
102

    
103
    if ((rc != 0) || (_name != name) || (_uid != uid))
104
    {
105
        return -1;
106
    }
107

    
108
    return 0;
109
}
110

    
111
/* -------------------------------------------------------------------------- */
112
/* -------------------------------------------------------------------------- */
113

    
114
int PoolObjectSQL::drop(SqlDB *db)
115
{
116
    ostringstream oss;
117
    int rc;
118

    
119
    oss << "DELETE FROM " << table << " WHERE oid=" << oid;
120

    
121
    rc = db->exec(oss);
122

    
123
    if ( rc == 0 )
124
    {
125
        set_valid(false);
126
    }
127

    
128
    return rc;
129
}
130

    
131
/* -------------------------------------------------------------------------- */
132
/* -------------------------------------------------------------------------- */
133

    
134
const char * PoolObjectSQL::error_attribute_name = "ERROR";
135

    
136
void PoolObjectSQL::set_template_error_message(const string& message)
137
{
138
    VectorAttribute *  attr;
139
    map<string,string> error_value;
140
    
141
    char   str[26];
142
    time_t the_time;
143

    
144
    the_time = time(NULL);
145

    
146
#ifdef SOLARIS
147
    ctime_r(&(the_time),str,sizeof(char)*26);
148
#else
149
    ctime_r(&(the_time),str);
150
#endif
151

    
152
    str[24] = '\0'; // Get rid of final enter character
153

    
154
    error_value.insert(make_pair("TIMESTAMP",str));
155
    error_value.insert(make_pair("MESSAGE",message));
156

    
157
    attr = new VectorAttribute(error_attribute_name,error_value);
158

    
159
    obj_template->set(attr);
160
}
161

    
162
/* -------------------------------------------------------------------------- */
163
/* -------------------------------------------------------------------------- */
164

    
165
int PoolObjectSQL::replace_template(const string& tmpl_str, string& error)
166
{
167
    Template * new_tmpl  = get_new_template();
168
    char *     error_msg = 0;
169

    
170
    if ( new_tmpl == 0 )
171
    {
172
        error = "Can not allocate a new template";
173
        return -1;
174
    }
175
    
176
    if ( new_tmpl->parse(tmpl_str, &error_msg) != 0 )
177
    {
178
        ostringstream oss;
179
        
180
        oss << "Parse error";
181
         
182
        if (error_msg != 0)
183
        {
184
            oss << ": " << error_msg;
185
        }
186
        
187
        error = oss.str();
188

    
189
        return -1;
190
    }
191

    
192
    delete obj_template;
193

    
194
    obj_template = new_tmpl;
195

    
196
    return 0;
197
} 
198

    
199
/* -------------------------------------------------------------------------- */
200
/* -------------------------------------------------------------------------- */
201