Address.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 "Address.h"
00026 #include "Value.h"
00027 #include "Sequence.h"
00028 #include <rcudb/Server.h>
00029 #include <rcudb/Result.h>
00030 #include <rcudb/Row.h>
00031 #include <rcudb/Sql.h>
00032 #include <iostream>
00033 
00034 namespace 
00035 {
00036   const std::string fgName("Address");
00037 }
00038 
00039 //_____________________________________________________________________
00040 RcuConf::Address::Address(RcuDb::Row& row) 
00041   : Table(row)
00042 {
00043   row.Field(1, fAddress);
00044 }
00045 
00046 //_____________________________________________________________________
00047 void
00048 RcuConf::Address::Print() const
00049 {
00050   std::cout << "Address: id="     << fId 
00051             << "\taddress="       << fAddress << std::endl;
00052 }
00053 
00054 //_____________________________________________________________________
00055 bool
00056 RcuConf::Address::Insert(RcuDb::Server& server) 
00057 {
00058   // Get previous versions. 
00059   RcuDb::Sql sql1;
00060   sql1 << "address=" << fAddress;
00061   List vers;
00062   if (!Select(vers, server, sql1)) return false;
00063   // We can have only one instance of an address
00064   if (vers.size() > 0) {
00065     Address* a = *(vers.begin());
00066     fId        = a->fId;
00067     return true;
00068   }
00069 
00070   // Get a unique ID from the sequence table 
00071   if (!MakeId(server)) return false;
00072 
00073   // And then do the insert. 
00074   RcuDb::Sql sql2;
00075   sql2 << "INSERT INTO " << fgName << " VALUES(" 
00076        << fId          << "," 
00077        << fAddress     << ")";
00078   return server.Exec(sql2);
00079 }
00080 
00081 
00082 //_____________________________________________________________________
00083 bool
00084 RcuConf::Address::Create(RcuDb::Server& server) 
00085 {
00086   RcuDb::Sql sql;
00087   sql << "CREATE TABLE " << fgName << " ("
00088       << "  id          INT KEY, " 
00089       << "  address     INT NOT NULL, " 
00090       << "  INDEX(address))";
00091   return server.Exec(sql);
00092 }
00093 
00094 //_____________________________________________________________________
00095 bool
00096 RcuConf::Address::Drop(RcuDb::Server& server) 
00097 {
00098   return Table::Drop(server, fgName);
00099 }
00100 
00101 //_____________________________________________________________________
00102 bool
00103 RcuConf::Address::Select(List& l, RcuDb::Server& server, const RcuDb::Sql& cond) 
00104 {
00105   // Make the query 
00106   RcuDb::Sql     sql;
00107   sql << "SELECT * FROM " << fgName 
00108       << (cond.Text().empty() ? "" : " WHERE ") 
00109       << (cond.Text().empty() ? "" : cond);
00110 
00111   // Execute query, and check error status
00112   RcuDb::Result* res = server.Query(sql);
00113   if (server.IsError()) return false;
00114 
00115   // Make the result table if any 
00116   if (!res) return true;
00117   RcuDb::Row* row = 0;
00118   while ((row = res->Next())) l.push_back(new Address(*row));
00119 
00120   // delete result, and return OK.
00121   delete res;
00122   return true;  
00123 }
00124 
00125 //_____________________________________________________________________
00126 bool
00127 RcuConf::Address::Select(List& l, RcuDb::Server& server, const Value& v)
00128 {
00129   RcuDb::Sql sql;
00130   sql << "id=" << v.AddressId();
00131   return Select(l, server, sql);
00132 }
00133 
00134 //_____________________________________________________________________
00135 //
00136 // EOF
00137 //
00138 
00139 
00140   
00141 
00142   
Top of page Last update Fri Apr 27 01:54:15 2007
Copyright © 2004 Christian Holm Created by DoxyGen 1.3.5