Config.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 "Config.h"
00026 #include "Sequence.h"
00027 #include <rcudb/Row.h>
00028 #include <rcudb/Result.h>
00029 #include <rcudb/Server.h>
00030 #include <rcudb/Sql.h>
00031 #include <iostream>
00032 
00033 const std::string RcuConf::Config::fgName("Config");
00034 
00035 //_____________________________________________________________________
00036 RcuConf::Config::Config(RcuDb::Row& row) 
00037   : Table(row)
00038 {
00039   row.Field(1, fTag);
00040   row.Field(2, fX);
00041   row.Field(3, fY);
00042   row.Field(4, fZ);
00043   row.Field(5, fPriorityId);
00044   row.Field(6, fVersion);
00045   fDescription = std::string(row.FieldStr(7), row.FieldLen(7));
00046 }
00047 
00048 //_____________________________________________________________________
00049 void
00050 RcuConf::Config::Print() const
00051 {
00052   std::cout << "Config: id="     << fId 
00053             << "\ttag="          << fTag
00054             << "\tx="            << fX
00055             << "\ty="            << fY
00056             << "\tz="            << fZ
00057             << "\tpriorityid="      << fPriorityId
00058             << "\tversion="      << fVersion 
00059             << "\tdescription='" << fDescription 
00060             << "'"               << std::endl;
00061 }
00062 
00063 //_____________________________________________________________________
00064 bool
00065 RcuConf::Config::Insert(RcuDb::Server& server) 
00066 {
00067   // Get previous versions. 
00068   List vers;
00069   if (!Select(vers, server, fTag, fX, fY, fZ)) return false;
00070 
00071   // If we have other entries with the same tag, auto-update the
00072   // version number 
00073   if (vers.size() <= 0)  fVersion = 0;
00074   else {
00075     Config* c = *(vers.begin());
00076     fVersion  = c->fVersion + 1;
00077   }
00078 
00079   // Get a unique ID from the sequence table 
00080   if (!MakeId(server)) return false;
00081 
00082   // And then do the insert. 
00083   RcuDb::Sql sql2;
00084   sql2 << "INSERT INTO " << fgName << " VALUES(" 
00085        << fId          << "," 
00086        << fTag         << "," 
00087        << fX           << "," 
00088        << fY           << "," 
00089        << fZ           << "," 
00090        << fPriorityId     << ","
00091        << fVersion     << ",'" 
00092        << fDescription << "')";
00093   return server.Exec(sql2);
00094 }
00095 
00096 
00097 //_____________________________________________________________________
00098 bool
00099 RcuConf::Config::Create(RcuDb::Server& server) 
00100 {
00101   RcuDb::Sql sql;
00102   sql << "CREATE TABLE " << fgName << " ("
00103       << "  id          INT KEY, " 
00104       << "  tag         INT NOT NULL, " 
00105       << "  x           INT NOT NULL, "
00106       << "  y           INT NOT NULL, "
00107       << "  z           INT NOT NULL, "
00108       << "  priorityid     INT NOT NULL, "
00109       << "  version     INT NOT NULL, " 
00110       << "  description TEXT, "
00111       << "  INDEX(tag), "
00112       << "  INDEX(x), "
00113       << "  INDEX(y), "
00114       << "  INDEX(z), "
00115       << "  INDEX(priorityid), "
00116       << "  INDEX(version))";
00117   return server.Exec(sql);
00118 }
00119 
00120 //_____________________________________________________________________
00121 bool
00122 RcuConf::Config::Drop(RcuDb::Server& server) 
00123 {
00124   return Table::Drop(server, fgName);
00125 }
00126 
00127 //_____________________________________________________________________
00128 bool
00129 RcuConf::Config::Select(List& l, RcuDb::Server& server, const RcuDb::Sql& cond) 
00130 {
00131   // Make the query 
00132   RcuDb::Sql     sql;
00133   sql << "SELECT * FROM " << fgName 
00134       << (cond.Text().empty() ? "" : " WHERE ") 
00135       << (cond.Text().empty() ? "" : cond);
00136 
00137   // Execute query, and check error status
00138   RcuDb::Result* res = server.Query(sql);
00139   if (server.IsError()) return false;
00140 
00141   // Make the result table if any 
00142   if (!res) return true;
00143   RcuDb::Row* row = 0;
00144   while ((row = res->Next())) l.push_back(new Config(*row));
00145 
00146   // delete result, and return OK.
00147   delete res;
00148   return true;  
00149 }
00150 
00151 //_____________________________________________________________________
00152 bool
00153 RcuConf::Config::Select(List& l, RcuDb::Server& server, int tag, 
00154                         int x, int y, int z) 
00155 {
00156   RcuDb::Sql cond;
00157   cond << "tag=" << tag << " AND "
00158        << "x="   << x   << " AND " 
00159        << "y="   << y   << " AND " 
00160        << "z="   << z   
00161        << " ORDER BY version DESC";
00162   return Select(l, server, cond);
00163 }
00164 
00165 //_____________________________________________________________________
00166 //
00167 // EOF
00168 //
00169 
00170 
00171   
00172 
00173   
Top of page Last update Fri Apr 27 01:54:15 2007
Copyright © 2004 Christian Holm Created by DoxyGen 1.3.5