Package org.restsql.core.sqlresource

Examples of org.restsql.core.sqlresource.Table


    }
  }

  private void buildJoinTableMetadata(final Connection connection) throws SQLException {
    // Join table could have been identified in buildTablesAndColumns(), but not always
    final Table joinDef = SqlResourceDefinitionUtils.getTable(definition, TableRole.Join);
    if (joinDef != null && joinTable == null) {
      // Determine table and database name
      String tableName, databaseName;
      final String possiblyQualifiedTableName = joinDef.getName();
      final int dotIndex = possiblyQualifiedTableName.indexOf('.');
      if (dotIndex > 0) {
        tableName = possiblyQualifiedTableName.substring(0, dotIndex);
        databaseName = possiblyQualifiedTableName.substring(dotIndex + 1);
      } else {
View Full Code Here


          resultSetMetaData.getColumnType(colNumber), readOnly);

      TableMetaData table = (TableMetaDataImpl) tableMap.get(column.getQualifiedTableName());
      if (table == null) {
        // Create table metadata object and add to special references
        final Table tableDef = SqlResourceDefinitionUtils.getTable(definition, column);
        if (tableDef == null) {
          throw new SqlResourceException("Definition requires table element for "
              + column.getTableName() + ", referenced by column " + column.getColumnLabel());
        }
        table = Factory.getTableMetaData();
        table.setAttributes(tableName, qualifiedTableName, databaseName,
            TableRole.valueOf(tableDef.getRole()));
        tableMap.put(column.getQualifiedTableName(), table);
        tables.add(table);

        switch (table.getTableRole()) {
          case Parent:
            parentTable = table;
            if (tableDef.getAlias() != null) {
              table.setTableAlias(tableDef.getAlias());
            }
            // fall through
          case ParentExtension:
            parentPlusExtTables.add(table);
            break;
          case Child:
            childTable = table;
            if (tableDef.getAlias() != null) {
              table.setTableAlias(tableDef.getAlias());
            }
            // fall through
          case ChildExtension:
            childPlusExtTables.add(table);
            break;
View Full Code Here

    def.setQuery(query);
    final MetaData metaData = objectFactory.createMetaData();
    final Database database = objectFactory.createDatabase();
    database.setDefault(databaseName);
    metaData.setDatabase(database);
    final Table table = objectFactory.createTable();
    table.setRole("Parent");
    metaData.getTable().add(table);
    def.setMetadata(metaData);

    StringBuilder queryString = null;
    int defsCreated = 0;

    // Now inspect the information schema for columns and tables, build definition and write the files
    Connection connection = null;
    try {
      connection = Factory.getConnection(databaseName);
 
      // Build SQL query, prepare statement and execute
      String sql = getColumnsQuery();
      if (exclusionPattern != null) {
        sql += getTableExclusionQueryClause();
      }
      final PreparedStatement statement = connection.prepareStatement(sql);
      statement.setString(1, databaseName);
      if (exclusionPattern != null) {
        statement.setString(2, exclusionPattern);
      }
      Config.logger.info(sql);
      final ResultSet resultSet = statement.executeQuery();
     
      // Iterate through results, create build def and write the files
      while (resultSet.next()) {
        final String columnName = resultSet.getString(1);
        final String tableName = resultSet.getString(2);

        if (!tableName.equals(table.getName())) {
          if (defsCreated > 0) {
            // Complete previous def and write it
            queryString.append("\n\t\tFROM ");
            queryString.append(table.getName());
            queryString.append("\n\t");
            query.setValue(queryString.toString());
            writeDef(subDirObj, def, table.getName());
            table.setName(tableName);
          } else {
            table.setName(tableName);
          }

          // Start new def
          defsCreated++;
          queryString = new StringBuilder();
          queryString.append("\n\t\tSELECT ");
          queryString.append(columnName);
        } else {
          queryString.append(", ");
          queryString.append(columnName);
        }
      }
      // Finish up the last one
      if (defsCreated > 0) {
        queryString.append(" FROM ");
        queryString.append(table.getName());
        query.setValue(queryString.toString());
        writeDef(subDirObj, def, table.getName());
      }
    } catch (final SQLException exception) {
      throw new GenerationException(exception.toString());
    } finally {
      if (connection != null) {
View Full Code Here

TOP

Related Classes of org.restsql.core.sqlresource.Table

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.