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

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

Go to the documentation of this file.
00001 // tutorial places class                                         -*- C++ -*-
00002 // $Id: places.cc,v 1.5 2004/01/17 23:30:46 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 Place::row_ptr
00083 Place::create(pqxx::result::const_iterator row,
00084               pqxxobject::transaction& tran)
00085 {
00086   row_ptr p(new Place);
00087   row->at("id").to(p->m_id.get_value());
00088   row->at("name").to(p->m_name.get_value());
00089   row->at("gridref").to(p->m_gridref.get_value());
00090   return p;
00091 }
00092 
00093 void
00094 Place::insert_impl(pqxxobject::transaction& tran)
00095 {
00096   ostringstream query;
00097 
00098   query << "INSERT INTO places (name, gridref) "
00099         << "VALUES ('" << get_name()
00100         << "', '" << get_gridref() << "')";
00101 
00102   tran.perform(query.str(), 1, 1);
00103 }
00104 
00105 void
00106 Place::update_impl(pqxxobject::transaction& tran)
00107 {
00108   ostringstream query;
00109 
00110   query << "UPDATE places "
00111         << "SET name = '" << get_name()
00112         << "', gridref = '" << get_gridref() << "' "
00113         << "WHERE (id = " << get_id() << ")";
00114 
00115   tran.perform(query.str(), 1, 1);
00116 }
00117 
00118 void
00119 Place::erase_impl(pqxxobject::transaction& tran)
00120 {
00121   ostringstream query;
00122 
00123   query << "DELETE FROM places "
00124         << "WHERE (id = " << get_id() << ")";
00125 
00126   tran.perform(query.str(), 1, 1);
00127 }
00128 
00129 void
00130 Place::refresh_impl(pqxxobject::transaction& tran)
00131 {
00132 }
00133 
00134 
00135 PlaceTable::PlaceTable(pqxxobject::transaction& tran):
00136   table_base(tran)
00137 {
00138 }
00139 
00140 PlaceTable::~PlaceTable()
00141 {
00142 }
00143 
00144 PlaceTable::row_list_ptr
00145 PlaceTable::get_list(sort_order order)
00146 {
00147   ostringstream query;
00148   query << "SELECT id, name, gridref FROM places "
00149         << "ORDER BY ";
00150 
00151   if (order == ORDER_ID)
00152     query << "id";
00153   else if (order == ORDER_NAME)
00154     query << "name";
00155   else if (order == ORDER_GRIDREF)
00156     query << "gridref";
00157   else // fallback
00158     query << "name";
00159 
00160   return find_many(query.str());
00161 }
00162 
00163 PlaceTable::row_ptr
00164 PlaceTable::find(int place_id)
00165 {
00166   ostringstream query;
00167   query << "SELECT id, name, gridref FROM places "
00168         << "WHERE (id = " << place_id << ")";
00169 
00170   return find_one(query.str());
00171 }
00172 
00173 
00174 PlaceTable::row_ptr
00175 PlaceTable::find_name(const std::string& name)
00176 {
00177   ostringstream query;
00178   query << "SELECT id, name, gridref FROM places "
00179         << "WHERE (name = '" << name << "')";
00180 
00181   return find_one(query.str());
00182 }
00183 
00184 PlaceTable::row_list_ptr
00185 PlaceTable::find_gridref(const std::string& gridref)
00186 {
00187   ostringstream query;
00188   query << "SELECT id, name, gridref FROM places "
00189         << "WHERE (gridref = '" << gridref << "')";
00190 
00191   return find_many(query.str());
00192 }

Generated on Sun Jan 18 12:35:48 2004 for places API Reference by doxygen 1.3.4