001: /*
002: * GetMetaDataSql.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: /**
015: *
016: * @author support@sql-workbench.net
017: */
018: public class GetMetaDataSql {
019: private String baseSql;
020: private String schema;
021: private String schemaField;
022: private String catalog;
023: private String catalogField;
024: private String objectName;
025: private String objectNameField;
026: private String orderBy;
027: private boolean useUpperCase;
028: private boolean useLowerCase;
029: private boolean isProcedureCall;
030: private int schemaArgumentPos;
031: private int catalogArgumentPos;
032: private int objectNameArgumentPos;
033:
034: public GetMetaDataSql() {
035: }
036:
037: public String getSql() {
038: if (this .isProcedureCall)
039: return this .getProcedureCallSql();
040: else
041: return this .getSelectSql();
042: }
043:
044: private String getSelectSql() {
045: boolean needsAnd = false;
046: boolean needsWhere = false;
047: StringBuilder sql = new StringBuilder(baseSql);
048: if (baseSql.toLowerCase().indexOf("where") == -1) {
049: needsWhere = true;
050: } else {
051: needsAnd = true;
052: }
053:
054: if (schema != null && schemaField != null) {
055: if (needsWhere) {
056: sql.append(" WHERE ");
057: needsWhere = false;
058: }
059:
060: if (needsAnd)
061: sql.append(" AND ");
062: sql.append(schemaField + " = '" + getNameValue(schema)
063: + "'");
064: needsAnd = true;
065: }
066: if (catalog != null && catalogField != null) {
067: if (needsWhere) {
068: sql.append(" WHERE ");
069: needsWhere = false;
070: }
071: if (needsAnd)
072: sql.append(" AND ");
073: sql.append(catalogField + " = '" + getNameValue(catalog)
074: + "'");
075: needsAnd = true;
076: }
077: if (objectName != null && objectNameField != null) {
078: if (needsWhere) {
079: sql.append(" WHERE ");
080: needsWhere = false;
081: }
082: if (needsAnd)
083: sql.append(" AND ");
084: sql.append(objectNameField + " = '"
085: + getNameValue(objectName) + "'");
086: }
087: if (this .orderBy != null) {
088: sql.append(" " + this .orderBy);
089: }
090: return sql.toString();
091: }
092:
093: private String getNameValue(String value) {
094: if (value == null)
095: return null;
096: if (useLowerCase)
097: return value.toLowerCase();
098: if (useUpperCase)
099: return value.toUpperCase();
100: return value;
101: }
102:
103: private String getProcedureCallSql() {
104: StringBuilder sql = new StringBuilder(this .baseSql);
105: sql.append(' ');
106: for (int i = 1; i < 4; i++) {
107: if (schemaArgumentPos == i && this .schema != null) {
108: if (i > 1)
109: sql.append(',');
110: sql.append(this .schema);
111: } else if (catalogArgumentPos == i && this .catalog != null) {
112: if (i > 1)
113: sql.append(',');
114: sql.append(this .catalog);
115: } else if (this .objectNameArgumentPos == i
116: && this .objectName != null) {
117: if (i > 1)
118: sql.append(',');
119: sql.append(this .objectName);
120: }
121: }
122: return sql.toString();
123: }
124:
125: public String getBaseSql() {
126: return baseSql;
127: }
128:
129: public void setBaseSql(String sql) {
130: this .baseSql = sql;
131: }
132:
133: public String getSchema() {
134: return schema;
135: }
136:
137: public void setSchema(String schem) {
138: this .schema = schem;
139: }
140:
141: public String getCatalog() {
142: return catalog;
143: }
144:
145: public void setCatalog(String cat) {
146: this .catalog = cat;
147: }
148:
149: public String getObjectName() {
150: return objectName;
151: }
152:
153: public void setObjectName(String name) {
154: this .objectName = name;
155: }
156:
157: public String toString() {
158: return getSql();
159: }
160:
161: public String getSchemaField() {
162: return schemaField;
163: }
164:
165: public void setSchemaField(String field) {
166: this .schemaField = field;
167: }
168:
169: public String getCatalogField() {
170: return catalogField;
171: }
172:
173: public void setCatalogField(String field) {
174: this .catalogField = field;
175: }
176:
177: public String getObjectNameField() {
178: return objectNameField;
179: }
180:
181: public void setObjectNameField(String field) {
182: this .objectNameField = field;
183: }
184:
185: public String getOrderBy() {
186: return orderBy;
187: }
188:
189: public void setOrderBy(String order) {
190: this .orderBy = order;
191: }
192:
193: public boolean getUseUpperCase() {
194: return useUpperCase;
195: }
196:
197: public void setUseUpperCase(boolean upperCase) {
198: this .useUpperCase = upperCase;
199: }
200:
201: public boolean getUseLowerCase() {
202: return useLowerCase;
203: }
204:
205: public void setUseLowerCase(boolean lowerCase) {
206: this .useLowerCase = lowerCase;
207: }
208:
209: public boolean isIsProcedureCall() {
210: return isProcedureCall;
211: }
212:
213: public void setIsProcedureCall(boolean isCall) {
214: this .isProcedureCall = isCall;
215: }
216:
217: public int getSchemaArgumentPos() {
218: return schemaArgumentPos;
219: }
220:
221: public void setSchemaArgumentPos(int pos) {
222: this .schemaArgumentPos = pos;
223: }
224:
225: public int getCatalogArgumentPos() {
226: return catalogArgumentPos;
227: }
228:
229: public void setCatalogArgumentPos(int pos) {
230: this .catalogArgumentPos = pos;
231: }
232:
233: public int getObjectNameArgumentPos() {
234: return objectNameArgumentPos;
235: }
236:
237: public void setObjectNameArgumentPos(int pos) {
238: this.objectNameArgumentPos = pos;
239: }
240: }
|