00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00038
00039 #include <iostream>
00040 #include <sstream>
00041
00042 #include <pqxx-object/exceptions.h>
00043
00044 #include "places.h"
00045
00046 using std::ostringstream;
00047 using std::string;
00048 using std::vector;
00049
00050 using namespace pqxxobject;
00051
00052 Place::Place():
00053 m_id(0),
00054 m_name(""),
00055 m_gridref("")
00056 {
00057 }
00058
00059 Place::Place(const std::string& name,
00060 const std::string& gridref):
00061 m_id(0),
00062 m_name(name),
00063 m_gridref(gridref)
00064 {
00065 }
00066
00067 Place::~Place()
00068 {
00069 }
00070
00071 int
00072 Place::get_id() const
00073 {
00074 return m_id;
00075 }
00076
00077 const std::string&
00078 Place::get_name() const
00079 {
00080 return m_name;
00081 }
00082
00083 void
00084 Place::set_name(const std::string& name)
00085 {
00086 m_name = name;
00087 }
00088
00089 const std::string&
00090 Place::get_gridref() const
00091 {
00092 return m_gridref;
00093 }
00094
00095 void
00096 Place::set_gridref(const std::string& gridref)
00097 {
00098 m_gridref = gridref;
00099 }
00100
00101 void
00102 Place::insert_impl(pqxxobject::transaction& tran)
00103 {
00104
00105
00106 tran.begin("Place::insert_impl");
00107
00108 pqxx::result R(tran.exec("SELECT nextval('places_id_seq') AS sequence"));
00109 if (R.size() == 1)
00110 R.begin()->at("sequence").to(m_id.get_value());
00111 else
00112 {
00113 ostringstream reason;
00114 reason << "Place insertion failed.\n"
00115 << "Failed to get next place sequence number.\n";
00116 throw pqxxobject::DatabaseError(reason.str());
00117 }
00118
00119 ostringstream query;
00120 query << "INSERT INTO places (id, name, gridref) "
00121 << "VALUES (currval('places_id_seq'), "
00122 << "'" << get_name()
00123 << "', '" << get_gridref()
00124 << "')";
00125
00126 tran.perform(query.str(), 1, 1);
00127
00128 tran.commit();
00129 }
00130
00131 void
00132 Place::update_impl(pqxxobject::transaction& tran)
00133 {
00134
00135
00136 tran.begin("Place::update_impl");
00137 ostringstream query;
00138 query << "UPDATE places "
00139 << "SET name = '" << get_name()
00140 << "', gridref = '" << get_gridref() << "' "
00141 << "WHERE (id = " << get_id() << ")";
00142
00143 tran.perform(query.str(), 1, 1);
00144
00145 tran.commit();
00146 }
00147
00148 void
00149 Place::erase_impl(pqxxobject::transaction& tran)
00150 {
00151
00152
00153 tran.begin("Place::erase_impl");
00154 ostringstream query;
00155 query << "DELETE FROM places "
00156 << "WHERE (id = " << get_id() << ")";
00157
00158 tran.perform(query.str(), 1, 1);
00159 tran.commit();
00160
00161 m_id = 0;
00162 m_state = STATE_UNINITIALISED;
00163 }
00164
00165 void
00166 Place::refresh_impl(pqxxobject::transaction& tran)
00167 {
00168
00169
00170 tran.begin("Place::refresh_impl");
00171 ostringstream query;
00172 query << "SELECT id, name, gridref FROM places "
00173 << "WHERE (id = " << get_id() << ")";
00174
00175 pqxx::result R(tran.exec(query.str()));
00176 if (R.size() == 1)
00177 {
00178 convert_impl(R.begin(), tran);
00179
00180 m_state = STATE_INITIALISED;
00181 }
00182 else
00183 {
00184 m_id = 0;
00185 m_state = STATE_UNINITIALISED;
00186 }
00187 tran.commit();
00188 }
00189
00190 void
00191 Place::convert_impl(pqxx::result::const_iterator row,
00192 pqxxobject::transaction& tran)
00193 {
00194 row->at("id").to(m_id.get_value());
00195 row->at("name").to(m_name.get_value());
00196 row->at("gridref").to(m_gridref.get_value());
00197 }
00198
00199
00200 PlaceTable::PlaceTable(pqxxobject::transaction& tran):
00201 table_base(tran)
00202 {
00203 }
00204
00205 PlaceTable::~PlaceTable()
00206 {
00207 }
00208
00209 PlaceTable::row_list_ptr
00210 PlaceTable::get_list(sort_order order)
00211 {
00212 ostringstream query;
00213 query << "SELECT id, name, gridref FROM places "
00214 << "ORDER BY ";
00215
00216 if (order == ORDER_ID)
00217 query << "id";
00218 else if (order == ORDER_NAME)
00219 query << "name";
00220 else if (order == ORDER_GRIDREF)
00221 query << "gridref";
00222 else
00223 query << "name";
00224
00225 return find_many(query.str());
00226 }
00227
00228 PlaceTable::row_ptr
00229 PlaceTable::find(int place_id)
00230 {
00231 ostringstream query;
00232 query << "SELECT id, name, gridref FROM places "
00233 << "WHERE (id = " << place_id << ")";
00234
00235 return find_one(query.str());
00236 }
00237
00238
00239 PlaceTable::row_ptr
00240 PlaceTable::find_name(const std::string& name)
00241 {
00242 ostringstream query;
00243 query << "SELECT id, name, gridref FROM places "
00244 << "WHERE (name = '" << name << "')";
00245
00246 return find_one(query.str());
00247 }
00248
00249 PlaceTable::row_list_ptr
00250 PlaceTable::find_gridref(const std::string& gridref)
00251 {
00252 ostringstream query;
00253 query << "SELECT id, name, gridref FROM places "
00254 << "WHERE (gridref = '" << gridref << "')";
00255
00256 return find_many(query.str());
00257 }