NightGetDB.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***************************************************************************
  2. * Copyright (C) USIC 2008 by Sergiy Kibrik, Olha Yevtushenko *
  3. * sakib@meta.ua,caelum@meta.ua *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <cassert>
  21. #include <db_cxx.h>
  22. #include <string>
  23. #include <list>
  24. #include <map>
  25. #include "NightGetDB.h"
  26. struct NightGetDB::DBImpl {
  27. DBImpl(): _db(NULL,0){}
  28. Db _db;
  29. };
  30. NightGetDB::NightGetDB():
  31. _db( new DBImpl ){}
  32. NightGetDB::~NightGetDB(){
  33. do_disconnect();
  34. if ( _db ) delete _db;
  35. }
  36. int NightGetDB::do_connect(const string& database, const string& host, const string& user, const string& password){
  37. u_int32_t open_flags = DB_CREATE;
  38. //TODO fix damned quickstart params !!!
  39. _db->_db.open(
  40. NULL,
  41. database.c_str(),
  42. NULL,
  43. DB_BTREE,
  44. open_flags,
  45. 0
  46. );
  47. return EXIT_SUCCESS;
  48. }
  49. void NightGetDB::do_disconnect(){
  50. _db->_db.close(0);
  51. }
  52. map<string,string> NightGetDB::do_getRecord(const string& id, const string& table) const {
  53. Dbc *_cursorPtr;
  54. _db->_db.cursor(NULL, &_cursorPtr, 0);
  55. Dbt _key,_data;
  56. _data.set_data(NULL);
  57. _data.set_ulen(0);
  58. map<string,string> _map;
  59. int ret = 0;
  60. string _buf;
  61. if (table.empty()){
  62. _key.set_data( id.c_str() );
  63. _key.set_ulen( id.size() + 1);
  64. const Dbt _ckey(_key);
  65. while ( ( ret = _cursorPtr->get(&_key, &_data, DB_NEXT) ) == 0 ){
  66. _buf.assign( _key.get_data() + id.size() + 1, _key.get_size() - id.size() - 1);
  67. _map[_buf] = string( static_casr<char*>( _data.get_data() ), _data.get_size() );
  68. _key = _ckey;
  69. _buf.clear();
  70. }
  71. _cursorPtr->close();
  72. } else {
  73. _buf.assign( id + '\0' + table );
  74. _key.set_data( _buf.c_str() );
  75. _key.set_ulen( id.size() + table.size() + 1);
  76. ret = _db->_db.get(NULL,&_key,&_data,0);
  77. _map[table] = string( static_casr<char*>( _data.get_data() ), _data.get_size() );
  78. }
  79. return _map;
  80. }
  81. void NightGetDB::do_updateRecord(const string& id, const string& table, const string& value) {
  82. Dbt _key, _data;
  83. string _buf(id + '\0' + table);
  84. _key.set_data( _buf.c_str() );
  85. _key.set_ulen( id.size() + table.size() + 1);
  86. _data.set_ulen( value.size() + 1 );
  87. _data,set_data( value.c_str() );
  88. int ret = _db->_db.put(NULL, &_key, &_data,0);
  89. }
  90. void NightGetDB::do_createRecord(const strinf& id, const map<string,string>& record){
  91. Dbt _key, _data;
  92. map<string,string>::iterator iter = record.begin();
  93. string _buf;
  94. while (iter++ != record.end() ){
  95. _buf.assign( id + '\0' + iter->first);
  96. _key.set_data( _buf.c_str() );
  97. _key.set_ulen( _buf.size() + 1);
  98. _data.set_data( iter->second.c_str() );
  99. _data.set_ulen( iter->second.size() + 1);
  100. _db->_db.put(NULL, &_key, &_data, 0);
  101. _buf.clear();
  102. }
  103. }
  104. list<string> NightGetDB::do_getRecords(const string& table ) const {
  105. string::size_type loc = table.find('=');
  106. const string _field(table, 0, loc);
  107. const string _value(table, loc+1, table.size() - _field.size() - 1);
  108. Dbc *_cursorPtr;
  109. _db->_db.cursor(NULL, &_cursorPtr, 0);
  110. Dbt _key(NUll, 0), _data(NULL,0);
  111. int ret = 0, _field_lenght = 0;
  112. string _id, _buf;
  113. list<string> _result_list();
  114. while ( ( ret = _cursorPtr->get(&_key, &_data, DB_NEXT) ) == 0 ){
  115. if ( _value.compare( static_cast<char*>_data.get_data() ) != 0 ) continue;
  116. _id.assign( _key.get_data() );
  117. _buf.assign( _key.get_data() + _id.size() + 1);
  118. if ( _buf.compare(_field) == 0 ){
  119. _result_list.push_back(_id);
  120. }
  121. _data.set_data(NULL);
  122. _data.set_ulen(0);
  123. _key.set_data(NULL);
  124. _key.set_ulen(0);
  125. _buf.clear(); _id.clear();
  126. }
  127. _cursorPtr->close();
  128. return _result_list;
  129. }
  130. map<string,string> NightGetDB::do_getColumnsInfo(const string& table) const{
  131. // to be written :(
  132. return map<string,string>();
  133. }
  134. unsigned int NightGetDB::do_getNumberColumns() const{
  135. // to be written :(
  136. return 0;
  137. }
  138. unsigned int NightGetDB::do_getNumberRows(const string& table="") const{
  139. // to be written :(
  140. return 0;
  141. }