From cd8c19f93091c009230db2fcb5192137f72f495f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 9 Mar 2011 12:44:39 +0100 Subject: [PATCH] Feature #407: Refator of PoolObjectSQL class --- include/PoolObjectSQL.h | 84 ++--------------------------- src/pool/PoolObjectSQL.cc | 110 ++++++++++++++++++++++++++++++++++++++ src/pool/SConstruct | 3 +- 3 files changed, 115 insertions(+), 82 deletions(-) create mode 100644 src/pool/PoolObjectSQL.cc diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index 83554ed22b..58c9f8a35e 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -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(&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(&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 diff --git a/src/pool/PoolObjectSQL.cc b/src/pool/PoolObjectSQL.cc new file mode 100644 index 0000000000..103ab92e50 --- /dev/null +++ b/src/pool/PoolObjectSQL.cc @@ -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(&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(&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; +} diff --git a/src/pool/SConstruct b/src/pool/SConstruct index c97e4031e5..1eaba21b4f 100644 --- a/src/pool/SConstruct +++ b/src/pool/SConstruct @@ -22,7 +22,8 @@ lib_name='nebula_pool' # Sources to generate the library source_files=[ - 'PoolSQL.cc' + 'PoolSQL.cc', + 'PoolObjectSQL.cc' ] # Build library