1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

Safer insert strings in the DB

git-svn-id: http://svn.opennebula.org/one/trunk@515 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Rubén S. Montero 2009-05-07 22:16:03 +00:00
parent 80160c6fc6
commit 191d4f9a42
5 changed files with 147 additions and 17 deletions

View File

@ -199,10 +199,15 @@ int Host::insert(SqliteDB *db)
int Host::update(SqliteDB *db)
{
ostringstream oss;
int rc;
int managed_i = managed?1:0;
int rc;
int managed_i = managed?1:0;
char * sql_hostname;
char * sql_im_mad_name;
char * sql_tm_mad_name;
char * sql_vmm_mad_name;
//Update template.
rc = host_template.update(db);
@ -221,21 +226,62 @@ int Host::update(SqliteDB *db)
return rc;
}
sql_hostname = sqlite3_mprintf("%q",hostname.c_str());
if ( sql_hostname == 0 )
{
goto error_hostname;
}
sql_im_mad_name = sqlite3_mprintf("%q",im_mad_name.c_str());
if ( sql_im_mad_name == 0 )
{
goto error_im;
}
sql_tm_mad_name = sqlite3_mprintf("%q",tm_mad_name.c_str());
if ( sql_tm_mad_name == 0 )
{
goto error_tm;
}
sql_vmm_mad_name = sqlite3_mprintf("%q",vmm_mad_name.c_str());
if ( sql_vmm_mad_name == 0 )
{
goto error_vmm;
}
// Construct the SQL statement to Insert or Replace (effectively, update)
oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES ("<<
oid << "," <<
"'" << hostname << "'," <<
"'" << sql_hostname << "'," <<
state << "," <<
"'" << im_mad_name << "'," <<
"'" << vmm_mad_name << "'," <<
"'" << tm_mad_name << "'," <<
"'" << sql_im_mad_name << "'," <<
"'" << sql_vmm_mad_name << "'," <<
"'" << sql_tm_mad_name << "'," <<
last_monitored << "," <<
managed_i << ")";
rc = db->exec(oss);
sqlite3_free(sql_hostname);
sqlite3_free(sql_im_mad_name);
sqlite3_free(sql_im_mad_name);
sqlite3_free(sql_vmm_mad_name);
return rc;
error_vmm:
sqlite3_free(sql_tm_mad_name);
error_tm:
sqlite3_free(sql_im_mad_name);
error_im:
sqlite3_free(sql_hostname);
error_hostname:
return -1;
}
/* -------------------------------------------------------------------------- */

View File

@ -138,21 +138,55 @@ void History::non_persistent_data()
int History::insert(SqliteDB * db)
{
ostringstream oss;
int rc;
int rc;
char * sql_hostname;
char * sql_vm_dir;
char * sql_vmm_mad_name;
char * sql_tm_mad_name;
if (seq == -1)
{
return 0;
}
sql_hostname = sqlite3_mprintf("%q",hostname.c_str());
if ( sql_hostname == 0 )
{
goto error_hostname;
}
sql_vm_dir = sqlite3_mprintf("%q",vm_dir.c_str());
if ( sql_vm_dir == 0 )
{
goto error_vm_dir;
}
sql_vmm_mad_name = sqlite3_mprintf("%q",vmm_mad_name.c_str());
if ( sql_vmm_mad_name == 0 )
{
goto error_vmm;
}
sql_tm_mad_name = sqlite3_mprintf("%q",tm_mad_name.c_str());
if ( sql_tm_mad_name == 0 )
{
goto error_tm;
}
oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES ("<<
oid << "," <<
seq << "," <<
"'" << hostname << "',"<<
"'" << vm_dir << "'," <<
"'" << sql_hostname << "',"<<
"'" << sql_vm_dir << "'," <<
hid << "," <<
"'" << vmm_mad_name << "'," <<
"'" << tm_mad_name << "'," <<
"'" << sql_vmm_mad_name << "'," <<
"'" << sql_tm_mad_name << "'," <<
stime << "," <<
etime << "," <<
prolog_stime << "," <<
@ -165,7 +199,21 @@ int History::insert(SqliteDB * db)
rc = db->exec(oss);
sqlite3_free(sql_hostname);
sqlite3_free(sql_vm_dir);
sqlite3_free(sql_vmm_mad_name);
sqlite3_free(sql_tm_mad_name);
return rc;
error_tm:
sqlite3_free(sql_vmm_mad_name);
error_vmm:
sqlite3_free(sql_vm_dir);
error_vm_dir:
sqlite3_free(sql_hostname);
error_hostname:
return -1;
}
/* -------------------------------------------------------------------------- */

View File

@ -354,6 +354,13 @@ int VirtualMachine::update(SqliteDB * db)
ostringstream oss;
int rc;
char * sql_deploy_id = sqlite3_mprintf("%q",deploy_id.c_str());
if ( sql_deploy_id == 0 )
{
return -1;
}
oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES ("<<
oid << "," <<
uid << "," <<
@ -363,12 +370,14 @@ int VirtualMachine::update(SqliteDB * db)
lcm_state << "," <<
stime << "," <<
etime << "," <<
"'" << deploy_id << "'," <<
"'" << sql_deploy_id << "'," <<
memory << "," <<
cpu << "," <<
net_tx << "," <<
net_rx << ")";
sqlite3_free(sql_deploy_id);
rc = db->exec(oss);
return rc;

View File

@ -356,16 +356,34 @@ int VirtualNetwork::update(SqliteDB * db)
{
ostringstream oss;
int rc;
char * sql_name = sqlite3_mprintf("%q",name.c_str());
if ( sql_name == 0 )
{
return -1;
}
char * sql_bridge = sqlite3_mprintf("%q",bridge.c_str());
if ( sql_bridge == 0 )
{
sqlite3_free(sql_name);
return -1;
}
oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES ("<<
oid << "," <<
uid << "," <<
"'" << name << "'," <<
"'" << sql_name << "'," <<
type << "," <<
"'" << bridge << "')";
"'" << sql_bridge << "')";
rc = db->exec(oss);
sqlite3_free(sql_name);
sqlite3_free(sql_bridge);
return rc;
}

View File

@ -153,12 +153,21 @@ VirtualNetwork * VirtualNetworkPool::get(const string& name, bool lock)
int oid;
int rc;
char * sql_name = sqlite3_mprintf("%q",name.c_str());
if ( sql_name == 0 )
{
return 0;
}
oss << "SELECT oid FROM " << VirtualNetwork::table << " WHERE name = '"
<< name << "'";
<< sql_name << "'";
rc = db->exec(oss, select_name_cb, (void *) (&oid));
sqlite3_free(sql_name);
if (rc != 0)
{
return 0;