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

pqxxobject/update_query.cc

Go to the documentation of this file.
00001 // database update query class                                   -*- C++ -*-
00002 // $Id: update_query.cc,v 1.2 2004/04/02 22:44:55 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 <sstream>
00040 
00041 #include <pqxxobject/update_query.h>
00042 
00043 using namespace pqxxobject;
00044 
00045 update_query::update_query():
00046   pqxxobject::query(),
00047   m_table_name(),
00048   m_field_list(),
00049   m_where()
00050 {
00051 }
00052 
00053 update_query::update_query(const std::string& table):
00054   pqxxobject::query(),
00055   m_table_name(table),
00056   m_field_list(),
00057   m_where()
00058 {
00059 }
00060 
00061 update_query::update_query(const update_query& rhs):
00062   pqxxobject::query(*this),
00063   m_table_name(rhs.m_table_name),
00064   m_field_list(rhs.m_field_list),
00065   m_where(rhs.m_where)
00066 {
00067 }
00068 
00069 update_query::~update_query()
00070 {
00071 }
00072 
00073 void
00074 update_query::remove(const pqxxobject::field_base& field)
00075 {
00076   for (std::list<field_value>::iterator iter = m_field_list.begin();
00077        iter != m_field_list.end();
00078        ++iter)
00079     {
00080       if (iter->first == field.get_column_name())
00081         {
00082           iter = m_field_list.erase(iter);
00083         }
00084     }
00085 }
00086 
00087 #include <iostream>
00088 
00089 std::string update_query::str() const
00090 {
00091   // Erase the previous query (if any).
00092   std::ostringstream qs;
00093   // Construct the new query.
00094   if (m_field_list.size() > 0)
00095     {
00096       qs << "UPDATE " << m_table_name << " SET ";
00097       for (std::list<field_value>::const_iterator iter = m_field_list.begin();
00098            iter != m_field_list.end();
00099            ++iter)
00100         {
00101           if (iter != m_field_list.begin())
00102             qs << ", "; // For all but the first field.
00103           qs << iter->first << " = " << iter->second;
00104         }
00105       if (m_where.length())
00106         qs << " WHERE (" << m_where << ')';
00107     }
00108   return qs.str();
00109 }

Generated on Sat May 22 18:33:58 2004 for pqxxobject API Reference by doxygen 1.3.6-20040222