01: /*
02: * FirstSqlMetadata.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db.firstsql;
13:
14: import java.sql.Connection;
15: import java.sql.PreparedStatement;
16: import java.sql.ResultSet;
17: import java.sql.SQLException;
18: import java.util.Collections;
19: import java.util.Map;
20: import workbench.db.ConstraintReader;
21: import workbench.db.TableIdentifier;
22: import workbench.log.LogMgr;
23: import workbench.util.SqlUtil;
24:
25: /**
26: * Metadata support for <a href="http://www.firstsql.com/">FirstSQL</a>
27: *
28: * @author support@sql-workbench.net
29: */
30: public class FirstSqlMetadata implements ConstraintReader {
31: private static final String SQL = "select ch.check_clause, ch.constraint_name \n"
32: + "from definition_schema.syschecks ch, \n"
33: + " definition_schema.sysconstraints cons \n"
34: + "where cons.constraint_type = 'check' \n"
35: + " and cons.constraint_name = ch.constraint_name"
36: + " and cons.table_schema = ? \n"
37: + " and cons.table_name = ? ";
38:
39: public FirstSqlMetadata() {
40: }
41:
42: public Map<String, String> getColumnConstraints(
43: Connection dbConnection, TableIdentifier aTable) {
44: return Collections.emptyMap();
45: }
46:
47: public String getTableConstraints(Connection dbConnection,
48: TableIdentifier aTable, String indent) throws SQLException {
49: PreparedStatement pstmt = null;
50: ResultSet rs = null;
51: StringBuilder result = new StringBuilder(200);
52: try {
53: pstmt = dbConnection.prepareStatement(SQL);
54: pstmt.setString(1, aTable.getSchema());
55: pstmt.setString(2, aTable.getTableName());
56: rs = pstmt.executeQuery();
57: int count = 0;
58: while (rs.next()) {
59: String constraint = rs.getString(1);
60: String name = rs.getString(2);
61:
62: if (count > 0) {
63: result.append('\n');
64: result.append(indent);
65: result.append(',');
66: }
67: if (name != null) {
68: result.append("CONSTRAINT ");
69: result.append(name);
70: result.append(' ');
71: }
72: result.append("CHECK (");
73: result.append(constraint);
74: result.append(')');
75: count++;
76: }
77: } catch (SQLException e) {
78: LogMgr.logError("FirstSqlMetadata.getTableConstraints()",
79: "Could not retrieve table constraints for "
80: + aTable.getTableExpression(), e);
81: throw e;
82: } finally {
83: SqlUtil.closeAll(rs, pstmt);
84: }
85: return result.toString();
86: }
87: }
|