00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00059 RcuDb::Sql sql1;
00060 sql1 << "address=" << fAddress;
00061 List vers;
00062 if (!Select(vers, server, sql1)) return false;
00063
00064 if (vers.size() > 0) {
00065 Address* a = *(vers.begin());
00066 fId = a->fId;
00067 return true;
00068 }
00069
00070
00071 if (!MakeId(server)) return false;
00072
00073
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
00106 RcuDb::Sql sql;
00107 sql << "SELECT * FROM " << fgName
00108 << (cond.Text().empty() ? "" : " WHERE ")
00109 << (cond.Text().empty() ? "" : cond);
00110
00111
00112 RcuDb::Result* res = server.Query(sql);
00113 if (server.IsError()) return false;
00114
00115
00116 if (!res) return true;
00117 RcuDb::Row* row = 0;
00118 while ((row = res->Next())) l.push_back(new Address(*row));
00119
00120
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
00137
00138
00139
00140
00141
00142