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

pqxx-object/table.h

Go to the documentation of this file.
00001 // database table convenience wrapper                            -*- C++ -*-
00002 // $Id: table.h,v 1.8 2004/02/20 22:49:15 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 #ifndef PQXX_OBJECT_TABLE_H
00040 #define PQXX_OBJECT_TABLE_H
00041 
00042 #include <list>
00043 #include <memory>
00044 
00045 #include <pqxx-object/row.h>
00046 
00047 namespace pqxxobject
00048 {
00056   template<typename Row>
00057   class table
00058   {
00059   public:
00061     typedef Row row_type;
00063     typedef typename Row::row_ptr row_ptr;
00065     typedef std::list<Row> row_list;
00067     typedef std::auto_ptr<row_list> row_list_ptr;
00068 
00069 
00070   protected:
00079     explicit table(pqxxobject::transaction& tran):
00080       m_transaction(tran)
00081     {}
00082 
00083   public:
00085     virtual ~table()
00086     {}
00087 
00092     virtual
00093     void
00094     insert(row_type& row)
00095     {
00096       row.insert(m_transaction);
00097     }
00098 
00103     virtual
00104     void
00105     update(row_type& row)
00106     {
00107       row.update(m_transaction);
00108     }
00109 
00114     virtual
00115     void
00116     erase(row_type& row)
00117     {
00118       row.erase(m_transaction);
00119     }
00120 
00125     virtual
00126     void
00127     refresh(row_type& row)
00128     {
00129       row.refresh(m_transaction);
00130     }
00131 
00132   protected:
00138     virtual
00139     row_ptr
00140     find_one(const std::string& query)
00141     {
00142       try
00143         {
00144           m_transaction.begin("pqxxobject::table::find_one");
00145 
00146           pqxx::result R (m_transaction.exec(query));
00147 
00148 
00149           row_ptr ret;
00150 
00151           if (R.size() > 0)
00152             {
00153               pqxx::result::const_iterator cur = R.begin();
00154               // Assign result (if any) to temporary row.
00155               if (cur != R.end())
00156                 ret = row_type::create(cur, m_transaction);
00157             }
00158 
00159           m_transaction.commit();
00160           return ret;
00161         }
00162       catch (const std::exception& e)
00163         {
00164           m_transaction.abort();
00165           throw DatabaseError(e.what());
00166         }
00167     }
00168 
00174     virtual
00175     row_list_ptr
00176     find_many(const std::string& query)
00177     {
00178       try
00179         {
00180           m_transaction.begin("pqxxobject::table::find_many");
00181 
00182           pqxx::result R (m_transaction.exec(query));
00183 
00184           row_list_ptr ret(new row_list);
00185 
00186           for (pqxx::result::const_iterator cur = R.begin(); cur != R.end(); cur++)
00187             {
00188               row_ptr tmp_row;
00189               // Assign results (one row) to temporary row
00190               tmp_row = row_type::create(cur, m_transaction);
00191               // Add this member to the list
00192               ret->push_back(*tmp_row);
00193             }
00194 
00195           m_transaction.commit();
00196 
00197           return ret;
00198         }
00199       catch (const std::exception& e)
00200         {
00201           m_transaction.abort();
00202           throw DatabaseError(e.what());
00203         }
00204     }
00205 
00206     pqxxobject::transaction& m_transaction;
00207 
00208   }; // class table
00209 
00210 }; // namespace pqxxobject
00211 
00212 #endif // PQXX_OBJECT_TABLE_H

Generated on Thu Apr 1 12:53:10 2004 for pqxx-object API Reference by doxygen 1.3.5