00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "db_property.hh"
00039
00040
00041
00042
00043
00044
00045
00046 void DBProperty::init(const string& select_query, const string& update_query, const string& type) {
00047 select_data_query = select_query;
00048 update_data_query = update_query;
00049 force_type = type;
00050 }
00051
00052
00053 void DBProperty::init(const string& select_all_q, const string& select_data_q, const string& select_type_q, const string& update_all_q, const string& update_data_q, const string& update_type_q) {
00054 select_all_query = select_all_q;
00055 select_data_query = select_data_q;
00056 select_type_query = select_type_q;
00057 update_all_query = update_all_q;
00058 update_data_query = update_data_q;
00059 update_type_query = update_type_q;
00060 }
00061
00062
00063
00064 void DBProperty::init_components(const string& table, const string& name_col, const string& val_col, const string& type_col, const string& name_val) {
00065 select_all_query = "SELECT "+val_col+", "+type_col+" FROM "+table+" WHERE "+name_col+" = "+name_val;
00066 select_data_query = "SELECT "+val_col+" FROM "+table+" WHERE "+name_col+" = "+name_val;
00067 select_type_query = "SELECT "+type_col+" FROM "+table+" WHERE "+name_col+" = "+name_val;
00068 update_all_query = "UPDATE "+table+" SET "+val_col+" = \"%d\", "+type_col+" = \"%t\" WHERE "+name_col+" = "+name_val;
00069 update_data_query = "UPDATE "+table+" SET "+val_col+" = \"%d\" WHERE "+name_col+" = "+name_val;
00070 }
00071
00072
00073 void DBProperty::updateFromDB() throw(DBAccessError) {
00074 LOG("DBProperty", 9, "updateFromDB(): querying...");
00075 string data, type;
00076 data = queryDB(select_data_query);
00077 if (force_type == "") {
00078
00079 type = queryDB(select_type_query);
00080 if( (data != readRaw()) || (type != getDataType()) )
00081 LocalProperty::replace(queryDB(select_data_query), queryDB(select_type_query));
00082 } else {
00083 if(data != readRaw())
00084 LocalProperty::replace( queryDB(select_data_query), force_type);
00085 }
00086 }
00087
00088
00089
00090
00091 void DBProperty::updateToDB() throw(DBAccessError) {
00092
00093 LOG("DBProperty", 9, "updateToDB(): querying...");
00094 if(force_type == "") {
00095 queryDB( sreplace( sreplace(update_all_query, "%d", data), "%t", datatype) );
00096 } else {
00097 queryDB( sreplace( update_data_query, "%d", data) );
00098 }
00099 }
00100
00101
00102
00103
00104
00105
00106 string DBProperty::sreplace( const string& s, const string& key, const string& value) throw (range_error) {
00107 string tmp(s);
00108 unsigned int k;
00109 k = s.find(key);
00110 if(k == s.npos) throw range_error("substring not found.");
00111 return tmp.replace( k, key.length(), value);
00112 }