00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "DBBase.h"
00023 #include <qfile.h>
00024 #include <qfileinfo.h>
00025 #include <qtextstream.h>
00026
00027
00028 DBBase::DBBase ()
00029 {
00030 db = 0;
00031 }
00032
00033 DBBase::~DBBase ()
00034 {
00035 close();
00036 }
00037
00038 int DBBase::open (QString &d)
00039 {
00040 int rc = db_create(&db, NULL, 0);
00041 if (rc)
00042 {
00043 qDebug("DBBase::open: %s", db_strerror(rc));
00044 return TRUE;
00045 }
00046
00047 rc = db->open(db, NULL, (char *) d.latin1(), NULL, DB_BTREE, DB_CREATE, 0664);
00048 if (rc)
00049 {
00050 qDebug("DBBase::open: %s", db_strerror(rc));
00051 return TRUE;
00052 }
00053
00054 symbol = d;
00055
00056 QFileInfo fi(symbol);
00057 indexKey = fi.fileName();
00058
00059 return FALSE;
00060 }
00061
00062 void DBBase::close ()
00063 {
00064 if (db)
00065 {
00066 db->close(db, 0);
00067 db = 0;
00068 }
00069 }
00070
00071 void DBBase::getData (QString &k, QString &d)
00072 {
00073 DBT key, data;
00074 memset(&key, 0, sizeof(DBT));
00075 memset(&data, 0, sizeof(DBT));
00076
00077 key.data = (char *) k.latin1();
00078 key.size = k.length() + 1;
00079
00080 d.truncate(0);
00081
00082 int ret = db->get(db, NULL, &key, &data, 0);
00083 if (ret == 0)
00084 d = (char *) data.data;
00085 else
00086 {
00087
00088
00089 }
00090 }
00091
00092 void DBBase::setData (QString &k, QString &d)
00093 {
00094 DBT key, data;
00095 memset(&key, 0, sizeof(DBT));
00096 memset(&data, 0, sizeof(DBT));
00097
00098 key.data = (char *) k.latin1();
00099 key.size = k.length() + 1;
00100
00101 data.data = (char *) d.latin1();
00102 data.size = d.length() + 1;
00103
00104 int ret = db->put(db, NULL, &key, &data, 0);
00105 if (ret != 0)
00106 {
00107 char *err = db_strerror(ret);
00108 qDebug("DBBase::setData: %s", err);
00109 }
00110 }
00111
00112 void DBBase::deleteData (QString &k)
00113 {
00114 DBT key;
00115 memset(&key, 0, sizeof(DBT));
00116
00117 key.data = (char *) k.latin1();
00118 key.size = k.length() + 1;
00119
00120 int ret = db->del(db, NULL, &key, 0);
00121 if (ret != 0)
00122 {
00123
00124
00125 }
00126 }
00127
00128 void DBBase::dump (QString &d)
00129 {
00130 QFile outFile(d);
00131 if (! outFile.open(IO_WriteOnly))
00132 return;
00133 QTextStream outStream(&outFile);
00134
00135 DBT key, data;
00136 DBC *cur;
00137 memset(&key, 0, sizeof(DBT));
00138 memset(&data, 0, sizeof(DBT));
00139 db->cursor(db, NULL, &cur, 0);
00140
00141 while (! cur->c_get(cur, &key, &data, DB_NEXT))
00142 outStream << (char *) key.data << "=" << (char *) data.data << "\n";
00143
00144 cur->c_close(cur);
00145 outFile.close();
00146 }
00147
00148 void DBBase::getSymbol (QString &d)
00149 {
00150 d = symbol;
00151 }
00152
00153 void DBBase::flush ()
00154 {
00155 if (db)
00156 db->sync(db, 0);
00157 }
00158
00159 void DBBase::getIndexKey (QString &d)
00160 {
00161 d = indexKey;
00162 }
00163