use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.
the class Neo4jSchemaForm method generateSchema.
private void generateSchema() throws NoSQLExtractSchemaException {
if (metadataTable == null) {
return;
}
String cypher = metadataTable.getAdditionalProperties().get(INeo4jConstants.CYPHER);
if (cypher == null) {
return;
}
IMetadataProvider metadataProvider = NoSQLRepositoryFactory.getInstance().getMetadataProvider(getConnection().getDbType());
if (metadataProvider == null) {
return;
}
List<MetadataColumn> columns = metadataProvider.extractColumns(getConnection(), cypher);
metadataTable.getColumns().clear();
metadataTable.getColumns().addAll(columns);
}
use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.
the class MongoDBMetadataProvider method extractColumns.
@Override
public List<MetadataColumn> extractColumns(NoSQLConnection connection, INoSQLSchemaNode node) throws NoSQLExtractSchemaException {
List<MetadataColumn> metadataColumns = new ArrayList<MetadataColumn>();
if (connection == null || node == null) {
return metadataColumns;
}
try {
if (IMongoConstants.COLLECTION.equals(node.getNodeType())) {
String dbName = null;
INoSQLSchemaNode parent = node.getParent();
if (parent != null && IMongoConstants.DATABASE.equals(parent.getNodeType())) {
dbName = parent.getName();
} else {
dbName = ConnectionContextHelper.getParamValueOffContext(connection, connection.getAttributes().get(IMongoDBAttributes.DATABASE));
}
if (dbName == null) {
return metadataColumns;
}
String collectionName = node.getName();
if (MongoDBConnectionUtil.isUpgradeLatestVersion(connection)) {
metadataColumns.addAll(extractTheColumns4Upgrade(connection, dbName, collectionName));
} else {
metadataColumns.addAll(extractTheColumns(connection, dbName, collectionName));
}
}
} catch (Exception e) {
throw new NoSQLExtractSchemaException(e);
}
return metadataColumns;
}
use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.
the class MongoDBMetadataProvider method extractTheColumns4Upgrade.
private List<MetadataColumn> extractTheColumns4Upgrade(NoSQLConnection connection, String dbName, String collectionName) throws NoSQLExtractSchemaException {
List<MetadataColumn> metadataColumns = new ArrayList<MetadataColumn>();
try {
Object db = MongoDBConnectionUtil.getDB(connection, dbName);
if (db == null) {
return metadataColumns;
}
ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection);
// $NON-NLS-1$
Class<?> dbObjectClasszz = Class.forName("com.mongodb.DBObject", true, classLoader);
// $NON-NLS-1$
Class<?> documentClasszz = Class.forName("org.bson.Document", true, classLoader);
String documentClassName = documentClasszz.getName();
List<String> existColumnNames = new ArrayList<String>();
// $NON-NLS-1$
Object dbCollection = NoSQLReflection.invokeMethod(db, "getCollection", new Object[] { collectionName });
Set<String> indexColNames = new HashSet<String>();
// $NON-NLS-1$
Iterable<Object> indexIter = (Iterable<Object>) NoSQLReflection.invokeMethod(dbCollection, "listIndexes", new Object[] { dbObjectClasszz });
Iterator<Object> indexIterator = indexIter.iterator();
while (indexIterator.hasNext()) {
Object index = indexIterator.next();
// $NON-NLS-1$//$NON-NLS-2$
Object keyObj = NoSQLReflection.invokeMethod(index, "get", new Object[] { "key" });
if (keyObj != null) {
// $NON-NLS-1$
Set<String> indexKeys = (Set<String>) NoSQLReflection.invokeMethod(keyObj, "keySet", new Object[0]);
indexColNames.addAll(indexKeys);
}
}
int rowNum = 0;
// $NON-NLS-1$
Iterable<Object> dbCursorIter = (Iterable<Object>) NoSQLReflection.invokeMethod(dbCollection, "find");
Iterator<Object> dbCursorIterator = dbCursorIter.iterator();
while (dbCursorIterator.hasNext()) {
if (rowNum > COUNT_ROWS) {
break;
}
Object dbObject = dbCursorIterator.next();
// $NON-NLS-1$
Set<String> columnNames = (Set<String>) NoSQLReflection.invokeMethod(dbObject, "keySet");
for (String colName : columnNames) {
colName = MetadataToolHelper.validateValue(colName);
if (existColumnNames.contains(colName)) {
continue;
}
MetadataColumn column = ConnectionFactory.eINSTANCE.createMetadataColumn();
column.setName(colName);
column.setLabel(colName);
// $NON-NLS-1$
Object value = NoSQLReflection.invokeMethod(dbObject, "get", new Object[] { colName }, Object.class);
JavaType javaType = null;
if (value != null) {
if (!documentClassName.equals(value.getClass().getName())) {
javaType = JavaTypesManager.getJavaTypeFromName(value.getClass().getSimpleName());
} else {
javaType = JavaTypesManager.OBJECT;
}
}
if (javaType == null) {
javaType = JavaTypesManager.STRING;
}
column.setTalendType(javaType.getId());
if (indexColNames.contains(colName)) {
column.setKey(true);
column.setNullable(false);
}
metadataColumns.add(column);
existColumnNames.add(colName);
}
rowNum++;
}
} catch (Exception e) {
throw new NoSQLExtractSchemaException(e);
}
return metadataColumns;
}
use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.
the class MongoDBMetadataProvider method extractColumns.
@Override
public List<MetadataColumn> extractColumns(NoSQLConnection connection, String tableName) throws NoSQLExtractSchemaException {
List<MetadataColumn> metadataColumns = new ArrayList<MetadataColumn>();
if (connection == null || tableName == null) {
return metadataColumns;
}
try {
String dbName = NoSQLSchemaUtil.getSchemaNameByTableLabel(connection, tableName);
dbName = ConnectionContextHelper.getParamValueOffContext(connection, dbName);
if (MongoDBConnectionUtil.isUpgradeLatestVersion(connection)) {
metadataColumns.addAll(extractTheColumns4Upgrade(connection, dbName, tableName));
} else {
metadataColumns.addAll(extractTheColumns(connection, dbName, tableName));
}
} catch (Exception e) {
throw new NoSQLExtractSchemaException(e);
}
return metadataColumns;
}
use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.
the class Neo4jMetadataProvider method getTheColumns.
private List<MetadataColumn> getTheColumns(NoSQLConnection connection, ClassLoader classLoader, String cypher) throws NoSQLReflectionException, ClassNotFoundException, NoSQLServerException {
List<MetadataColumn> metadataColumns = new ArrayList<MetadataColumn>();
String usename = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.USERNAME));
String password = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.PASSWORD));
String serverUrl = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.SERVER_URL));
if (connection.isContextMode()) {
ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
if (contextType != null) {
usename = ContextParameterUtils.getOriginalValue(contextType, usename);
password = ContextParameterUtils.getOriginalValue(contextType, password);
serverUrl = ContextParameterUtils.getOriginalValue(contextType, serverUrl);
}
} else {
password = connection.getValue(password, false);
}
Object basic = NoSQLReflection.invokeStaticMethod("org.neo4j.driver.v1.AuthTokens", "basic", new Object[] { usename, password }, classLoader, String.class, String.class);
Object driver = NoSQLReflection.invokeStaticMethod("org.neo4j.driver.v1.GraphDatabase", "driver", new Object[] { serverUrl, basic }, classLoader, String.class, Class.forName("org.neo4j.driver.v1.AuthToken", true, classLoader));
Object session = NoSQLReflection.invokeMethod(driver, "session");
Iterator<Map<String, Object>> resultIterator = (Iterator<Map<String, Object>>) NoSQLReflection.invokeMethod(session, "run", new Object[] { cypher }, String.class);
List<String> columnLabels = new ArrayList<String>();
int rowNum = 0;
while (resultIterator.hasNext()) {
if (rowNum > COUNT_ROWS) {
break;
}
rowNum++;
Object record = resultIterator.next();
Map<String, Object> row = (Map<String, Object>) NoSQLReflection.invokeMethod(record, "asMap");
for (Entry<String, Object> column : row.entrySet()) {
String key = column.getKey();
Object value = column.getValue();
if (StringUtils.isEmpty(key) || value == null) {
continue;
}
addMetadataColumns(classLoader, key, value, metadataColumns, columnLabels);
}
}
return metadataColumns;
}
Aggregations