Package org.restsql.core

Examples of org.restsql.core.ColumnMetaData


  private void addRequestParamsToResponseValues(final Request request,
      final Set<ResponseValue> responseValues, final TableMetaData table)
      throws InvalidRequestException {
    if (request.getParameters() != null) {
      for (final RequestValue param : request.getParameters()) {
        final ColumnMetaData column = table.getColumns().get(param.getName());
        if (column != null && !column.isNonqueriedForeignKey()) {
          responseValues.add(new ResponseValue(param.getName(), param.getValue(), column
              .getColumnNumber()));
        }
      }
    }
    if (request.getResourceIdentifiers() != null) {
      for (final RequestValue param : request.getResourceIdentifiers()) {
        final ColumnMetaData column = table.getColumns().get(param.getName());
        if (column != null && !column.isNonqueriedForeignKey()) {
          column.normalizeValue(param); // this is called in the SQL Builder as well, but it's required here
                          // for parent res ids
          responseValues.add(new ResponseValue(param.getName(), param.getValue(), column
              .getColumnNumber()));
        }
      }
    }
  }
View Full Code Here


        newParent = true;
      } else {
        // Not the first row, check if parent differs from the last
        newParent = false;
        for (int i = 0; i < currentParentPkValues.size(); i++) {
          final ColumnMetaData column = metaData.getParent().getPrimaryKeys().get(i);
          if (!currentParentPkValues.get(i).equals(column.getResultByLabel(resultSet))) {
            newParent = true;
            break;
          }
        }
      }

      // Set current parent row pk values as well as in the parent row object
      if (newParent) {
        childRows = new ArrayList<Map<String, Object>>();
        parentRow = new HashMap<String, Object>(numberParentElementColumns);
        parentRow.put(childRowsName, childRows);
        results.add(parentRow);
        currentParentPkValues.clear();

        for (final ColumnMetaData column : metaData.getParentReadColumns()) {
          final Object value = column.getResultByLabel(resultSet);
          if (column.isPrimaryKey() && column.getTableRole() == TableRole.Parent) {
            currentParentPkValues.add(value);
          }
          parentRow.put(column.getColumnLabel(), value);
        }
      }

      // Populate the child row object
      Map<String, Object> childRow = new HashMap<String, Object>(numberChildElementColumns);
      boolean nullPk = false;
      for (final ColumnMetaData column : metaData.getChildReadColumns()) {
        final Object value = column.getResultByLabel(resultSet);
        if (column.isPrimaryKey()) {
          nullPk = value == null;
        }
        childRow.put(column.getColumnLabel(), value);
      }
      if (nullPk) {
        childRow = null;
      } else {
        childRows.add(childRow);
View Full Code Here

                  mainTable = parentTable;
              }
              // Look for a pk on the main table with the same name
              for (final ColumnMetaData pk : mainTable.getPrimaryKeys()) {
                if (columnName.equals(pk.getColumnName())) {
                  final ColumnMetaData fkColumn = Factory.getColumnMetaData();
                  fkColumn.setAttributes(
                      table.getDatabaseName(),
                      table.getQualifiedTableName(),
                      table.getTableName(),
                      table.getTableRole(),
                      columnName,
View Full Code Here

        statement.setString(1, (isDbMetaDataUpperCase() ? databaseName.toUpperCase() : databaseName));
        statement.setString(2, (isDbMetaDataUpperCase() ? tableName.toUpperCase() : tableName));
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
          final String columnName = resultSet.getString(1);
          final ColumnMetaData column = Factory.getColumnMetaData();
          column.setAttributes(databaseName, qualifiedTableName, tableName, TableRole.Join,
              columnName,
              getQualifiedColumnName(tableName, qualifiedTableName, false, columnName),
              columnName,
              getQualifiedColumnLabel(tableName, qualifiedTableName, false, columnName),
              resultSet.getString(2));
View Full Code Here

        qualifiedTableName = getQualifiedTableName(definition, resultSetMetaData, colNumber);
      }

      final String columnName = getColumnName(definition, resultSetMetaData, colNumber);
      final String columnLabel = resultSetMetaData.getColumnLabel(colNumber);
      final ColumnMetaData column = Factory.getColumnMetaData();
      column.setAttributes(colNumber, databaseName, qualifiedTableName, tableName, columnName,
          getQualifiedColumnName(tableName, qualifiedTableName, readOnly, columnName), columnLabel,
          getQualifiedColumnLabel(tableName, qualifiedTableName, readOnly, columnLabel),
          resultSetMetaData.getColumnTypeName(colNumber),
          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;
          case Join: // unlikely to be in the select columns, but just in case
            joinTable = table;
            joinList = new ArrayList<TableMetaData>(1);
            joinList.add(joinTable);
            break;
          default: // Unknown
        }
      }

      // Add column to the table
      table.addColumn(column);
      column.setTableRole(table.getTableRole());

      // Add column to special column lists
      allReadColumns.add(column);
      switch (table.getTableRole()) {
        case Parent:
View Full Code Here

      throws InvalidRequestException {
    if (requestParams != null) {
      for (final RequestValue requestParam : requestParams) {
        final List<TableMetaData> tables = metaData.getWriteTables(Request.Type.DELETE, doParent);
        for (final TableMetaData table : tables) {
          final ColumnMetaData column = table.getColumns().get(requestParam.getName());
          if (column != null) {
            if (column.isReadOnly()) {
              throw new InvalidRequestException(InvalidRequestException.MESSAGE_READONLY_PARAM,
                  column.getColumnLabel());
            }
            final String qualifiedTableName = column.getQualifiedTableName();
            SqlStruct sql = sqls.get(qualifiedTableName);
            if (sql == null) {
              // Create new sql holder
              sql = new SqlStruct(DEFAULT_DELETE_SIZE, DEFAULT_DELETE_SIZE / 2);
              sqls.put(qualifiedTableName, sql);
View Full Code Here

    // Iterate through the params and build the sql for each table
    for (final RequestValue param : request.getParameters()) {
      final List<TableMetaData> tables = metaData.getWriteTables(request.getType(), doParent);
      for (final TableMetaData table : tables) {
        final ColumnMetaData column = table.getColumns().get(param.getName());
        if (column != null) {
          if (column.isReadOnly()) {
            throw new InvalidRequestException(InvalidRequestException.MESSAGE_READONLY_PARAM,
                column.getColumnLabel());
          }
          final String qualifiedTableName = column.getQualifiedTableName();
          SqlStruct sql = sqls.get(qualifiedTableName);
          if (sql == null) {
            // Create new sql holder
            sql = new SqlStruct(DEFAULT_INSERT_SIZE, DEFAULT_INSERT_SIZE / 2);
            sqls.put(qualifiedTableName, sql);
            sql.getMain().append("INSERT INTO ");
            sql.getMain().append(qualifiedTableName);
            sql.getMain().append(" (");

            sql.appendToBothClauses(" VALUES (");
          } else {
            sql.getMain().append(',');
            sql.appendToBothClauses(",");
          }
          sql.getMain().append(column.getColumnName()); // since parameter may use column label

          // Begin quote the column value
          if (column.isCharOrDateTimeType()) {
            sql.getClause().append('\'');
          }

          // Convert String to appropriate object
          column.normalizeValue(param);

          // Set the value in the printable clause, the ? in the prepared clause, and prepared clause value
          sql.getClause().append(param.getValue().toString());
          sql.getPreparedClause().append(buildPreparedParameterSql(column));
          sql.getPreparedValues().add(param.getValue());

          // End quote the column value
          if (column.isCharOrDateTimeType()) {
            sql.getClause().append('\'');
          }
        }
      }
    }
View Full Code Here

        } else {
          sql.appendToBothClauses(" WHERE ");
        }

        for (final TableMetaData table : metaData.getTables()) {
          final ColumnMetaData column = table.getColumns().get(param.getName());
          if (column != null) {
            if (column.isReadOnly()) {
              throw new InvalidRequestException(InvalidRequestException.MESSAGE_READONLY_PARAM,
                  column.getColumnLabel());
            }
            if (!column.isNonqueriedForeignKey()) {
              validParamFound = true;
              setNameValue(Request.Type.SELECT, metaData, column, param, true, sql, false);
            }
          }
        }
View Full Code Here

    final List<TableMetaData> tables = metaData.getWriteTables(request.getType(), doParent);

    boolean validParamFound = false;
    for (final RequestValue param : request.getParameters()) {
      for (final TableMetaData table : tables) {
        final ColumnMetaData column = table.getColumns().get(param.getName());
        if (column != null) {
          if (column.isReadOnly()) {
            throw new InvalidRequestException(InvalidRequestException.MESSAGE_READONLY_PARAM,
                column.getColumnLabel());
          }
          if (column.isPrimaryKey()) {
            // Add this to the res Ids - assume resIds is non null
            resIds.add(param);
          } else if (!column.isNonqueriedForeignKey()) {
            SqlStruct sql = sqls.get(column.getQualifiedTableName());
            if (sql == null) {
              // Create new sql holder
              sql = new SqlStruct(DEFAULT_UPDATE_SIZE, DEFAULT_UPDATE_SIZE / 2, true);
              sqls.put(column.getQualifiedTableName(), sql);
              sql.appendToBothMains("UPDATE ");
              sql.appendToBothMains(column.getQualifiedTableName());
              sql.appendToBothMains(" SET ");
            } else {
              sql.appendToBothMains(",");
            }

            validParamFound = true;
            setNameValue(request.getType(), metaData, column, param, false, sql, true);
          }
        }
      }
    }

    if (!validParamFound) {
      throw new InvalidRequestException(InvalidRequestException.MESSAGE_INVALID_PARAMS);
    }
    validParamFound = false;

    for (final String qualifiedTableName : sqls.keySet()) {
      final SqlStruct sql = sqls.get(qualifiedTableName);
      if (sql == null) {
        sqls.remove(qualifiedTableName);
      } else {
        // Iterate through the resourceIds and build the where clause sql for each table
        for (final RequestValue resId : resIds) {
          final TableMetaData table = metaData.getTableMap().get(qualifiedTableName);
          final ColumnMetaData column = table.getColumns().get(resId.getName());
          if (column != null) {
            if (sql.getClause().length() == 0) {
              sql.appendToBothClauses(" WHERE ");
            } else { // sql.getClause().length() > 0
              sql.appendToBothClauses(" AND ");
View Full Code Here

TOP

Related Classes of org.restsql.core.ColumnMetaData

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.