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

/home/roger/projects/libpqxx-object-0.1.3/tutorial/places.cc

Go to the documentation of this file.
00001 // tutorial places class                                         -*- C++ -*-
00002 // $Id: places.cc,v 1.8 2004/01/28 21:21:10 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 <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 //   std::cerr << "Place::insert_impl method called\n";
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   // Don't alter m_state--it's set during commit.
00128   tran.commit();
00129 }
00130 
00131 void
00132 Place::update_impl(pqxxobject::transaction& tran)
00133 {
00134 //   std::cerr << "Place::update_impl method called\n";
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   // Don't alter m_state--it's set during commit.
00145   tran.commit();
00146 }
00147 
00148 void
00149 Place::erase_impl(pqxxobject::transaction& tran)
00150 {
00151 //   std::cerr << "Place::erase_impl method called\n";
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   // Row is unusable now it's deleted.
00161   m_id = 0;
00162   m_state = STATE_UNINITIALISED;
00163 }
00164 
00165 void
00166 Place::refresh_impl(pqxxobject::transaction& tran)
00167 {
00168 //   std::cerr << "Place::refresh_impl method called\n";
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       // If the refresh worked, we're up-to-date
00180       m_state = STATE_INITIALISED;
00181     }
00182   else // The refresh failed, perhaps the row was deleted?
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 // fallback
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 }

Generated on Wed Jan 28 21:23:29 2004 for places API Reference by doxygen 1.3.4