00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00093 if (vers.size() > 0) {
00094 Parameter* a = *(vers.begin());
00095 fId = a->fId;
00096 return true;
00097 }
00098
00099
00100 if (!MakeId(server)) return false;
00101
00102
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
00142 RcuDb::Sql sql;
00143 sql << "SELECT * FROM " << fgName
00144 << (cond.Text().empty() ? "" : " WHERE ")
00145 << (cond.Text().empty() ? "" : cond);
00146
00147
00148 RcuDb::Result* res = server.Query(sql);
00149 if (server.IsError()) return false;
00150
00151
00152 if (!res) return true;
00153 RcuDb::Row* row = 0;
00154 while ((row = res->Next())) l.push_back(new Parameter(*row));
00155
00156
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
00175
00176
00177
00178
00179
00180