@Override public String getAlias() { return column == null ? columnName : column.getName(); }
@Override public String getColumnName() { return columnName != null ? columnName : column.getName(); }
/** * 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); }
/** * Keep a collection of the columns to pass to update if a duplicate key * happens, for MySQL-style INSERT ... ON DUPLICATE KEY UPDATE .... * * @param column the column * @param expression the expression */ public void addAssignmentForDuplicate(Column column, Expression expression) { if (duplicateKeyAssignmentMap == null) { duplicateKeyAssignmentMap = New.hashMap(); } if (duplicateKeyAssignmentMap.containsKey(column)) { throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, column.getName()); } duplicateKeyAssignmentMap.put(column, expression); }
/** * Add an assignment of the form column = expression. * * @param column the column * @param expression the expression */ public void setAssignment(Column column, Expression expression) { if (expressionMap.containsKey(column)) { throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, column .getName()); } columns.add(column); expressionMap.put(column, expression); if (expression instanceof Parameter) { Parameter p = (Parameter) expression; p.setColumn(column); } }
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); } }
private int expandColumnList(TableFilter filter, int index) { Table t = filter.getTable(); String alias = filter.getTableAlias(); Column[] columns = t.getColumns(); for (Column c : columns) { if (filter.isNaturalJoinColumn(c)) { continue; } ExpressionColumn ec = new ExpressionColumn(session.getDatabase(), null, alias, c.getName()); expressions.add(index++, ec); } return index; }
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; }
Column c = columns.get(i); if (c.getType() == Value.UNKNOWN) { c = new Column(c.getName(), Value.STRING, 0, 0, 0); columns.set(i, c);
for (Column column : ruleColumns) { if(valueMap.get(column) != null) { throw DbException.get(ErrorCode.SHARDING_COLUMNS_CANNOT_BE_MODIFIED, column.getName());
List<RoutingArgument> args = New.arrayList(ruleCols.length); for (Column ruleCol : ruleCols) { String ruleColName = ruleCol.getName(); RoutingArgument arg; int idx = ruleCol.getColumnId();
for (Column column : ruleColumns) { if (valueMap.get(column) != null) { throw DbException.get(ErrorCode.SHARDING_COLUMNS_CANNOT_BE_MODIFIED, column.getName());
private StatementBuilder uniqueConstraint(AlterTableAddConstraint prepared, ObjectNode node, String keyType) { StatementBuilder buff = new StatementBuilder("ALTER TABLE "); buff.append(identifier(node.getCompositeObjectName())).append(" ADD CONSTRAINT "); String constraintName = prepared.getConstraintName(); // MySQL constraintName is optional if (!StringUtils.isNullOrEmpty(constraintName)) { buff.append(constraintName); } buff.append(' ').append(keyType); if (prepared.isPrimaryKeyHash()) { buff.append(" USING ").append("HASH"); } buff.append('('); for (IndexColumn c : prepared.getIndexColumns()) { buff.appendExceptFirst(", "); buff.append(identifier(c.column.getName())); } buff.append(')'); return buff; }
@Override public SQLTranslated translate(Column[] searchColumns, TableFilter filter, ObjectNode node) { // can not use the field sqlStatement because the parameter // indexes may be incorrect: ? may be in fact ?2 for a subquery // but indexes may be set manually as well if (node instanceof GroupObjectNode) { return translate(searchColumns, filter, (GroupObjectNode) node); } List<Value> params = New.arrayList(10); StatementBuilder buff = new StatementBuilder("SELECT"); int visibleColumnCount = searchColumns.length; for (int i = 0; i < visibleColumnCount; i++) { buff.appendExceptFirst(","); buff.append(' '); buff.append(identifier(searchColumns[i].getName())); } buff.append(" FROM "); buff.append(identifier(node.getCompositeObjectName())); buff.append(" AS "); buff.append(filter.getTableAlias()); Expression condition = filter.getFilterCondition(); if (condition != null) { buff.append(" WHERE ").append(StringUtils.unEnclose(condition.getPreparedSQL(filter.getSession(), params))); } return SQLTranslated.build().sql(buff.toString()).sqlParams(params); }
for (IndexColumn c : prepared.getIndexColumns()) { sql.appendExceptFirst(", "); sql.append(identifier(c.column.getName()));
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);