1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-25 23:21:29 +03:00

Feature #407: Refator of PoolObjectSQL class

This commit is contained in:
Carlos Martín 2011-03-09 12:44:39 +01:00
parent b2f1051e07
commit cd8c19f930
3 changed files with 115 additions and 82 deletions

View File

@ -143,99 +143,21 @@ protected:
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
};
virtual int select(SqlDB *db);
/**
* Reads the PoolObjectSQL (identified by its OID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db, const string& _name, int _uid)
{
ostringstream oss;
int rc;
char * sql_name;
sql_name = db->escape_str(_name.c_str());
if ( sql_name == 0 )
{
return -1;
}
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table << " WHERE name = '" <<_name << "'";
if ( _uid != -1 )
{
oss << " AND uid = " << _uid;
}
name = "";
uid = -1;
rc = db->exec(oss, this);
unset_callback();
db->free_str(sql_name);
if ((rc != 0) || (_name != name) || (_uid != uid))
{
return -1;
}
return 0;
};
virtual int select(SqlDB *db, const string& _name, int _uid);
/**
* Drops object from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
};
virtual int drop(SqlDB *db);
/**
* Function to output a pool object into a stream in XML format

110
src/pool/PoolObjectSQL.cc Normal file
View File

@ -0,0 +1,110 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "PoolObjectSQL.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::select(SqlDB *db, const string& _name, int _uid)
{
ostringstream oss;
int rc;
char * sql_name;
sql_name = db->escape_str(_name.c_str());
if ( sql_name == 0 )
{
return -1;
}
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table << " WHERE name = '" <<_name << "'";
if ( _uid != -1 )
{
oss << " AND uid = " << _uid;
}
name = "";
uid = -1;
rc = db->exec(oss, this);
unset_callback();
db->free_str(sql_name);
if ((rc != 0) || (_name != name) || (_uid != uid))
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::drop(SqlDB *db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}

View File

@ -22,7 +22,8 @@ lib_name='nebula_pool'
# Sources to generate the library
source_files=[
'PoolSQL.cc'
'PoolSQL.cc',
'PoolObjectSQL.cc'
]
# Build library