Value.cxx

Go to the documentation of this file.
00001 // -*- mode: C++ -*- 
00002 //
00003 // Copyright (C) 2006 Christian Holm Christensen <cholm@nbi.dk>
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public License
00007 // as published by the Free Software Foundation; either version 2.1
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free
00017 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018 // 02111-1307 USA
00019 //
00025 #include "Value.h"
00026 #include "SingleValue.h"
00027 #include "BlobValue.h"
00028 #include "Sequence.h"
00029 #include <rcudb/Server.h>
00030 #include <rcudb/Result.h>
00031 #include <rcudb/Sql.h>
00032 #include <rcudb/Row.h>
00033 #include <iostream>
00034 
00035 //_____________________________________________________________________
00036 RcuConf::Value::Value(RcuDb::Row& row) 
00037   : Table(row)
00038 {
00039   row.Field(1, fConfigId);
00040   row.Field(2, fParamId);
00041   row.Field(3, fAddressId);
00042   row.Field(4, fVersion);
00043 }
00044 
00045 //_____________________________________________________________________
00046 void
00047 RcuConf::Value::Print() const
00048 {
00049   std::cout << "Value: id="     << fId 
00050             << "\tconfig="      << fConfigId
00051             << "\tparam="       << fParamId 
00052             << "\taddress="     << fAddressId 
00053             << "\tversion="     << fVersion << std::flush;
00054 }
00055 
00056 //_____________________________________________________________________
00057 bool
00058 RcuConf::Value::Select(List& r, RcuDb::Server& server, 
00059                        const std::string& table, const RcuDb::Sql& cond)
00060 {
00061   RcuDb::Result* res = 0;
00062   bool ret = Select(res, server, table, cond);
00063   if (!ret) return ret;
00064   
00065   // Make the result table if any 
00066   if (!res) return true;
00067 
00068   // Check if this is a blob table
00069   bool blob = (table == BlobValue::fgName);
00070 
00071   RcuDb::Row* row = 0;
00072   while ((row = res->Next())) {
00073     Value*    v = 0;
00074     if (blob) v = new BlobValue(*row);
00075     else      v = new SingleValue(*row);
00076     r.push_back(v);
00077   }
00078   
00079 
00080   // delete result, and return OK.
00081   delete res;
00082   return true;  
00083 }
00084 
00085 //_____________________________________________________________________
00086 bool
00087 RcuConf::Value::Select(List& r, RcuDb::Server& s, 
00088                        const std::string& table, int config, int param, 
00089                        int addr)
00090 {
00091   RcuDb::Result* res = 0;
00092   bool ret = Select(res, s, table, config, param, addr);
00093   if (!ret) return ret;
00094   
00095   // Make the result table if any 
00096   if (!res) return true;
00097 
00098   // Check if this is a blob table
00099   bool blob = (table == BlobValue::fgName);
00100 
00101   RcuDb::Row* row = 0;
00102   while ((row = res->Next())) {
00103     Value* v = 0;
00104     if (blob) v = new BlobValue(*row);
00105     else      v = new SingleValue(*row);
00106     r.push_back(v);
00107   }
00108   
00109   // delete result, and return OK.
00110   delete res;
00111   return true;  
00112 }
00113 
00114   
00115 //_____________________________________________________________________
00116 bool
00117 RcuConf::Value::Select(RcuDb::Result*& r, RcuDb::Server& server, 
00118                        const std::string& table, const RcuDb::Sql& cond)
00119 {
00120   // Set result to point to nothing. 
00121   r = 0;
00122 
00123   // Make query 
00124   RcuDb::Sql     sql;
00125   sql << "SELECT * FROM " << table
00126       << (cond.Text().empty() ? "" : " WHERE ") 
00127       << (cond.Text().empty() ? "" : cond);
00128 
00129   // Execute query, and check error status
00130   r = server.Query(sql);
00131   if (server.IsError()) { 
00132     if (r) {
00133       delete r;
00134       r = 0;
00135     }
00136     return false;
00137   }
00138   return true;
00139 }
00140 
00141 //_____________________________________________________________________
00142 bool
00143 RcuConf::Value::Select(RcuDb::Result*& r, RcuDb::Server& s, 
00144                        const std::string& table, int config, int param, 
00145                        int addr)
00146 {
00147   RcuDb::Sql cond;
00148   cond << "configid=" << config << " AND "
00149        << "paramid="  << param  << " AND " 
00150        << "addressid";
00151   if      (addr <  0) cond << "<=0";
00152   else if (addr == 0) cond << ">0";
00153   else if (addr >  0) cond << "=" << addr;
00154   cond << " ORDER BY " << (addr <= 0 ? "" : "addressid ASC, ") 
00155        << "version DESC";
00156   // std::cout << "Select condition: " << cond.Text() << std::endl;
00157   return Select(r, s, table, cond);
00158 }
00159 
00160 
00161 //_____________________________________________________________________
00162 RcuDb::Sql&
00163 RcuConf::Value::ValueInsert(RcuDb::Sql& sql) 
00164 {
00165   // Get previous versions. 
00166   sql << fId << "," << fConfigId << "," << fParamId << "," 
00167       << fAddressId << "," << fVersion << ",";
00168   return sql;
00169 }
00170 
00171 
00172 //_____________________________________________________________________
00173 RcuDb::Sql&
00174 RcuConf::Value::ValueCreate(RcuDb::Sql& sql) 
00175 {
00176   sql << " id         INT KEY, "
00177       << " configid   INT NOT NULL, "
00178       << " paramid    INT NOT NULL, "
00179       << " addressid  INT NOT NULL, "
00180       << " version    INT NOT NULL, ";
00181   return sql;
00182 }
00183 
00184 
00185 //_____________________________________________________________________
00186 //
00187 // EOF
00188 //
00189 
00190 
00191   
00192 
00193   
Top of page Last update Fri Apr 27 01:54:16 2007
Copyright © 2004 Christian Holm Created by DoxyGen 1.3.5