001: package org.mandarax.jdbc.server;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library 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 library 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:
021: import java.util.*;
022: import java.sql.*;
023: import org.apache.commons.collections.IteratorUtils;
024: import org.mandarax.jdbc.*;
025: import org.mandarax.jdbc.JDBCUtils;
026: import org.mandarax.kernel.KnowledgeBase;
027: import org.mandarax.kernel.Predicate;
028: import org.mandarax.lib.AbstractPredicate;
029: import org.mandarax.util.EqualsFilterIterator;
030:
031: /**
032: * Meta data implementation.
033: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
034: * @version 3.3.2 <29 December 2004>
035: * @since 3.0
036: */
037:
038: public class DatabaseMetaDataImpl extends AbstractDatabaseMetaDataImpl {
039: private KnowledgeBase kb = null;
040:
041: // private class to filter predicates by names (patterns)
042: class PredicateNameFilter implements
043: org.apache.commons.collections.Predicate {
044: private String pattern = null;
045:
046: PredicateNameFilter(String pattern) {
047: super ();
048: this .pattern = pattern;
049: }
050:
051: public boolean evaluate(Object obj) {
052: if (obj instanceof Predicate) {
053: Predicate p = (Predicate) obj;
054: // predicate names should not contain white spaces, and built-in predicates are not used !
055: return p.getName().indexOf(' ') == -1
056: && !(p instanceof AbstractPredicate)
057: && matcher.matches(pattern, p.getName());
058: }
059: return false;
060: }
061: }
062:
063: /**
064: * Constructor.
065: * @param url the database url
066: * @param connection the connection used to produce this object
067: * @param kb the knowledge base
068: */
069: DatabaseMetaDataImpl(String url, Connection connection,
070: KnowledgeBase kb) {
071: super (url, connection);
072: this .kb = kb;
073: }
074:
075: /**
076: * Constructor. Can be used to create an instance that provides static information (such as versioning information).
077: */
078: public DatabaseMetaDataImpl() {
079: this (null, null, null);
080: }
081:
082: /**
083: * Retrieves the user name as known to this database.
084: * @return java.lang.String
085: * @throws java.sql.SQLException
086: */
087: public String getUserName() throws SQLException {
088: return System.getProperty("user.name");
089: }
090:
091: /**
092: * Retrieves the name of this JDBC driver.
093: * @return JDBC driver name
094: * @throws java.sql.SQLException
095: */
096: public String getDriverName() throws SQLException {
097: return "mandarax jdbc local driver";
098:
099: }
100:
101: /**
102: * Retrieves this JDBC driver's major version number.
103: * @return JDBC driver major version number
104: */
105: public int getDriverMajorVersion() {
106: return 1;
107:
108: }
109:
110: /**
111: * Retrieves this JDBC driver's minor version number.
112: * @return JDBC driver minor version number
113: */
114: public int getDriverMinorVersion() {
115: return 0;
116:
117: }
118:
119: /**
120: * Retrieves whether this database stores tables in a local file.
121: * @return boolean
122: * @throws java.sql.SQLException
123: */
124: public boolean usesLocalFiles() throws SQLException {
125: return true;
126:
127: }
128:
129: /**
130: * Retrieves a description of table columns available in the specified catalog.
131: * @param catalog
132: * @param schemaPattern
133: * @param tableNamePattern
134: * @param columnNamePattern
135: * @return java.sql.ResultSet
136: * @throws java.sql.SQLException
137: */
138: public ResultSet getColumns(String catalog, String schemaPattern,
139: String tableNamePattern, String columnNamePattern)
140: throws SQLException {
141: String[] cols = { "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME",
142: "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME", "COLUMN_SIZE",
143: "BUFFER_LENGTH", "DECIMAL_DIGITS", "NUM_PREC_RADIX",
144: "NULLABLE", "REMARKS", "COLUMN_DEF", "SQL_DATA_TYPE",
145: "SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH",
146: "ORDINAL_POSITION", "IS_NULLABLE", "SCOPE_CATLOG",
147: "SCOPE_SCHEMA", "SCOPE_TABLE", "SOURCE_DATA_TYPE" };
148: int[] types = { VARCHAR, VARCHAR, VARCHAR, VARCHAR, SMALLINT,
149: VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR,
150: VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, SMALLINT,
151: VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR };
152: List colInfo = new ArrayList();
153: if (checkCatalogAndSchema(catalog, schemaPattern)) {
154: Iterator predicates = kb.predicates();
155: predicates = IteratorUtils.filteredIterator(predicates,
156: new PredicateNameFilter(tableNamePattern));
157: predicates = new EqualsFilterIterator(predicates);
158: while (predicates.hasNext()) {
159: Predicate p = (Predicate) predicates.next();
160: int size = p.getStructure().length;
161: // "real" columns
162: for (int i = 0; i < size; i++) {
163: String name = JDBC2KBUtils.getSlotName(p, i);
164: if (matcher.matches(columnNamePattern, name)) {
165: List info = new ArrayList();
166: info.add(DEFAULT_CATALOG);
167: info.add(DEFAULT_SCHEMA);
168: info.add(p.getName());
169: info.add(name);
170: info.add(new Integer(JDBCUtils.getTypeMapping(p
171: .getStructure()[i])));
172: info.add(JDBCUtils.getSQLTypeName(p
173: .getStructure()[i]));
174: info.add(null);
175: info.add(null);
176: info.add(null);
177: info.add(null);
178: info.add("NO");
179: info.add("Column for slot " + i
180: + " in predicate " + p);
181: info.add(null);
182: info.add(null);
183: info.add(null);
184: info.add(null);
185: info.add(new Integer(i + 1));
186: info.add("NO");
187: info.add(null);
188: info.add(null);
189: info.add(null);
190: info.add(null);
191: colInfo.add(info);
192: }
193: }
194: // pseudo columns
195: for (int i = 0; i < PseudoColumns.PSEUDO_COLUMNS.length; i++) {
196: String name = PseudoColumns.PSEUDO_COLUMNS[i];
197: if (matcher.matches(columnNamePattern, name)) {
198: Class type = PseudoColumns.PSEUDO_COLUMN_TYPES[i];
199: List info = new ArrayList();
200: info.add(DEFAULT_CATALOG);
201: info.add(DEFAULT_SCHEMA);
202: info.add(p.getName());
203: info.add(name);
204: info.add(new Integer(JDBCUtils
205: .getTypeMapping(type)));
206: info.add(JDBCUtils.getSQLTypeName(type));
207: info.add(null);
208: info.add(null);
209: info.add(null);
210: info.add(null);
211: info.add("NO");
212: info
213: .add("Automatically appended mandarax pseudo column ");
214: info.add(null);
215: info.add(null);
216: info.add(null);
217: info.add(null);
218: info.add(new Integer(size + i));
219: info.add("NO");
220: info.add(null);
221: info.add(null);
222: info.add(null);
223: info.add(null);
224: colInfo.add(info);
225: }
226: }
227: }
228: }
229: return new StaticResultSet(cols, colInfo, types);
230: }
231:
232: /**
233: * Retrieves a description of the tables available in the given catalog.
234: * @param catalog
235: * @param schemaPattern
236: * @param tableNamePattern
237: * @param types
238: * @return java.sql.ResultSet
239: * @throws java.sql.SQLException
240: */
241: public ResultSet getTables(String catalog, String schemaPattern,
242: String tableNamePattern, String[] types)
243: throws SQLException {
244: String[] cols = { "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME",
245: "TABLE_TYPE", "REMARKS", "TYPE_CAT", "TYPE_SCHEM",
246: "TYPE_NAME ", "SELF_REFERENCING_COL_NAME",
247: "REF_GENERATION" };
248: List tableInfo = new ArrayList();
249: if (checkCatalogAndSchema(catalog, schemaPattern)
250: && checkTableTypes(types)) {
251: Iterator predicates = kb.predicates();
252: predicates = IteratorUtils.filteredIterator(predicates,
253: new PredicateNameFilter(tableNamePattern));
254: predicates = new EqualsFilterIterator(predicates);
255: while (predicates.hasNext()) {
256: Predicate p = (Predicate) predicates.next();
257: List info = new ArrayList();
258: info.add(DEFAULT_CATALOG);
259: info.add(DEFAULT_SCHEMA);
260: info.add(p.getName());
261: info.add(PREDICATE_TABLE_TYPE);
262: info.add("Predicate " + p);
263: info.add(null);
264: info.add(null);
265: info.add(null);
266: info.add(null);
267: info.add(null);
268: tableInfo.add(info);
269: }
270: }
271: return new StaticResultSet(cols, tableInfo);
272: }
273:
274: }
|