diff --git a/src/main/java/com/dbschema/mongo/MongoDatabaseMetaData.java b/src/main/java/com/dbschema/mongo/MongoDatabaseMetaData.java index f6fa69e..498c7a0 100644 --- a/src/main/java/com/dbschema/mongo/MongoDatabaseMetaData.java +++ b/src/main/java/com/dbschema/mongo/MongoDatabaseMetaData.java @@ -1,10 +1,7 @@ package com.dbschema.mongo; import com.dbschema.mongo.resultSet.ListResultSet; -import com.dbschema.mongo.schema.MetaCollection; -import com.dbschema.mongo.schema.MetaField; -import com.dbschema.mongo.schema.MetaIndex; -import com.dbschema.mongo.schema.MetaJson; +import com.dbschema.mongo.schema.*; import org.jetbrains.annotations.NotNull; import java.sql.*; @@ -192,13 +189,13 @@ public ResultSet getPrimaryKeys(String catalogName, String schemaName, String ta for (MetaCollection collection : collections) { for (MetaIndex index : collection.metaIndexes) { if (index.pk) { - for (MetaField field : index.metaFields) { + for (MetaIndexField indexField : index.metaIndexFields) { result.addRow(new String[]{ collection.name, // "TABLE_CAT", null, // "TABLE_SCHEMA", collection.name, // "TABLE_NAME", (i.e. MongoDB Collection Name) - field.getNameWithPath(), // "COLUMN_NAME", - "" + index.metaFields.indexOf(field), // "ORDINAL_POSITION" + indexField.getNameWithPath(), // "COLUMN_NAME", + "" + index.metaIndexFields.indexOf(indexField), // "ORDINAL_POSITION" index.name // "INDEX_NAME", }); } @@ -258,23 +255,21 @@ public ResultSet getIndexInfo(String catalogName, String schemaName, String tabl List collections = con.getService().getMetaCollections(schemaName, tableNamePattern); for (MetaCollection collection : collections) { for (MetaIndex index : collection.metaIndexes) { - if (!index.pk) { - for (MetaField field : index.metaFields) { - result.addRow(new String[]{collection.name, // "TABLE_CAT", - null, // "TABLE_SCHEMA", - collection.name, // "TABLE_NAME", (i.e. MongoDB Collection Name) - "YES", // "NON-UNIQUE", - collection.name, // "INDEX QUALIFIER", - index.name, // "INDEX_NAME", - "0", // "TYPE", - "" + index.metaFields.indexOf(field), // "ORDINAL_POSITION" - field.getNameWithPath(), // "COLUMN_NAME", - "A", // "ASC_OR_DESC", - "0", // "CARDINALITY", - "0", // "PAGES", - "" // "FILTER_CONDITION", - }); - } + for (MetaIndexField indexField : index.metaIndexFields) { + result.addRow(new String[]{collection.name, // "TABLE_CAT", + null, // "TABLE_SCHEMA", + collection.name, // "TABLE_NAME", (i.e. MongoDB Collection Name) + index.pk || index.unique ? "NO" : "YES", // "NON-UNIQUE", + collection.name, // "INDEX QUALIFIER", + index.name, // "INDEX_NAME", + "" + DatabaseMetaData.tableIndexOther, // "TYPE", + "" + index.metaIndexFields.indexOf(indexField), // "ORDINAL_POSITION" + indexField.getNameWithPath(), // "COLUMN_NAME" + indexField.ascOrDesc == 1 ? "A" : indexField.ascOrDesc == -1 ? "D" : null, // "ASC_OR_DESC", + "0", // "CARDINALITY", + "0", // "PAGES", + "" // "FILTER_CONDITION", + }); } } } diff --git a/src/main/java/com/dbschema/mongo/resultSet/ListResultSet.java b/src/main/java/com/dbschema/mongo/resultSet/ListResultSet.java index 1744d19..5490cce 100644 --- a/src/main/java/com/dbschema/mongo/resultSet/ListResultSet.java +++ b/src/main/java/com/dbschema/mongo/resultSet/ListResultSet.java @@ -102,48 +102,27 @@ public boolean getBoolean(int columnIndex) throws SQLException { return Boolean.parseBoolean(getString(columnIndex)); } - public byte getByte(int columnIndex) { - - return 0; + public byte getByte(int columnIndex) throws SQLException { + return Byte.parseByte(getString(columnIndex)); } - /** - * @see java.sql.ResultSet#getShort(int) - */ public short getShort(int columnIndex) throws SQLException { - checkClosed(); return Short.parseShort(getString(columnIndex)); } - /** - * @see java.sql.ResultSet#getInt(int) - */ public int getInt(int columnIndex) throws SQLException { - checkClosed(); return Integer.parseInt(getString(columnIndex)); } - /** - * @see java.sql.ResultSet#getLong(int) - */ public long getLong(int columnIndex) throws SQLException { - checkClosed(); return Long.parseLong(getString(columnIndex)); } - /** - * @see java.sql.ResultSet#getFloat(int) - */ public float getFloat(int columnIndex) throws SQLException { - checkClosed(); return Float.parseFloat(getString(columnIndex)); } - /** - * @see java.sql.ResultSet#getDouble(int) - */ public double getDouble(int columnIndex) throws SQLException { - checkClosed(); return Double.parseDouble(getString(columnIndex)); } @@ -152,9 +131,8 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) { return null; } - public byte[] getBytes(int columnIndex) { - - return null; + public byte[] getBytes(int columnIndex) throws SQLException { + return getString(columnIndex).getBytes(); } public Date getDate(int columnIndex) { @@ -188,7 +166,6 @@ public InputStream getBinaryStream(int columnIndex) { } public String getString(String columnLabel) throws SQLException { - checkClosed(); int index = -1; if (columnNames == null) { throw new SQLException("Use of columnLabel requires setColumnNames to be called first."); @@ -206,34 +183,27 @@ public String getString(String columnLabel) throws SQLException { } public boolean getBoolean(String columnLabel) throws SQLException { - checkClosed(); - - return false; + return Boolean.parseBoolean(getString(columnLabel)); } - public byte getByte(String columnLabel) { - - return 0; + public byte getByte(String columnLabel) throws SQLException { + return Byte.parseByte(getString(columnLabel)); } - public short getShort(String columnLabel) { - - return 0; + public short getShort(String columnLabel) throws SQLException { + return Short.parseShort(getString(columnLabel)); } - public int getInt(String columnLabel) { - - return 0; + public int getInt(String columnLabel) throws SQLException { + return Integer.parseInt(getString(columnLabel)); } - public long getLong(String columnLabel) { - - return 0; + public long getLong(String columnLabel) throws SQLException { + return Long.parseLong(getString(columnLabel)); } - public float getFloat(String columnLabel) { - - return 0; + public float getFloat(String columnLabel) throws SQLException { + return Float.parseFloat(getString(columnLabel)); } public double getDouble(String columnLabel) throws SQLException { diff --git a/src/main/java/com/dbschema/mongo/resultSet/ResultSetIterator.java b/src/main/java/com/dbschema/mongo/resultSet/ResultSetIterator.java index c7d8260..178f7cc 100644 --- a/src/main/java/com/dbschema/mongo/resultSet/ResultSetIterator.java +++ b/src/main/java/com/dbschema/mongo/resultSet/ResultSetIterator.java @@ -92,38 +92,38 @@ public String getString(int columnIndex) throws SQLException { } @Override - public boolean getBoolean(int columnIndex) { - return false; + public boolean getBoolean(int columnIndex) throws SQLException { + return Boolean.parseBoolean(getString(columnIndex)); } @Override - public byte getByte(int columnIndex) { - return 0; + public byte getByte(int columnIndex) throws SQLException { + return Byte.parseByte(getString(columnIndex)); } @Override - public short getShort(int columnIndex) { - return 0; + public short getShort(int columnIndex) throws SQLException { + return Short.parseShort(getString(columnIndex)); } @Override - public int getInt(int columnIndex) { - return 0; + public int getInt(int columnIndex) throws SQLException { + return Integer.parseInt(getString(columnIndex)); } @Override - public long getLong(int columnIndex) { - return 0; + public long getLong(int columnIndex) throws SQLException { + return Long.parseLong(getString(columnIndex)); } @Override - public float getFloat(int columnIndex) { - return 0; + public float getFloat(int columnIndex) throws SQLException { + return Float.parseFloat(getString(columnIndex)); } @Override - public double getDouble(int columnIndex) { - return 0; + public double getDouble(int columnIndex) throws SQLException { + return Double.parseDouble(getString(columnIndex)); } @Override @@ -132,8 +132,8 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) { } @Override - public byte[] getBytes(int columnIndex) { - return new byte[0]; + public byte[] getBytes(int columnIndex) throws SQLException { + return getString(columnIndex).getBytes(); } @Override diff --git a/src/main/java/com/dbschema/mongo/schema/MetaCollection.java b/src/main/java/com/dbschema/mongo/schema/MetaCollection.java index abc5c1a..5b2a67a 100644 --- a/src/main/java/com/dbschema/mongo/schema/MetaCollection.java +++ b/src/main/java/com/dbschema/mongo/schema/MetaCollection.java @@ -28,7 +28,7 @@ public MetaCollection(final MongoCollection mongoCollection, final int fetchD } public MetaIndex createMetaIndex(String name, boolean pk, boolean unique) { - MetaIndex index = new MetaIndex(this, name, "_id_".endsWith(name), false); + MetaIndex index = new MetaIndex(this, name, pk, unique); metaIndexes.add(index); return index; } @@ -133,13 +133,13 @@ private void discoverIndexes(MongoCollection dbCollection) { if (columnsObj instanceof Map) { final Map columnsMap = (Map) columnsObj; MetaIndex metaIndex = createMetaIndex(indexName, indexIsPk, indexIsUnique); - for (Object fieldNameObj : columnsMap.keySet()) { - final MetaField metaField = findField((String) fieldNameObj); + for (Map.Entry fieldEntry : columnsMap.entrySet()) { + final MetaField metaField = findField((String) fieldEntry.getKey()); if (metaField != null) { - metaIndex.addColumn(metaField); + metaIndex.addColumn(new MetaIndexField(metaField, (Integer) fieldEntry.getValue())); } else { - System.err.println("MongoJDBC discover index cannot find metaField '" + fieldNameObj + "' for index " + indexObject); + System.err.println("MongoJDBC discover index cannot find metaField '" + fieldEntry.getKey() + "' for index " + indexObject); } } } diff --git a/src/main/java/com/dbschema/mongo/schema/MetaIndex.java b/src/main/java/com/dbschema/mongo/schema/MetaIndex.java index 7f13eed..a7c33f8 100644 --- a/src/main/java/com/dbschema/mongo/schema/MetaIndex.java +++ b/src/main/java/com/dbschema/mongo/schema/MetaIndex.java @@ -8,7 +8,7 @@ public class MetaIndex { public final MetaJson metaMap; public final String name; - public final List metaFields = new ArrayList(); + public final List metaIndexFields = new ArrayList<>(); public final boolean pk, unique; public MetaIndex(MetaJson metaMap, String name, boolean pk, boolean unique) { @@ -18,9 +18,9 @@ public MetaIndex(MetaJson metaMap, String name, boolean pk, boolean unique) { this.unique = unique; } - public void addColumn(MetaField metaField) { - if (metaField != null) { - metaFields.add(metaField); + public void addColumn(MetaIndexField metaIndexField) { + if (metaIndexField != null) { + metaIndexFields.add(metaIndexField); } } } diff --git a/src/main/java/com/dbschema/mongo/schema/MetaIndexField.java b/src/main/java/com/dbschema/mongo/schema/MetaIndexField.java new file mode 100644 index 0000000..7db8eb2 --- /dev/null +++ b/src/main/java/com/dbschema/mongo/schema/MetaIndexField.java @@ -0,0 +1,23 @@ +package com.dbschema.mongo.schema; + + +public class MetaIndexField { + + public final MetaField metaField; + public final int ascOrDesc; + + + public MetaIndexField(final MetaField metaField, final int ascOrDesc) { + this.metaField = metaField; + this.ascOrDesc = ascOrDesc; + } + + public String getNameWithPath() { + return metaField.getNameWithPath(); + } + + @Override + public String toString() { + return metaField.toString() + ": " + ascOrDesc; + } +}