Package org.restsql.core.sqlresource

Examples of org.restsql.core.sqlresource.Query


   */
  protected int createDefs(final File subDirObj, final String databaseName, final String exclusionPattern) throws GenerationException {
    // Create definition object
    final ObjectFactory objectFactory = new ObjectFactory();
    final SqlResourceDefinition def = objectFactory.createSqlResourceDefinition();
    final Query query = objectFactory.createQuery();
    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 {
View Full Code Here

TOP

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

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.