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

/home/roger/projects/libpqxx-object/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 #include <pqxx-object/insert_query.h>
00044 #include <pqxx-object/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 const std::string&
00086 Place::get_name() const
00087 {
00088   return m_name;
00089 }
00090 
00091 void
00092 Place::set_name(const std::string& name)
00093 {
00094   m_name = name;
00095 }
00096 
00097 const std::string&
00098 Place::get_gridref() const
00099 {
00100   return m_gridref;
00101 }
00102 
00103 void
00104 Place::set_gridref(const std::string& gridref)
00105 {
00106   m_gridref = gridref;
00107 }
00108 
00109 void
00110 Place::insert_impl(pqxxobject::transaction& tran)
00111 {
00112 //   std::cerr << "Place::insert_impl method called\n";
00113 
00114   tran.begin("Place::insert_impl");
00115 
00116   pqxx::result R(tran.exec("SELECT nextval('places_id_seq') AS sequence"));
00117   if (R.size() == 1)
00118     R.begin()->at("sequence").to(m_id.get_value());
00119   else
00120     {
00121       ostringstream reason;
00122       reason << "Place insertion failed.\n"
00123              << "Failed to get next place sequence number.\n";
00124       throw pqxxobject::DatabaseError(reason.str());
00125     }
00126 
00127   pqxxobject::insert_query query("places");
00128   query.add(m_id);
00129   query.add(m_name);
00130   query.add(m_gridref);
00131 
00132   tran.perform(query.str(), 1, 1);
00133   // Don't alter m_state--it's set during commit.
00134   tran.commit();
00135 }
00136 
00137 void
00138 Place::update_impl(pqxxobject::transaction& tran)
00139 {
00140 //   std::cerr << "Place::update_impl method called\n";
00141 
00142   tran.begin("Place::update_impl");
00143 //   ostringstream query;
00144 //   query << "UPDATE places "
00145 //      << "SET name = '" << get_name()
00146 //      << "', gridref = '" << get_gridref() << "' "
00147 //      << "WHERE (id = " << get_id() << ")";
00148   pqxxobject::update_query query("places");
00149   query.add(m_name);
00150   query.add(m_gridref);
00151   query.where(m_id);
00152 
00153   tran.perform(query.str(), 1, 1);
00154   // Don't alter m_state--it's set during commit.
00155   tran.commit();
00156 }
00157 
00158 void
00159 Place::erase_impl(pqxxobject::transaction& tran)
00160 {
00161 //   std::cerr << "Place::erase_impl method called\n";
00162 
00163   tran.begin("Place::erase_impl");
00164   ostringstream query;
00165   query << "DELETE FROM places "
00166         << "WHERE (id = " << get_id() << ")";
00167 
00168   tran.perform(query.str(), 1, 1);
00169   tran.commit();
00170   // Row is unusable now it's deleted.
00171   m_id = 0;
00172   m_state = STATE_UNINITIALISED;
00173 }
00174 
00175 void
00176 Place::refresh_impl(pqxxobject::transaction& tran)
00177 {
00178 //   std::cerr << "Place::refresh_impl method called\n";
00179 
00180   tran.begin("Place::refresh_impl");
00181   ostringstream query;
00182   query << "SELECT id, name, gridref FROM places "
00183         << "WHERE (id = " << get_id() << ")";
00184 
00185   pqxx::result R(tran.exec(query.str()));
00186   if (R.size() == 1)
00187     {
00188       convert_impl(R.begin(), tran);
00189       // If the refresh worked, we're up-to-date
00190       m_state = STATE_INITIALISED;
00191     }
00192   else // The refresh failed, perhaps the row was deleted?
00193     {
00194       m_id = 0;
00195       m_state = STATE_UNINITIALISED;
00196     }
00197   tran.commit();
00198 }
00199 
00200 void
00201 Place::convert_impl(pqxx::result::const_iterator row,
00202                     pqxxobject::transaction& tran)
00203 {
00204   row->at("id").to(m_id.get_value());
00205   row->at("name").to(m_name.get_value());
00206   row->at("gridref").to(m_gridref.get_value());
00207 }
00208 
00209 
00210 PlaceTable::PlaceTable(pqxxobject::transaction& tran):
00211   table_base(tran)
00212 {
00213 }
00214 
00215 PlaceTable::~PlaceTable()
00216 {
00217 }
00218 
00219 PlaceTable::row_list_ptr
00220 PlaceTable::get_list(sort_order order)
00221 {
00222   ostringstream query;
00223   query << "SELECT id, name, gridref FROM places "
00224         << "ORDER BY ";
00225 
00226   if (order == ORDER_ID)
00227     query << "id";
00228   else if (order == ORDER_NAME)
00229     query << "name";
00230   else if (order == ORDER_GRIDREF)
00231     query << "gridref";
00232   else // fallback
00233     query << "name";
00234 
00235   return find_many(query.str());
00236 }
00237 
00238 PlaceTable::row_ptr
00239 PlaceTable::find(int place_id)
00240 {
00241   ostringstream query;
00242   query << "SELECT id, name, gridref FROM places "
00243         << "WHERE (id = " << place_id << ")";
00244 
00245   return find_one(query.str());
00246 }
00247 
00248 
00249 PlaceTable::row_ptr
00250 PlaceTable::find_name(const std::string& name)
00251 {
00252   ostringstream query;
00253   query << "SELECT id, name, gridref FROM places "
00254         << "WHERE (name = '" << name << "')";
00255 
00256   return find_one(query.str());
00257 }
00258 
00259 PlaceTable::row_list_ptr
00260 PlaceTable::find_gridref(const std::string& gridref)
00261 {
00262   ostringstream query;
00263   query << "SELECT id, name, gridref FROM places "
00264         << "WHERE (gridref = '" << gridref << "')";
00265 
00266   return find_many(query.str());
00267 }

Generated on Thu Apr 1 10:37:46 2004 for places API Reference by doxygen 1.3.5