private RoutingResult getRoutingResult(TableMate table, SearchRow row) { ShardedTableRule tr = (ShardedTableRule) table.getTableRule(); Column[] ruleCols = table.getRuleColumns(); List<RoutingArgument> args = New.arrayList(ruleCols.length); for (Column ruleCol : ruleCols) { Value v = row.getValue(ruleCol.getColumnId()); v = ruleCol.convert(v); RoutingArgument arg = new RoutingArgument(ruleCol.getName(), v); args.add(arg); } RoutingResult rr; if (args.size() == 1) { RoutingArgument argument = args.get(0); rr = trc.calculate(tr, argument); } else { rr = trc.calculate(tr, args); } if (rr.isMultipleNode()) { throw new TableRoutingException(table.getName() + " routing error."); } return rr; }
protected void setColumns(Column[] columns) { this.columns = columns; if (columnMap.size() > 0) { columnMap.clear(); } for (int i = 0; i < columns.length; i++) { Column col = columns[i]; int dataType = col.getType(); if (dataType == Value.UNKNOWN) { throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1, col.getSQL()); } col.setTable(this, i); String columnName = col.getName(); if (columnMap.get(columnName) != null) { throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, columnName); } columnMap.put(columnName, col); } }
/** * Get the system columns that this table understands. This is used for * compatibility with other databases. The columns are only returned if the * current mode supports system columns. * * @return the system columns */ @Override public Column[] getSystemColumns() { if (!session.getDatabase().getMode().systemColumns) { return null; } Column[] sys = new Column[3]; sys[0] = new Column("oid", Value.INT); sys[0].setTable(table, 0); sys[1] = new Column("ctid", Value.STRING); sys[1].setTable(table, 0); sys[2] = new Column("CTID", Value.STRING); sys[2].setTable(table, 0); return sys; }
public void setColumn(Column column) { table = column.getTable(); sequence = column.getSequence(); if (sequence == null) { throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, column.getSQL()); } }
boolean isIdentity = false; if (readIf("IDENTITY") || readIf("BIGSERIAL")) { column = new Column(columnName, Value.LONG); column.setOriginalSQL("IDENTITY"); parseAutoIncrement(column); column.setPrimaryKey(true); column = new Column(columnName, Value.INT); column.setOriginalSQL("SERIAL"); parseAutoIncrement(column); column.setPrimaryKey(true); column.setNullable(false); } else if (readIf("NULL")) { column.setNullable(true); } else { column.setNullable(defaultNullable & column.isNullable()); column.setComputedExpression(expr); } else if (readIf("DEFAULT")) { Expression defaultExpression = readExpression(); column.setDefaultExpression(session, defaultExpression); } else if (readIf("GENERATED")) { if (!readIf("ALWAYS")) { column.setPrimaryKey(true);
for (int j = 0; j < cols.length; j++) { Column c = cols[j]; Sequence sequence = c.getSequence(); add(rows, identifier(c.getName()), c.getDefaultSQL(), c.isNullable() ? "YES" : "NO", "" + DataType.convertTypeToSQLType(c.getType()), "" + c.getPrecisionAsInt(), "" + c.getPrecisionAsInt(), "" + c.getPrecisionAsInt(), "" + c.getScale(), identifier(DataType.getDataType(c.getType()).name), "" + (c.isNullable() ? DatabaseMetaData.columnNullable : DatabaseMetaData.columnNoNulls) , "" + (c.getComputed() ? "TRUE" : "FALSE"),
scale = dt.defaultScale; displaySize = dt.defaultDisplaySize; column = new Column(columnName, type, prec, scale, displaySize); columns.add(column); type = Value.getHigherOrder(c.getType(), type); prec = Math.max(c.getPrecision(), prec); scale = Math.max(c.getScale(), scale); displaySize = Math.max(c.getDisplaySize(), displaySize); column = new Column(columnName, type, prec, scale, displaySize); columns.set(i, column); row.add(expr); if (c.getType() == Value.UNKNOWN) { c = new Column(c.getName(), Value.STRING, 0, 0, 0); columns.set(i, c);
@Override public void addRow(Value[] values) { TableMate table = toTableMate(prepared.getTable()); Row newRow = table.getTemplateRow(); Column[] columns = prepared.getColumns(); prepared.setCurrentRowNumber(++rowNumber); for (int j = 0, len = columns.length; j < len; j++) { Column c = columns[j]; int index = c.getColumnId(); try { Value v = c.convert(values[j]); newRow.setValue(index, v); } catch (DbException ex) { throw prepared.setRow(ex, rowNumber, Prepared.getSQL(values)); } } addNewRowFlushIfNeed(newRow); }
String columnName = readColumnIdentifier(); Column column = parseColumnForTable(columnName, true); if (column.isAutoIncrement() && column.isPrimaryKey()) { column.setPrimaryKey(false); IndexColumn[] cols = {new IndexColumn()}; cols[0].columnName = column.getName(); AlterTableAddConstraint pk = new AlterTableAddConstraint( session, schema, false); boolean hash = readIf("HASH"); IndexColumn[] cols = {new IndexColumn()}; cols[0].columnName = column.getName(); AlterTableAddConstraint pk = new AlterTableAddConstraint( session, schema, false); column.setNullable(false); } else { readIf("NULL"); column.addCheckConstraint(session, expr);
@Override public String getAlias() { return column == null ? columnName : column.getName(); }
@Override public Expression optimize(ExpressionColumn expressionColumn, Column column) { return expressions[column.getColumnId()]; }
/** * Get the SQL snippet for this index column. * * @return the SQL snippet */ public String getSQL() { StringBuilder buff = new StringBuilder(column.getSQL()); if ((sortType & SortOrder.DESCENDING) != 0) { buff.append(" DESC"); } if ((sortType & SortOrder.NULLS_FIRST) != 0) { buff.append(" NULLS FIRST"); } else if ((sortType & SortOrder.NULLS_LAST) != 0) { buff.append(" NULLS LAST"); } return buff.toString(); } }
buff.append(identifier(forTable)); buff.append(" CHANGE COLUMN "); buff.append(oldColumn.getSQL()).append(' '); buff.append(oldColumn.getCreateSQL()); return SQLTranslated.build().sql(buff.toString()); buff.append(identifier(forTable)); buff.append(" CHANGE COLUMN "); buff.append(oldColumn.getSQL()).append(' '); buff.append(oldColumn.getCreateSQL()); return SQLTranslated.build().sql(buff.toString()); buff.append(identifier(forTable)); buff.append(" ALTER COLUMN "); buff.append(prepared.getOldColumn().getSQL()); buff.append(" SET DEFAULT "); buff.append(prepared.getDefaultExpression().getSQL()); buff.append(identifier(forTable)); buff.append(" CHANGE COLUMN "); buff.append(oldColumn.getSQL()).append(' '); buff.append(newColumn.getCreateSQL()); return SQLTranslated.build().sql(buff.toString()); buff.appendExceptFirst(", "); buff.append(" ADD COLUMN "); buff.append(column.getCreateSQL()); buff.append(identifier(forTable));
int scale = expr.getScale(); int displaySize = expr.getDisplaySize(); Column col = new Column(name, type, precision, scale, displaySize); col.setTable(this, i); Expression checkExpression = fromColumn.getColumn().getCheckConstraint(session, name); if (checkExpression != null) { col.addCheckConstraint(session, checkExpression); cols = new Column[columnNames.length]; for (int i = 0; i < columnNames.length; i++) { cols[i] = new Column(columnNames[i], Value.STRING);
case CAST: { buff.append(args[0].getPreparedSQL(session, parameters)).append(" AS ") .append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL()); break; .append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL()); break;
boolean found = false; for (Column column : columns) { if (column.getColumnId() == key.getColumnId()) { found = true; break; for (Column c : columns) { buff.appendExceptFirst(", "); buff.append(c.getSQL()).append("=?"); for (Column c : keys) { buff.appendExceptFirst(" AND "); buff.append(c.getSQL()).append("=?");
/** * Rename a column of this table. * * @param column the column to rename * @param newName the new column name */ public void renameColumn(Column column, String newName) { for (Column c : columns) { if (c == column) { continue; } if (c.getName().equals(newName)) { throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, newName); } } columnMap.remove(column.getName()); column.rename(newName); columnMap.put(newName, column); }
/** * Convert a value to this column's type. * * @param v the value * @return the value */ public Value convert(Value v) { try { return v.convertTo(type); } catch (DbException e) { if (e.getErrorCode() == ErrorCode.DATA_CONVERSION_ERROR_1) { String target = (table == null ? "" : table.getName() + ": ") + getCreateSQL(); throw DbException.get( ErrorCode.DATA_CONVERSION_ERROR_1, v.getSQL() + " (" + target + ")"); } throw e; } }
/** * Create a new range with the given start and end expressions. * * @param schema the schema (always the main schema) * @param min the start expression * @param max the end expression * @param noColumns whether this table has no columns */ public RangeTable(Schema schema, Expression min, Expression max, boolean noColumns) { super(schema, NAME); Column[] cols = noColumns ? new Column[0] : new Column[] { new Column("X", Value.LONG) }; this.min = min; this.max = max; setColumns(cols); }