001package com.hfg.sql;
002
003import com.hfg.sql.table.DatabaseTable;
004import com.hfg.sql.table.field.DatabaseField;
005import com.hfg.util.StringBuilderPlus;
006
007import java.sql.Connection;
008import java.sql.SQLException;
009import java.util.Collection;
010
011//------------------------------------------------------------------------------
012/**
013 Container for building a SQL insert statement.
014 <div>
015 @author J. Alex Taylor, hairyfatguy.com
016 </div>
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039
040public class SQLInsert extends SQLCmd
041{
042   private DatabaseTable             mTable;
043   private Collection<DatabaseField> mFieldList;
044
045
046   //###########################################################################
047   // PUBLIC METHODS
048   //###########################################################################
049
050   //---------------------------------------------------------------------------
051   public boolean execute(Connection inConn)
052         throws SQLException
053   {
054      return SQLUtil.execute(inConn, toSQL());
055   }
056
057   //---------------------------------------------------------------------------
058   public String toSQL()
059   {
060      StringBuilderPlus sql = new StringBuilderPlus("INSERT INTO ");
061      sql.appendln(mTable.getQualifiedName());
062
063      sql.append(" (");
064
065      StringBuilderPlus colNames = new StringBuilderPlus().setDelimiter(", ");
066      for (DatabaseField field : mFieldList)
067      {
068         if (! field.isNull()
069             || (field.getCol().isId()
070                 && field.getCol().getSequence() != null))
071         {
072            colNames.delimitedAppend(field.getCol().name());
073         }
074      }
075
076      sql.append(colNames.toString());
077
078      sql.appendln(")");
079      sql.append(" VALUES (");
080
081      StringBuilderPlus values = new StringBuilderPlus().setDelimiter(", ");
082      for (DatabaseField field : mFieldList)
083      {
084
085         if (field.getCol().isId())
086         {
087            String value = null;
088            if (field.getCol().getSequence() != null)
089            {
090               value = field.getCol().getSequence().nextvalSQL();
091            }
092            else if (! field.isNull())
093            {
094               value = field.getSQLValue();
095            }
096
097            if (value != null)
098            {
099               values.delimitedAppend(value);
100            }
101         }
102         else if (! field.isNull())
103         {
104            values.delimitedAppend(field.getSQLValue());
105         }
106      }
107
108      sql.append(values.toString());
109
110      sql.append(")");
111
112      return sql.toString();
113   }
114
115   //---------------------------------------------------------------------------
116   @Override
117   public String toString()
118   {
119      return toSQL();
120   }
121
122   //---------------------------------------------------------------------------
123   public SQLInsert setTable(DatabaseTable inValue)
124   {
125      mTable = inValue;
126
127      return this;
128   }
129
130   //---------------------------------------------------------------------------
131   public SQLInsert setFields(Collection<DatabaseField> inValue)
132   {
133      mFieldList = inValue;
134
135      return this;
136   }
137
138}