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.apache.log4j.Logger; 27 import org.enableit.db.beans.Column; 28 import org.enableit.db.beans.ForeignKey; 29 30 31 /*** 32 * Encapsulate the information required to perform a database update, 33 * (includes deletes and data definition statements). 34 * 35 * <p>The 'base' SQL is considered that part before any 'WHERE' clause.</p> 36 * 37 * @author Tim Stephenson 38 */ 39 public class DBUpdate extends DBStatement { 40 /*** 41 * The Log4J <code>Logger</code> doing the logging. 42 */ 43 private static Logger logger = Logger.getLogger(DBUpdate.class); 44 45 /*** 46 * CVS info ABOUT this class and its current version 47 */ 48 public static final String ABOUT = "$Id: DBUpdate.java,v 1.10 2004/03/19 19:11:41 tim Exp $"; 49 protected List joins; 50 51 /* 52 * Constructors 53 */ 54 55 /*** 56 * Default Constructor 57 */ 58 public DBUpdate() { 59 super(); 60 } 61 62 /*** 63 * Construct instance and set the base SQL statement. 64 */ 65 public DBUpdate(String baseSql) { 66 super(baseSql); 67 } 68 69 /* 70 * Methods 71 */ 72 73 /*** 74 * This method appends SQL to represent any update clauses added with 75 * <code>addUpdate</code> to the original base SQL passed to the constructor. 76 * 77 * @return baseSql The base SQL (up to the start of the WHERE clause). 78 */ 79 public String getBaseSql() { 80 logger.info("METHOD_ENTRY getBaseSql"); 81 82 StringBuffer sql = new StringBuffer(super.getBaseSql()); 83 84 boolean firstCol = true; 85 86 for (Iterator it = cols.iterator(); it.hasNext();) { 87 if (firstCol) { 88 if (!sql.toString().trim().endsWith("SET")) { 89 sql.append(" SET "); 90 } else if (sql.charAt(sql.length() - 1) != ' ') { 91 sql.append(" "); 92 } 93 94 firstCol = false; 95 } else { 96 sql.append(','); 97 } 98 99 Column col = (Column) it.next(); 100 101 sql.append(col.getColName() + " = ?"); 102 } 103 104 logger.info("METHOD_EXIT getBaseSql, sql: " + sql); 105 106 return sql.toString(); 107 } 108 109 public void addUpdate(Column col, Object val) { 110 cols.add(col); 111 112 if (val == null) { 113 vals.add(new SqlType(col)); 114 } else { 115 vals.add(getParam(col, val)); 116 } 117 } 118 119 /*** 120 * @return <code>List</code> of update clause parameters. 121 */ 122 public List getUpdParams() { 123 return vals; 124 } 125 126 /* 127 * Properties 128 */ 129 130 /*** 131 * @param fk Encapsulates information on what to join to. 132 */ 133 public void addJoin(ForeignKey fk) { 134 joins.add(fk); 135 } 136 }

This page was automatically generated by Maven