001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.content;
022:
023: import java.util.Arrays;
024:
025: import nickyb.sqleonardo.querybuilder.syntax.QueryTokens;
026: import nickyb.sqleonardo.querybuilder.syntax.SQLFormatter;
027:
028: public class UpdateModel {
029: private QueryTokens.Table table = null;
030: private QueryTokens.Column[] rowid = null;
031:
032: public QueryTokens.Table getTable() {
033: return table;
034: }
035:
036: public void setTable(QueryTokens.Table table) {
037: this .table = table;
038: }
039:
040: public QueryTokens.Column getRowIdentifier(int idx) {
041: return rowid[idx];
042: }
043:
044: public int getRowIdentifierCount() {
045: return rowid.length;
046: }
047:
048: public void setRowIdentifier(QueryTokens.Column[] where) {
049: this .rowid = where;
050: }
051:
052: public String getDeleteSyntax() {
053: Object[] whereValues = new Object[rowid.length];
054: Arrays.fill(whereValues, "?");
055:
056: return getDeleteSyntax(whereValues);
057: }
058:
059: public String getDeleteSyntax(Object[] whereValues) {
060: return "DELETE FROM " + table.getIdentifier()
061: + SQLFormatter.SPACE + getWhereSyntax(whereValues);
062: }
063:
064: public String getInsertSyntax(String[] columns) {
065: Object[] fieldValues = new Object[columns.length];
066: Arrays.fill(fieldValues, "?");
067:
068: return getInsertSyntax(columns, fieldValues);
069: }
070:
071: public String getInsertSyntax(String[] columns, Object[] fieldValues) {
072: return "INSERT INTO " + table.getIdentifier() + " ("
073: + SQLFormatter.concatCommaDelimited(columns, false)
074: + ") VALUES ("
075: + SQLFormatter.concatCommaDelimited(fieldValues, false)
076: + ")";
077: }
078:
079: public String getUpdateSyntax(String[] columns) {
080: Object[] whereValues = new Object[rowid.length];
081: Arrays.fill(whereValues, "?");
082:
083: return getUpdateSyntax(columns, whereValues);
084: }
085:
086: public String getUpdateSyntax(String[] columns, Object[] whereValues) {
087: Object[] fieldValues = new Object[columns.length];
088: Arrays.fill(fieldValues, "?");
089:
090: return getUpdateSyntax(columns, fieldValues, whereValues);
091: }
092:
093: public String getUpdateSyntax(String[] columns,
094: Object[] fieldValues, Object[] whereValues) {
095: String changes = new String();
096: for (int i = 0; i < fieldValues.length; i++)
097: changes += columns[i] + " = " + fieldValues[i] + ",";
098:
099: return "UPDATE " + table.getIdentifier() + " SET "
100: + changes.substring(0, changes.length() - 1)
101: + SQLFormatter.SPACE + getWhereSyntax(whereValues);
102: }
103:
104: private String getWhereSyntax(Object[] params) {
105: if (rowid == null || params == null)
106: return new String();
107:
108: QueryTokens.Condition[] c = new QueryTokens.Condition[rowid.length];
109: for (int i = 0; i < c.length; i++)
110: c[i] = new QueryTokens.Condition(SQLFormatter.AND,
111: rowid[i], "=", new QueryTokens.DefaultExpression(
112: params[i].toString()));
113: c[0].setAppend(null);
114:
115: return SQLFormatter.WHERE + SQLFormatter.SPACE
116: + SQLFormatter.concat(c, false);
117: }
118: /*
119: public static void main(String[] args)
120: {
121: QueryTokens.Table t = new QueryTokens.Table(null,"Tabella1");
122: QueryTokens.Column[] c = new QueryTokens.Column[2];
123: c[0] = new QueryTokens.Column(t,"Colonna1");
124: c[1] = new QueryTokens.Column(t,"Colonna2");
125:
126: UpdateModel um = new UpdateModel();
127: um.setTable(t);
128: um.setRowIdentifier(c);
129:
130: System.out.println(um.getInsertSyntax(new String[]{"Colonna1","Colonna2"}));
131: System.out.println(um.getInsertSyntax(new String[]{"Colonna1","Colonna2"},new Object[]{new Integer(1),"bla bla bla"}));
132:
133: System.out.println(um.getDeleteSyntax(new Object[]{new Integer(1),new Integer(1)}));
134: }
135: */
136: }
|