00001
00002
00003
00004
00005
00006
00007
00008 #include "ManagedObject.h"
00009
00015 ManagedObject::ManagedObject(string anEntityName)
00016 {
00017 initEntity(anEntityName);
00018 }
00019
00024 ManagedObject::ManagedObject(string anEntityName, Row & aRow)
00025 {
00026 initEntity(anEntityName);
00027
00028 for (set<string>::iterator it=_keys.begin(); it != _keys.end(); it++) {
00029 string key = *it;
00030 setValueForKey(key, (string) aRow[key.c_str()]);
00031 }
00032
00033 _fault = false;
00034 _updatedKeys.clear();
00035 }
00036
00037 ManagedObject::~ManagedObject()
00038 {
00039 }
00040
00044 void ManagedObject::initEntity(string anEntityName)
00045 {
00046 _entityName = anEntityName;
00047 lastInsertID = 0;
00048 _fault = false;
00049
00050 Database& db = Database::Instance();
00051
00052 try {
00053 Connection *conn = db.getConnection();
00054 Query q = conn->query("SELECT * FROM " + _entityName);
00055 StoreQueryResult res = q.store();
00056
00057 for (size_t i = 0; i < res.field_names()->size(); i++) {
00058 _keys.insert(res.field_name(i).c_str());
00059 }
00060 }
00061 catch (std::exception & e) {
00062
00063 }
00064 }
00065
00073 void ManagedObject::setValueForKey(string aKey, string aValue) throw (InvalidArgument)
00074 {
00075 set<string>::iterator it = _keys.find(aKey);
00076 if (it == _keys.end())
00077 throw InvalidArgument(aKey);
00078
00079 willChangeValueForKey(aKey);
00080 _fields[aKey] = aValue;
00081 _fault = true;
00082 _updatedKeys.insert(aKey);
00083 didChangevalueForKey(aKey);
00084 }
00085
00092 void ManagedObject::setIntForKey(string aKey, int aValue)
00093 {
00094 setFloatForKey(aKey, (float)aValue);
00095 }
00096
00103 void ManagedObject::setFloatForKey(string aKey, float aValue)
00104 {
00105 stringstream tmpSS;
00106 string tmpStr;
00107
00108 tmpSS << aValue;
00109 tmpSS >> tmpStr;
00110 setValueForKey(aKey, tmpStr);
00111 }
00112
00119 void ManagedObject::setBoolForKey(string aKey, bool aValue)
00120 {
00121 setValueForKey(aKey, (aValue?"1":"0"));
00122 }
00123
00131 string ManagedObject::valueForKey(string aKey) throw (InvalidArgument)
00132 {
00133 set<string>::iterator it = _keys.find(aKey);
00134 if (it == _keys.end())
00135 throw InvalidArgument(aKey);
00136
00137 return _fields[aKey];
00138 }
00139
00147 bool ManagedObject::boolForKey(string aKey) throw (InvalidArgument)
00148 {
00149 string aValue = valueForKey(aKey);
00150 istringstream aStream(aValue);
00151 int i;
00152
00153 if (!(aStream >> i))
00154 throw InvalidArgument(aKey);
00155
00156 return (i == 1);
00157 }
00158
00166 int ManagedObject::intForKey(string aKey) throw (InvalidArgument)
00167 {
00168 string value = valueForKey(aKey);
00169 istringstream aStream(value);
00170 int i;
00171
00172 if (!(aStream >> i))
00173 throw InvalidArgument(aKey);
00174
00175 return i;
00176 }
00177
00185 float ManagedObject::floatForKey(string aKey) throw (InvalidArgument)
00186 {
00187 string value = valueForKey(aKey);
00188 istringstream aStream(value);
00189 int f;
00190
00191 if (!(aStream >> f))
00192 throw InvalidArgument(aKey);
00193
00194 return f;
00195 }
00196
00207 bool ManagedObject::store()
00208 {
00209 SQLQueryParms qp;
00210 string cols = " (";
00211 stringstream values;
00212 int i=0;
00213
00214 values << "VALUES (";
00215 set<string>::reverse_iterator it;
00216 for (it = _keys.rbegin(); it != _keys.rend(); it++) {
00217 cols += *it + ",";
00218 qp += _fields[*it];
00219 values << "%" << i++ << "q,";
00220 }
00221 cols[cols.length()-1] = ')', cols += " ";
00222 string sql = "REPLACE " + _entityName + cols + values.str();
00223 sql[sql.length()-1] = ')';
00224
00225 Database& db = Database::Instance();
00226
00227 try {
00228 Connection *conn = db.getConnection();
00229 Query q = conn->query(sql);
00230 q.parse();
00231
00232 SimpleResult r = q.execute(qp);
00233 if (r == false) {
00234 cerr << "An error occurred during Person::store()\n" << endl
00235 << q.error() << endl << "Query was: " << q.str(qp) << endl;
00236 return false;
00237 }
00238
00239 lastInsertID = q.insert_id();
00240 }
00241 catch (const Exception& er) {
00242 cerr << "Error: " << er.what() << endl;
00243
00244 return false;
00245 }
00246
00247 _fault = false;
00248
00249 return true;
00250 }
00251
00262 bool ManagedObject::update()
00263 {
00264 if (!_fault)
00265 return true;
00266
00267 SQLQueryParms qp;
00268 set<string>::const_iterator ckit;
00269 vector<string> vValues;
00270 stringstream aValue;
00271 string sql;
00272 int i;
00273
00274 for (i=0, ckit = _updatedKeys.begin(); ckit != _updatedKeys.end(); ckit++, i++) {
00275 aValue.str("");
00276 aValue << *ckit << "=%" << i << "q";
00277 vValues.push_back(aValue.str());
00278 qp += _fields[*ckit];
00279 }
00280
00281 string pk = primaryKey();
00282 sql = "UPDATE " + _entityName + " SET " +
00283 valueMerge(vValues.begin(), vValues.end(), (string)",");
00284
00285 aValue.str("");
00286 aValue << pk << "=%" << i << "q";
00287 qp += _fields[pk];
00288 sql += " WHERE "+aValue.str();
00289
00290 Database& db = Database::Instance();
00291
00292 try {
00293 Connection *conn = db.getConnection();
00294 Query q = conn->query(sql);
00295 q.parse();
00296
00297 cout << "SQL-2: " << q.str(qp) << endl;
00298
00299 SimpleResult r = q.execute(qp);
00300 if (r == false) {
00301 cerr << "An error occurred during Person::store()\n" << endl
00302 << q.error() << endl << "Query was: " << q.str() << endl;
00303 return false;
00304 }
00305 }
00306 catch (const Exception& er) {
00307 cerr << "Error: " << er.what() << endl;
00308 return false;
00309 }
00310
00311 _fault = false;
00312
00313 return true;
00314 }
00315
00321 ulonglong ManagedObject::getLastInsertID() const
00322 {
00323 return lastInsertID;
00324 }