Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

places.cc

Go to the documentation of this file.
00001 // tutorial places class                                         -*- C++ -*-
00002 // $Id: places.cc,v 1.12 2004/05/22 17:58:17 roger Exp $
00003 //
00004 // Copyright (C) 2003  Roger Leigh <rleigh@debian.org>
00005 //
00006 //
00007 //  All rights reserved.
00008 //
00009 //  Redistribution and use in source and binary forms, with or without
00010 //  modification, are permitted provided that the following conditions
00011 //  are met:
00012 //
00013 //  * Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //  * Redistributions in binary form must reproduce the above
00016 //    copyright notice, this list of conditions and the following
00017 //    disclaimer in the documentation and/or other materials provided
00018 //    with the distribution.
00019 //  * Neither the name of the author, nor the names of other
00020 //    contributors may be used to endorse or promote products derived
00021 //    from this software without specific prior written permission.
00022 //
00023 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
00024 //  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
00025 //  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00026 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 //  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
00028 //  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00029 //  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00030 //  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031 //  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00032 //  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00033 //  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00034 //  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00035 //  SUCH DAMAGE.
00036 //
00038 
00039 #include <iostream>
00040 #include <sstream>
00041 
00042 #include <pqxxobject/exceptions.h>
00043 #include <pqxxobject/insert_query.h>
00044 #include <pqxxobject/update_query.h>
00045 
00046 #include "places.h"
00047 
00048 using std::ostringstream;
00049 using std::string;
00050 using std::vector;
00051 
00052 using namespace pqxxobject;
00053 
00054 Place::Place():
00055   m_id(0),
00056   m_name(""),
00057   m_gridref("")
00058 {
00059   m_id.set_column_name("id");
00060   m_name.set_column_name("name");
00061   m_gridref.set_column_name("gridref");
00062 }
00063 
00064 Place::Place(const std::string& name,
00065              const std::string& gridref):
00066   m_id(0),
00067   m_name(name),
00068   m_gridref(gridref)
00069 {
00070   m_id.set_column_name("id");
00071   m_name.set_column_name("name");
00072   m_gridref.set_column_name("gridref");
00073 }
00074 
00075 Place::~Place()
00076 {
00077 }
00078 
00079 int
00080 Place::get_id() const
00081 {
00082   return m_id;
00083 }
00084 
00085 pqxxobject::field_proxy_readonly< pqxxobject::field<int> >
00086 Place::field_id()
00087 {
00088   return pqxxobject::field_proxy_readonly< pqxxobject::field<int> >(m_id);
00089 }
00090 
00091 const std::string&
00092 Place::get_name() const
00093 {
00094   return m_name;
00095 }
00096 
00097 void
00098 Place::set_name(const std::string& name)
00099 {
00100   m_name = name;
00101 }
00102 
00103 pqxxobject::field_proxy< pqxxobject::field<std::string> >
00104 Place::field_name()
00105 {
00106   return pqxxobject::field_proxy< pqxxobject::field<std::string> >(m_name);
00107 }
00108 
00109 const std::string&
00110 Place::get_gridref() const
00111 {
00112   return m_gridref;
00113 }
00114 
00115 void
00116 Place::set_gridref(const std::string& gridref)
00117 {
00118   m_gridref = gridref;
00119 }
00120 
00121 pqxxobject::field_proxy< pqxxobject::field<std::string> >
00122 Place::field_gridref()
00123 {
00124   return pqxxobject::field_proxy< pqxxobject::field<std::string> >(m_gridref);
00125 }
00126 
00127 void
00128 Place::insert_impl(pqxxobject::transaction& tran)
00129 {
00130 //   std::cerr << "Place::insert_impl method called\n";
00131 
00132   tran.begin("Place::insert_impl");
00133 
00134   pqxx::result R(tran.exec("SELECT nextval('places_id_seq') AS sequence"));
00135   if (R.size() == 1)
00136     R.begin()->at("sequence").to(m_id.get_value());
00137   else
00138     {
00139       ostringstream reason;
00140       reason << "Place insertion failed.\n"
00141              << "Failed to get next place sequence number.\n";
00142       throw pqxxobject::DatabaseError(reason.str());
00143     }
00144 
00145   pqxxobject::insert_query query("places");
00146   query.add(m_id);
00147   query.add(m_name);
00148   query.add(m_gridref);
00149 
00150   tran.perform(query, 1, 1);
00151   // Don't alter m_state--it's set during commit.
00152   tran.commit();
00153 }
00154 
00155 void
00156 Place::update_impl(pqxxobject::transaction& tran)
00157 {
00158 //   std::cerr << "Place::update_impl method called\n";
00159 
00160   tran.begin("Place::update_impl");
00161 //   ostringstream query;
00162 //   query << "UPDATE places "
00163 //      << "SET name = '" << get_name()
00164 //      << "', gridref = '" << get_gridref() << "' "
00165 //      << "WHERE (id = " << get_id() << ")";
00166   pqxxobject::update_query query("places");
00167   query.add(m_name);
00168   query.add(m_gridref);
00169   query.where(m_id);
00170 
00171   tran.perform(query, 1, 1);
00172   // Don't alter m_state--it's set during commit.
00173   tran.commit();
00174 }
00175 
00176 void
00177 Place::erase_impl(pqxxobject::transaction& tran)
00178 {
00179 //   std::cerr << "Place::erase_impl method called\n";
00180 
00181   tran.begin("Place::erase_impl");
00182   ostringstream query;
00183   query << "DELETE FROM places "
00184         << "WHERE (id = " << get_id() << ")";
00185 
00186   tran.perform(query, 1, 1);
00187   tran.commit();
00188   // Row is unusable now it's deleted.
00189   m_id = 0;
00190   m_state = STATE_UNINITIALISED;
00191 }
00192 
00193 void
00194 Place::refresh_impl(pqxxobject::transaction& tran)
00195 {
00196 //   std::cerr << "Place::refresh_impl method called\n";
00197 
00198   tran.begin("Place::refresh_impl");
00199   ostringstream query;
00200   query << "SELECT id, name, gridref FROM places "
00201         << "WHERE (id = " << get_id() << ")";
00202 
00203   pqxx::result R(tran.exec(query));
00204   if (R.size() == 1)
00205     {
00206       convert_impl(R.begin(), tran);
00207       // If the refresh worked, we're up-to-date
00208       m_state = STATE_INITIALISED;
00209     }
00210   else // The refresh failed, perhaps the row was deleted?
00211     {
00212       m_id = 0;
00213       m_state = STATE_UNINITIALISED;
00214     }
00215   tran.commit();
00216 }
00217 
00218 void
00219 Place::convert_impl(pqxx::result::const_iterator row,
00220                     pqxxobject::transaction& tran)
00221 {
00222   row->at("id").to(m_id.get_value());
00223   row->at("name").to(m_name.get_value());
00224   row->at("gridref").to(m_gridref.get_value());
00225 }
00226 
00227 
00228 PlaceTable::PlaceTable(pqxxobject::transaction& tran):
00229   table_base(tran)
00230 {
00231 }
00232 
00233 PlaceTable::~PlaceTable()
00234 {
00235 }
00236 
00237 PlaceTable::row_list_ptr
00238 PlaceTable::get_list(sort_order order)
00239 {
00240   ostringstream query;
00241   query << "SELECT id, name, gridref FROM places "
00242         << "ORDER BY ";
00243 
00244   if (order == ORDER_ID)
00245     query << "id";
00246   else if (order == ORDER_NAME)
00247     query << "name";
00248   else if (order == ORDER_GRIDREF)
00249     query << "gridref";
00250   else // fallback
00251     query << "name";
00252 
00253   return find_many(query.str());
00254 }
00255 
00256 PlaceTable::row_ptr
00257 PlaceTable::find(int place_id)
00258 {
00259   ostringstream query;
00260   query << "SELECT id, name, gridref FROM places "
00261         << "WHERE (id = " << place_id << ")";
00262 
00263   return find_one(query.str());
00264 }
00265 
00266 
00267 PlaceTable::row_ptr
00268 PlaceTable::find_name(const std::string& name)
00269 {
00270   ostringstream query;
00271   query << "SELECT id, name, gridref FROM places "
00272         << "WHERE (name = '" << name << "')";
00273 
00274   return find_one(query.str());
00275 }
00276 
00277 PlaceTable::row_list_ptr
00278 PlaceTable::find_gridref(const std::string& gridref)
00279 {
00280   ostringstream query;
00281   query << "SELECT id, name, gridref FROM places "
00282         << "WHERE (gridref = '" << gridref << "')";
00283 
00284   return find_many(query.str());
00285 }

Generated on Sat May 22 19:01:36 2004 for places API Reference by doxygen 1.3.6-20040222