Ejemplo n.º 1
0
 private ValueResultSet getTable(
     Session session, Expression[] argList, boolean onlyColumnList, boolean distinctRows) {
   int len = columnList.length;
   Expression[] header = new Expression[len];
   Database db = session.getDatabase();
   for (int i = 0; i < len; i++) {
     Column c = columnList[i];
     ExpressionColumn col = new ExpressionColumn(db, c);
     header[i] = col;
   }
   LocalResult result = new LocalResult(session, header, len);
   if (distinctRows) {
     result.setDistinct();
   }
   if (!onlyColumnList) {
     Value[][] list = new Value[len][];
     int rows = 0;
     for (int i = 0; i < len; i++) {
       Value v = argList[i].getValue(session);
       if (v == ValueNull.INSTANCE) {
         list[i] = new Value[0];
       } else {
         ValueArray array = (ValueArray) v.convertTo(Value.ARRAY);
         Value[] l = array.getList();
         list[i] = l;
         rows = Math.max(rows, l.length);
       }
     }
     for (int row = 0; row < rows; row++) {
       Value[] r = new Value[len];
       for (int j = 0; j < len; j++) {
         Value[] l = list[j];
         Value v;
         if (l.length <= row) {
           v = ValueNull.INSTANCE;
         } else {
           Column c = columnList[j];
           v = l[row];
           v = c.convert(v);
           v = v.convertPrecision(c.getPrecision(), false);
           v = v.convertScale(true, c.getScale());
         }
         r[j] = v;
       }
       result.addRow(r);
     }
   }
   result.done();
   ValueResultSet vr = ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
   return vr;
 }
Ejemplo n.º 2
0
  @Override
  public ResultInterface query(int maxrows) {
    session.getUser().checkAdmin();
    reset();
    Database db = session.getDatabase();
    if (schemaNames != null) {
      for (String schemaName : schemaNames) {
        Schema schema = db.findSchema(schemaName);
        if (schema == null) {
          throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
        }
      }
    }
    try {
      result = createResult();
      deleteStore();
      openOutput();
      if (out != null) {
        buffer = new byte[Constants.IO_BUFFER_SIZE];
      }
      if (settings) {
        for (Setting setting : db.getAllSettings()) {
          if (setting.getName().equals(SetTypes.getTypeName(SetTypes.CREATE_BUILD))) {
            // don't add CREATE_BUILD to the script
            // (it is only set when creating the database)
            continue;
          }
          add(setting.getCreateSQL(), false);
        }
      }
      if (out != null) {
        add("", true);
      }
      for (User user : db.getAllUsers()) {
        add(user.getCreateSQL(passwords), false);
      }
      for (Role role : db.getAllRoles()) {
        add(role.getCreateSQL(true), false);
      }
      for (Schema schema : db.getAllSchemas()) {
        if (excludeSchema(schema)) {
          continue;
        }
        add(schema.getCreateSQL(), false);
      }
      for (UserDataType datatype : db.getAllUserDataTypes()) {
        if (drop) {
          add(datatype.getDropSQL(), false);
        }
        add(datatype.getCreateSQL(), false);
      }
      for (SchemaObject obj : db.getAllSchemaObjects(DbObject.CONSTANT)) {
        if (excludeSchema(obj.getSchema())) {
          continue;
        }
        Constant constant = (Constant) obj;
        add(constant.getCreateSQL(), false);
      }

      final ArrayList<Table> tables = db.getAllTablesAndViews(false);
      // sort by id, so that views are after tables and views on views
      // after the base views
      Collections.sort(
          tables,
          new Comparator<Table>() {
            @Override
            public int compare(Table t1, Table t2) {
              return t1.getId() - t2.getId();
            }
          });

      // Generate the DROP XXX ... IF EXISTS
      for (Table table : tables) {
        if (excludeSchema(table.getSchema())) {
          continue;
        }
        if (excludeTable(table)) {
          continue;
        }
        if (table.isHidden()) {
          continue;
        }
        table.lock(session, false, false);
        String sql = table.getCreateSQL();
        if (sql == null) {
          // null for metadata tables
          continue;
        }
        if (drop) {
          add(table.getDropSQL(), false);
        }
      }
      for (SchemaObject obj : db.getAllSchemaObjects(DbObject.FUNCTION_ALIAS)) {
        if (excludeSchema(obj.getSchema())) {
          continue;
        }
        if (drop) {
          add(obj.getDropSQL(), false);
        }
        add(obj.getCreateSQL(), false);
      }
      for (UserAggregate agg : db.getAllAggregates()) {
        if (drop) {
          add(agg.getDropSQL(), false);
        }
        add(agg.getCreateSQL(), false);
      }
      for (SchemaObject obj : db.getAllSchemaObjects(DbObject.SEQUENCE)) {
        if (excludeSchema(obj.getSchema())) {
          continue;
        }
        Sequence sequence = (Sequence) obj;
        if (drop) {
          add(sequence.getDropSQL(), false);
        }
        add(sequence.getCreateSQL(), false);
      }

      // Generate CREATE TABLE and INSERT...VALUES
      int count = 0;
      for (Table table : tables) {
        if (excludeSchema(table.getSchema())) {
          continue;
        }
        if (excludeTable(table)) {
          continue;
        }
        if (table.isHidden()) {
          continue;
        }
        table.lock(session, false, false);
        String createTableSql = table.getCreateSQL();
        if (createTableSql == null) {
          // null for metadata tables
          continue;
        }
        final String tableType = table.getTableType();
        add(createTableSql, false);
        final ArrayList<Constraint> constraints = table.getConstraints();
        if (constraints != null) {
          for (Constraint constraint : constraints) {
            if (Constraint.PRIMARY_KEY.equals(constraint.getConstraintType())) {
              add(constraint.getCreateSQLWithoutIndexes(), false);
            }
          }
        }
        if (Table.TABLE.equals(tableType)) {
          if (table.canGetRowCount()) {
            String rowcount =
                "-- "
                    + table.getRowCountApproximation()
                    + " +/- SELECT COUNT(*) FROM "
                    + table.getSQL();
            add(rowcount, false);
          }
          if (data) {
            count = generateInsertValues(count, table);
          }
        }
        final ArrayList<Index> indexes = table.getIndexes();
        for (int j = 0; indexes != null && j < indexes.size(); j++) {
          Index index = indexes.get(j);
          if (!index.getIndexType().getBelongsToConstraint()) {
            add(index.getCreateSQL(), false);
          }
        }
      }
      if (tempLobTableCreated) {
        add("DROP TABLE IF EXISTS SYSTEM_LOB_STREAM", true);
        add("CALL SYSTEM_COMBINE_BLOB(-1)", true);
        add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_CLOB", true);
        add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_BLOB", true);
        tempLobTableCreated = false;
      }
      // Generate CREATE CONSTRAINT ...
      final ArrayList<SchemaObject> constraints = db.getAllSchemaObjects(DbObject.CONSTRAINT);
      Collections.sort(
          constraints,
          new Comparator<SchemaObject>() {
            @Override
            public int compare(SchemaObject c1, SchemaObject c2) {
              return ((Constraint) c1).compareTo((Constraint) c2);
            }
          });
      for (SchemaObject obj : constraints) {
        if (excludeSchema(obj.getSchema())) {
          continue;
        }
        Constraint constraint = (Constraint) obj;
        if (excludeTable(constraint.getTable())) {
          continue;
        }
        if (constraint.getTable().isHidden()) {
          continue;
        }
        if (!Constraint.PRIMARY_KEY.equals(constraint.getConstraintType())) {
          add(constraint.getCreateSQLWithoutIndexes(), false);
        }
      }
      // Generate CREATE TRIGGER ...
      for (SchemaObject obj : db.getAllSchemaObjects(DbObject.TRIGGER)) {
        if (excludeSchema(obj.getSchema())) {
          continue;
        }
        TriggerObject trigger = (TriggerObject) obj;
        if (excludeTable(trigger.getTable())) {
          continue;
        }
        add(trigger.getCreateSQL(), false);
      }
      // Generate GRANT ...
      for (Right right : db.getAllRights()) {
        Table table = right.getGrantedTable();
        if (table != null) {
          if (excludeSchema(table.getSchema())) {
            continue;
          }
          if (excludeTable(table)) {
            continue;
          }
        }
        add(right.getCreateSQL(), false);
      }
      // Generate COMMENT ON ...
      for (Comment comment : db.getAllComments()) {
        add(comment.getCreateSQL(), false);
      }
      if (out != null) {
        out.close();
      }
    } catch (IOException e) {
      throw DbException.convertIOException(e, getFileName());
    } finally {
      closeIO();
    }
    result.done();
    LocalResult r = result;
    reset();
    return r;
  }
Ejemplo n.º 3
0
 @Override
 public ResultInterface queryMeta() {
   LocalResult r = createResult();
   r.done();
   return r;
 }