Skip to content

Commit

Permalink
fixes column names metadata
Browse files Browse the repository at this point in the history
fixes indexes metadata

refs #7126
  • Loading branch information
robfrank committed Feb 6, 2017
1 parent 222a65b commit fe26586
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.metadata.OMetadata;
import com.orientechnologies.orient.core.metadata.function.OFunction;
import com.orientechnologies.orient.core.metadata.function.OFunctionLibrary;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OClass.INDEX_TYPE;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;

Expand Down Expand Up @@ -623,16 +625,21 @@ public ResultSet getProcedures(String catalog, String schemaPattern, String proc
database.activateOnCurrentThread();
final List<ODocument> records = new ArrayList<ODocument>();

for (String fName : database.getMetadata().getFunctionLibrary().getFunctionNames()) {
final ODocument doc = new ODocument()
.field("PROCEDURE_CAT", (Object) null)
.field("PROCEDURE_SCHEM", (Object) null)
.field("PROCEDURE_NAME", fName)
.field("REMARKS", "")
.field("PROCEDURE_TYPE", procedureResultUnknown)
.field("SPECIFIC_NAME", fName);
OFunctionLibrary functionLibrary = database.getMetadata().getFunctionLibrary();

records.add(doc);
for (String functionName : functionLibrary.getFunctionNames()) {

if (OrientJdbcUtils.like(functionName, procedureNamePattern)) {
final ODocument doc = new ODocument()
.field("PROCEDURE_CAT", (Object) null)
.field("PROCEDURE_SCHEM", (Object) null)
.field("PROCEDURE_NAME", functionName)
.field("REMARKS", "")
.field("PROCEDURE_TYPE", procedureResultUnknown)
.field("SPECIFIC_NAME", functionName);

records.add(doc);
}
}

return new OrientJdbcResultSet(new OrientJdbcStatement(connection), records, ResultSet.TYPE_FORWARD_ONLY,
Expand All @@ -644,31 +651,39 @@ public ResultSet getProcedureColumns(String catalog, String schemaPattern, Strin
database.activateOnCurrentThread();
final List<ODocument> records = new ArrayList<ODocument>();

final OFunction f = database.getMetadata().getFunctionLibrary().getFunction(procedureNamePattern);
OFunctionLibrary functionLibrary = database.getMetadata().getFunctionLibrary();

for (String p : f.getParameters()) {
final ODocument doc = new ODocument()
.field("PROCEDURE_CAT", (Object) null)
.field("PROCEDURE_SCHEM", (Object) null)
.field("PROCEDURE_NAME", f.getName())
.field("COLUMN_NAME", p)
.field("COLUMN_TYPE", procedureColumnIn)
.field("DATA_TYPE", java.sql.Types.OTHER)
.field("SPECIFIC_NAME", f.getName());
for (String functionName : functionLibrary.getFunctionNames()) {

records.add(doc);
}
if (OrientJdbcUtils.like(functionName, procedureNamePattern)) {

final ODocument doc = new ODocument()
.field("PROCEDURE_CAT", (Object) null)
.field("PROCEDURE_SCHEM", (Object) null)
.field("PROCEDURE_NAME", f.getName())
.field("COLUMN_NAME", "return")
.field("COLUMN_TYPE", procedureColumnReturn)
.field("DATA_TYPE", java.sql.Types.OTHER)
.field("SPECIFIC_NAME", f.getName());
final OFunction f = functionLibrary.getFunction(procedureNamePattern);

records.add(doc);
for (String p : f.getParameters()) {
final ODocument doc = new ODocument()
.field("PROCEDURE_CAT", database.getName())
.field("PROCEDURE_SCHEM", database.getName())
.field("PROCEDURE_NAME", f.getName())
.field("COLUMN_NAME", p)
.field("COLUMN_TYPE", procedureColumnIn)
.field("DATA_TYPE", java.sql.Types.OTHER)
.field("SPECIFIC_NAME", f.getName());

records.add(doc);
}

final ODocument doc = new ODocument()
.field("PROCEDURE_CAT", database.getName())
.field("PROCEDURE_SCHEM", database.getName())
.field("PROCEDURE_NAME", f.getName())
.field("COLUMN_NAME", "return")
.field("COLUMN_TYPE", procedureColumnReturn)
.field("DATA_TYPE", java.sql.Types.OTHER)
.field("SPECIFIC_NAME", f.getName());

records.add(doc);
}
}

return new OrientJdbcResultSet(new OrientJdbcStatement(connection), records, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
Expand All @@ -691,7 +706,10 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
type = "TABLE";

if (tableTypes.contains(type) &&
(tableNamePattern == null || tableNamePattern.equals("%") || tableNamePattern.equalsIgnoreCase(className))) {
(tableNamePattern == null ||
tableNamePattern.equals("%") ||
tableNamePattern.equalsIgnoreCase(className))) {

final ODocument doc = new ODocument()
.field("TABLE_CAT", database.getName())
.field("TABLE_SCHEM", database.getName())
Expand Down Expand Up @@ -742,21 +760,27 @@ public ResultSet getTableTypes() throws SQLException {
ResultSet.HOLD_CURSORS_OVER_COMMIT);
}

@Override
public ResultSet getColumns(final String catalog, final String schemaPattern, final String tableNamePattern,
final String columnNamePattern) throws SQLException {
database.activateOnCurrentThread();

final List<ODocument> records = new ArrayList<ODocument>();
final OClass clazz = database.getMetadata().getSchema().getClass(tableNamePattern);
if (clazz != null) {
if (columnNamePattern == null) {

OSchema schema = database.getMetadata().getSchema();

for (OClass clazz : schema.getClasses()) {
if (OrientJdbcUtils.like(clazz.getName(), tableNamePattern)) {
for (OProperty prop : clazz.properties()) {
records.add(getPropertyAsDocument(clazz, prop));
}
} else {
final OProperty prop = clazz.getProperty(columnNamePattern);
if (prop != null) {
records.add(getPropertyAsDocument(clazz, prop));
if (columnNamePattern == null) {
records.add(getPropertyAsDocument(clazz, prop));
} else {
if (OrientJdbcUtils.like(prop.getName(), columnNamePattern)) {
records.add(getPropertyAsDocument(clazz, prop));
}
}
}

}
}
return new OrientJdbcResultSet(new OrientJdbcStatement(connection), records, ResultSet.TYPE_FORWARD_ONLY,
Expand Down Expand Up @@ -785,6 +809,7 @@ public ResultSet getVersionColumns(String catalog, String schema, String table)
return null;
}

@Override
public ResultSet getPrimaryKeys(final String catalog, final String schema, final String table) throws SQLException {
database.activateOnCurrentThread();
final Set<OIndex<?>> classIndexes = database.getMetadata().getIndexManager().getClassIndexes(table);
Expand Down Expand Up @@ -922,6 +947,7 @@ public ResultSet getTypeInfo() throws SQLException {
ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}

@Override
public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate)
throws SQLException {
database.activateOnCurrentThread();
Expand Down Expand Up @@ -949,7 +975,7 @@ public ResultSet getIndexInfo(String catalog, String schema, String table, boole
.field("TABLE_CAT", catalog)
.field("TABLE_SCHEM", schema)
.field("TABLE_NAME", table)
.field("COLUMN_NAME", fieldNames.substring(1, fieldNames.length() - 2))
.field("COLUMN_NAME", fieldNames.substring(1, fieldNames.length() - 1))
.field("NON_UNIQUE", notUniqueIndex)
.field("INDEX_NAME", idx.getName())
.field("ASC_OR_DESC", "ASC");
Expand Down Expand Up @@ -1247,11 +1273,50 @@ private ODocument getPropertyAsDocument(final OClass clazz, final OProperty prop
database.activateOnCurrentThread();
final OType type = prop.getType();
return new ODocument()
.field("TABLE_CAT", database.getName()).field("TABLE_NAME", clazz.getName())
.field("COLUMN_NAME", prop.getName()).field("DATA_TYPE", OrientJdbcResultSetMetaData.getSqlType(type))
.field("TYPE_NAME", type.name()).field("COLUMN_SIZE", 1)
.field("TABLE_CAT", database.getName())
.field("TABLE_SCHEM", database.getName())
.field("TABLE_NAME", clazz.getName())
.field("COLUMN_NAME", prop.getName())
.field("DATA_TYPE", OrientJdbcResultSetMetaData.getSqlType(type))
.field("TYPE_NAME", type.name())
.field("COLUMN_SIZE", 1)
.field("BUFFER_LENGTH", null, OType.INTEGER)
.field("DECIMAL_DIGITS", null, OType.INTEGER)
.field("NUM_PREC_RADIX", 10)
.field("NULLABLE", !prop.isNotNull() ? columnNoNulls : columnNullable)
.field("REMARKS", prop.getDescription())
.field("COLUMN_DEF", prop.getDefaultValue())
.field("SQL_DATA_TYPE", null, OType.INTEGER)
.field("SQL_DATETIME_SUB", null, OType.INTEGER)
.field("CHAR_OCTET_LENGTH", null, OType.INTEGER)
.field("ORDINAL_POSITION", prop.getId(), OType.INTEGER)
.field("IS_NULLABLE", prop.isNotNull() ? "NO" : "YES");

//
// * <LI><B>SCOPE_CATALOG</B> String {@code =>} catalog of table that is the scope
// * of a reference attribute (<code>null</code> if DATA_TYPE isn't REF)
// * <LI><B>SCOPE_SCHEMA</B> String {@code =>} schema of table that is the scope
// * of a reference attribute (<code>null</code> if the DATA_TYPE isn't REF)
// * <LI><B>SCOPE_TABLE</B> String {@code =>} table name that this the scope
// * of a reference attribute (<code>null</code> if the DATA_TYPE isn't REF)
// * <LI><B>SOURCE_DATA_TYPE</B> short {@code =>} source type of a distinct type or user-generated
// * Ref type, SQL type from java.sql.Types (<code>null</code> if DATA_TYPE
// * isn't DISTINCT or user-generated REF)
// * <LI><B>IS_AUTOINCREMENT</B> String {@code =>} Indicates whether this column is auto incremented
// * <UL>
// * <LI> YES --- if the column is auto incremented
// * <LI> NO --- if the column is not auto incremented
// * <LI> empty string --- if it cannot be determined whether the column is auto incremented
// * </UL>
// * <LI><B>IS_GENERATEDCOLUMN</B> String {@code =>} Indicates whether this is a generated column
// * <UL>
// * <LI> YES --- if this a generated column
// * <LI> NO --- if this not a generated column
// * <LI> empty string --- if it cannot be determined whether this is a generated column
// * </UL>
// * </OL>
//

}

public <T> T unwrap(Class<T> iface) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information: http://www.orientechnologies.com
*/
package com.orientechnologies.orient.jdbc;

import java.util.regex.Pattern;

/**
* Created by frank on 06/02/2017.
*/
public class OrientJdbcUtils {


public static boolean like(final String str, final String expr) {
String regex = quotemeta(expr);
regex = regex.replace("_", ".").replace("%", ".*?");
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
return p.matcher(str).matches();
}

public static String quotemeta(String s) {
if (s == null) {
throw new IllegalArgumentException("String cannot be null");
}

int len = s.length();
if (len == 0) {
return "";
}

StringBuilder sb = new StringBuilder(len * 2);
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if ("[](){}.*+?$^|#\\".indexOf(c) != -1) {
sb.append("\\");
}
sb.append(c);
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.orientechnologies.orient.jdbc;

import com.orientechnologies.orient.core.OConstants;
import com.orientechnologies.orient.core.metadata.schema.OType;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -179,7 +180,7 @@ public void getSingleTable() throws SQLException {
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {

System.out.println(
rs.getMetaData().getColumnName(i));
rs.getMetaData().getColumnName(i));
}
assertThat(rs.getString("TABLE_NAME")).isEqualTo("OUser");
assertThat(rs.getString("TABLE_CAT")).isEqualTo("OrientJdbcDatabaseMetaDataTest");
Expand All @@ -199,6 +200,8 @@ public void shouldGetSingleColumnOfArticle() throws SQLException {

assertThat(rs.getString("TABLE_NAME")).isEqualTo("Article");
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("uuid");
assertThat(rs.getString("TYPE_NAME")).isEqualTo("INTEGER");
assertThat(rs.getInt("DATA_TYPE")).isEqualTo(4);

assertThat(rs.next()).isFalse();
}
Expand All @@ -207,19 +210,37 @@ public void shouldGetSingleColumnOfArticle() throws SQLException {
public void shouldGetAllColumnsOfArticle() throws SQLException {
ResultSet rs = metaData.getColumns(null, null, "Article", null);

assertThat(sizeOf(rs)).isEqualTo(5);
while (rs.next()) {
assertThat(rs.getString("TABLE_NAME")).isEqualTo("Article");
assertThat(rs.getString("COLUMN_NAME")).isIn("date", "uuid", "author", "title", "content");
assertThat(rs.getInt("DATA_TYPE")).isIn(9, 12, 4, 91, 2000);
assertThat(rs.getString("TYPE_NAME")).isIn("LINK", "DATE", "STRING", "INTEGER");

}
}

@Test
//FIXME this is not a test: what is the target?
public void shouldGetAllFields() throws SQLException {
final ResultSet rsmc = conn.getMetaData().getColumns(null, null, "OUser", null);
Set<String> fieldNames = new HashSet<String>();
while (rsmc.next()) {
fieldNames.add(rsmc.getString("COLUMN_NAME"));
}
public void shouldGetAllIndexesOnArticle() throws Exception {
ResultSet rs = metaData.getIndexInfo(null, null, "Article", true, true);

rs.next();

assertThat(rs.getString("COLUMN_NAME")).isEqualTo("uuid");
assertThat(rs.getString("INDEX_NAME")).isEqualTo("Article.uuid");
assertThat(rs.getBoolean("NON_UNIQUE")).isFalse();

}

@Test
public void shouldGetPrimaryKeyOfArticle() throws Exception {
ResultSet rs = metaData.getPrimaryKeys(null, null, "Article");

rs.next();
assertThat(rs.getString("TABLE_NAME")).isEqualTo("Article");
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("uuid");
assertThat(rs.getString("PK_NAME")).isEqualTo("Article.uuid");
assertThat(rs.getInt("KEY_SEQ")).isEqualTo(1);

fieldNames.removeAll(Arrays.asList("name", "password", "roles", "status"));
}

private int sizeOf(ResultSet rs) throws SQLException {
Expand Down

0 comments on commit fe26586

Please sign in to comment.