Package org.apache.metamodel.schema

Examples of org.apache.metamodel.schema.Schema


            "/dependencies/dependency/version",
            "/dependencies/dependency/scope" });
    DataContext dc = new XmlSaxDataContext(new File(
        "src/test/resources/xml_input_flatten_tables.xml"), tableDef);

    final Schema schema = dc.getDefaultSchema();
    assertEquals("Schema[name=/dependencies]", schema.toString());
    assertEquals("[/dependency]", Arrays.toString(schema.getTableNames()));

    final Table table = schema.getTableByName("/dependency");
    assertEquals("[row_id, /artifactId, /groupId, /version, /scope]",
        Arrays.toString(table.getColumnNames()));

    // perform a regular query
    DataSet ds = dc.query().from(table).select(table.getColumns())
View Full Code Here


    final DataContext dc = new XmlSaxDataContext(
        new File(
            "src/test/resources/xml_input_parent_and_child_relationship.xml"),
        employeeTableDef, organizationTableDef);

    final Schema schema = dc.getDefaultSchema();
    assertEquals("Schema[name=/root]", schema.toString());
    assertEquals("[/employee, /organization]",
        Arrays.toString(schema.getTableNames()));

    // organization assertions
    final Table organizationTable = schema.getTableByName("/organization");
    {
      assertEquals("[row_id, /name, @type]",
          Arrays.toString(organizationTable.getColumnNames()));

      DataSet ds = dc.query().from(organizationTable)
          .select(organizationTable.getColumns()).execute();
      assertTrue(ds.next());
      assertEquals("Row[values=[0, Company A, governmental]]", ds
          .getRow().toString());
      assertTrue(ds.next());
      assertEquals("Row[values=[1, Company B, company]]", ds.getRow()
          .toString());
      assertTrue(ds.next());
      assertEquals("Row[values=[2, Company C, company]]", ds.getRow()
          .toString());
      assertFalse(ds.next());
      ds.close();
    }

    // employee assertions
    final Table employeeTable = schema.getTableByName("/employee");
    {
      assertEquals(
          "[row_id, /name, /gender, index(/root/organization/employees), index(/root/organization)]",
          Arrays.toString(employeeTable.getColumnNames()));
View Full Code Here

            }
        }
    }

    private void loadPrimaryKeys(JdbcTable table, DatabaseMetaData metaData) throws MetaModelException {
        Schema schema = table.getSchema();
        ResultSet rs = null;

        try {
            if (_usesCatalogsAsSchemas) {
                rs = metaData.getPrimaryKeys(schema.getName(), null, table.getName());
            } else {
                rs = metaData.getPrimaryKeys(_dataContext.getCatalogName(), schema.getName(), table.getName());
            }
            while (rs.next()) {
                String columnName = rs.getString(4);
                if (columnName != null) {
                    MutableColumn column = (MutableColumn) table.getColumnByName(columnName);
View Full Code Here

            _dataContext.close(null, rs, null);
        }
    }

    private void loadIndexes(Table table, DatabaseMetaData metaData) throws MetaModelException {
        Schema schema = table.getSchema();
        ResultSet rs = null;
        // Ticket #170: IndexInfo is nice-to-have, not need-to-have, so
        // we will do a nice failover on SQLExceptions
        try {
            if (_usesCatalogsAsSchemas) {
                rs = metaData.getIndexInfo(schema.getName(), null, table.getName(), false, true);
            } else {
                rs = metaData.getIndexInfo(_dataContext.getCatalogName(), schema.getName(), table.getName(), false,
                        true);
            }
            while (rs.next()) {
                String columnName = rs.getString(9);
                if (columnName != null) {
View Full Code Here

    public final Schema[] getSchemas() throws MetaModelException {
        String[] schemaNames = getSchemaNames();
        Schema[] schemas = new Schema[schemaNames.length];
        for (int i = 0; i < schemaNames.length; i++) {
            final String name = schemaNames[i];
            final Schema schema = _schemaCache.get(getSchemaCacheKey(name));
            if (schema == null) {
                final Schema newSchema = getSchemaByName(name);
                if (newSchema == null) {
                    throw new MetaModelException("Declared schema does not exist: " + name);
                }
                final Schema existingSchema = _schemaCache.putIfAbsent(getSchemaCacheKey(name), newSchema);
                if (existingSchema == null) {
                    schemas[i] = newSchema;
                } else {
                    schemas[i] = existingSchema;
                }
View Full Code Here

    /**
     * {@inheritDoc}
     */
    @Override
    public final Schema getDefaultSchema() throws MetaModelException {
        Schema result = null;
        String defaultSchemaName = getDefaultSchemaName();
        if (defaultSchemaName != null) {
            result = getSchemaByName(defaultSchemaName);
        }
        if (result == null) {
            Schema[] schemas = getSchemas();
            if (schemas.length == 1) {
                result = schemas[0];
            } else {
                int highestTableCount = -1;
                for (int i = 0; i < schemas.length; i++) {
                    final Schema schema = schemas[i];
                    String name = schema.getName();
                    if (schema != null) {
                        name = name.toLowerCase();
                        boolean isInformationSchema = name.startsWith("information") && name.endsWith("schema");
                        if (!isInformationSchema && schema.getTableCount() > highestTableCount) {
                            highestTableCount = schema.getTableCount();
                            result = schema;
                        }
                    }
                }
            }
View Full Code Here

        return "true".equals(systemProperty);
    }

    private void loadColumns(JdbcTable table, DatabaseMetaData metaData) {
        final boolean convertLobs = isLobConversionEnabled();
        final Schema schema = table.getSchema();
        ResultSet rs = null;
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Querying for columns in table: " + table.getName());
            }
            int columnNumber = -1;
            if (_usesCatalogsAsSchemas) {
                rs = metaData.getColumns(schema.getName(), null, table.getName(), null);
            } else {
                rs = metaData.getColumns(_dataContext.getCatalogName(), schema.getName(), table.getName(), null);
            }
            while (rs.next()) {
                columnNumber++;
                final String columnName = rs.getString(4);
                if (_identifierQuoteString == null && new StringTokenizer(columnName).countTokens() > 1) {
                    logger.warn("column name contains whitespace: \"" + columnName + "\".");
                }

                final int jdbcType = rs.getInt(5);
                final String nativeType = rs.getString(6);
                final Integer columnSize = rs.getInt(7);

                if (logger.isDebugEnabled()) {
                    logger.debug("Found column: table=" + table.getName() + ",columnName=" + columnName
                            + ",nativeType=" + nativeType + ",columnSize=" + columnSize);
                }

                ColumnType columnType = _dataContext.getQueryRewriter().getColumnType(jdbcType, nativeType, columnSize);
                if (convertLobs) {
                    if (columnType == ColumnType.CLOB || columnType == ColumnType.NCLOB) {
                        columnType = JdbcDataContext.COLUMN_TYPE_CLOB_AS_STRING;
                    } else if (columnType == ColumnType.BLOB) {
                        columnType = JdbcDataContext.COLUMN_TYPE_BLOB_AS_BYTES;
                    }
                }

                final int jdbcNullable = rs.getInt(11);
                final Boolean nullable;
                if (jdbcNullable == DatabaseMetaData.columnNullable) {
                    nullable = true;
                } else if (jdbcNullable == DatabaseMetaData.columnNoNulls) {
                    nullable = false;
                } else {
                    nullable = null;
                }

                final String remarks = rs.getString(12);

                final JdbcColumn column = new JdbcColumn(columnName, columnType, table, columnNumber, nullable);
                column.setRemarks(remarks);
                column.setNativeType(nativeType);
                column.setColumnSize(columnSize);
                column.setQuote(_identifierQuoteString);
                table.addColumn(column);
            }

            final int columnsReturned = columnNumber + 1;
            if (columnsReturned == 0) {
                logger.info("No column metadata records returned for table '{}' in schema '{}'", table.getName(),
                        schema.getName());
            } else {
                logger.debug("Returned {} column metadata records for table '{}' in schema '{}'", new Object[] {
                        columnsReturned, table.getName(), schema.getName() });
            }

        } catch (SQLException e) {
            throw JdbcUtils.wrapException(e, "retrieve table metadata for " + table.getName());
        } finally {
View Full Code Here

            System.err.println(getInvalidConfigurationMessage());
            return;
        }
        dataContext = new SugarCrmDataContext(BASE_URL + "/", getUsername(), getPassword(), "Test");

        final Schema schema = dataContext.getDefaultSchema();
        final Table table = schema.getTableByName("Prospects");
        final Column[] numberColumns = table.getNumberColumns();
        final Column[] booleanColumns = table.getBooleanColumns();
        final Column[] timeBasedColumns = table.getTimeBasedColumns();
        assertTrue(numberColumns.length > 0);
        assertTrue(booleanColumns.length > 0);
View Full Code Here

    /**
     * {@inheritDoc}
     */
    @Override
    public final Schema getSchemaByName(String name) throws MetaModelException {
        Schema schema = _schemaCache.get(getSchemaCacheKey(name));
        if (schema == null) {
            if (name == null) {
                schema = getSchemaByNameInternal(null);
            } else {
                String[] schemaNames = getSchemaNames();
                for (String schemaName : schemaNames) {
                    if (name.equalsIgnoreCase(schemaName)) {
                        schema = getSchemaByNameInternal(name);
                        break;
                    }
                }
                if (schema == null) {
                    for (String schemaName : schemaNames) {
                        if (name.equalsIgnoreCase(schemaName)) {
                            // try again with "schemaName" as param instead of
                            // "name".
                            schema = getSchemaByNameInternal(schemaName);
                            break;
                        }
                    }
                }
            }
            if (schema != null) {
                Schema existingSchema = _schemaCache.putIfAbsent(getSchemaCacheKey(schema.getName()), schema);
                if (existingSchema != null) {
                    // race conditions may cause two schemas to be created.
                    // We'll favor the existing schema if possible, since schema
                    // may contain lazy-loading logic and so on.
                    return existingSchema;
View Full Code Here

            }
        }
    }

    private void loadRelations(Table table, DatabaseMetaData metaData) {
        Schema schema = table.getSchema();
        ResultSet rs = null;
        try {
            if (_usesCatalogsAsSchemas) {
                rs = metaData.getImportedKeys(schema.getName(), null, table.getName());
            } else {
                rs = metaData.getImportedKeys(_dataContext.getCatalogName(), schema.getName(), table.getName());
            }
            loadRelations(rs, schema);
        } catch (SQLException e) {
            throw JdbcUtils.wrapException(e, "retrieve imported keys for " + table.getName());
        } finally {
View Full Code Here

TOP

Related Classes of org.apache.metamodel.schema.Schema

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.