00001 // database row base class -*- C++ -*- 00002 // $Id: row_base.cc,v 1.5 2004/01/28 21:21:08 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 <pqxx-object/row_base.h> 00040 00041 using namespace pqxxobject; 00042 00043 namespace 00044 { 00045 std::string describe_status(row_base::row_state current_state) 00046 { 00047 std::string status; 00048 switch (current_state) 00049 { 00050 case row_base::STATE_UNINITIALISED: 00051 status = "the row is not initialised"; 00052 break; 00053 case row_base::STATE_INITIALISED: 00054 status = "the row is initialised"; 00055 break; 00056 case row_base::STATE_INCONSISTENT: 00057 status = "the row is in an inconsistent state"; 00058 break; 00059 default: 00060 status = "the row is in an unknown state"; 00061 } 00062 return status; 00063 } 00064 } 00065 00066 row_base::row_base(): 00067 m_state(STATE_UNINITIALISED), 00068 m_modified(false) 00069 { 00070 } 00071 00072 row_base::row_base(row_state status, 00073 bool modified): 00074 m_state(status), 00075 m_modified(modified) 00076 { 00077 } 00078 00079 row_base::row_base(const row_base& rhs): 00080 SigC::Object(), 00081 m_state(rhs.m_state), 00082 m_modified(rhs.m_modified) 00083 { 00084 } 00085 00086 row_base::~row_base() 00087 { 00088 } 00089 00090 row_base& 00091 row_base::operator = (const row_base& rhs) 00092 { 00093 m_state = rhs.m_state; 00094 m_modified = rhs.m_modified; 00095 return *this; 00096 } 00097 00098 row_base::row_state 00099 row_base::get_state() const 00100 { 00101 return m_state; 00102 } 00103 00104 bool 00105 row_base::is_modified() const 00106 { 00107 return m_modified; 00108 } 00109 00110 void 00111 row_base::refresh(pqxxobject::transaction& tran) 00112 { 00113 if (m_state != STATE_UNINITIALISED) 00114 { 00115 m_state = STATE_INCONSISTENT; 00116 refresh_impl(tran); 00117 m_signal_refreshed.emit(); 00118 m_signal_changed.emit(); 00119 } 00120 else 00121 throw DatabaseError("The row could not be refreshed: " + describe_status(m_state)); 00122 } 00123 00124 void 00125 row_base::insert(pqxxobject::transaction& tran) 00126 { 00127 if (m_state == STATE_UNINITIALISED) 00128 { 00129 // Register for refresh on transaction commit/abort. 00130 tran.signal_commit().connect 00131 (SigC::slot(*this, &row_base::refresh) ); 00132 tran.signal_abort().connect 00133 (SigC::slot(*this, &row_base::refresh) ); 00134 m_state = STATE_INCONSISTENT; 00135 insert_impl(tran); 00136 m_modified = false; 00137 m_signal_inserted.emit(); 00138 } 00139 else 00140 throw DatabaseError("The row could not be inserted: " + describe_status(m_state)); 00141 } 00142 00143 void 00144 row_base::update(pqxxobject::transaction& tran) 00145 { 00146 if (m_state == STATE_INITIALISED) 00147 { 00148 // Register for refresh on transaction commit/abort. 00149 tran.signal_commit().connect 00150 ( SigC::slot(*this, &row_base::refresh) ); 00151 tran.signal_abort().connect 00152 ( SigC::slot(*this, &row_base::refresh) ); 00153 m_state = STATE_INCONSISTENT; 00154 update_impl(tran); 00155 m_modified = false; 00156 m_signal_updated.emit(); 00157 } 00158 else 00159 throw DatabaseError("The row could not be updated: " + describe_status(m_state)); 00160 } 00161 00162 void 00163 row_base::erase(pqxxobject::transaction& tran) 00164 { 00165 if (m_state == STATE_INITIALISED) 00166 { 00167 m_state = STATE_INCONSISTENT; 00168 erase_impl(tran); 00169 m_signal_erased.emit(); 00170 m_signal_changed.emit(); 00171 } 00172 else 00173 throw DatabaseError("The row could not be deleted: " + describe_status(m_state)); 00174 } 00175 00176 SigC::Signal0<void>& 00177 row_base::signal_changed() 00178 { 00179 return m_signal_changed; 00180 } 00181 00182 SigC::Signal0<void>& 00183 row_base::signal_refreshed() 00184 { 00185 return m_signal_refreshed; 00186 } 00187 00188 SigC::Signal0<void>& 00189 row_base::signal_inserted() 00190 { 00191 return m_signal_inserted; 00192 } 00193 00194 SigC::Signal0<void>& 00195 row_base::signal_updated() 00196 { 00197 return m_signal_updated; 00198 } 00199 00200 SigC::Signal0<void>& 00201 row_base::signal_erased() 00202 { 00203 return m_signal_erased; 00204 } 00205 00206 void 00207 row_base::refresh_impl(pqxxobject::transaction& tran) 00208 { 00209 } 00210 00211 void 00212 row_base::insert_impl(pqxxobject::transaction& tran) 00213 { 00214 } 00215 00216 void 00217 row_base::update_impl(pqxxobject::transaction& tran) 00218 { 00219 } 00220 00221 void 00222 row_base::erase_impl(pqxxobject::transaction& tran) 00223 { 00224 }