Skip to content

Commit

Permalink
B #3946: Optionally forced encoding for oned connections
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmontero committed Nov 19, 2019
1 parent 5dc8f65 commit 8fb2010
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions include/MySqlDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MySqlDB : public SqlDB
const string& _user,
const string& _password,
const string& _database,
const string& _encoding,
int _connections);

~MySqlDB();
Expand Down
1 change: 1 addition & 0 deletions share/etc/oned.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
# passwd : (mysql) the password for user
# db_name : (mysql) the database name
# connections: (mysql) number of max. connections to mysql server
# encoding: charset to use for the db connections
#
# VNC_PORTS: VNC port pool for automatic VNC port assignment, if possible the
# port will be set to ``START`` + ``VMID``
Expand Down
8 changes: 7 additions & 1 deletion src/nebula/Nebula.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ void Nebula::start(bool bootstrap_only)
string user;
string passwd;
string db_name;
string encoding;
int connections;

const VectorAttribute * _db = nebula_configuration->get("DB");
Expand Down Expand Up @@ -307,6 +308,11 @@ void Nebula::start(bool bootstrap_only)
{
connections = 50;
}

if (_db->vector_value("ENCODING", encoding) == -1)
{
encoding = "";
}
}

if ( db_backend_type == "sqlite" )
Expand All @@ -316,7 +322,7 @@ void Nebula::start(bool bootstrap_only)
else
{
db_backend = new MySqlDB(server, port, user, passwd, db_name,
connections);
encoding, connections);
}

// ---------------------------------------------------------------------
Expand Down
51 changes: 28 additions & 23 deletions src/sql/MySqlDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,40 @@ int MySqlDB::db_encoding(std::string& error)
return -1;
}

//Set encoding for the database clients
std::string encoding_sql = "SELECT default_character_set_name FROM "
"information_schema.SCHEMATA WHERE schema_name = \"" + database + "\"";

if ( mysql_query(connection, encoding_sql.c_str()) != 0 )
if (encoding.empty())
{
error = "Could not read database encoding.";
return -1;
}
//Set encoding for the database clients
std::string encoding_sql = "SELECT default_character_set_name FROM "
"information_schema.SCHEMATA WHERE schema_name = \"" + database + "\"";

MYSQL_RES * result = mysql_store_result(connection);
if ( mysql_query(connection, encoding_sql.c_str()) != 0 )
{
error = "Could not read database encoding.";
return -1;
}

if (result == nullptr)
{
error = "Could not read database encoding: ";
error.append(mysql_error(connection));
MYSQL_RES * result = mysql_store_result(connection);

return -1;
}
if (result == nullptr)
{
error = "Could not read database encoding: ";
error.append(mysql_error(connection));

MYSQL_ROW row = mysql_fetch_row(result);
return -1;
}

if ( row == nullptr )
{
error = "Could not read databse encoding";
return -1;
}
MYSQL_ROW row = mysql_fetch_row(result);

encoding = ((char **) row)[0];
if ( row == nullptr )
{
error = "Could not read databse encoding";
return -1;
}

encoding = ((char **) row)[0];

mysql_free_result(result);
mysql_free_result(result);
}

mysql_close(connection);

Expand All @@ -92,6 +95,7 @@ MySqlDB::MySqlDB(
const string& _user,
const string& _password,
const string& _database,
const string& _encoding,
int _max_connections)
{
vector<MYSQL *> connections(_max_connections);
Expand All @@ -105,6 +109,7 @@ MySqlDB::MySqlDB(
user = _user;
password = _password;
database = _database;
encoding = _encoding;

max_connections = _max_connections;

Expand Down

0 comments on commit 8fb2010

Please sign in to comment.