001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.model.utils;
042:
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.regex.Pattern;
046:
047: import org.netbeans.modules.sql.framework.model.ColumnRef;
048: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
049: import org.netbeans.modules.sql.framework.model.RuntimeInput;
050: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
051: import org.netbeans.modules.sql.framework.model.SQLDBTable;
052: import org.netbeans.modules.sql.framework.model.SQLDefinition;
053: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
054: import org.netbeans.modules.sql.framework.model.SQLObject;
055:
056: import com.sun.sql.framework.exception.BaseException;
057: import org.netbeans.modules.sql.framework.model.DatabaseModel;
058:
059: /**
060: * @author Ritesh Adval
061: */
062: public class SQLParserUtil {
063:
064: Pattern p1;
065: Pattern p2;
066: Pattern p3;
067: Pattern p4;
068: private SQLDefinition def;
069:
070: public SQLParserUtil() {
071: p1 = Pattern.compile(".*\\..*");
072: p2 = Pattern.compile(".*\\..*\\..*");
073: p3 = Pattern.compile("\\.");
074: p4 = Pattern.compile(".*\\..*\\..*\\..*");
075: }
076:
077: /** Creates a new instance of SQLParserUtil */
078: public SQLParserUtil(SQLDefinition sqlDef) {
079: this ();
080: this .def = sqlDef;
081: }
082:
083: /**
084: * this method is called when column name is specified as TABLE_NAME.COLUMN_NAME, this
085: * method first column it finds from source or target tables. For accurate result user
086: * should type fully qualified column name.
087: */
088: public SQLObject getColumn(String fullName) throws BaseException {
089: SQLDBColumn column = null;
090: ColumnRef columnRef = null;
091: String tableName = getTableName(fullName);
092: String columnName = getColumnName(fullName);
093:
094: if (tableName != null && columnName != null && def != null) {
095: SQLDBTable table = getTable(def.getSourceTables(),
096: tableName);
097: if (table != null) {
098: column = getColumn(table.getColumnList(), columnName);
099: }
100:
101: // if column is still null check in target tables
102: if (column == null) {
103: table = getTable(def.getTargetTables(), tableName);
104: if (table != null) {
105: column = getColumn(table.getColumnList(),
106: columnName);
107: }
108: }
109: }
110:
111: // if column is not null create a ColumnRefImpl and return
112: if (column != null) {
113: columnRef = SQLModelObjectFactory.getInstance()
114: .createColumnRef(column);
115: } else {
116: throw new BaseException("Column \"" + fullName
117: + "\"is not available in collaboration \""
118: + def.getDisplayName() + "\"");
119: }
120:
121: return columnRef;
122: }
123:
124: /**
125: * This method will return a column when user types a fully qualified column name. for
126: * oracle fully qualified name is SCHEMA_NAME.TABLE.NAME.COLUMN.NAME for sql server it
127: * is CATALOG_NAME.SCHEMA_NAME.TABLE.NAME.COLUMN.NAME
128: */
129: public SQLObject getColumnForFullyQualifiedName(String fullName)
130: throws BaseException {
131: SQLDBColumn column = null;
132: ColumnRef columnRef = null;
133:
134: String tableName = getFullyQualifiedTableName(fullName);
135: String columnName = getColumnName(fullName);
136:
137: if (tableName != null && columnName != null) {
138: SQLDBTable table = getTableForFullyQualifiedName(def
139: .getSourceTables(), tableName);
140: if (table != null) {
141: column = getColumn(table.getColumnList(), columnName);
142: }
143:
144: // if column is still null check in target tables
145: if (column == null) {
146: table = getTableForFullyQualifiedName(def
147: .getTargetTables(), tableName);
148: if (table != null) {
149: column = getColumn(table.getColumnList(),
150: columnName);
151: }
152: }
153: }
154:
155: // if column is not null create a ColumnRefImpl and return
156: if (column != null) {
157: columnRef = SQLModelObjectFactory.getInstance()
158: .createColumnRef(column);
159: } else {
160: throw new BaseException("Column \"" + fullName
161: + "\"is not available in collaboration \""
162: + def.getDisplayName() + "\"");
163: }
164:
165: return columnRef;
166:
167: }
168:
169: public SQLObject getRuntimeInput(String argName)
170: throws BaseException {
171: SQLDBColumn column = null;
172: ColumnRef columnRef = null;
173: String argumentName = argName;
174:
175: RuntimeDatabaseModel runDb = def.getRuntimeDbModel();
176: if (runDb != null) {
177: RuntimeInput rInput = runDb.getRuntimeInput();
178:
179: if (rInput != null) {
180: int idx = argName.indexOf('$');
181: if (idx != -1 && idx + 1 < argName.length()) {
182: argumentName = argName.substring(idx + 1);
183: column = (SQLDBColumn) rInput
184: .getColumn(argumentName);
185: }
186: }
187: }
188:
189: if (column != null) {
190: columnRef = SQLModelObjectFactory.getInstance()
191: .createColumnRef(column);
192: } else {
193: throw new BaseException("Runtime input argument \""
194: + argumentName
195: + "\"is not defined in collaboration \""
196: + def.getDisplayName() + "\"");
197: }
198:
199: return columnRef;
200: }
201:
202: private SQLDBColumn getColumn(List columns, String name) {
203: Iterator it = columns.iterator();
204: while (it.hasNext()) {
205: SQLDBColumn column = (SQLDBColumn) it.next();
206: if (column.getName().equalsIgnoreCase(name)) {
207: return column;
208: }
209: }
210: return null;
211: }
212:
213: private String getColumnName(String fullName) {
214: // last part will be column name
215: int dotIdx = fullName.lastIndexOf('.');
216: if (dotIdx != -1) {
217: return fullName.substring(dotIdx + 1, fullName.length());
218: }
219:
220: return null;
221: }
222:
223: private String getFullyQualifiedTableName(String fullName) {
224: // last part will be column name
225: // and last -1 will be table name
226: int dotIdx = fullName.lastIndexOf('.');
227: if (dotIdx != -1) {
228: return fullName.substring(0, dotIdx);
229: }
230:
231: return null;
232: }
233:
234: private SQLDBTable getTable(List tables, String name) {
235: Iterator it = tables.iterator();
236:
237: while (it.hasNext()) {
238: SQLDBTable table = (SQLDBTable) it.next();
239: if (table.getName().equalsIgnoreCase(name)
240: || (table.getAliasName() != null && table
241: .getAliasName().equalsIgnoreCase(name))) {
242: return table;
243: }
244: }
245:
246: return null;
247: }
248:
249: private SQLDBTable getTableForFullyQualifiedName(List tables,
250: String fullName) {
251: Iterator it = tables.iterator();
252:
253: while (it.hasNext()) {
254: SQLDBTable table = (SQLDBTable) it.next();
255: DatabaseModel dbModel = table.getParent();
256: if (dbModel != null) {
257: String tableFName = table.getFullyQualifiedName();
258: if (tableFName != null
259: && tableFName.equalsIgnoreCase(fullName)) {
260: return table;
261: }
262: }
263: }
264:
265: return null;
266: }
267:
268: private String getTableName(String fullName) {
269: // last part will be column name
270: // and last -1 will be table name
271: int dotIdx = fullName.lastIndexOf('.');
272: if (dotIdx != -1) {
273: return fullName.substring(0, dotIdx);
274: }
275:
276: return null;
277: }
278:
279: }
|