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

pqxx-object/table.h

Go to the documentation of this file.
00001 // database table convenience wrapper                            -*- C++ -*-
00002 // $Id: table.h,v 1.4 2004/01/07 14:29:39 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 #ifndef PQXX_OBJECT_TABLE_H
00022 #define PQXX_OBJECT_TABLE_H
00023 
00024 #include <list>
00025 #include <memory>
00026 
00027 #include <pqxx-object/row.h>
00028 
00029 namespace pqxxobject
00030 {
00040   template<typename Row>
00041   class table
00042   {
00043   public:
00045     typedef Row row_type;
00046     typedef std::auto_ptr<Row> row_ptr;
00047     typedef std::list<Row> row_list;
00048     typedef std::auto_ptr<row_list> row_list_ptr;
00049 
00050 
00051   protected:
00060     table(pqxxobject::transaction& tran):
00061       m_transaction(tran)
00062     {}
00063 
00064   public:
00066     virtual
00067     ~table()
00068     {}
00069 
00074     virtual
00075     void
00076     insert(row_type& row)
00077     {
00078       row.insert(m_transaction);
00079     }
00080 
00085     virtual
00086     void
00087     update(row_type& row)
00088     {
00089       row.update(m_transaction);
00090     }
00091 
00096     virtual
00097     void
00098     erase(row_type& row)
00099     {
00100       row.erase(m_transaction);
00101     }
00102 
00107     virtual
00108     void
00109     refresh(row_type& row)
00110     {
00111       row.refresh(m_transaction);
00112     }
00113 
00114   protected:
00120     virtual
00121     row_ptr
00122     find_one(const std::string& query)
00123     {
00124       try
00125         {
00126           m_transaction.begin("pqxxobject::table::find_one");
00127 
00128           pqxx::result R (m_transaction.exec(query));
00129 
00130 
00131           row_ptr ret(new row_type);
00132 
00133           if (R.size() > 0)
00134             {
00135               pqxx::result::const_iterator cur = R.begin();
00136               // Assign result (if any) to temporary row.
00137               if (cur != R.end())
00138                 ret->initialise(cur, m_transaction);
00139             }
00140           else
00141             ret.release();
00142 
00143           m_transaction.end();
00144           return ret;
00145         }
00146       catch (const std::exception& e)
00147         {
00148           m_transaction.end();
00149           throw DatabaseError(e.what());
00150         }
00151     }
00152 
00158     virtual
00159     row_list_ptr
00160     find_many(const std::string& query)
00161     {
00162       try
00163         {
00164           m_transaction.begin("pqxxobject::table::find_many");
00165 
00166           pqxx::result R (m_transaction.exec(query));
00167 
00168           row_list_ptr ret(new row_list);
00169 
00170           for (pqxx::result::const_iterator cur = R.begin(); cur != R.end(); cur++)
00171             {
00172               row_ptr tmp_row(new row_type);
00173               // Assign results (one row) to temporary row
00174               tmp_row->initialise(cur, m_transaction);
00175               // Add this member to the list
00176               ret->push_back(*tmp_row);
00177             }
00178 
00179           m_transaction.end();
00180 
00181           return ret;
00182         }
00183       catch (const std::exception& e)
00184         {
00185           m_transaction.end();
00186           throw DatabaseError(e.what());
00187         }
00188     }
00189 
00190     pqxxobject::transaction& m_transaction;
00191 
00192   }; // class table
00193 
00194 }; // namespace pqxxobject
00195 
00196 #endif // PQXX_OBJECT_TABLE_H

Generated on Sat Jan 17 20:58:42 2004 for pqxx-object API Reference by doxygen 1.3.4