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

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

Go to the documentation of this file.
00001 // tutorial places class                                         -*- C++ -*-
00002 // $Id: places.cc,v 1.4 2004/01/07 14:29:41 roger Exp $
00003 //
00004 // Copyright (C) 2003  Roger Leigh <rleigh@debian.org>
00005 //
00006 //
00007 // This program is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 #include <sstream>
00022 
00023 #include <pqxx-object/exceptions.h>
00024 
00025 #include "places.h"
00026 
00027 using std::ostringstream;
00028 using std::string;
00029 using std::vector;
00030 
00031 using namespace pqxxobject;
00032 
00033 Place::Place():
00034   m_id(0),
00035   m_name(""),
00036   m_gridref("")
00037 {
00038 }
00039 
00040 Place::Place(const std::string& name,
00041              const std::string& gridref):
00042   m_id(0),
00043   m_name(name),
00044   m_gridref(gridref)
00045 {
00046 }
00047 
00048 Place::~Place()
00049 {
00050 }
00051 
00052 int
00053 Place::get_id() const
00054 {
00055   return m_id;
00056 }
00057 
00058 const std::string&
00059 Place::get_name() const
00060 {
00061   return m_name;
00062 }
00063 
00064 void
00065 Place::set_name(const std::string& name)
00066 {
00067   m_name = name;
00068 }
00069 
00070 const std::string&
00071 Place::get_gridref() const
00072 {
00073   return m_gridref;
00074 }
00075 
00076 void
00077 Place::set_gridref(const std::string& gridref)
00078 {
00079   m_gridref = gridref;
00080 }
00081 
00082 void
00083 Place::initialise(pqxx::result::const_iterator row,
00084                   pqxxobject::transaction& tran)
00085 {
00086   row->at("id").to(m_id.get_value());
00087   row->at("name").to(m_name.get_value());
00088   row->at("gridref").to(m_gridref.get_value());
00089 }
00090 
00091 void
00092 Place::insert(pqxxobject::transaction& tran)
00093 {
00094   ostringstream query;
00095 
00096   query << "INSERT INTO places (name, gridref) "
00097         << "VALUES ('" << get_name()
00098         << "', '" << get_gridref() << "')";
00099 
00100   tran.perform(query.str(), 1, 1);
00101 }
00102 
00103 void
00104 Place::update(pqxxobject::transaction& tran)
00105 {
00106   ostringstream query;
00107 
00108   query << "UPDATE places "
00109         << "SET name = '" << get_name()
00110         << "', gridref = '" << get_gridref() << "' "
00111         << "WHERE (id = " << get_id() << ")";
00112 
00113   tran.perform(query.str(), 1, 1);
00114 }
00115 
00116 void
00117 Place::erase(pqxxobject::transaction& tran)
00118 {
00119   ostringstream query;
00120 
00121   query << "DELETE FROM places "
00122         << "WHERE (id = " << get_id() << ")";
00123 
00124   tran.perform(query.str(), 1, 1);
00125 }
00126 
00127 void
00128 Place::refresh(pqxxobject::transaction& tran)
00129 {
00130 }
00131 
00132 
00133 PlaceTable::PlaceTable(pqxxobject::transaction& tran):
00134   table_base(tran)
00135 {
00136 }
00137 
00138 PlaceTable::~PlaceTable()
00139 {
00140 }
00141 
00142 PlaceTable::row_list_ptr
00143 PlaceTable::get_list(sort_order order)
00144 {
00145   ostringstream query;
00146   query << "SELECT id, name, gridref FROM places "
00147         << "ORDER BY ";
00148 
00149   if (order == ORDER_ID)
00150     query << "id";
00151   else if (order == ORDER_NAME)
00152     query << "name";
00153   else if (order == ORDER_GRIDREF)
00154     query << "gridref";
00155   else // fallback
00156     query << "name";
00157 
00158   return find_many(query.str());
00159 }
00160 
00161 PlaceTable::row_ptr
00162 PlaceTable::find(int place_id)
00163 {
00164   ostringstream query;
00165   query << "SELECT id, name, gridref FROM places "
00166         << "WHERE (id = " << place_id << ")";
00167 
00168   return find_one(query.str());
00169 }
00170 
00171 
00172 PlaceTable::row_ptr
00173 PlaceTable::find_name(const std::string& name)
00174 {
00175   ostringstream query;
00176   query << "SELECT id, name, gridref FROM places "
00177         << "WHERE (name = '" << name << "')";
00178 
00179   return find_one(query.str());
00180 }
00181 
00182 PlaceTable::row_list_ptr
00183 PlaceTable::find_gridref(const std::string& gridref)
00184 {
00185   ostringstream query;
00186   query << "SELECT id, name, gridref FROM places "
00187         << "WHERE (gridref = '" << gridref << "')";
00188 
00189   return find_many(query.str());
00190 }

Generated on Sat Jan 17 20:15:56 2004 for places API Reference by doxygen 1.3.4