Statement stmt = null;
ResultSet rs = null;
- if (databaseProductType == DatabaseProductType.MYSQL) {
- sql = "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS "
- + "WHERE TABLE_SCHEMA = '" + getDatabaseName(field) + "'"
+ try {
+ conn = JDBCTools.getConnection(dataSource);
+ stmt = conn.createStatement();
+ if (databaseProductType == DatabaseProductType.MYSQL) {
+ sql = "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS "
+ + "WHERE TABLE_SCHEMA = '" + JDBCTools.getDatabaseName(conn) + "'"
+ " AND TABLE_NAME = '" + getTableName(field) + "'"
+ " AND COLUMN_NAME = '" + field.getCol() + "'";
- } else if (databaseProductType == DatabaseProductType.POSTGRESQL) {
- sql = "SELECT data_type FROM information_schema.columns "
- + "WHERE table_catalog = '" + getDatabaseName(field) + "'"
+ } else if (databaseProductType == DatabaseProductType.POSTGRESQL) {
+ sql = "SELECT data_type FROM information_schema.columns "
+ + "WHERE table_catalog = '" + JDBCTools.getDatabaseName(conn) + "'"
+ " AND table_name = '" + getTableName(field) + "'"
+ " AND column_name = '" + field.getCol() + "'";
- }
-
- try {
- conn = JDBCTools.getConnection(dataSource);
- stmt = conn.createStatement();
+ }
rs = stmt.executeQuery(sql);
while (rs.next()) {
currentDatatype = rs.getString(1);
\r
//todo: make sure this will get instantiated in the right order\r
final static Logger logger = LoggerFactory.getLogger(JDBCTools.class);\r
+ private static String JDBC_URL_DATABASE_SEPARATOR = "\\/";\r
\r
public static DataSource getDataSource(String repositoryName) throws NamingException {\r
DataSource result = null;\r
private static String getDefaultRepositoryName() {\r
return DEFAULT_REPOSITORY_NAME;\r
}\r
+ \r
+ /**\r
+ * Returns the catalog name for an open JDBC connection.\r
+ * \r
+ * @param conn an open JDBC Connection\r
+ * @return the catalog name.\r
+ * @throws SQLException \r
+ */\r
+ public static String getDatabaseName(Connection conn) throws Exception {\r
+ String databaseName = "";\r
+ if (conn == null) {\r
+ return databaseName;\r
+ }\r
+ DatabaseMetaData metadata = conn.getMetaData();\r
+ String urlStr = metadata.getURL();\r
+ \r
+ // Format of the PostgreSQL JDBC URL:\r
+ // http://jdbc.postgresql.org/documentation/80/connect.html\r
+ if (getDatabaseProductType() == DatabaseProductType.POSTGRESQL) {\r
+ String tokens[] = urlStr.split(JDBC_URL_DATABASE_SEPARATOR);\r
+ databaseName = tokens[tokens.length - 1];\r
+ // Format of the MySQL JDBC URL:\r
+ // http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html\r
+ // FIXME: the last token could contain optional parameters, not accounted for here.\r
+ } else if (getDatabaseProductType() == DatabaseProductType.MYSQL) {\r
+ String tokens[] = urlStr.split(JDBC_URL_DATABASE_SEPARATOR);\r
+ databaseName = tokens[tokens.length - 1];\r
+ }\r
+ return databaseName;\r
+ }\r
\r
/**\r
* Prints metadata, such as database username and connection URL,\r