Skip to content

Commit

Permalink
Support indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirisa committed Nov 4, 2024
1 parent 88a0db6 commit f99eda8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 94 deletions.
43 changes: 19 additions & 24 deletions src/main/java/com/dbschema/mongo/MongoDatabaseMetaData.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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",
});
}
Expand Down Expand Up @@ -258,23 +255,21 @@ public ResultSet getIndexInfo(String catalogName, String schemaName, String tabl
List<MetaCollection> 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",
});
}
}
}
Expand Down
60 changes: 15 additions & 45 deletions src/main/java/com/dbschema/mongo/resultSet/ListResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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.");
Expand All @@ -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 {
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/com/dbschema/mongo/resultSet/ResultSetIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/dbschema/mongo/schema/MetaCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/dbschema/mongo/schema/MetaIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class MetaIndex {

public final MetaJson metaMap;
public final String name;
public final List<MetaField> metaFields = new ArrayList<MetaField>();
public final List<MetaIndexField> metaIndexFields = new ArrayList<>();
public final boolean pk, unique;

public MetaIndex(MetaJson metaMap, String name, boolean pk, boolean unique) {
Expand All @@ -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);
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/dbschema/mongo/schema/MetaIndexField.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit f99eda8

Please sign in to comment.