示例#1
0
  public void upgradeBaseSchema(Connection conn, int currentVersion) {
    if (!isLoaded()) {
      throw new TajoInternalError("Database schema files are not loaded.");
    }

    final List<SchemaPatch> candidatePatches = new ArrayList<>();
    Statement stmt;

    for (SchemaPatch patch : this.catalogStore.getPatches()) {
      if (currentVersion >= patch.getPriorVersion()) {
        candidatePatches.add(patch);
      }
    }

    Collections.sort(candidatePatches);
    try {
      stmt = conn.createStatement();
    } catch (SQLException e) {
      throw new TajoInternalError(e);
    }

    for (SchemaPatch patch : candidatePatches) {
      for (DatabaseObject object : patch.getObjects()) {
        try {
          stmt.executeUpdate(object.getSql());
          LOG.info(object.getName() + " " + object.getType() + " was created or altered.");
        } catch (SQLException e) {
          throw new TajoInternalError(e);
        }
      }
    }

    CatalogUtil.closeQuietly(stmt);
  }
示例#2
0
  private boolean checkExistenceByQuery(
      PreparedStatement pstmt, BaseSchema baseSchema, String... params) throws SQLException {
    int paramIdx = 1;
    boolean result = false;

    if (baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty()) {
      pstmt.setString(paramIdx++, baseSchema.getSchemaName().toUpperCase());
    }

    for (; paramIdx <= pstmt.getParameterMetaData().getParameterCount(); paramIdx++) {
      pstmt.setString(paramIdx, params[paramIdx - 1].toUpperCase());
    }

    ResultSet rs = null;
    try {
      rs = pstmt.executeQuery();
      while (rs.next()) {
        if (rs.getString(1).toUpperCase().equals(params[params.length - 1].toUpperCase())) {
          result = true;
          break;
        }
      }
    } finally {
      CatalogUtil.closeQuietly(rs);
    }

    return result;
  }
示例#3
0
  public void dropBaseSchema(Connection conn) {
    if (!isLoaded()) {
      throw new TajoInternalError("Schema files are not loaded yet.");
    }

    List<DatabaseObject> failedObjects = new ArrayList<>();
    Statement stmt = null;

    try {
      stmt = conn.createStatement();
    } catch (SQLException e) {
      throw new TajoInternalError(e);
    }

    for (DatabaseObject object : catalogStore.getSchema().getObjects()) {
      try {
        if (DatabaseObjectType.TABLE == object.getType()
            || DatabaseObjectType.SEQUENCE == object.getType()
            || DatabaseObjectType.VIEW == object.getType()) {
          stmt.executeUpdate(getDropSQL(object.getType(), object.getName()));
        }
      } catch (SQLException e) {
        failedObjects.add(object);
      }
    }
    CatalogUtil.closeQuietly(stmt);

    if (failedObjects.size() > 0) {
      StringBuffer errorMessage = new StringBuffer(64);
      errorMessage.append("Failed to drop database objects ");

      for (int idx = 0; idx < failedObjects.size(); idx++) {
        DatabaseObject object = failedObjects.get(idx);
        errorMessage.append(object.getType().toString()).append(" ").append(object.getName());
        if ((idx + 1) < failedObjects.size()) {
          errorMessage.append(',');
        }
      }

      LOG.warn(errorMessage.toString());
    }
  }
示例#4
0
  public void createBaseSchema(Connection conn) {
    Statement stmt;

    if (!isLoaded()) {
      throw new TajoInternalError("Database schema files are not loaded.");
    }

    try {
      stmt = conn.createStatement();
    } catch (SQLException e) {
      throw new TajoInternalError(e);
    }

    for (DatabaseObject object : catalogStore.getSchema().getObjects()) {
      try {
        String[] params;
        if (DatabaseObjectType.INDEX == object.getType()) {
          params = new String[2];
          params[0] = object.getDependsOn();
          params[1] = object.getName();
        } else {
          params = new String[1];
          params[0] = object.getName();
        }

        if (checkExistence(conn, object.getType(), params)) {
          LOG.info("Skip to create " + object.getName() + " databse object. Already exists.");
        } else {
          stmt.executeUpdate(object.getSql());
          LOG.info(object.getName() + " " + object.getType() + " is created.");
        }
      } catch (SQLException e) {
        throw new TajoInternalError(e);
      }
    }

    CatalogUtil.closeQuietly(stmt);
  }
示例#5
0
  protected boolean checkExistence(Connection conn, DatabaseObjectType type, String... params)
      throws SQLException {
    boolean result = false;
    DatabaseMetaData metadata = null;
    PreparedStatement pstmt = null;
    BaseSchema baseSchema = catalogStore.getSchema();

    if (params == null || params.length < 1) {
      throw new IllegalArgumentException("checkExistence function needs at least one argument.");
    }

    switch (type) {
      case DATA:
        metadata = conn.getMetaData();
        ResultSet data =
            metadata.getUDTs(
                null,
                baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty()
                    ? baseSchema.getSchemaName().toUpperCase()
                    : null,
                params[0].toUpperCase(),
                null);
        result = data.next();
        CatalogUtil.closeQuietly(data);
        break;
      case FUNCTION:
        metadata = conn.getMetaData();
        ResultSet functions =
            metadata.getFunctions(
                null,
                baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty()
                    ? baseSchema.getSchemaName().toUpperCase()
                    : null,
                params[0].toUpperCase());
        result = functions.next();
        CatalogUtil.closeQuietly(functions);
        break;
      case INDEX:
        if (params.length != 2) {
          throw new IllegalArgumentException(
              "Finding index object is needed two strings, table name and index name");
        }

        pstmt = getExistQuery(conn, type);
        if (pstmt != null) {
          result = checkExistenceByQuery(pstmt, baseSchema, params);
        } else {
          metadata = conn.getMetaData();
          ResultSet indexes =
              metadata.getIndexInfo(
                  null,
                  baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty()
                      ? baseSchema.getSchemaName().toUpperCase()
                      : null,
                  params[0].toUpperCase(),
                  false,
                  true);
          while (indexes.next()) {
            if (indexes.getString("INDEX_NAME").equals(params[1].toUpperCase())) {
              result = true;
              break;
            }
          }
          CatalogUtil.closeQuietly(indexes);
        }
        break;
      case TABLE:
        pstmt = getExistQuery(conn, type);
        if (pstmt != null) {
          result = checkExistenceByQuery(pstmt, baseSchema, params);
        } else {
          metadata = conn.getMetaData();
          ResultSet tables =
              metadata.getTables(
                  null,
                  baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty()
                      ? baseSchema.getSchemaName().toUpperCase()
                      : null,
                  params[0].toUpperCase(),
                  new String[] {"TABLE"});
          result = tables.next();
          CatalogUtil.closeQuietly(tables);
        }
        break;
      case DOMAIN:
      case OPERATOR:
      case RULE:
      case SEQUENCE:
      case TRIGGER:
      case VIEW:
        pstmt = getExistQuery(conn, type);

        if (pstmt == null) {
          throw new TajoInternalError(
              "Finding "
                  + type
                  + " type of database object is not supported on this database system.");
        }

        result = checkExistenceByQuery(pstmt, baseSchema, params);
        break;
    }

    return result;
  }