Parameter.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 "Parameter.h"
00026 #include "Value.h"
00027 #include "Sequence.h"
00028 #include <iostream>
00029 #include <rcudb/Server.h>
00030 #include <rcudb/Result.h>
00031 #include <rcudb/Sql.h>
00032 #include <rcudb/Row.h>
00033 
00034 const std::string RcuConf::Parameter::fgName("Parameter");
00035 
00036 namespace  RcuConf 
00037 {
00038   std::ostream& operator<<(std::ostream& o, const Parameter::Where w) 
00039   {
00040     switch (w) {
00041     case Parameter::kRcu:     o << "RCU";     break;
00042     case Parameter::kBc:      o << "BC";      break;
00043     case Parameter::kAltro:   o << "ALTRO";   break;
00044     case Parameter::kInvalid: o << "invalid"; break;
00045     }
00046     return o;
00047   }
00048   std::istream& operator>>(std::istream& i, Parameter::Where& w) 
00049   {
00050     std::string s;
00051     i >> s;
00052     if      (s == "RCU")    w = Parameter::kRcu;
00053     else if (s == "BC")     w = Parameter::kBc;
00054     else if (s == "ALTRO")  w = Parameter::kAltro;
00055     else                    w = Parameter::kInvalid;
00056     return i;
00057   }
00058 }
00059 
00060 //_____________________________________________________________________
00061 RcuConf::Parameter::Parameter(RcuDb::Row& row) 
00062   : Table(row)
00063 {
00064   fName = std::string(row.FieldStr(1), row.FieldLen(1));
00065   row.Field(2, fDestination);
00066   row.Field(3, fIsBlob);
00067   row.Field(4, fMask);
00068 }
00069 
00070 //_____________________________________________________________________
00071 void
00072 RcuConf::Parameter::Print() const
00073 {
00074   std::cout << "Parameter: id="         << fId 
00075             << "\tname="                << fName 
00076             << "\tdestination="         << fDestination
00077             << "\tblob=" << (fIsBlob ? "yes" : "no") 
00078             << "\tmask=0x" << std::hex << fMask << std::dec << std::endl;
00079 }
00080 
00081 //_____________________________________________________________________
00082 bool
00083 RcuConf::Parameter::Insert(RcuDb::Server& server) 
00084 {
00085   // Get previous versions. 
00086   RcuDb::Sql sql1;
00087   sql1 << "name='"        << fName        << "' AND " 
00088        << "destination='" << fDestination << "' AND " 
00089        << "isblob="       << fIsBlob;
00090   List vers;
00091   if (!Select(vers, server, sql1)) return false;
00092   // We can have only one instance of a parameter
00093   if (vers.size() > 0) {
00094     Parameter* a = *(vers.begin());
00095     fId          = a->fId;
00096     return true;
00097   }
00098 
00099   // Get a unique ID from the sequence table 
00100   if (!MakeId(server)) return false;
00101 
00102   // And then do the insert. 
00103   RcuDb::Sql sql2;
00104   sql2 << "INSERT INTO " << fgName << " VALUES(" 
00105        << fId          << ",'" 
00106        << fName        << "','" 
00107        << fDestination << "'," 
00108        << fIsBlob      << ","
00109        << fMask        << ")";
00110   return server.Exec(sql2);
00111 }
00112 
00113 
00114 //_____________________________________________________________________
00115 bool
00116 RcuConf::Parameter::Create(RcuDb::Server& server) 
00117 {
00118   RcuDb::Sql sql;
00119   sql << "CREATE TABLE " << fgName << " ("
00120       << "  id          INT KEY, " 
00121       << "  name        VARCHAR(64) NOT NULL, " 
00122       << "  destination ENUM('RCU','BC','ALTRO') NOT NULL, "
00123       << "  isblob      BOOL NOT NULL, "
00124       << "  mask        INT NOT NULL, " 
00125       << "  INDEX(name), " 
00126       << "  INDEX(destination))";
00127   return server.Exec(sql);
00128 }
00129 
00130 //_____________________________________________________________________
00131 bool
00132 RcuConf::Parameter::Drop(RcuDb::Server& server) 
00133 {
00134   return Table::Drop(server, fgName);
00135 }
00136 
00137 //_____________________________________________________________________
00138 bool
00139 RcuConf::Parameter::Select(List& l, RcuDb::Server& server, const RcuDb::Sql& cond) 
00140 {
00141   // Make the query 
00142   RcuDb::Sql     sql;
00143   sql << "SELECT * FROM " << fgName 
00144       << (cond.Text().empty() ? "" : " WHERE ") 
00145       << (cond.Text().empty() ? "" : cond);
00146 
00147   // Execute query, and check error status
00148   RcuDb::Result* res = server.Query(sql);
00149   if (server.IsError()) return false;
00150 
00151   // Make the result table if any 
00152   if (!res) return true;
00153   RcuDb::Row* row = 0;
00154   while ((row = res->Next())) l.push_back(new Parameter(*row));
00155 
00156   // delete result, and return OK.
00157   delete res;
00158   return true;  
00159 }
00160 
00161 //_____________________________________________________________________
00162 bool
00163 RcuConf::Parameter::Select(List& l, RcuDb::Server& server, 
00164                            Where dest, const std::string& name)
00165 {
00166   RcuDb::Sql sql;
00167   sql << "destination=" << dest;
00168   if (!name.empty()) sql << " AND name=" << name;
00169   return Select(l, server, sql);
00170 }
00171 
00172 //_____________________________________________________________________
00173 //
00174 // EOF
00175 //
00176 
00177 
00178   
00179 
00180   
Top of page Last update Fri Apr 27 01:54:15 2007
Copyright © 2004 Christian Holm Created by DoxyGen 1.3.5