Esempio n. 1
0
  @Override
  public void add(Session session, Row row) {
    StatementBuilder sql = new StatementBuilder("insert into ");
    sql.append(getName()).append("(_gui_row_id_");

    for (Column c : getColumns()) {
      sql.append(",");
      sql.append(c.getName());
    }

    sql.append(") values (");
    sql.append(row.getKey());

    for (Column c : getColumns()) {
      sql.append(",");
      Value v = row.getValue(c.getColumnId());
      if (v == null) {
        sql.append("DEFAULT");
      } else {
        sql.append(v.getSQL());
      }
    }
    sql.append(")");

    Prepared prepared = session.prepare(sql.toString(), true);
    prepared.setLocal(false);
    prepared.update();
  }
Esempio n. 2
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;
 }
Esempio n. 3
0
  @Override
  public void remove(Session session, Row row) {
    StatementBuilder sql = new StatementBuilder("delete from ");
    sql.append(getName());
    if (row != null) {
      sql.append(" where ");

      for (Column c : getColumns()) {
        sql.appendExceptFirst(" and ");
        sql.append(c.getName()).append("=");
        Value v = row.getValue(c.getColumnId());
        if (v != null) {
          sql.append(v.getSQL());
        }
      }
    }

    Prepared prepared = session.prepare(sql.toString(), true);
    prepared.setLocal(false);
    prepared.update();
  }
Esempio n. 4
0
 private int generateInsertValues(int count, Table table) throws IOException {
   PlanItem plan = table.getBestPlanItem(session, null, null);
   Index index = plan.getIndex();
   Cursor cursor = index.find(session, null, null);
   Column[] columns = table.getColumns();
   StatementBuilder buff = new StatementBuilder("INSERT INTO ");
   buff.append(table.getSQL()).append('(');
   for (Column col : columns) {
     buff.appendExceptFirst(", ");
     buff.append(Parser.quoteIdentifier(col.getName()));
   }
   buff.append(") VALUES");
   if (!simple) {
     buff.append('\n');
   }
   buff.append('(');
   String ins = buff.toString();
   buff = null;
   while (cursor.next()) {
     Row row = cursor.get();
     if (buff == null) {
       buff = new StatementBuilder(ins);
     } else {
       buff.append(",\n(");
     }
     for (int j = 0; j < row.getColumnCount(); j++) {
       if (j > 0) {
         buff.append(", ");
       }
       Value v = row.getValue(j);
       if (v.getPrecision() > lobBlockSize) {
         int id;
         if (v.getType() == Value.CLOB) {
           id = writeLobStream(v);
           buff.append("SYSTEM_COMBINE_CLOB(" + id + ")");
         } else if (v.getType() == Value.BLOB) {
           id = writeLobStream(v);
           buff.append("SYSTEM_COMBINE_BLOB(" + id + ")");
         } else {
           buff.append(v.getSQL());
         }
       } else {
         buff.append(v.getSQL());
       }
     }
     buff.append(')');
     count++;
     if ((count & 127) == 0) {
       checkCanceled();
     }
     if (simple || buff.length() > Constants.IO_BUFFER_SIZE) {
       add(buff.toString(), true);
       buff = null;
     }
   }
   if (buff != null) {
     add(buff.toString(), true);
   }
   return count;
 }
Esempio n. 5
0
 /**
  * Compare two values with the current comparison mode. The values may be of different type.
  *
  * @param a the first value
  * @param b the second value
  * @return 0 if both values are equal, -1 if the first value is smaller, and 1 otherwise
  */
 public int compareTypeSave(Value a, Value b) {
   if (a == b) {
     return 0;
   }
   int dataType = Value.getHigherOrder(a.getType(), b.getType());
   a = a.convertTo(dataType);
   b = b.convertTo(dataType);
   return a.compareTypeSave(b, compareMode);
 }
Esempio n. 6
0
  private Cursor find(Session session, SearchRow first, boolean bigger, SearchRow last) {
    StatementBuilder sql = new StatementBuilder("select _gui_row_id_");
    for (Column c : getColumns()) {
      sql.append(",");
      sql.append(c.getName());
    }
    sql.append(" from ").append(getName());

    if (first != null || last != null) {
      sql.append(" where ");

      for (Column c : getColumns()) {
        sql.appendExceptFirst(" and ");
        if (first != null) {
          sql.append(c.getName()).append(">=");
          Value v = first.getValue(c.getColumnId());
          if (v != null) {
            sql.append(v.getSQL());
          }
        }
        if (last != null) {
          sql.append(c.getName()).append("<=");
          Value v = last.getValue(c.getColumnId());
          if (v != null) {
            sql.append(v.getSQL());
          }
        }
      }
    }

    Prepared prepared = session.prepare(sql.toString(), true);
    prepared.setLocal(false);
    ResultInterface result = prepared.query(0);
    if (bigger) result.next();
    return new GlobalUniqueIndexTableCursor(result);
  }
Esempio n. 7
0
 private int writeLobStream(Value v) throws IOException {
   if (!tempLobTableCreated) {
     add(
         "CREATE TABLE IF NOT EXISTS SYSTEM_LOB_STREAM"
             + "(ID INT NOT NULL, PART INT NOT NULL, CDATA VARCHAR, BDATA BINARY)",
         true);
     add(
         "CREATE PRIMARY KEY SYSTEM_LOB_STREAM_PRIMARY_KEY " + "ON SYSTEM_LOB_STREAM(ID, PART)",
         true);
     add(
         "CREATE ALIAS IF NOT EXISTS "
             + "SYSTEM_COMBINE_CLOB FOR \""
             + this.getClass().getName()
             + ".combineClob\"",
         true);
     add(
         "CREATE ALIAS IF NOT EXISTS "
             + "SYSTEM_COMBINE_BLOB FOR \""
             + this.getClass().getName()
             + ".combineBlob\"",
         true);
     tempLobTableCreated = true;
   }
   int id = nextLobId++;
   switch (v.getType()) {
     case Value.BLOB:
       {
         byte[] bytes = new byte[lobBlockSize];
         InputStream input = v.getInputStream();
         try {
           for (int i = 0; ; i++) {
             StringBuilder buff = new StringBuilder(lobBlockSize * 2);
             buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(" + id + ", " + i + ", NULL, '");
             int len = IOUtils.readFully(input, bytes, lobBlockSize);
             if (len <= 0) {
               break;
             }
             buff.append(StringUtils.convertBytesToHex(bytes, len)).append("')");
             String sql = buff.toString();
             add(sql, true);
           }
         } finally {
           IOUtils.closeSilently(input);
         }
         break;
       }
     case Value.CLOB:
       {
         char[] chars = new char[lobBlockSize];
         Reader reader = v.getReader();
         try {
           for (int i = 0; ; i++) {
             StringBuilder buff = new StringBuilder(lobBlockSize * 2);
             buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(" + id + ", " + i + ", ");
             int len = IOUtils.readFully(reader, chars, lobBlockSize);
             if (len < 0) {
               break;
             }
             buff.append(StringUtils.quoteStringSQL(new String(chars, 0, len))).append(", NULL)");
             String sql = buff.toString();
             add(sql, true);
           }
         } finally {
           IOUtils.closeSilently(reader);
         }
         break;
       }
     default:
       DbException.throwInternalError("type:" + v.getType());
   }
   return id;
 }