You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.7 KiB
101 lines
2.7 KiB
package de.memtext.db; |
|
|
|
import de.memtext.util.StringUtils; |
|
/** |
|
* Hilfsklasse zum Erstellen von Prozeduraufrüfen für verschd. |
|
* Datenbanken. |
|
* Wird mit DB Object erstellt, z.B. ProcedureSql(DB.INFORMIX) |
|
* dann setName() |
|
* dann beliebig oft addParam(),oder addDateParam() |
|
* Falls nach Aufruf der Prozedur ein select notwendig ist, |
|
* reinsetzen mit setFinalSelect() |
|
* zum Schluss. getFinishedCall für fertigen Datenbankspezifischen |
|
* Prozeduraufruf |
|
*/ |
|
public class ProcedureSql { |
|
|
|
private DB dbsystem; |
|
private boolean isAddPossible = true,isNameSet, isFinished; |
|
private String finalSelect = ""; |
|
|
|
private StringBuffer result = new StringBuffer(); |
|
|
|
public ProcedureSql(DB db) { |
|
this.dbsystem = db; |
|
if (dbsystem.equals(DB.INFORMIX)) |
|
result.append("execute procedure "); |
|
if (dbsystem.equals(DB.POSTGRES)) |
|
result.append("select "); |
|
|
|
} |
|
|
|
public ProcedureSql(DB dbsystem, String procname) { |
|
this(dbsystem); |
|
result.append(procname + ("(")); |
|
isNameSet=true; |
|
} |
|
public void setName(String procName) { |
|
if (isNameSet)throw new IllegalStateException("Name wurde schon gesetzt"); |
|
result.append(procName+("(")); |
|
isNameSet=true; |
|
} |
|
public void addParam(String param) { |
|
if (!isAddPossible) |
|
throw new IllegalStateException("kann keine Parameter hinzufügen, vielleicht wurde getFinishedCall schon aufgerufen"); |
|
result.append(param + ","); |
|
} |
|
public void addDateParam(String param) { |
|
if (!isAddPossible) |
|
throw new IllegalStateException("kann keine Parameter hinzufügen, vielleicht wurde getFinishedCall schon aufgerufen"); |
|
|
|
if (dbsystem.equals(DB.INFORMIX)) |
|
result.append("date"); |
|
if (dbsystem.equals(DB.POSTGRES)) |
|
result.append("date_val"); |
|
result.append("('" + param + "'),"); |
|
} |
|
public void setFinalSelect(String sel) { |
|
finalSelect = sel; |
|
} |
|
|
|
public String getFinishedCall() { |
|
if (!isFinished) |
|
finish(); |
|
return result.toString(); |
|
} |
|
|
|
private void finish() { |
|
addClosingBracket(); |
|
result.append(finalSelect); |
|
isAddPossible = false; |
|
isFinished = true; |
|
} |
|
|
|
private void addClosingBracket() { |
|
char lc=result.charAt(result.length()-1);// StringUtils.getLastChar(result); |
|
if (lc==',') |
|
StringUtils.deleteLastChar(result); |
|
result.append(");"); |
|
} |
|
|
|
public void addParam(Integer integer) { |
|
addParam(integer.toString()); |
|
} |
|
|
|
public void addParam(int in) { |
|
addParam(new Integer(in).toString()); |
|
} |
|
|
|
public void addParam(Object param) { |
|
addParam(param.toString()); |
|
} |
|
|
|
public static void main(String args[]) { |
|
ProcedureSql p=new ProcedureSql(DB.POSTGRES,"test"); |
|
p.addParam(23); |
|
System.out.println(p.getFinishedCall()); |
|
} |
|
} |
|
|
|
|
|
//Created on 03.12.2003 at 15:56:43
|