SingleValue.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 "SingleValue.h"
00026 #include "Parameter.h"
00027 #include "Config.h"
00028 #include "Sequence.h"
00029 #include <rcudb/Server.h>
00030 #include <rcudb/Sql.h>
00031 #include <rcudb/Row.h>
00032 #include <rcudb/Result.h>
00033 #include <iostream>
00034 
00035 const std::string RcuConf::SingleValue::fgName("SingleValue");
00036 
00037 //_____________________________________________________________________
00038 RcuConf::SingleValue::SingleValue(RcuDb::Row& row) 
00039   : Value(row)
00040 {
00041   row.Field(5, fValue);
00042 }
00043 
00044 //_____________________________________________________________________
00045 void
00046 RcuConf::SingleValue::Print() const
00047 {
00048   Value::Print();
00049   std::cout << "\tvalue="       << fValue << std::endl;
00050 }
00051 
00052 //_____________________________________________________________________
00053 bool
00054 RcuConf::SingleValue::Insert(RcuDb::Server& server) 
00055 {
00056   // Get previous versions. 
00057   List vers;
00058   if (!Select(vers, server, fConfigId, fParamId, 
00059               (fAddressId <= 0 ? -1 : fAddressId))) return false;
00060   // We can have only one instance of an singlevalue
00061   fVersion = 0;
00062   if (vers.size() > 0) {
00063     SingleValue* v = *(vers.begin());
00064     fVersion       = v->fVersion + 1;
00065   }
00066 
00067   // Get a unique ID from the sequence table 
00068   if (!MakeId(server)) return false;
00069 
00070   // And then do the insert. 
00071   RcuDb::Sql sql2;
00072   sql2 << "INSERT INTO " << fgName << " VALUES(";
00073   ValueInsert(sql2);
00074   sql2 << fValue << ")";
00075   if (fAddressId > 0) 
00076     std::cout << "Insert with " << sql2.Text() << std::endl;
00077   return server.Exec(sql2);
00078 }
00079 
00080 
00081 //_____________________________________________________________________
00082 bool
00083 RcuConf::SingleValue::Create(RcuDb::Server& server) 
00084 {
00085   RcuDb::Sql sql;
00086   sql << "CREATE TABLE " << fgName << " (";
00087   Value::ValueCreate(sql);
00088   sql << "  value     INT, " 
00089       << "  INDEX(configid)," 
00090       << "  INDEX(paramid), "
00091       << "  INDEX(version))";
00092   return server.Exec(sql);
00093 }
00094 
00095 //_____________________________________________________________________
00096 bool
00097 RcuConf::SingleValue::Drop(RcuDb::Server& server) 
00098 {
00099   return Table::Drop(server, fgName);
00100 }
00101 
00102 //_____________________________________________________________________
00103 bool
00104 RcuConf::SingleValue::Select(List& l, RcuDb::Server& server, const RcuDb::Sql& cond) 
00105 {
00106   // Make the query 
00107   RcuDb::Result* res = 0;
00108   if (!Value::Select(res, server, fgName, cond)) return false;
00109 
00110   // Make the result table if any 
00111   if (!res) return true;
00112   RcuDb::Row* row = 0;
00113   while ((row = res->Next())) l.push_back(new SingleValue(*row));
00114 
00115   // delete result, and return OK.
00116   delete res;
00117   return true;  
00118 }
00119 
00120 //_____________________________________________________________________
00121 bool
00122 RcuConf::SingleValue::Select(List& l, RcuDb::Server& server, 
00123                            const Config& c, const Parameter& p, int addr)
00124 {
00125   // If parameter object p does not describe a single, then fail. 
00126   if (p.IsBlob()) return false;
00127   return Select(l, server, c.Id(), p.Id(), addr);
00128 }
00129 
00130 //_____________________________________________________________________
00131 bool
00132 RcuConf::SingleValue::Select(List& l, RcuDb::Server& server, 
00133                            int config, int param, int addr)
00134 {
00135   RcuDb::Result* res = 0;
00136   if (!Value::Select(res, server, fgName, config, param, addr)) return false;
00137 
00138   // Make the result table if any 
00139   if (!res) return true;
00140   RcuDb::Row* row = 0;
00141   while ((row = res->Next())) l.push_back(new SingleValue(*row));
00142 
00143   // delete result, and return OK.
00144   delete res;
00145   return true;  
00146 }
00147 
00148 //_____________________________________________________________________
00149 //
00150 // EOF
00151 //
00152 
00153 
00154   
00155 
00156   
Top of page Last update Fri Apr 27 01:54:16 2007
Copyright © 2004 Christian Holm Created by DoxyGen 1.3.5