lib/DBIndex.cpp

Go to the documentation of this file.
00001 /*
00002  *  Qtstalker stock charter
00003  *
00004  *  Copyright (C) 2001-2007  Stefan S. Stratigakos
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019  *  USA.
00020  */
00021 
00022 #include "DBIndex.h"
00023 #include "Config.h"
00024 #include <qstringlist.h>
00025 #include <qfileinfo.h>
00026 
00027 
00028 DBIndex::DBIndex ()
00029 {
00030 }
00031 
00032 DBIndex::~DBIndex ()
00033 {
00034   close();
00035 }
00036 
00037 void DBIndex::setIndexItem (QString &k, DBIndexItem &d)
00038 {
00039   DBT key, data;
00040   memset(&key, 0, sizeof(DBT));
00041   memset(&data, 0, sizeof(DBT));
00042 
00043   DBIndexItemItem item;
00044   memset(&item, 0, sizeof(DBIndexItemItem));
00045   d.getItem(item);
00046   data.data = &item;
00047   data.size = sizeof(DBIndexItemItem); 
00048   
00049   key.data = (char *) k.latin1();
00050   key.size = k.length() + 1;
00051 
00052   db->put(db, NULL, &key, &data, 0);
00053 
00054   flush();
00055 }
00056 
00057 void DBIndex::getIndexItem (QString &k, DBIndexItem &d)
00058 {
00059   DBT key, data;
00060   memset(&key, 0, sizeof(DBT));
00061   memset(&data, 0, sizeof(DBT));
00062 
00063   key.data = (char *) k.latin1();
00064   key.size = k.length() + 1;
00065 
00066   DBIndexItemItem item;
00067   memset(&item, 0, sizeof(DBIndexItemItem));
00068   data.data = &item;
00069   data.ulen = sizeof(DBIndexItemItem); 
00070   data.flags = DB_DBT_USERMEM;  
00071 
00072   db->get(db, NULL, &key, &data, 0);
00073 
00074   d.setItem(item);
00075 }
00076 
00077 void DBIndex::deleteIndicator (QString &k, QString &d)
00078 {
00079   // d = the path to the indicator
00080 
00081   QString s;
00082   Config config;
00083   config.getData(Config::LocalIndicatorsPath, s);
00084   DBBase li;
00085   if (li.open(s))
00086   {
00087     qDebug("DBIndex::deleteIndicator: could not open LI db");
00088     return;
00089   }
00090 
00091   li.getData(k, s);
00092   QStringList l = QStringList::split(",", s, FALSE);
00093   l.remove(d);
00094   if (l.count())
00095   {
00096     s = l.join(",");
00097     li.setData(k, s);
00098   }
00099   else
00100   {
00101     s = "";
00102     li.setData(k, s);
00103   }
00104 
00105   li.close();
00106 }
00107 
00108 void DBIndex::addIndicator (QString &k, QString &d)
00109 {
00110   // d = the path to the indicator
00111 
00112   QString s;
00113   Config config;
00114   config.getData(Config::LocalIndicatorsPath, s);
00115   DBBase li;
00116   if (li.open(s))
00117   {
00118     qDebug("DBIndex::addIndicator: could not open LI db");
00119     return;
00120   }
00121 
00122   li.getData(k, s);
00123   QStringList l = QStringList::split(",", s, FALSE);
00124   int i = l.findIndex(d);
00125   if (i != -1)
00126     return;
00127   
00128   l.append(d);
00129   s = l.join(",");
00130   li.setData(k, s);
00131   li.close();
00132 }
00133 
00134 void DBIndex::getIndicators (QString &k, QString &d)
00135 {
00136   QString s;
00137   Config config;
00138   config.getData(Config::LocalIndicatorsPath, s);
00139   DBBase li;
00140   if (li.open(s))
00141   {
00142     qDebug("DBIndex::addIndicator: could not open LI db");
00143     return;
00144   }
00145 
00146   li.getData(k, d);
00147   li.close();
00148 }
00149 
00150 void DBIndex::deleteChart (QString &d)
00151 {
00152   // delete the index record
00153   QFileInfo fi(d);
00154   QString key = fi.fileName();
00155   deleteData(key);
00156 
00157   // delete the local indicator record
00158   Config config;
00159   QString s;
00160   config.getData(Config::LocalIndicatorsPath, s);
00161   DBBase tdb;
00162   tdb.open(s);
00163   tdb.deleteData(key);
00164   tdb.close();
00165 
00166   // delete the chart objects
00167   deleteAllChartObjects(key);
00168 
00169   // delete the fundamentals record
00170   config.getData(Config::FundamentalsPath, s);
00171   tdb.open(s);
00172   tdb.deleteData(key);
00173   tdb.close();
00174 }
00175 
00176 // ****************************************************************************
00177 // ************************* CHART OBJECTS ************************************
00178 // ****************************************************************************
00179 
00180 void DBIndex::getChartObjects (QString &k, QStringList &d)
00181 {
00182   d.clear();
00183 
00184   QString s;
00185   Config config;
00186   config.getData(Config::COPath, s);
00187   DBBase codb;
00188   if (codb.open(s))
00189   {
00190     qDebug("DBIndex::deleteChartObject: could not open co.db");
00191     return;
00192   }
00193 
00194   s = k + "_LIST";
00195   QString s2;
00196   codb.getData(s, s2);
00197   QStringList l = QStringList::split(",", s2, FALSE);
00198   int loop;
00199   for (loop = 0; loop < (int) l.count(); loop++)
00200   {
00201     // key is symbol + number
00202     s = k + l[loop];
00203     codb.getData(s, s2);
00204     d.append(s2);
00205   }
00206 
00207   codb.close();
00208 }
00209 
00210 void DBIndex::setChartObject (QString &k, QString &d, Setting &set)
00211 {
00212   QString s;
00213   Config config;
00214   config.getData(Config::COPath, s);
00215   DBBase codb;
00216   if (codb.open(s))
00217   {
00218     qDebug("DBIndex::setChartObject: could not open co.db");
00219     return;
00220   }
00221 
00222   s = k + "_LIST";
00223   QString s2;
00224   codb.getData(s, s2);
00225   QStringList l = QStringList::split(",", s2, FALSE);
00226   if (l.findIndex(d) == -1)
00227   {
00228     // enter new # list
00229     l.append(d);
00230     s2 = l.join(",");
00231     codb.setData(s, s2);
00232   }
00233 
00234   s = k + d;
00235   set.getString(s2);
00236   codb.setData(s, s2);
00237   codb.close();
00238 }
00239 
00240 void DBIndex::deleteChartObject (QString &k, QString &d)
00241 {
00242   QString s;
00243   Config config;
00244   config.getData(Config::COPath, s);
00245   DBBase codb;
00246   if (codb.open(s))
00247   {
00248     qDebug("DBIndex::deleteChartObject: could not open co.db");
00249     return;
00250   }
00251 
00252   s = k + "_LIST";
00253   QString s2;
00254   codb.getData(s, s2);
00255   QStringList l = QStringList::split(",", s2, FALSE);
00256   l.remove(d);
00257   if (! l.count())
00258     s2 = "";
00259   else
00260     s2 = l.join(",");
00261   codb.setData(s, s2);
00262 
00263   s = k + d;
00264   codb.deleteData(s);
00265   codb.close();
00266 }
00267 
00268 void DBIndex::deleteAllChartObjects (QString &k)
00269 {
00270   QString s;
00271   Config config;
00272   config.getData(Config::COPath, s);
00273   DBBase codb;
00274   if (codb.open(s))
00275   {
00276     qDebug("DBIndex::deleteAllChartObjects: could not open co.db");
00277     return;
00278   }
00279 
00280   // get the key list
00281   s = k + "_LIST";
00282   QString s2;
00283   codb.getData(s, s2);
00284   QStringList l = QStringList::split(",", s2, FALSE);
00285 
00286   // loop through the list and delete each co
00287   int loop;
00288   for (loop = 0; loop < (int) l.count(); loop++)
00289   {
00290     s = k + l[loop];
00291     codb.deleteData(s);
00292   }
00293 
00294   // remove keys from the list
00295   s = k + "_LIST";
00296   s2 = "";
00297   codb.setData(s, s2);
00298 
00299   codb.close();
00300 }
00301 
00302 void DBIndex::getNewChartObjectName (QString &k, QString &name)
00303 {
00304   QString s;
00305   Config config;
00306   config.getData(Config::COPath, s);
00307   DBBase codb;
00308   if (codb.open(s))
00309   {
00310     qDebug("DBIndex::getNewChartObjectName: could not open co.db");
00311     return;
00312   }
00313 
00314   // get the key list for the symbol
00315   s = k + "_LIST";
00316   QString s2;
00317   codb.getData(s, s2);
00318   QStringList l = QStringList::split(",", s2, FALSE);
00319 
00320   int loop = 0;
00321   while (1)
00322   {
00323     name = QString::number(loop);
00324     if (l.findIndex(name) != -1)
00325       loop++;
00326     else
00327       break;
00328   }
00329 
00330   codb.close();
00331 }
00332 
00333 // ****************************************************************************
00334 // ************************* FUNDAMENTALS *************************************
00335 // ****************************************************************************
00336 
00337 void DBIndex::getFundamentals (QString &k, QString &d)
00338 {
00339   QString s;
00340   Config config;
00341   config.getData(Config::FundamentalsPath, s);
00342   DBBase fdb;
00343   if (fdb.open(s))
00344   {
00345     qDebug("DBIndex::getFundamentals: could not open fund.db");
00346     return;
00347   }
00348 
00349   fdb.getData(k, d);
00350   fdb.close();
00351 }
00352 
00353 void DBIndex::setFundamentals (QString &k, QString &d)
00354 {
00355   QString s;
00356   Config config;
00357   config.getData(Config::FundamentalsPath, s);
00358   DBBase fdb;
00359   if (fdb.open(s))
00360   {
00361     qDebug("DBIndex::setFundamentals: could not open fund.db");
00362     return;
00363   }
00364 
00365   fdb.setData(k, d);
00366   fdb.close();
00367 }
00368