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

pqxx-object/insert_query.cc

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

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