View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db; 22 23 import java.util.Iterator; 24 import java.util.List; 25 26 import org.enableit.db.beans.Column; 27 import org.enableit.db.beans.ForeignKey; 28 29 30 /*** 31 * @author TimSt 32 */ 33 public class DBInsert extends DBStatement { 34 private StringBuffer drivingTable; 35 private StringBuffer fromClause; 36 private StringBuffer selectClause; 37 protected List joins; 38 39 public DBInsert(String baseSql) { 40 super(baseSql.trim()); 41 } 42 43 public void addInsert(Column col, Object val) { 44 cols.add(col); 45 46 //SqlType sqlType = 47 if (val == null) { 48 vals.add(new SqlType(col)); 49 } else { 50 vals.add(getParam(col, val)); 51 } 52 } 53 54 /*** 55 * Add an INSERT clause. 56 * @param col Metadata for the column to be inserted. 57 * @param val The value to insert. 58 */ 59 60 /*public void addInsert(Column col, String val) { 61 addInsert(col, getParam(col, val)) ; 62 63 }*/ 64 65 /*** 66 * @return All parameters for INSERT clause and WHERE clause in order. 67 */ 68 public List getParams() { 69 List params = vals; 70 71 params.addAll(super.getParameters()); 72 73 return params; 74 } 75 76 /*** 77 * @return The base SQL statement (up to the start of the WHERE clause) 78 * @see org.enableit.db.DBStatement#getBaseSql() 79 */ 80 public String getBaseSql() { 81 StringBuffer sql = new StringBuffer(super.getBaseSql()); 82 83 sql.append(" ("); 84 85 for (Iterator it = cols.iterator(); it.hasNext();) { 86 Column col = (Column) it.next(); 87 88 sql.append(col.getColName()); 89 90 if (it.hasNext()) { 91 sql.append(","); 92 } 93 } 94 95 sql.append(") VALUES ("); 96 97 for (int i = 0; i < cols.size(); i++) { 98 sql.append("?"); 99 100 if (i < (cols.size() - 1)) { 101 sql.append(","); 102 } 103 } 104 105 sql.append(") "); 106 107 return sql.toString(); 108 } 109 110 /*** 111 * Reset the INSERT clause, whilst leaving the base SQL and 112 * any WHERE clause in place. 113 */ 114 public void resetInsert() { 115 cols.clear(); 116 vals.clear(); 117 } 118 119 /*** 120 * @param fk Encapsulates information on what to join to. 121 */ 122 public void addJoin(ForeignKey fk) { 123 joins.add(fk); 124 } 125 }

This page was automatically generated by Maven