2010-04-17 02:25:04 +04:00
/* -------------------------------------------------------------------------- */
2021-02-09 18:07:56 +03:00
/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */
2010-04-17 02:25:04 +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 MYSQL_DB_H_
# define MYSQL_DB_H_
# include <string>
# include <sstream>
# include <stdexcept>
2013-09-11 14:38:58 +04:00
# include <queue>
2020-09-10 14:32:52 +03:00
# include <condition_variable>
2010-04-17 02:25:04 +04:00
# include <sys/time.h>
# include <sys/types.h>
# include <unistd.h>
2010-04-21 02:40:16 +04:00
# include "NebulaLog.h"
2010-04-17 02:25:04 +04:00
# include "SqlDB.h"
# include "ObjectSQL.h"
2010-05-07 16:45:27 +04:00
# ifdef MYSQL_DB
# include <mysql.h>
2010-04-17 02:25:04 +04:00
/**
* SqliteDB class . Provides a wrapper to the mysql database interface .
*/
class MySqlDB : public SqlDB
{
public :
2020-04-13 18:32:21 +03:00
MySqlDB ( const std : : string & _server ,
int _port ,
const std : : string & _user ,
const std : : string & _password ,
const std : : string & _database ,
const std : : string & _encoding ,
2020-06-03 19:11:04 +03:00
int _connections ,
std : : string & _compare_binary ) ;
2010-04-17 02:25:04 +04:00
2010-05-07 16:45:27 +04:00
~ MySqlDB ( ) ;
2010-04-17 02:25:04 +04:00
/**
* This function returns a legal SQL string that can be used in an SQL
* statement . The string is encoded to an escaped SQL string , taking into
* account the current character set of the connection .
* @ param str the string to be escaped
* @ return a valid SQL string or NULL in case of failure
*/
2020-07-05 23:01:32 +03:00
char * escape_str ( const std : : string & str ) const ;
2010-04-17 02:25:04 +04:00
/**
* Frees a previously scaped string
* @ param str pointer to the str
*/
2020-07-05 23:01:32 +03:00
void free_str ( char * str ) const
2020-03-31 16:07:23 +03:00
{
delete [ ] str ;
}
2010-04-17 02:25:04 +04:00
2017-04-18 17:32:23 +03:00
protected :
/**
* Wraps the mysql_query function call
* @ param cmd the SQL command
* @ param obj Callbackable obj to call if the query succeeds
* @ return 0 on success
*/
2020-04-13 18:32:21 +03:00
int exec_ext ( std : : ostringstream & c , Callbackable * o , bool q ) override ;
2017-04-18 17:32:23 +03:00
2010-04-17 02:25:04 +04:00
private :
2019-11-19 16:50:53 +03:00
/**
* This functions set the encoding to that being used for the OpenNebula
* database and creates the database if needed
*/
int db_encoding ( std : : string & error ) ;
2010-04-17 02:25:04 +04:00
/**
2013-09-11 14:38:58 +04:00
* Number of concurrent DB connections .
2010-04-17 02:25:04 +04:00
*/
2018-03-18 01:31:52 +03:00
int max_connections ;
2013-09-11 14:38:58 +04:00
/**
* The MySql connection pool handler
*/
2020-07-02 23:42:10 +03:00
std : : queue < MYSQL * > db_connect ;
2013-09-11 14:38:58 +04:00
/**
* Cached DB connection to escape strings ( it uses the server character set )
*/
2019-11-19 16:50:53 +03:00
MYSQL * db_escape_connect ;
2010-05-06 18:16:50 +04:00
2011-05-06 19:06:57 +04:00
/**
* MySQL Connection parameters
*/
2020-04-13 18:32:21 +03:00
std : : string server ;
2019-11-19 16:50:53 +03:00
int port ;
2011-05-06 19:06:57 +04:00
2020-04-13 18:32:21 +03:00
std : : string user ;
2011-05-06 19:06:57 +04:00
2020-04-13 18:32:21 +03:00
std : : string password ;
2011-05-06 19:06:57 +04:00
2020-04-13 18:32:21 +03:00
std : : string database ;
2011-05-06 19:06:57 +04:00
2020-04-13 18:32:21 +03:00
std : : string encoding ;
2020-03-31 16:07:23 +03:00
2010-05-06 18:16:50 +04:00
/**
2013-09-11 14:38:58 +04:00
* Fine - grain mutex for DB access ( pool of DB connections )
2010-05-06 18:16:50 +04:00
*/
2020-09-10 14:32:52 +03:00
std : : mutex _mutex ;
2010-05-06 18:16:50 +04:00
/**
2013-09-11 14:38:58 +04:00
* Conditional variable to wake - up waiting threads .
2010-05-06 18:16:50 +04:00
*/
2020-09-10 14:32:52 +03:00
std : : condition_variable cond ;
2010-05-06 18:16:50 +04:00
/**
2013-09-11 14:38:58 +04:00
* Gets a free DB connection from the pool .
2010-05-06 18:16:50 +04:00
*/
2013-09-11 14:38:58 +04:00
MYSQL * get_db_connection ( ) ;
/**
* Returns the connection to the pool .
*/
2020-03-31 16:07:23 +03:00
void free_db_connection ( MYSQL * db ) ;
2010-04-17 02:25:04 +04:00
} ;
2010-05-07 16:45:27 +04:00
# else
//CLass stub
class MySqlDB : public SqlDB
{
public :
2020-04-13 18:32:21 +03:00
MySqlDB ( const std : : string & _server ,
2020-06-04 11:59:16 +03:00
int _port ,
2020-04-13 18:32:21 +03:00
const std : : string & _user ,
const std : : string & _password ,
const std : : string & _database ,
const std : : string & _encoding ,
2020-06-04 11:59:16 +03:00
int _connections ,
2020-06-04 12:02:39 +03:00
std : : string & _compare_binary )
2010-05-07 16:45:27 +04:00
{
2020-07-06 16:48:53 +03:00
throw std : : runtime_error ( " Aborting oned, MySQL support not compiled! " ) ;
2010-05-07 16:45:27 +04:00
} ;
~ MySqlDB ( ) { } ;
2020-07-05 23:01:32 +03:00
char * escape_str ( const std : : string & str ) const override { return nullptr ; } ;
2010-05-07 16:45:27 +04:00
2020-07-05 23:01:32 +03:00
void free_str ( char * str ) const override { } ;
2019-01-31 19:44:23 +03:00
2017-04-18 17:32:23 +03:00
protected :
2020-04-13 18:32:21 +03:00
int exec_ext ( std : : ostringstream & c , Callbackable * o , bool q ) override {
return - 1 ;
} ;
2010-05-07 16:45:27 +04:00
} ;
# endif
2010-04-17 02:25:04 +04:00
2010-09-02 22:44:14 +04:00
# endif /*MYSQL_DB_H_*/