001: /*
002: * ProcedureDefinition.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db;
013:
014: import java.sql.DatabaseMetaData;
015: import java.sql.SQLException;
016: import workbench.util.SqlUtil;
017: import workbench.util.StringUtil;
018:
019: /**
020: *
021: * @author support@sql-workbench.net
022: */
023: public class ProcedureDefinition implements DbObject {
024: private String schema;
025: private String catalog;
026: private String procName;
027:
028: // as returned by the JDBC driver corresponds to
029: // DatabaseMetaData.procedureNoResult
030: // DatabaseMetaData.procedureReturnsResult
031: private int resultType;
032:
033: private boolean isOraclePackage = false;
034: private CharSequence source;
035:
036: public static ProcedureDefinition createOraclePackage(String schem,
037: String name) {
038: ProcedureDefinition def = new ProcedureDefinition(name, schem,
039: null, DatabaseMetaData.procedureResultUnknown);
040: def.setOraclePackage(true);
041: return def;
042: }
043:
044: public ProcedureDefinition(String name, int type) {
045: procName = name;
046: resultType = type;
047: }
048:
049: public ProcedureDefinition(String cat, String schem, String name,
050: int type) {
051: schema = schem;
052: catalog = cat;
053: procName = name;
054: resultType = type;
055: }
056:
057: public ProcedureDefinition(String cat, String schem, String name,
058: int type, boolean isOracle) {
059: schema = schem;
060: catalog = cat;
061: procName = name;
062: resultType = type;
063: if (isOracle) {
064: this .isOraclePackage = !StringUtil.isEmptyString(catalog);
065: }
066: }
067:
068: public CharSequence getSource(WbConnection con) throws SQLException {
069: if (con == null)
070: return null;
071: if (this .source == null) {
072: try {
073: con.getMetadata().readProcedureSource(this );
074: } catch (NoConfigException e) {
075: this .source = "N/A";
076: }
077: }
078: return this .source;
079: }
080:
081: public String getObjectName(WbConnection conn) {
082: return conn.getMetadata().quoteObjectname(this .procName);
083: }
084:
085: public String getObjectExpression(WbConnection conn) {
086: return SqlUtil.buildExpression(conn, catalog, schema, procName);
087: }
088:
089: public String getObjectName() {
090: return getProcedureName();
091: }
092:
093: public void setSource(CharSequence s) {
094: this .source = s;
095: }
096:
097: public CharSequence getSource() {
098: return this .source;
099: }
100:
101: public void setOraclePackage(boolean flag) {
102: this .isOraclePackage = true;
103: }
104:
105: public boolean isOraclePackage() {
106: return this .isOraclePackage;
107: }
108:
109: public String getCatalog() {
110: if (this .isOraclePackage)
111: return null;
112: return this .catalog;
113: }
114:
115: public String getSchema() {
116: return this .schema;
117: }
118:
119: public String getProcedureName() {
120: if (this .isOraclePackage)
121: return catalog;
122: return this .procName;
123: }
124:
125: public int getResultType() {
126: return this .resultType;
127: }
128:
129: public String getObjectType() {
130: if (this .isOraclePackage) {
131: return "PACKAGE";
132: }
133: if (resultType == DatabaseMetaData.procedureReturnsResult) {
134: return "FUNCTION";
135: } else if (resultType == DatabaseMetaData.procedureNoResult) {
136: return "PROCEDURE";
137: }
138:
139: return "";
140: }
141:
142: public String toString() {
143: return procName;
144: }
145:
146: }
|