001: /*
002: * sqlc 1
003: * SQL Compiler
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/products/sqlc/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sqlc;
024:
025: import java.sql.SQLException;
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import org.apache.bcel.Constants;
030: import org.apache.bcel.generic.ACONST_NULL;
031: import org.apache.bcel.generic.ALOAD;
032: import org.apache.bcel.generic.DUP;
033: import org.apache.bcel.generic.IRETURN;
034: import org.apache.bcel.generic.InstructionList;
035: import org.apache.bcel.generic.LDC;
036:
037: import biz.hammurapi.codegen.Class;
038: import biz.hammurapi.codegen.Consumer;
039: import biz.hammurapi.codegen.GenerationException;
040: import biz.hammurapi.codegen.MethodPrototype;
041: import biz.hammurapi.sql.Parameterizer;
042: import biz.hammurapi.sql.SQLProcessor;
043: import biz.hammurapi.sql.metadata.GenerationPolicy;
044: import biz.hammurapi.util.Parameter;
045:
046: /**
047: * @author Pavel Vlasov
048: * @version $Revision: 1.5 $
049: */
050: public class NamedUpdate extends NamedStatement {
051:
052: /**
053: * @param name
054: * @param description
055: * @param sql
056: * @param parameterDescriptors
057: * @param parameterDescriptors2 TODO
058: */
059: public NamedUpdate(String name, String description, String sql,
060: Collection parameterDescriptors,
061: Collection parameterDescriptors2) {
062: super (name, description, sql, parameterDescriptors,
063: parameterDescriptors2);
064: }
065:
066: /**
067: * @param name
068: * @param sql
069: * @param processor
070: * @param policy
071: * @param hasNullableParameters
072: * @throws SQLException
073: */
074: public NamedUpdate(String name, String description, String sql,
075: SQLProcessor processor, GenerationPolicy policy,
076: boolean hasNullableParameters) throws SQLException {
077: super (name, description, sql, processor, policy,
078: hasNullableParameters);
079: }
080:
081: protected void generateEngineMethods(String packageName, Class c)
082: throws GenerationException {
083: MethodPrototype mp = new MethodPrototype(
084: c,
085: getEngineMethodsVisibility()
086: + " int "
087: + (Character.toLowerCase(name.charAt(0)) + (name
088: .length() == 1 ? "" : name.substring(1)))
089: + "() throws " + SQLException.class.getName(),
090: parametersWithProperTypes());
091:
092: InstructionList il = new InstructionList();
093:
094: // 0: aload_0
095: il.append(new ALOAD(0));
096: // 1: getfield org.jincarnate.util.SampleClass.processor Lcom/pavelvlasov/sql/SQLProcessor; (52)
097: il.append(c.createGetField("_processor"));
098: // 4: ldc "ABC" (61)
099: il.append(new LDC(c.getClassGen().getConstantPool().addString(
100: getSql())));
101: if (parameterDescriptors.isEmpty()) {
102: il.append(new ACONST_NULL());
103: } else {
104: // 6: new <org.jincarnate.util.SampleClass$PackageQueryParameterizer> (63)
105: String parameterizerClass = generateParameterizerName(c
106: .getClassGen().getClassName());
107: il.append(c.getInstructionFactory().createNew(
108: parameterizerClass));
109: // 9: dup
110: il.append(new DUP());
111: // 10: aload_1
112: // 11: iload_2
113: // 12: iload_3
114: Iterator it = parameterDescriptors.iterator();
115: for (int k = 0; it.hasNext(); k++) {
116: Parameter parameter = (Parameter) it.next();
117: il.append(mp.createVariableLoad(parameter.getName()));
118: }
119: // 13: invokespecial org.jincarnate.util.SampleClass$PackageQueryParameterizer.<init> (Ljava/lang/String;SI)V (66)
120: il.append(c.createInvoke(parameterizerClass,
121: "void <init>()", parametersWithProperTypes(),
122: Constants.INVOKESPECIAL));
123: }
124: // 16: invokevirtual biz.hammurapi.sql.SQLProcessor.processUpdate (Ljava/lang/String;Lcom/pavelvlasov/sql/Parameterizer;)I (72)
125: il.append(c.createInvoke(SQLProcessor.class.getName(),
126: "int processUpdate(java.lang.String, "
127: + Parameterizer.class.getName() + ")", null,
128: Constants.INVOKEVIRTUAL));
129: // 19: ireturn
130: il.append(new IRETURN());
131:
132: mp.addMethod(il, null,
133: "Executes update and returns number of affected rows. "
134: + getDescription());
135: }
136:
137: public void generate(String packageName, Consumer consumer,
138: Class masterEngine) throws GenerationException {
139: generateParameterizer(packageName, consumer, masterEngine);
140: if (masterEngine == null) {
141: generateEngine(packageName, consumer);
142: } else {
143: generateEngineMethods(packageName, masterEngine);
144: }
145: }
146: }
|