/* -------------------------------------------------------------------------- */ /* 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. */ /* -------------------------------------------------------------------------- */ #ifndef OBJECT_SQL_H_ #define OBJECT_SQL_H_ #include "SqlDB.h" using namespace std; class SqlDB; /** * ObjectSQL class. Provides a SQL backend interface, it should be implemented * by persistent objects. */ class ObjectSQL { public: ObjectSQL(){}; virtual ~ObjectSQL(){}; /** * Datatype for call back pointers */ typedef int (ObjectSQL::*CallBackPtr)(void *, int, char ** ,char **); /** * Set the callback function and custom arguments to be executed by the * next SQL command * @param ptr to the callback function * @param arg custom arguments for the callback function */ void set_callback(CallBackPtr ptr, void * arg) { callback.cb = ptr; callback.arg = arg; }; /** * Test if the CallBack is set for the object. * @return true if the callback is set */ bool isCallBackSet() { return (callback.cb != 0); }; /** * Set the callback function and custom arguments to be executed by the * next SQL command * @param ptr to the callback function * @param arg custom arguments for the callback function */ int do_callback(int num, char **values, char **names) { return (this->*(callback.cb))(callback.arg, num, values, names); }; protected: /** * Reads the ObjectSQL (identified with its OID) from the database. * @param db pointer to the db * @return 0 on success */ virtual int select( SqlDB * db) = 0; /** * Writes the ObjectSQL in the database. * @param db pointer to the db * @return 0 on success */ virtual int insert( SqlDB * db) = 0; /** * Updates the ObjectSQL in the database. * @param db pointer to the db * @return 0 on success */ virtual int update( SqlDB * db) = 0; /** * Removes the ObjectSQL from the database. * @param db pointer to the db * @return 0 on success */ virtual int drop( SqlDB * db) = 0; private: /** * A Callback defined by the function to be executed, a generic argument * and the columns returned by the query */ struct CallBackRegister { CallBackPtr cb; void * arg; }; /** * SQL callback to be executed for each row result of an SQL statement */ CallBackRegister callback; }; #endif /*OBJECT_SQL_H_*/