PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/cyphesis-0.5.26/common/Database.cpp

#
C++ | 1566 lines | 1320 code | 211 blank | 35 comment | 260 complexity | 4250654209731a1f733962f17d98bc0d MD5 | raw file
Possible License(s): GPL-2.0
  1. // Cyphesis Online RPG Server and AI Engine
  2. // Copyright (C) 2000-2007 Alistair Riddoch
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software Foundation,
  16. // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. // $Id: 15a017976039e5ddfda0b520cbc0cd13419d4603 $
  18. #include "Database.h"
  19. #include "id.h"
  20. #include "log.h"
  21. #include "debug.h"
  22. #include "globals.h"
  23. #include "compose.hpp"
  24. #include "const.h"
  25. #include <Atlas/Message/MEncoder.h>
  26. #include <Atlas/Message/Element.h>
  27. #include <Atlas/Codecs/XML.h>
  28. #include <varconf/config.h>
  29. #include <sstream>
  30. #include <cstring>
  31. #include <cstdlib>
  32. #include <cassert>
  33. using Atlas::Message::Element;
  34. using Atlas::Message::MapType;
  35. using Atlas::Objects::Root;
  36. using String::compose;
  37. typedef Atlas::Codecs::XML Serialiser;
  38. static const bool debug_flag = false;
  39. Database * Database::m_instance = NULL;
  40. static void databaseNotice(void * arg, const char * message)
  41. {
  42. log(NOTICE, "Notice from database:");
  43. log_formatted(NOTICE, message);
  44. }
  45. Database::Database() : m_rule_db("rules"),
  46. m_queryInProgress(false),
  47. m_connection(NULL)
  48. {
  49. }
  50. bool Database::tuplesOk()
  51. {
  52. assert(m_connection != 0);
  53. bool status = false;
  54. PGresult * res;
  55. while ((res = PQgetResult(m_connection)) != NULL) {
  56. if (PQresultStatus(res) == PGRES_TUPLES_OK) {
  57. status = true;
  58. }
  59. PQclear(res);
  60. };
  61. return status;
  62. }
  63. bool Database::commandOk()
  64. {
  65. assert(m_connection != 0);
  66. bool status = false;
  67. PGresult * res;
  68. while ((res = PQgetResult(m_connection)) != NULL) {
  69. if (PQresultStatus(res) == PGRES_COMMAND_OK) {
  70. status = true;
  71. } else {
  72. reportError();
  73. }
  74. PQclear(res);
  75. };
  76. return status;
  77. }
  78. int Database::createInstanceDatabase()
  79. {
  80. assert(::instance != CYPHESIS);
  81. std::string error_message;
  82. if (connect(CYPHESIS, error_message) != 0) {
  83. log(ERROR, compose("Connection to master database failed: \n%1",
  84. error_message));
  85. return -1;
  86. }
  87. std::string dbname;
  88. if (::instance == CYPHESIS) {
  89. dbname = CYPHESIS;
  90. } else {
  91. dbname = compose("%1_%2", CYPHESIS, ::instance);
  92. }
  93. readConfigItem(::instance, "dbname", dbname);
  94. if (!runCommandQuery(compose("CREATE DATABASE %1", dbname))) {
  95. shutdownConnection();
  96. return -1;
  97. }
  98. shutdownConnection();
  99. return 0;
  100. }
  101. int Database::connect(const std::string & context, std::string & error_msg)
  102. {
  103. std::stringstream conninfos;
  104. std::string db_server;
  105. if (readConfigItem(context, "dbserver", db_server) == 0) {
  106. if (db_server.empty()) {
  107. log(WARNING, "Empty database hostname specified in config file. "
  108. "Using none.");
  109. } else {
  110. conninfos << "host=" << db_server << " ";
  111. }
  112. }
  113. std::string dbname;
  114. if (context == CYPHESIS) {
  115. dbname = CYPHESIS;
  116. } else {
  117. dbname = compose("%1_%2", CYPHESIS, ::instance);
  118. }
  119. readConfigItem(context, "dbname", dbname);
  120. conninfos << "dbname=" << dbname << " ";
  121. std::string db_user;
  122. if (readConfigItem(context, "dbuser", db_user) == 0) {
  123. if (db_user.empty()) {
  124. log(WARNING, "Empty username specified in config file. "
  125. "Using current user.");
  126. } else {
  127. conninfos << "user=" << db_user << " ";
  128. }
  129. }
  130. std::string db_passwd;
  131. if (readConfigItem(context, "dbpasswd", db_passwd) == 0) {
  132. conninfos << "password=" << db_passwd << " ";
  133. }
  134. const std::string cinfo = conninfos.str();
  135. m_connection = PQconnectdb(cinfo.c_str());
  136. if (m_connection == NULL) {
  137. error_msg = "Unknown error";
  138. return -1;
  139. }
  140. if (PQstatus(m_connection) != CONNECTION_OK) {
  141. error_msg = PQerrorMessage(m_connection);
  142. PQfinish(m_connection);
  143. m_connection = 0;
  144. return -1;
  145. }
  146. return 0;
  147. }
  148. int Database::initConnection()
  149. {
  150. std::string error_message;
  151. if (connect(::instance, error_message) != 0) {
  152. log(ERROR, "Connection to database failed:");
  153. log_formatted(ERROR, error_message);
  154. return -1;
  155. }
  156. PQsetNoticeProcessor(m_connection, databaseNotice, 0);
  157. return 0;
  158. }
  159. bool Database::initRule(bool createTables)
  160. {
  161. assert(m_connection != 0);
  162. int status = 0;
  163. clearPendingQuery();
  164. status = PQsendQuery(m_connection, "SELECT * FROM rules WHERE "
  165. "id = 'test' AND contents = 'test'");
  166. if (!status) {
  167. reportError();
  168. return false;
  169. }
  170. if (!tuplesOk()) {
  171. debug(std::cout << "Rule table does not exist"
  172. << std::endl << std::flush;);
  173. if (createTables) {
  174. std::string query = compose("CREATE TABLE rules ( "
  175. "id varchar(%1) PRIMARY KEY, "
  176. "ruleset varchar(%1), "
  177. "contents text ) "
  178. "WITHOUT OIDS", consts::id_len);
  179. status = PQsendQuery(m_connection, query.c_str());
  180. if (!status) {
  181. reportError();
  182. return false;
  183. }
  184. if (!commandOk()) {
  185. log(ERROR, "Error creating rules table in database");
  186. reportError();
  187. return false;
  188. }
  189. allTables.insert("rules");
  190. } else {
  191. log(ERROR, "Server table does not exist in database");
  192. return false;
  193. }
  194. }
  195. allTables.insert("rules");
  196. return true;
  197. }
  198. void Database::shutdownConnection()
  199. {
  200. if (m_connection != 0) {
  201. PQfinish(m_connection);
  202. m_connection = 0;
  203. }
  204. }
  205. Database * Database::instance()
  206. {
  207. if (m_instance == NULL) {
  208. m_instance = new Database();
  209. }
  210. return m_instance;
  211. }
  212. void Database::cleanup()
  213. {
  214. if (m_instance != 0) {
  215. delete m_instance;
  216. }
  217. m_instance = 0;
  218. }
  219. bool Database::decodeObject(const std::string & data,
  220. Root &o)
  221. {
  222. if (data.empty()) {
  223. return true;
  224. }
  225. std::stringstream str(data, std::ios::in);
  226. Serialiser codec(str, m_od);
  227. Atlas::Message::Encoder enc(codec);
  228. // Clear the decoder
  229. m_d.get();
  230. codec.poll();
  231. if (!m_od.check()) {
  232. log(WARNING, "Database entry does not appear to be decodable");
  233. return false;
  234. }
  235. o = m_od.get();
  236. return true;
  237. }
  238. bool Database::decodeMessage(const std::string & data,
  239. MapType &o)
  240. {
  241. if (data.empty()) {
  242. return true;
  243. }
  244. std::stringstream str(data, std::ios::in);
  245. Serialiser codec(str, m_d);
  246. Atlas::Message::Encoder enc(codec);
  247. // Clear the decoder
  248. m_d.get();
  249. codec.poll();
  250. if (!m_d.check()) {
  251. log(WARNING, "Database entry does not appear to be decodable");
  252. return false;
  253. }
  254. o = m_d.get();
  255. return true;
  256. }
  257. bool Database::encodeObject(const MapType & o,
  258. std::string & data)
  259. {
  260. std::stringstream str;
  261. Serialiser codec(str, m_d);
  262. Atlas::Message::Encoder enc(codec);
  263. codec.streamBegin();
  264. enc.streamMessageElement(o);
  265. codec.streamEnd();
  266. std::string raw = str.str();
  267. char safe[raw.size() * 2 + 1];
  268. int errcode;
  269. PQescapeStringConn(m_connection, safe, raw.c_str(), raw.size(), &errcode);
  270. if (errcode != 0) {
  271. std::cerr << "ERROR: " << errcode << std::endl << std::flush;
  272. }
  273. data = safe;
  274. return true;
  275. }
  276. bool Database::getObject(const std::string & table, const std::string & key,
  277. MapType & o)
  278. {
  279. assert(m_connection != 0);
  280. debug(std::cout << "Database::getObject() " << table << "." << key
  281. << std::endl << std::flush;);
  282. std::string query = std::string("SELECT * FROM ") + table + " WHERE id = '" + key + "'";
  283. clearPendingQuery();
  284. int status = PQsendQuery(m_connection, query.c_str());
  285. if (!status) {
  286. reportError();
  287. return false;
  288. }
  289. PGresult * res;
  290. if ((res = PQgetResult(m_connection)) == NULL) {
  291. debug(std::cout << "Error accessing " << key << " in " << table
  292. << " table" << std::endl << std::flush;);
  293. return false;
  294. }
  295. if (PQntuples(res) < 1 || PQnfields(res) < 2) {
  296. debug(std::cout << "No entry for " << key << " in " << table
  297. << " table" << std::endl << std::flush;);
  298. PQclear(res);
  299. while ((res = PQgetResult(m_connection)) != NULL) {
  300. PQclear(res);
  301. }
  302. return false;
  303. }
  304. const char * data = PQgetvalue(res, 0, 1);
  305. debug(std::cout << "Got record " << key << " from database, value " << data
  306. << std::endl << std::flush;);
  307. bool ret = decodeMessage(data, o);
  308. PQclear(res);
  309. while ((res = PQgetResult(m_connection)) != NULL) {
  310. PQclear(res);
  311. log(ERROR, "Extra database result to simple query.");
  312. };
  313. return ret;
  314. }
  315. bool Database::putObject(const std::string & table,
  316. const std::string & key,
  317. const MapType & o,
  318. const StringVector & c)
  319. {
  320. debug(std::cout << "Database::putObject() " << table << "." << key
  321. << std::endl << std::flush;);
  322. std::stringstream str;
  323. Serialiser codec(str, m_d);
  324. Atlas::Message::Encoder enc(codec);
  325. codec.streamBegin();
  326. enc.streamMessageElement(o);
  327. codec.streamEnd();
  328. debug(std::cout << "Encoded to: " << str.str() << " "
  329. << str.str().size() << std::endl << std::flush;);
  330. std::string query = std::string("INSERT INTO ") + table + " VALUES ('" + key;
  331. StringVector::const_iterator Iend = c.end();
  332. for (StringVector::const_iterator I = c.begin(); I != Iend; ++I) {
  333. query += "', '";
  334. query += *I;
  335. }
  336. query += "', '";
  337. query += str.str();
  338. query += "')";
  339. return scheduleCommand(query);
  340. }
  341. bool Database::updateObject(const std::string & table,
  342. const std::string & key,
  343. const MapType & o)
  344. {
  345. debug(std::cout << "Database::updateObject() " << table << "." << key
  346. << std::endl << std::flush;);
  347. std::stringstream str;
  348. Serialiser codec(str, m_d);
  349. Atlas::Message::Encoder enc(codec);
  350. codec.streamBegin();
  351. enc.streamMessageElement(o);
  352. codec.streamEnd();
  353. std::string query = std::string("UPDATE ") + table + " SET contents = '" +
  354. str.str() + "' WHERE id='" + key + "'";
  355. return scheduleCommand(query);
  356. }
  357. bool Database::delObject(const std::string & table, const std::string & key)
  358. {
  359. #if 0
  360. Dbt key, data;
  361. key.set_data((void*)keystr);
  362. key.set_size(strlen(keystr) + 1);
  363. int err;
  364. if ((err = db.del(NULL, &key, 0)) != 0) {
  365. debug(cout << "db.del.ERROR! " << err << endl << flush;);
  366. return false;
  367. }
  368. return true;
  369. #endif
  370. return true;
  371. }
  372. bool Database::hasKey(const std::string & table, const std::string & key)
  373. {
  374. assert(m_connection != 0);
  375. std::string query = std::string("SELECT id FROM ") + table +
  376. " WHERE id='" + key + "'";
  377. clearPendingQuery();
  378. int status = PQsendQuery(m_connection, query.c_str());
  379. if (!status) {
  380. reportError();
  381. return false;
  382. }
  383. PGresult * res;
  384. bool ret = false;
  385. if ((res = PQgetResult(m_connection)) == NULL) {
  386. debug(std::cout << "Error accessing " << table
  387. << " table" << std::endl << std::flush;);
  388. return false;
  389. }
  390. int results = PQntuples(res);
  391. if (results > 0) {
  392. ret = true;
  393. }
  394. PQclear(res);
  395. while ((res = PQgetResult(m_connection)) != NULL) {
  396. PQclear(res);
  397. }
  398. return ret;
  399. }
  400. bool Database::getTable(const std::string & table,
  401. std::map<std::string, Root> & contents)
  402. {
  403. if (m_connection == 0) {
  404. log(CRITICAL, "Database connection is down. This is okay during tests");
  405. return false;
  406. }
  407. std::string query = std::string("SELECT * FROM ") + table;
  408. clearPendingQuery();
  409. int status = PQsendQuery(m_connection, query.c_str());
  410. if (!status) {
  411. reportError();
  412. return false;
  413. }
  414. PGresult * res;
  415. if ((res = PQgetResult(m_connection)) == NULL) {
  416. debug(std::cout << "Error accessing " << table
  417. << " table" << std::endl << std::flush;);
  418. return false;
  419. }
  420. int results = PQntuples(res);
  421. if (results < 1 || PQnfields(res) < 2) {
  422. debug(std::cout << "No entries in " << table
  423. << " table" << std::endl << std::flush;);
  424. PQclear(res);
  425. while ((res = PQgetResult(m_connection)) != NULL) {
  426. PQclear(res);
  427. }
  428. return false;
  429. }
  430. int id_column = PQfnumber(res, "id"),
  431. contents_column = PQfnumber(res, "contents");
  432. if (id_column == -1 || contents_column == -1) {
  433. log(ERROR, "Could not find 'id' and 'contents' columns in database result");
  434. return false;
  435. }
  436. Root t;
  437. for(int i = 0; i < results; ++i) {
  438. const char * key = PQgetvalue(res, i, id_column);
  439. const char * data = PQgetvalue(res, i, contents_column);
  440. debug(std::cout << "Got record " << key << " from database, value "
  441. << data << std::endl << std::flush;);
  442. if (decodeObject(data, t)) {
  443. contents[key] = t;
  444. }
  445. }
  446. PQclear(res);
  447. while ((res = PQgetResult(m_connection)) != NULL) {
  448. PQclear(res);
  449. log(ERROR, "Extra database result to simple query.");
  450. };
  451. return true;
  452. }
  453. bool Database::clearTable(const std::string & table)
  454. {
  455. std::string query = std::string("DELETE FROM ") + table;
  456. return scheduleCommand(query);
  457. }
  458. void Database::reportError()
  459. {
  460. assert(m_connection != 0);
  461. char * message = PQerrorMessage(m_connection);
  462. assert(message != NULL);
  463. if (strlen(message) < 2) {
  464. log(WARNING, "Zero length database error message");
  465. }
  466. std::string msg = std::string("DATABASE: ") + message;
  467. msg = msg.substr(0, msg.size() - 1);
  468. log(ERROR, msg);
  469. }
  470. const DatabaseResult Database::runSimpleSelectQuery(const std::string & query)
  471. {
  472. assert(m_connection != 0);
  473. debug(std::cout << "QUERY: " << query << std::endl << std::flush;);
  474. clearPendingQuery();
  475. int status = PQsendQuery(m_connection, query.c_str());
  476. if (!status) {
  477. log(ERROR, "runSimpleSelectQuery(): Database query error.");
  478. reportError();
  479. return DatabaseResult(0);
  480. }
  481. debug(std::cout << "done" << std::endl << std::flush;);
  482. PGresult * res;
  483. if ((res = PQgetResult(m_connection)) == NULL) {
  484. log(ERROR, "Error selecting.");
  485. reportError();
  486. debug(std::cout << "Row query didn't work"
  487. << std::endl << std::flush;);
  488. return DatabaseResult(0);
  489. }
  490. if (PQresultStatus(res) != PGRES_TUPLES_OK) {
  491. log(ERROR, "Error selecting row.");
  492. debug(std::cout << "QUERY: " << query << std::endl << std::flush;);
  493. reportError();
  494. PQclear(res);
  495. res = 0;
  496. }
  497. PGresult * nres;
  498. while ((nres = PQgetResult(m_connection)) != NULL) {
  499. PQclear(nres);
  500. log(ERROR, "Extra database result to simple query.");
  501. };
  502. return DatabaseResult(res);
  503. }
  504. bool Database::runCommandQuery(const std::string & query)
  505. {
  506. assert(m_connection != 0);
  507. clearPendingQuery();
  508. int status = PQsendQuery(m_connection, query.c_str());
  509. if (!status) {
  510. log(ERROR, "runCommandQuery(): Database query error.");
  511. reportError();
  512. return false;
  513. }
  514. if (!commandOk()) {
  515. log(ERROR, "Error running command query row.");
  516. log(NOTICE, query);
  517. reportError();
  518. debug(std::cout << "Row query didn't work"
  519. << std::endl << std::flush;);
  520. } else {
  521. debug(std::cout << "Query worked" << std::endl << std::flush;);
  522. return true;
  523. }
  524. return false;
  525. }
  526. bool Database::registerRelation(std::string & tablename,
  527. const std::string & sourcetable,
  528. const std::string & targettable,
  529. RelationType kind)
  530. {
  531. assert(m_connection != 0);
  532. tablename = sourcetable + "_" + targettable;
  533. std::string query = compose("SELECT * FROM %1 WHERE source = 0 "
  534. "AND target = 0", tablename);
  535. debug(std::cout << "QUERY: " << query << std::endl << std::flush;);
  536. clearPendingQuery();
  537. int status = PQsendQuery(m_connection, query.c_str());
  538. if (!status) {
  539. log(ERROR, "registerRelation(): Database query error.");
  540. reportError();
  541. return false;
  542. }
  543. if (!tuplesOk()) {
  544. debug(reportError(););
  545. debug(std::cout << "Table does not yet exist"
  546. << std::endl << std::flush;);
  547. } else {
  548. debug(std::cout << "Table exists" << std::endl << std::flush;);
  549. allTables.insert(tablename);
  550. return true;
  551. }
  552. query = "CREATE TABLE ";
  553. query += tablename;
  554. if (kind == OneToOne || kind == ManyToOne) {
  555. query += " (source integer UNIQUE REFERENCES ";
  556. } else {
  557. query += " (source integer REFERENCES ";
  558. }
  559. query += sourcetable;
  560. if (kind == OneToOne || kind == OneToMany) {
  561. query += " (id), target integer UNIQUE REFERENCES ";
  562. } else {
  563. query += " (id), target integer REFERENCES ";
  564. }
  565. query += targettable;
  566. query += " (id) ON DELETE CASCADE ) WITHOUT OIDS";
  567. debug(std::cout << "CREATE QUERY: " << query
  568. << std::endl << std::flush;);
  569. if (!runCommandQuery(query)) {
  570. return false;
  571. }
  572. allTables.insert(tablename);
  573. #if 0
  574. if (kind == ManyToOne || kind == OneToOne) {
  575. return true;
  576. } else {
  577. std::string indexQuery = "CREATE INDEX ";
  578. indexQuery += tablename;
  579. indexQuery += "_source_idx ON ";
  580. indexQuery += tablename;
  581. indexQuery += " (source)";
  582. return runCommandQuery(indexQuery);
  583. }
  584. #else
  585. return true;
  586. #endif
  587. }
  588. const DatabaseResult Database::selectRelation(const std::string & name,
  589. const std::string & id)
  590. {
  591. std::string query = "SELECT target FROM ";
  592. query += name;
  593. query += " WHERE source = ";
  594. query += id;
  595. debug(std::cout << "Selecting on id = " << id << " ... " << std::flush;);
  596. return runSimpleSelectQuery(query);
  597. }
  598. bool Database::createRelationRow(const std::string & name,
  599. const std::string & id,
  600. const std::string & other)
  601. {
  602. std::string query = "INSERT INTO ";
  603. query += name;
  604. query += " (source, target) VALUES (";
  605. query += id;
  606. query += ", ";
  607. query += other;
  608. query += ")";
  609. return scheduleCommand(query);
  610. }
  611. bool Database::removeRelationRow(const std::string & name,
  612. const std::string & id)
  613. {
  614. std::string query = "DELETE FROM ";
  615. query += name;
  616. query += " WHERE source = ";
  617. query += id;
  618. return scheduleCommand(query);
  619. }
  620. bool Database::removeRelationRowByOther(const std::string & name,
  621. const std::string & other)
  622. {
  623. std::string query = "DELETE FROM ";
  624. query += name;
  625. query += " WHERE target = ";
  626. query += other;
  627. // return runCommandQuery(query);
  628. return scheduleCommand(query);
  629. }
  630. bool Database::registerSimpleTable(const std::string & name,
  631. const MapType & row)
  632. {
  633. assert(m_connection != 0);
  634. if (row.empty()) {
  635. log(ERROR, "Attempt to create empty database table");
  636. }
  637. // Check whether the table exists
  638. std::string query = "SELECT * FROM ";
  639. std::string createquery = "CREATE TABLE ";
  640. query += name;
  641. createquery += name;
  642. query += " WHERE id = 0";
  643. createquery += " (id integer UNIQUE PRIMARY KEY";
  644. MapType::const_iterator Iend = row.end();
  645. for (MapType::const_iterator I = row.begin(); I != Iend; ++I) {
  646. query += " AND ";
  647. createquery += ", ";
  648. const std::string & column = I->first;
  649. query += column;
  650. createquery += column;
  651. const Element & type = I->second;
  652. if (type.isString()) {
  653. query += " LIKE 'foo'";
  654. int size = type.String().size();
  655. if (size == 0) {
  656. createquery += " text";
  657. } else {
  658. char buf[32];
  659. snprintf(buf, 32, "%d", size);
  660. createquery += " varchar(";
  661. createquery += buf;
  662. createquery += ")";
  663. }
  664. } else if (type.isInt()) {
  665. query += " = 1";
  666. createquery += " integer";
  667. } else if (type.isFloat()) {
  668. query += " = 1.0";
  669. createquery += " float";
  670. } else {
  671. log(ERROR, "Illegal column type in database simple row");
  672. }
  673. }
  674. debug(std::cout << "QUERY: " << query << std::endl << std::flush;);
  675. clearPendingQuery();
  676. int status = PQsendQuery(m_connection, query.c_str());
  677. if (!status) {
  678. log(ERROR, "registerSimpleTable(): Database query error.");
  679. reportError();
  680. return false;
  681. }
  682. if (!tuplesOk()) {
  683. debug(reportError(););
  684. debug(std::cout << "Table does not yet exist"
  685. << std::endl << std::flush;);
  686. } else {
  687. debug(std::cout << "Table exists" << std::endl << std::flush;);
  688. allTables.insert(name);
  689. return true;
  690. }
  691. createquery += ") WITHOUT OIDS";
  692. debug(std::cout << "CREATE QUERY: " << createquery
  693. << std::endl << std::flush;);
  694. bool ret = runCommandQuery(createquery);
  695. if (ret) {
  696. allTables.insert(name);
  697. }
  698. return ret;
  699. }
  700. const DatabaseResult Database::selectSimpleRow(const std::string & id,
  701. const std::string & name)
  702. {
  703. std::string query = "SELECT * FROM ";
  704. query += name;
  705. query += " WHERE id = ";
  706. query += id;
  707. debug(std::cout << "Selecting on id = " << id << " ... " << std::flush;);
  708. return runSimpleSelectQuery(query);
  709. }
  710. const DatabaseResult Database::selectSimpleRowBy(const std::string & name,
  711. const std::string & column,
  712. const std::string & value)
  713. {
  714. std::string query = "SELECT * FROM ";
  715. query += name;
  716. query += " WHERE ";
  717. query += column;
  718. query += " = ";
  719. query += value;
  720. debug(std::cout << "Selecting on " << column << " = " << value
  721. << " ... " << std::flush;);
  722. return runSimpleSelectQuery(query);
  723. }
  724. bool Database::createSimpleRow(const std::string & name,
  725. const std::string & id,
  726. const std::string & columns,
  727. const std::string & values)
  728. {
  729. std::string query = "INSERT INTO ";
  730. query += name;
  731. query += " ( id, ";
  732. query += columns;
  733. query += " ) VALUES ( ";
  734. query += id;
  735. query += ", ";
  736. query += values;
  737. query += ")";
  738. // return runCommandQuery(query);
  739. return scheduleCommand(query);
  740. }
  741. bool Database::updateSimpleRow(const std::string & name,
  742. const std::string & key,
  743. const std::string & value,
  744. const std::string & columns)
  745. {
  746. std::string query = "UPDATE ";
  747. query += name;
  748. query += " SET ";
  749. query += columns;
  750. query += " WHERE ";
  751. query += key;
  752. query += "='";
  753. query += value;
  754. query += "'";
  755. // return runCommandQuery(query);
  756. return scheduleCommand(query);
  757. }
  758. int Database::registerEntityIdGenerator()
  759. {
  760. assert(m_connection != 0);
  761. clearPendingQuery();
  762. int status = PQsendQuery(m_connection, "SELECT * FROM entity_ent_id_seq");
  763. if (!status) {
  764. log(ERROR, "registerEntityIdGenerator(): Database query error.");
  765. reportError();
  766. return -1;
  767. }
  768. if (!tuplesOk()) {
  769. debug(reportError(););
  770. debug(std::cout << "Sequence does not yet exist"
  771. << std::endl << std::flush;);
  772. } else {
  773. debug(std::cout << "Sequence exists" << std::endl << std::flush;);
  774. return 0;
  775. }
  776. return runCommandQuery("CREATE SEQUENCE entity_ent_id_seq") ? 0 : -1;
  777. }
  778. long Database::newId(std::string & id)
  779. {
  780. assert(m_connection != 0);
  781. clearPendingQuery();
  782. int status = PQsendQuery(m_connection,
  783. "SELECT nextval('entity_ent_id_seq')");
  784. if (!status) {
  785. log(ERROR, "newId(): Database query error.");
  786. reportError();
  787. return -1;
  788. }
  789. PGresult * res;
  790. if ((res = PQgetResult(m_connection)) == NULL) {
  791. log(ERROR, "Error getting new ID.");
  792. reportError();
  793. return -1;
  794. }
  795. const char * cid = PQgetvalue(res, 0, 0);
  796. id = cid;
  797. PQclear(res);
  798. while ((res = PQgetResult(m_connection)) != NULL) {
  799. PQclear(res);
  800. log(ERROR, "Extra database result to simple query.");
  801. };
  802. if (id.empty()) {
  803. log(ERROR, "Unknown error getting ID from database.");
  804. return -1;
  805. }
  806. return forceIntegerId(id);
  807. }
  808. int Database::registerEntityTable(const std::map<std::string, int> & chunks)
  809. {
  810. assert(m_connection != 0);
  811. clearPendingQuery();
  812. int status = PQsendQuery(m_connection, "SELECT * FROM entities");
  813. if (!status) {
  814. log(ERROR, "registerEntityIdGenerator(): Database query error.");
  815. reportError();
  816. return -1;
  817. }
  818. if (!tuplesOk()) {
  819. debug(reportError(););
  820. debug(std::cout << "Table does not yet exist"
  821. << std::endl << std::flush;);
  822. } else {
  823. allTables.insert("entities");
  824. // FIXME Flush out the whole state of the databases, to ensure they
  825. // don't clog up while we are testing.
  826. // runCommandQuery("DELETE FROM properties");
  827. // runCommandQuery(compose("DELETE FROM entities WHERE id!=%1",
  828. // consts::rootWorldIntId));
  829. debug(std::cout << "Table exists" << std::endl << std::flush;);
  830. return 0;
  831. }
  832. std::string query = compose("CREATE TABLE entities ("
  833. "id integer UNIQUE PRIMARY KEY, "
  834. "loc integer, "
  835. "type varchar(%1), "
  836. "seq integer", consts::id_len);
  837. std::map<std::string, int>::const_iterator I = chunks.begin();
  838. std::map<std::string, int>::const_iterator Iend = chunks.end();
  839. for (; I != Iend; ++I) {
  840. query += compose(", %1 varchar(1024)", I->first);
  841. }
  842. query += ")";
  843. if (!runCommandQuery(query)) {
  844. return -1;
  845. }
  846. allTables.insert("entities");
  847. query = compose("INSERT INTO entities VALUES (%1, null, 'world')",
  848. consts::rootWorldIntId);
  849. if (!runCommandQuery(query)) {
  850. return -1;
  851. }
  852. return 0;
  853. }
  854. int Database::insertEntity(const std::string & id,
  855. const std::string & loc,
  856. const std::string & type,
  857. int seq,
  858. const std::string & value)
  859. {
  860. std::string query = compose("INSERT INTO entities VALUES "
  861. "(%1, %2, '%3', %4, '%5')",
  862. id, loc, type, seq, value);
  863. return scheduleCommand(query);
  864. }
  865. int Database::updateEntity(const std::string & id,
  866. int seq,
  867. const std::string & value)
  868. {
  869. std::string query = compose("UPDATE entities SET seq = %1, location = '%2'"
  870. " WHERE id = %3", seq, value, id);
  871. return scheduleCommand(query);
  872. }
  873. const DatabaseResult Database::selectEntities(const std::string & loc)
  874. {
  875. std::string query = compose("SELECT id, type, seq, location FROM entities"
  876. " WHERE loc = %1", loc);
  877. debug(std::cout << "Selecting on loc = " << loc << " ... " << std::flush;);
  878. return runSimpleSelectQuery(query);
  879. }
  880. int Database::dropEntity(long id)
  881. {
  882. std::string query = compose("DELETE FROM properties WHERE id = '%1'", id);
  883. scheduleCommand(query);
  884. query = compose("DELETE FROM entities WHERE id = %1", id);
  885. scheduleCommand(query);
  886. return 0;
  887. }
  888. int Database::registerPropertyTable()
  889. {
  890. assert(m_connection != 0);
  891. clearPendingQuery();
  892. int status = PQsendQuery(m_connection, "SELECT * FROM properties");
  893. if (!status) {
  894. log(ERROR, "registerPropertyIdGenerator(): Database query error.");
  895. reportError();
  896. return -1;
  897. }
  898. if (!tuplesOk()) {
  899. debug(reportError(););
  900. debug(std::cout << "Table does not yet exist"
  901. << std::endl << std::flush;);
  902. } else {
  903. allTables.insert("properties");
  904. debug(std::cout << "Table exists" << std::endl << std::flush;);
  905. return 0;
  906. }
  907. allTables.insert("properties");
  908. std::string query = compose("CREATE TABLE properties ("
  909. "id integer REFERENCES entities "
  910. "ON DELETE CASCADE, "
  911. "name varchar(%1), "
  912. "value text)", consts::id_len);
  913. if (!runCommandQuery(query)) {
  914. reportError();
  915. return -1;
  916. }
  917. query = "CREATE INDEX property_names on properties (name)";
  918. if (!runCommandQuery(query)) {
  919. reportError();
  920. return -1;
  921. }
  922. return 0;
  923. }
  924. int Database::insertProperties(const std::string & id,
  925. const KeyValues & tuples)
  926. {
  927. int first = 1;
  928. std::string query("INSERT INTO properties VALUES ");
  929. KeyValues::const_iterator I = tuples.begin();
  930. KeyValues::const_iterator Iend = tuples.end();
  931. for (; I != Iend; ++I) {
  932. if (first) {
  933. query += compose("(%1, '%2', '%3')", id, I->first, I->second);
  934. first = 0;
  935. } else {
  936. query += compose(", (%1, '%2', '%3')", id, I->first, I->second);
  937. }
  938. }
  939. return scheduleCommand(query);
  940. }
  941. const DatabaseResult Database::selectProperties(const std::string & id)
  942. {
  943. std::string query = compose("SELECT name, value FROM properties"
  944. " WHERE id = %1", id);
  945. debug(std::cout << "Selecting on id = " << id << " ... "
  946. << std::endl << std::flush;);
  947. return runSimpleSelectQuery(query);
  948. }
  949. int Database::updateProperties(const std::string & id,
  950. const KeyValues & tuples)
  951. {
  952. KeyValues::const_iterator I = tuples.begin();
  953. KeyValues::const_iterator Iend = tuples.end();
  954. for (; I != Iend; ++I) {
  955. std::string query = compose("UPDATE properties SET value = '%3' WHERE"
  956. " id=%1 AND name='%2'",
  957. id, I->first, I->second);
  958. scheduleCommand(query);
  959. }
  960. return 0;
  961. }
  962. #if 0
  963. // Interface for tables for sparse sequences or arrays of data. Terrain
  964. // control points and other spatial data.
  965. static const char * array_axes[] = { "i", "j", "k", "l", "m" };
  966. bool Database::registerArrayTable(const std::string & name,
  967. unsigned int dimension,
  968. const MapType & row)
  969. {
  970. if (m_connection == 0) {
  971. log(CRITICAL, "Database connection is down. This is okay during tests");
  972. return false;
  973. }
  974. assert(dimension <= 5);
  975. if (row.empty()) {
  976. log(ERROR, "Attempt to create empty array table");
  977. }
  978. std::string query("SELECT * from ");
  979. std::string createquery("CREATE TABLE ");
  980. std::string indexquery("CREATE UNIQUE INDEX ");
  981. query += name;
  982. query += " WHERE id = 0";
  983. createquery += name;
  984. createquery += " (id integer REFERENCES entities NOT NULL";
  985. indexquery += name;
  986. indexquery += "_point_idx on ";
  987. indexquery += name;
  988. indexquery += " (id";
  989. for (unsigned int i = 0; i < dimension; ++i) {
  990. query += " AND ";
  991. query += array_axes[i];
  992. query += " = 0";
  993. createquery += ", ";
  994. createquery += array_axes[i];
  995. createquery += " integer NOT NULL";
  996. indexquery += ", ";
  997. indexquery += array_axes[i];
  998. }
  999. MapType::const_iterator Iend = row.end();
  1000. for (MapType::const_iterator I = row.begin(); I != Iend; ++I) {
  1001. const std::string & column = I->first;
  1002. query += " AND ";
  1003. query += column;
  1004. createquery += ", ";
  1005. createquery += column;
  1006. const Element & type = I->second;
  1007. if (type.isString()) {
  1008. query += " LIKE 'foo'";
  1009. int size = type.String().size();
  1010. if (size == 0) {
  1011. createquery += " text";
  1012. } else {
  1013. char buf[32];
  1014. snprintf(buf, 32, "%d", size);
  1015. createquery += " varchar(";
  1016. createquery += buf;
  1017. createquery += ")";
  1018. }
  1019. } else if (type.isInt()) {
  1020. query += " = 1";
  1021. createquery += " integer";
  1022. } else if (type.isFloat()) {
  1023. query += " = 1.0";
  1024. createquery += " float";
  1025. } else {
  1026. log(ERROR, "Illegal column type in database array row");
  1027. }
  1028. }
  1029. debug(std::cout << "QUERY: " << query << std::endl << std::flush;);
  1030. clearPendingQuery();
  1031. int status = PQsendQuery(m_connection, query.c_str());
  1032. if (!status) {
  1033. log(ERROR, "registerArrayTable(): Database query error.");
  1034. reportError();
  1035. return false;
  1036. }
  1037. if (!tuplesOk()) {
  1038. debug(reportError(););
  1039. debug(std::cout << "Table does not yet exist"
  1040. << std::endl << std::flush;);
  1041. } else {
  1042. debug(std::cout << "Table exists" << std::endl << std::flush;);
  1043. allTables.insert(name);
  1044. return true;
  1045. }
  1046. createquery += ") WITHOUT OIDS";
  1047. debug(std::cout << "CREATE QUERY: " << createquery
  1048. << std::endl << std::flush;);
  1049. bool ret = runCommandQuery(createquery);
  1050. if (!ret) {
  1051. return false;
  1052. }
  1053. indexquery += ")";
  1054. debug(std::cout << "INDEX QUERY: " << indexquery
  1055. << std::endl << std::flush;);
  1056. ret = runCommandQuery(indexquery);
  1057. if (!ret) {
  1058. return false;
  1059. }
  1060. allTables.insert(name);
  1061. return true;
  1062. }
  1063. const DatabaseResult Database::selectArrayRows(const std::string & name,
  1064. const std::string & id)
  1065. {
  1066. std::string query("SELECT * FROM ");
  1067. query += name;
  1068. query += " WHERE id = ";
  1069. query += id;
  1070. debug(std::cout << "ARRAY QUERY: " << query << std::endl << std::flush;);
  1071. return runSimpleSelectQuery(query);
  1072. }
  1073. bool Database::createArrayRow(const std::string & name,
  1074. const std::string & id,
  1075. const std::vector<int> & key,
  1076. const MapType & data)
  1077. {
  1078. assert(key.size() > 0);
  1079. assert(key.size() <= 5);
  1080. assert(!data.empty());
  1081. std::stringstream query;
  1082. query << "INSERT INTO " << name << " ( id";
  1083. for (unsigned int i = 0; i < key.size(); ++i) {
  1084. query << ", " << array_axes[i];
  1085. }
  1086. MapType::const_iterator Iend = data.end();
  1087. for (MapType::const_iterator I = data.begin(); I != Iend; ++I) {
  1088. query << ", " << I->first;
  1089. }
  1090. query << " ) VALUES ( " << id;
  1091. std::vector<int>::const_iterator Jend = key.end();
  1092. for (std::vector<int>::const_iterator J = key.begin(); J != Jend; ++J) {
  1093. query << ", " << *J;
  1094. }
  1095. // We assume data has not been modified, so Iend is still valid
  1096. for (MapType::const_iterator I = data.begin(); I != Iend; ++I) {
  1097. const Element & e = I->second;
  1098. switch (e.getType()) {
  1099. case Element::TYPE_INT:
  1100. query << ", " << e.Int();
  1101. break;
  1102. case Element::TYPE_FLOAT:
  1103. query << ", " << e.Float();
  1104. break;
  1105. case Element::TYPE_STRING:
  1106. query << ", " << e.String();
  1107. break;
  1108. default:
  1109. log(ERROR, "Bad type constructing array database row for insert");
  1110. break;
  1111. }
  1112. }
  1113. query << ")";
  1114. std::string qstr = query.str();
  1115. debug(std::cout << "QUery: " << qstr << std::endl << std::flush;);
  1116. return scheduleCommand(qstr);
  1117. }
  1118. bool Database::updateArrayRow(const std::string & name,
  1119. const std::string & id,
  1120. const std::vector<int> & key,
  1121. const Atlas::Message::MapType & data)
  1122. {
  1123. assert(key.size() > 0);
  1124. assert(key.size() <= 5);
  1125. assert(!data.empty());
  1126. std::stringstream query;
  1127. query << "UPDATE " << name << " SET ";
  1128. MapType::const_iterator Iend = data.end();
  1129. for (MapType::const_iterator I = data.begin(); I != Iend; ++I) {
  1130. if (I != data.begin()) {
  1131. query << ", ";
  1132. }
  1133. query << I->first << " = ";
  1134. const Element & e = I->second;
  1135. switch (e.getType()) {
  1136. case Element::TYPE_INT:
  1137. query << e.Int();
  1138. break;
  1139. case Element::TYPE_FLOAT:
  1140. query << e.Float();
  1141. break;
  1142. case Element::TYPE_STRING:
  1143. query << "'" << e.String() << "'";
  1144. break;
  1145. default:
  1146. log(ERROR, "Bad type constructing array database row for update");
  1147. break;
  1148. }
  1149. }
  1150. query << " WHERE id='" << id << "'";
  1151. for (unsigned int i = 0; i < key.size(); ++i) {
  1152. query << " AND " << array_axes[i] << " = " << key[i];
  1153. }
  1154. std::string qstr = query.str();
  1155. debug(std::cout << "QUery: " << qstr << std::endl << std::flush;);
  1156. return scheduleCommand(qstr);
  1157. }
  1158. bool Database::removeArrayRow(const std::string & name,
  1159. const std::string & id,
  1160. const std::vector<int> & key)
  1161. {
  1162. /// Not sure we need this one yet, so lets no bother for now ;)
  1163. return false;
  1164. }
  1165. #endif // 0
  1166. // General functions for handling queries at the low level.
  1167. void Database::queryResult(ExecStatusType status)
  1168. {
  1169. if (!m_queryInProgress || pendingQueries.empty()) {
  1170. log(ERROR, "Got database result when no query was pending.");
  1171. return;
  1172. }
  1173. DatabaseQuery & q = pendingQueries.front();
  1174. if (q.second == PGRES_EMPTY_QUERY) {
  1175. log(ERROR, "Got database result which is already done.");
  1176. return;
  1177. }
  1178. if (q.second == status) {
  1179. debug(std::cout << "Query status ok" << std::endl << std::flush;);
  1180. // Mark this query as done
  1181. q.second = PGRES_EMPTY_QUERY;
  1182. } else {
  1183. log(ERROR, "Database error from async query");
  1184. std::cerr << "Query error in : " << q.first << std::endl << std::flush;
  1185. reportError();
  1186. q.second = PGRES_EMPTY_QUERY;
  1187. }
  1188. }
  1189. void Database::queryComplete()
  1190. {
  1191. if (!m_queryInProgress || pendingQueries.empty()) {
  1192. log(ERROR, "Got database query complete when no query was pending");
  1193. return;
  1194. }
  1195. DatabaseQuery & q = pendingQueries.front();
  1196. if (q.second != PGRES_EMPTY_QUERY) {
  1197. abort();
  1198. log(ERROR, "Got database query complete when query was not done");
  1199. return;
  1200. }
  1201. debug(std::cout << "Query complete" << std::endl << std::flush;);
  1202. pendingQueries.pop_front();
  1203. m_queryInProgress = false;
  1204. }
  1205. bool Database::launchNewQuery()
  1206. {
  1207. if (m_connection == 0) {
  1208. log(ERROR, "Can't launch new query while database is offline.");
  1209. return false;
  1210. }
  1211. if (m_queryInProgress) {
  1212. log(ERROR, "Launching new query when query is in progress");
  1213. return false;
  1214. }
  1215. if (pendingQueries.empty()) {
  1216. debug(std::cout << "No queries to launch" << std::endl << std::flush;);
  1217. return false;
  1218. }
  1219. debug(std::cout << pendingQueries.size() << " queries pending"
  1220. << std::endl << std::flush;);
  1221. DatabaseQuery & q = pendingQueries.front();
  1222. debug(std::cout << "Launching async query: " << q.first
  1223. << std::endl << std::flush;);
  1224. int status = PQsendQuery(m_connection, q.first.c_str());
  1225. if (!status) {
  1226. log(ERROR, "Database query error when launching.");
  1227. reportError();
  1228. return false;
  1229. } else {
  1230. m_queryInProgress = true;
  1231. PQflush(m_connection);
  1232. return true;
  1233. }
  1234. }
  1235. bool Database::scheduleCommand(const std::string & query)
  1236. {
  1237. pendingQueries.push_back(std::make_pair(query, PGRES_COMMAND_OK));
  1238. if (!m_queryInProgress) {
  1239. debug(std::cout << "Query: " << query << " launched"
  1240. << std::endl << std::flush;);
  1241. return launchNewQuery();
  1242. } else {
  1243. debug(std::cout << "Query: " << query << " scheduled"
  1244. << std::endl << std::flush;);
  1245. return true;
  1246. }
  1247. }
  1248. bool Database::clearPendingQuery()
  1249. {
  1250. if (!m_queryInProgress) {
  1251. return true;
  1252. }
  1253. assert(!pendingQueries.empty());
  1254. debug(std::cout << "Clearing a pending query" << std::endl << std::flush;);
  1255. DatabaseQuery & q = pendingQueries.front();
  1256. if (q.second == PGRES_COMMAND_OK) {
  1257. m_queryInProgress = false;
  1258. pendingQueries.pop_front();
  1259. return commandOk();
  1260. } else {
  1261. log(ERROR, "Pending query wants unknown status");
  1262. return false;
  1263. }
  1264. }
  1265. bool Database::runMaintainance(int command)
  1266. {
  1267. // VACUUM and REINDEX tables from a common store
  1268. if ((command & MAINTAIN_REINDEX) == MAINTAIN_REINDEX) {
  1269. std::string query("REINDEX TABLE ");
  1270. TableSet::const_iterator Iend = allTables.end();
  1271. for (TableSet::const_iterator I = allTables.begin(); I != Iend; ++I) {
  1272. scheduleCommand(query + *I);
  1273. }
  1274. }
  1275. if ((command & MAINTAIN_VACUUM) == MAINTAIN_VACUUM) {
  1276. std::string query("VACUUM ");
  1277. if ((command & MAINTAIN_VACUUM_ANALYZE) == MAINTAIN_VACUUM_ANALYZE) {
  1278. query += "ANALYZE ";
  1279. }
  1280. if ((command & MAINTAIN_VACUUM_FULL) == MAINTAIN_VACUUM_FULL) {
  1281. query += "FULL ";
  1282. }
  1283. TableSet::const_iterator Iend = allTables.end();
  1284. for(TableSet::const_iterator I = allTables.begin(); I != Iend; ++I) {
  1285. scheduleCommand(query + *I);
  1286. }
  1287. }
  1288. return true;
  1289. }
  1290. const char * DatabaseResult::field(const char * column, int row) const
  1291. {
  1292. int col_num = PQfnumber(m_res, column);
  1293. if (col_num == -1) {
  1294. return "";
  1295. }
  1296. return PQgetvalue(m_res, row, col_num);
  1297. }
  1298. const char * DatabaseResult::const_iterator::column(const char * column) const
  1299. {
  1300. int col_num = PQfnumber(m_dr.m_res, column);
  1301. if (col_num == -1) {
  1302. return "";
  1303. }
  1304. return PQgetvalue(m_dr.m_res, m_row, col_num);
  1305. }
  1306. void DatabaseResult::const_iterator::readColumn(const char * column,
  1307. int & val) const
  1308. {
  1309. int col_num = PQfnumber(m_dr.m_res, column);
  1310. if (col_num == -1) {
  1311. return;
  1312. }
  1313. const char * v = PQgetvalue(m_dr.m_res, m_row, col_num);
  1314. val = strtol(v, 0, 10);
  1315. }
  1316. void DatabaseResult::const_iterator::readColumn(const char * column,
  1317. float & val) const
  1318. {
  1319. int col_num = PQfnumber(m_dr.m_res, column);
  1320. if (col_num == -1) {
  1321. return;
  1322. }
  1323. const char * v = PQgetvalue(m_dr.m_res, m_row, col_num);
  1324. val = strtof(v, 0);
  1325. }
  1326. void DatabaseResult::const_iterator::readColumn(const char * column,
  1327. double & val) const
  1328. {
  1329. int col_num = PQfnumber(m_dr.m_res, column);
  1330. if (col_num == -1) {
  1331. return;
  1332. }
  1333. const char * v = PQgetvalue(m_dr.m_res, m_row, col_num);
  1334. val = strtod(v, 0);
  1335. }
  1336. void DatabaseResult::const_iterator::readColumn(const char * column,
  1337. std::string & val) const
  1338. {
  1339. int col_num = PQfnumber(m_dr.m_res, column);
  1340. if (col_num == -1) {
  1341. return;
  1342. }
  1343. const char * v = PQgetvalue(m_dr.m_res, m_row, col_num);
  1344. val = v;
  1345. }
  1346. void DatabaseResult::const_iterator::readColumn(const char * column,
  1347. MapType & val) const
  1348. {
  1349. int col_num = PQfnumber(m_dr.m_res, column);
  1350. if (col_num == -1) {
  1351. return;
  1352. }
  1353. const char * v = PQgetvalue(m_dr.m_res, m_row, col_num);
  1354. Database::instance()->decodeMessage(v, val);
  1355. }