1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-26 10:03:37 +03:00

feature #206: Isolation of INSERT and REPLACE SQL queries for Host objects.

This commit is contained in:
Constantino Vázquez Blanco 2010-04-28 15:00:07 +02:00
parent 5cf9d453e1
commit 0e1a364736
6 changed files with 148 additions and 46 deletions

View File

@ -447,6 +447,14 @@ private:
// DataBase implementation (Private)
// *************************************************************************
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a Host object (Host::select)
* @param num the number of columns read from the DB

View File

@ -198,6 +198,14 @@ private:
*/
int drop(SqlDB * db);
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a HostShare object (HostShare::select)
* @param num the number of columns read from the DB

View File

@ -86,6 +86,14 @@ protected:
*/
int drop(SqlDB *db);
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Removes a template attribute from the DB. If there are multiple
* attributes with the same name, only one will be replaced. The

View File

@ -146,19 +146,33 @@ int Host::insert(SqlDB *db)
map<int,HostShare *>::iterator iter;
// Set up the template ID, to insert it
if ( host_template.id == -1 )
{
host_template.id = oid;
}
// Set up the share ID, to insert it
if ( host_share.hsid == -1 )
{
host_share.hsid = oid;
}
// Update the Template
rc = host_template.insert(db);
if ( rc != 0 )
{
return rc;
}
// Update the HostShare
rc = host_share.insert(db);
if ( rc != 0 )
{
return rc;
}
//Insert the Host and its template
rc = insert_replace(db, false);
@ -170,10 +184,45 @@ int Host::insert(SqlDB *db)
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::update(SqlDB *db)
{
int rc;
// Update the Template
rc = host_template.update(db);
if ( rc != 0 )
{
return rc;
}
// Update the HostShare
rc = host_share.update(db);
if ( rc != 0 )
{
return rc;
}
rc = insert_replace(db, true);
if ( rc != 0 )
{
return rc;
}
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
@ -184,24 +233,6 @@ int Host::update(SqlDB *db)
char * sql_tm_mad_name;
char * sql_vmm_mad_name;
// Update the Template
rc = host_template.update(db);
if ( rc != 0 )
{
return rc;
}
// Update the HostShare
rc = host_share.update(db);
if ( rc != 0 )
{
return rc;
}
// Update the Host
sql_hostname = db->escape_str(hostname.c_str());
@ -232,9 +263,18 @@ int Host::update(SqlDB *db)
goto error_vmm;
}
// Construct the SQL statement to Insert or Replace (effectively, update)
if(replace)
{
oss << "REPLACE";
}
else
{
oss << "INSERT";
}
oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES ("
// Construct the SQL statement to Insert or Replace
oss <<" INTO "<< table <<" "<< db_names <<" VALUES ("
<< oid << ","
<< "'" << sql_hostname << "',"
<< state << ","

View File

@ -192,32 +192,48 @@ int HostShare::select(SqlDB * db)
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::insert(SqlDB * db)
{
int rc;
rc = update(db);
rc = insert_replace(db, false);
if ( rc != 0 )
{
return rc;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::update(SqlDB * db)
{
int rc;
rc = insert_replace(db, true);
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES ("
if(replace)
{
oss << "REPLACE";
}
else
{
oss << "INSERT";
}
oss << " INTO " << table << " "<< db_names <<" VALUES ("
<< hsid << ","
<< disk_usage <<","<< mem_usage <<","<< cpu_usage<< ","
<< max_disk <<","<< max_mem <<","<< max_cpu << ","

View File

@ -66,14 +66,25 @@ int TemplateSQL::insert(SqlDB * db)
return -1;
}
rc = update(db);
rc = insert_replace(db, false);
return rc;
}
/* -------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------ */
int TemplateSQL::update(SqlDB * db)
{
int rc;
rc = insert_replace(db, true);
return rc;
}
/* ------------------------------------------------------------------------ */
int TemplateSQL::insert_replace(SqlDB *db, bool replace)
{
multimap<string,Attribute *>::iterator it;
ostringstream oss;
@ -82,7 +93,9 @@ int TemplateSQL::update(SqlDB * db)
char * sql_attr;
Attribute::AttributeType atype;
for(it=attributes.begin(),oss.str("");it!=attributes.end();it++,oss.str(""))
for(it=attributes.begin(),oss.str("");
it!=attributes.end();
it++,oss.str(""))
{
if ( it->second == 0 )
{
@ -106,7 +119,16 @@ int TemplateSQL::update(SqlDB * db)
continue;
}
oss << "INSERT OR REPLACE INTO " << table << " " << db_names
if(replace)
{
oss << "REPLACE";
}
else
{
oss << "INSERT";
}
oss << " INTO " << table << " " << db_names
<< " VALUES (" << id << ",'" << it->first << "',"<< atype <<",'"
<< sql_attr << "')";
@ -129,8 +151,8 @@ error_sql:
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int TemplateSQL::select_cb(void *nil, int num, char **values, char **names)
{