2010-03-29 13:57:33 +02:00
/* -------------------------------------------------------------------------- */
2022-04-07 19:49:58 +02:00
/* Copyright 2002-2022, OpenNebula Project, OpenNebula Systems */
2010-03-29 13:57:33 +02: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>
2020-04-13 17:32:21 +02:00
# include <map>
2010-04-03 16:54:09 +02:00
# include "Callbackable.h"
2010-03-29 13:57:33 +02:00
/**
* SqlDB class . Provides an abstract interface to implement a SQL backend
*/
class SqlDB
{
public :
SqlDB ( ) { } ;
virtual ~ SqlDB ( ) { } ;
2019-02-13 12:58:56 +01:00
enum SqlError
{
SUCCESS = 0 ,
INTERNAL = - 1 ,
CONNECTION = - 100 ,
SQL = - 200 ,
SQL_DUP_KEY = - 201
} ;
2020-04-13 17:32:21 +02:00
enum class SqlFeature
{
MULTIPLE_VALUE , // syntax INSERT VALUES (data), (data), (data)
LIMIT , // LIMIT in queries with DELETE and UPDATE
2020-06-03 18:11:04 +02:00
FTS , // Full Text Search
COMPARE_BINARY // Use BINARY for comparing name in DB
2020-04-13 17:32:21 +02:00
} ;
2017-04-18 16:32:23 +02:00
/* ---------------------------------------------------------------------- */
/* Database Operations */
/* ---------------------------------------------------------------------- */
2010-03-29 13:57:33 +02:00
/**
2017-04-21 19:16:45 +02: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 13:57:33 +02:00
* @ param sql_cmd the SQL command
* @ param callbak function to execute on each data returned
* @ return 0 on success
*/
2019-02-13 10:30:49 +01:00
virtual int exec_local_wr ( std : : ostringstream & cmd )
2017-04-18 16:32:23 +02:00
{
return exec ( cmd , 0 , false ) ;
}
2019-02-13 10:30:49 +01:00
virtual int exec_rd ( std : : ostringstream & cmd , Callbackable * obj )
2017-04-18 16:32:23 +02:00
{
return exec ( cmd , obj , false ) ;
}
2019-02-13 10:30:49 +01:00
virtual int exec_wr ( std : : ostringstream & cmd )
2017-04-18 16:32:23 +02:00
{
return exec ( cmd , 0 , false ) ;
}
2010-03-29 13:57:33 +02:00
2019-02-13 10:30:49 +01:00
virtual int exec_wr ( std : : ostringstream & cmd , Callbackable * obj )
2018-04-19 15:21:30 +02:00
{
return exec ( cmd , obj , false ) ;
}
2019-02-13 10:30:49 +01:00
/* ---------------------------------------------------------------------- */
2019-02-13 12:58:56 +01:00
int exec_ext ( std : : ostringstream & cmd )
2019-02-13 10:30:49 +01:00
{
2019-02-13 12:58:56 +01:00
return exec_ext ( cmd , 0 , false ) ;
}
int exec_ext ( std : : ostringstream & cmd , Callbackable * obj )
{
return exec_ext ( cmd , obj , false ) ;
2019-02-13 10:30:49 +01:00
}
2018-04-23 12:43:29 +02:00
/**
2010-03-29 13:57:33 +02: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
*/
2020-07-05 22:01:32 +02:00
virtual char * escape_str ( const std : : string & str ) const = 0 ;
2010-03-29 13:57:33 +02:00
/**
* Frees a previously scaped string
* @ param str pointer to the str
*/
2020-07-05 22:01:32 +02:00
virtual void free_str ( char * str ) const = 0 ;
2015-02-24 15:34:11 +01:00
2017-04-18 16:32:23 +02:00
2020-07-05 22:01:32 +02:00
virtual bool supports ( SqlFeature ft ) const
2020-04-13 17:32:21 +02:00
{
auto it = features . find ( ft ) ;
2018-04-25 17:05:30 +02:00
2020-04-13 17:32:21 +02:00
if ( it = = features . end ( ) )
{
return false ;
}
2019-01-31 17:09:47 +01:00
2020-04-13 17:32:21 +02:00
return it - > second ;
}
2019-06-07 16:57:01 +02:00
/**
* @ return pointer to a non - federated version of this database
*/
virtual SqlDB * get_local_db ( )
{
return this ;
}
2020-04-13 17:32:21 +02:00
/**
* @ return string with compatible LIMIT clause syntax
* LIMIT [ offset ] , row_count
2020-05-25 17:43:10 +02:00
*
2020-04-13 17:32:21 +02:00
* + - - - + - - - + - - - + - - - + - - - + - - - + - - - + - - - + - -
* | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | . . .
* + - - - + - - - + - - - + - - - + - - - + - - - + - - - + - - - + - -
* | |
* / - - - - - - - - - - - - - - - - - - - /
* LIMIT 3 , 5
*/
2020-07-05 22:01:32 +02:00
virtual std : : string limit_string ( int sid , int eid ) const
2020-04-13 17:32:21 +02:00
{
std : : ostringstream oss ;
oss < < " LIMIT " < < sid < < " , " < < eid ;
return oss . str ( ) ;
}
2020-07-05 22:01:32 +02:00
virtual std : : string limit_string ( int sid ) const
2020-04-13 17:32:21 +02:00
{
std : : ostringstream oss ;
oss < < " LIMIT " < < sid ;
return oss . str ( ) ;
}
2020-06-03 18:11:04 +02:00
void add_binary ( std : : ostringstream & oss )
{
if ( supports ( SqlFeature : : COMPARE_BINARY ) )
{
oss < < " BINARY " ;
}
}
2021-01-25 18:55:50 +01:00
/**
* Set maximal number of consecutive errors to terminate oned in HA environment
* - 1 to disable this feature
*/
void set_errors_limit ( int limit )
{
errors_limit = limit ;
}
2017-04-18 16:32:23 +02: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
2019-02-13 12:58:56 +01:00
* @ return 0 on success - 1 on failure
2017-04-18 16:32:23 +02:00
*/
2021-01-25 18:55:50 +01:00
int exec ( std : : ostringstream & cmd , Callbackable * obj , bool quiet ) ;
2019-02-13 10:30:49 +01:00
2019-02-13 12:58:56 +01:00
/**
* This function performs a DB transaction and returns and extended error code
* @ return SqlError enum
*/
virtual int exec_ext ( std : : ostringstream & cmd , Callbackable * obj , bool quiet ) = 0 ;
2020-04-13 17:32:21 +02:00
/**
* Feature set
*/
std : : map < SqlFeature , bool > features = {
{ SqlFeature : : MULTIPLE_VALUE , false } ,
{ SqlFeature : : LIMIT , false } ,
2020-06-03 18:11:04 +02:00
{ SqlFeature : : FTS , false } ,
{ SqlFeature : : COMPARE_BINARY , false }
2020-04-13 17:32:21 +02:00
} ;
2021-01-25 18:55:50 +01:00
private :
/**
* Actual number of consecutive errors
*/
int consecutive_errors = 0 ;
/**
* Maximal number of consecutive errors to terminate oned in HA environment
* - 1 to disable this feature
*/
int errors_limit = - 1 ;
2010-03-29 13:57:33 +02:00
} ;
# endif /*SQL_DB_H_*/