2010-03-29 15:57:33 +04:00
/* -------------------------------------------------------------------------- */
2018-01-02 20:27:37 +03:00
/* Copyright 2002-2018, OpenNebula Project, OpenNebula Systems */
2010-03-29 15:57:33 +04:00
/* */
/* 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 SQL_DB_H_
# define SQL_DB_H_
# include <sstream>
2010-04-03 18:54:09 +04:00
# include "Callbackable.h"
2010-03-29 15:57:33 +04:00
using namespace std ;
/**
* SqlDB class . Provides an abstract interface to implement a SQL backend
*/
class SqlDB
{
public :
SqlDB ( ) { } ;
virtual ~ SqlDB ( ) { } ;
2017-04-18 17:32:23 +03:00
/* ---------------------------------------------------------------------- */
/* Database Operations */
/* ---------------------------------------------------------------------- */
2010-03-29 15:57:33 +04:00
/**
2017-04-21 20:16:45 +03:00
* Operations on the database :
* - exec_local_wr , perform modifications locally , without replication
* - exec_rd , read only access to local DB
* - exec_wr , update DB and replicate changes
2010-03-29 15:57:33 +04:00
* @ param sql_cmd the SQL command
* @ param callbak function to execute on each data returned
* @ return 0 on success
*/
2017-04-21 20:16:45 +03:00
virtual int exec_local_wr ( ostringstream & cmd )
2017-04-18 17:32:23 +03:00
{
return exec ( cmd , 0 , false ) ;
}
2017-04-19 21:35:39 +03:00
virtual int exec_rd ( ostringstream & cmd , Callbackable * obj )
2017-04-18 17:32:23 +03:00
{
return exec ( cmd , obj , false ) ;
}
2017-04-19 21:35:39 +03:00
virtual int exec_wr ( ostringstream & cmd )
2017-04-18 17:32:23 +03:00
{
return exec ( cmd , 0 , false ) ;
}
2010-03-29 15:57:33 +04:00
/**
* This function returns a legal SQL string that can be used in an SQL
* statement .
* @ param str the string to be escaped
* @ return a valid SQL string or NULL in case of failure
*/
virtual char * escape_str ( const string & str ) = 0 ;
/**
* Frees a previously scaped string
* @ param str pointer to the str
*/
virtual void free_str ( char * str ) = 0 ;
2015-02-24 17:34:11 +03:00
/**
* Returns true if the syntax INSERT VALUES ( data ) , ( data ) , ( data )
* is supported
*
* @ return true if supported
*/
virtual bool multiple_values_support ( ) = 0 ;
2017-04-18 17:32:23 +03:00
protected :
/**
* Performs a DB transaction
* @ param sql_cmd the SQL command
* @ param callbak function to execute on each data returned
* @ param quiet True to log errors with DDEBUG level instead of ERROR
* @ return 0 on success
*/
virtual int exec ( ostringstream & cmd , Callbackable * obj , bool quiet ) = 0 ;
2010-03-29 15:57:33 +04:00
} ;
# endif /*SQL_DB_H_*/