@Override public Value negate() { if (value == Long.MIN_VALUE) { throw getOverflow(); } return ValueLong.get(-value); }
@Override public Value getValue(int i) { return i == -1 ? ValueLong.get(key) : data[i]; }
@Override public Value divide(Value v) { ValueLong other = (ValueLong) v; if (other.value == 0) { throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); } return ValueLong.get(value / other.value); }
case Value.DOUBLE: long valueLong = value.getLong(); bytes = ValueLong.get(valueLong).getBytes(); break; case Value.DATE: ValueTimestamp v = (ValueTimestamp) value.convertTo(Value.TIMESTAMP); long toLong = v.getTimestamp().getTime(); bytes = ValueLong.get(toLong).getBytes(); break; case Value.STRING:
@Override public Value add(Value v) { ValueLong other = (ValueLong) v; long result = value + other.value; int sv = Long.signum(value); int so = Long.signum(other.value); int sr = Long.signum(result); // if the operands have different signs overflow can not occur // if the operands have the same sign, // and the result has a different sign, then it is an overflow // it can not be an overflow when one of the operands is 0 if (sv != so || sr == so || sv == 0 || so == 0) { return ValueLong.get(result); } throw getOverflow(); }
@Override public Value modulus(Value v) { ValueLong other = (ValueLong) v; if (other.value == 0) { throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); } return ValueLong.get(this.value % other.value); }
private Row createRow() { return new Row(new Value[]{ValueLong.get(current)}, Row.MEMORY_CALCULATE); }
@Override public Value multiply(Value v) { ValueLong other = (ValueLong) v; long result = value * other.value; if (value == 0 || value == 1 || other.value == 0 || other.value == 1) { return ValueLong.get(result); } if (isInteger(value) && isInteger(other.value)) { return ValueLong.get(result); } // just checking one case is not enough: Long.MIN_VALUE * -1 // probably this is correct but I'm not sure // if (result / value == other.value && result / other.value == value) { // return ValueLong.get(result); //} BigInteger bv = BigInteger.valueOf(value); BigInteger bo = BigInteger.valueOf(other.value); BigInteger br = bv.multiply(bo); if (br.compareTo(MIN) < 0 || br.compareTo(MAX) > 0) { throw getOverflow(); } return ValueLong.get(br.longValue()); }
@Override public Value subtract(Value v) { ValueLong other = (ValueLong) v; int sv = Long.signum(value); int so = Long.signum(other.value); // if the operands have the same sign, then overflow can not occur // if the second operand is 0, then overflow can not occur if (sv == so || so == 0) { return ValueLong.get(value - other.value); } // now, if the other value is Long.MIN_VALUE, it must be an overflow // x - Long.MIN_VALUE overflows for x>=0 return add(other.negate()); }
@Override Value getValue(Database database, int dataType, boolean distinct) { if (distinct) { throw DbException.throwInternalError(); } Value v = ValueLong.get(count); return v == null ? ValueNull.INSTANCE : v.convertTo(dataType); }
@Override Value getValue(Database database, int dataType, boolean distinct) { if (distinct) { if (distinctValues != null) { count = distinctValues.size(); } else { count = 0; } } Value v = ValueLong.get(count); return v.convertTo(dataType); }
private Table getDualTable(boolean noColumns) { Schema main = database.findSchema(Constants.SCHEMA_MAIN); Expression one = ValueExpression.get(ValueLong.get(1)); return new RangeTable(main, one, one, noColumns); }
private static Value divide(Value a, long by) { if (by == 0) { return ValueNull.INSTANCE; } int type = Value.getHigherOrder(a.getType(), Value.LONG); Value b = ValueLong.get(by).convertTo(type); a = a.convertTo(type).divide(b); return a; }
/** * Sets the value of a parameter. * * @param parameterIndex the parameter index (1, 2, ...) * @param x the value * @throws SQLException if this object is closed */ @Override public void setLong(int parameterIndex, long x) throws SQLException { try { if (isDebugEnabled()) { debugCode("setLong(" + parameterIndex + ", " + x + "L);"); } setParameter(parameterIndex, ValueLong.get(x)); } catch (Exception e) { throw logAndConvert(e); } }
/** * Updates a column in the current or insert row. * * @param columnIndex (1,2,...) * @param x the value * @throws SQLException if the result set is closed or not updatable */ @Override public void updateLong(int columnIndex, long x) throws SQLException { try { if (isDebugEnabled()) { debugCode("updateLong(" + columnIndex + ", " + x + "L);"); } update(columnIndex, ValueLong.get(x)); } catch (Exception e) { throw logAndConvert(e); } }
/** * Updates a column in the current or insert row. * * @param columnLabel the column label * @param x the value * @throws SQLException if the result set is closed or not updatable */ @Override public void updateLong(String columnLabel, long x) throws SQLException { try { if (isDebugEnabled()) { debugCode("updateLong(" + quote(columnLabel) + ", " + x + "L);"); } update(columnLabel, ValueLong.get(x)); } catch (Exception e) { throw logAndConvert(e); } }
@Override public Value getValue(Column column) { if (currentSearchRow == null) { return null; } int columnId = column.getColumnId(); if (columnId == -1) { return ValueLong.get(currentSearchRow.getKey()); } if (current == null) { Value v = currentSearchRow.getValue(columnId); if (v != null) { return v; } current = cursor.get(); if (current == null) { return ValueNull.INSTANCE; } } return current.getValue(columnId); }
@Override Value getValue(Database database, int dataType, boolean distinct) { if (distinct) { count = 0; groupDistinct(database, dataType); } ValueArray[] values = new ValueArray[distinctValues.size()]; int i = 0; for (Value dv : distinctValues.keys()) { AggregateDataHistogram d = distinctValues.get(dv); values[i] = ValueArray.get(new Value[]{dv, ValueLong.get(d.count)}); i++; } final CompareMode compareMode = database.getCompareMode(); Arrays.sort(values, new Comparator<ValueArray>() { @Override public int compare(ValueArray v1, ValueArray v2) { Value a1 = v1.getList()[0]; Value a2 = v2.getList()[0]; return a1.compareTo(a2, compareMode); } }); Value v = ValueArray.get(values); return v.convertTo(dataType); }
protected List<Value> enumRange(Value firstV, Value lastV) { if (isNull(firstV) || isNull(lastV)) { return null; } if (firstV.getType() != lastV.getType()) { return null; } int type = firstV.getType(); switch (type) { case Value.BYTE: case Value.INT: case Value.LONG: case Value.SHORT: if (lastV.subtract(firstV).getLong() > 200) { return null; } List<Value> enumValues = New.arrayList(10); Value enumValue = firstV; CompareMode compareMode = CompareMode.getInstance(null, 0); while (enumValue.compareTo(lastV, compareMode) <= 0) { enumValues.add(enumValue); Value increase = ValueLong.get(1).convertTo(enumValue.getType()); enumValue = enumValue.add(increase); } return enumValues; default: return null; } }