提交 237dda41 authored 作者: Sergey Kalashnikov's avatar Sergey Kalashnikov 提交者: Sergi Vladykin

Added support for invisible columns (like in Oracle) (#464)

上级 237bbf86
......@@ -258,13 +258,14 @@ ALTER TABLE TEST RENAME CONSTRAINT FOO TO BAR
"Commands (DDL)","ALTER TABLE ALTER COLUMN","
ALTER TABLE [ IF EXISTS ] tableName ALTER COLUMN columnName
{ { dataType [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] }
{ { dataType [ VISIBLE | INVISIBLE ] [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] }
| { RENAME TO name }
| { RESTART WITH long }
| { SELECTIVITY int }
| { SET DEFAULT expression }
| { SET NULL }
| { SET NOT NULL } }
| { SET NOT NULL }
| { SET { VISIBLE | INVISIBLE } } }
","
Changes the data type of a column, rename a column,
change the identity value, or change the selectivity.
......@@ -287,6 +288,9 @@ Single column indexes on this column are dropped.
SET NOT NULL sets a column to not allow NULL. Rows may not contains NULL in this column.
SET INVISIBLE makes the column hidden, i.e. it will not appear in SELECT * results.
SET VISIBLE has the reverse effect.
This command commits an open transaction in this connection.
","
ALTER TABLE TEST ALTER COLUMN NAME CLOB;
......@@ -296,6 +300,8 @@ ALTER TABLE TEST ALTER COLUMN NAME SELECTIVITY 100;
ALTER TABLE TEST ALTER COLUMN NAME SET DEFAULT '';
ALTER TABLE TEST ALTER COLUMN NAME SET NOT NULL;
ALTER TABLE TEST ALTER COLUMN NAME SET NULL;
ALTER TABLE TEST ALTER COLUMN NAME SET VISIBLE;
ALTER TABLE TEST ALTER COLUMN NAME SET INVISIBLE;
"
"Commands (DDL)","ALTER TABLE DROP COLUMN","
......@@ -1759,6 +1765,7 @@ AES
"Other Grammar","Column Definition","
columnName dataType
[ VISIBLE | INVISIBLE ]
[ { DEFAULT expression | AS computedColumnExpression } ] [ [ NOT ] NULL ]
[ { AUTO_INCREMENT | IDENTITY } [ ( startInt [, incrementInt ] ) ] ]
[ SELECTIVITY selectivity ] [ COMMENT expression ]
......@@ -1771,12 +1778,16 @@ Identity and auto-increment columns are columns with a sequence as the
default. The column declared as the identity columns is implicitly the
primary key column of this table (unlike auto-increment columns).
The invisible column will not be displayed as a result of SELECT * query.
Otherwise, it works as normal column.
The options PRIMARY KEY, UNIQUE, and CHECK are not supported for ALTER statements.
Check constraints can reference columns of the table,
and they can reference objects that exist while the statement is executed.
Conditions are only checked when a row is added or modified
in the table where the constraint exists.
","
CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255) DEFAULT '');
CREATE TABLE TEST(ID BIGINT IDENTITY);
......
......@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul>
<li>Added support for invisible columns.
</li>
</ul>
<h2>Version 1.4.194 (2017-03-10)</h2>
......
......@@ -461,6 +461,11 @@ public interface CommandInterface {
*/
int EXPLAIN_ANALYZE = 86;
/**
* The type of a ALTER TABLE ALTER COLUMN SET INVISIBLE statement.
*/
int ALTER_TABLE_ALTER_COLUMN_VISIBILITY = 87;
/**
* Get command type.
*
......
......@@ -3993,6 +3993,11 @@ public class Parser {
} else {
column = parseColumnWithType(columnName);
}
if (readIf("INVISIBLE")) {
column.setVisible(false);
} else if (readIf("VISIBLE")) {
column.setVisible(true);
}
if (readIf("NOT")) {
read("NULL");
column.setNullable(false);
......@@ -5718,6 +5723,14 @@ public class Parser {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT);
command.setDefaultExpression(defaultExpression);
return command;
} else if (readIf("INVISIBLE")) {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY);
command.setVisible(false);
return command;
} else if (readIf("VISIBLE")) {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY);
command.setVisible(true);
return command;
}
} else if (readIf("RESTART")) {
readIf("WITH");
......
......@@ -44,6 +44,8 @@ import org.h2.util.New;
* ALTER TABLE ALTER COLUMN SET DEFAULT,
* ALTER TABLE ALTER COLUMN SET NOT NULL,
* ALTER TABLE ALTER COLUMN SET NULL,
* ALTER TABLE ALTER COLUMN SET VISIBLE,
* ALTER TABLE ALTER COLUMN SET INVISIBLE,
* ALTER TABLE DROP COLUMN
*/
public class AlterTableAlterColumn extends SchemaCommand {
......@@ -60,6 +62,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
private boolean ifNotExists;
private ArrayList<Column> columnsToAdd;
private ArrayList<Column> columnsToRemove;
private boolean newVisibility;
public AlterTableAlterColumn(Session session, Schema schema) {
super(session, schema);
......@@ -156,9 +159,13 @@ public class AlterTableAlterColumn extends SchemaCommand {
} else if (!oldColumn.isNullable() && newColumn.isNullable()) {
checkNullable(table);
}
if (oldColumn.getVisible() ^ newColumn.getVisible()) {
oldColumn.setVisible(newColumn.getVisible());
}
convertAutoIncrementColumn(table, newColumn);
copyData(table);
}
table.setModified();
break;
}
case CommandInterface.ALTER_TABLE_ADD_COLUMN: {
......@@ -192,6 +199,12 @@ public class AlterTableAlterColumn extends SchemaCommand {
db.updateMeta(session, table);
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY: {
oldColumn.setVisible(newVisibility);
table.setModified();
db.updateMeta(session, table);
break;
}
default:
DbException.throwInternalError("type=" + type);
}
......@@ -550,4 +563,8 @@ public class AlterTableAlterColumn extends SchemaCommand {
public void setColumnsToRemove(ArrayList<Column> columnsToRemove) {
this.columnsToRemove = columnsToRemove;
}
public void setVisible(boolean visible) {
this.newVisibility = visible;
}
}
......@@ -731,6 +731,9 @@ public class Select extends Query {
String alias = filter.getTableAlias();
Column[] columns = t.getColumns();
for (Column c : columns) {
if (!c.getVisible()) {
continue;
}
if (filter.isNaturalJoinColumn(c)) {
continue;
}
......
......@@ -98,13 +98,14 @@ ALTER TABLE [ IF EXISTS ] tableName RENAME oldConstraintName TO newConstraintNam
Renames a constraint."
"Commands (DDL)","ALTER TABLE ALTER COLUMN","
ALTER TABLE [ IF EXISTS ] tableName ALTER COLUMN columnName
{ { dataType [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] }
{ { dataType [ VISIBLE | INVISIBLE ] [ DEFAULT expression ] [ [ NOT ] NULL ] [ AUTO_INCREMENT | IDENTITY ] }
| { RENAME TO name }
| { RESTART WITH long }
| { SELECTIVITY int }
| { SET DEFAULT expression }
| { SET NULL }
| { SET NOT NULL } }
| { SET NOT NULL }
| { SET { VISIBLE | INVISIBLE } } }
","
Changes the data type of a column, rename a column,
change the identity value, or change the selectivity."
......@@ -555,6 +556,7 @@ AES
Only the algorithm AES (""AES-128"") is supported currently."
"Other Grammar","Column Definition","
columnName dataType
[ VISIBLE | INVISIBLE ]
[ { DEFAULT expression | AS computedColumnExpression } ] [ [ NOT ] NULL ]
[ { AUTO_INCREMENT | IDENTITY } [ ( startInt [, incrementInt ] ) ] ]
[ SELECTIVITY selectivity ] [ COMMENT expression ]
......
......@@ -86,6 +86,7 @@ public class Column {
private SingleColumnResolver resolver;
private String comment;
private boolean primaryKey;
private boolean visible = true;
public Column(String name, int type) {
this(name, type, -1, -1, -1);
......@@ -257,6 +258,14 @@ public class Column {
nullable = b;
}
public boolean getVisible() {
return visible;
}
public void setVisible(boolean b) {
visible = b;
}
/**
* Validate the value, convert it if required, and update the sequence value
* if required. If the value is null, the default value (NULL if no default
......@@ -436,6 +445,11 @@ public class Column {
default:
}
}
if (!visible) {
buff.append(" INVISIBLE ");
}
if (defaultExpression != null) {
String sql = defaultExpression.getSQL();
if (sql != null) {
......@@ -748,6 +762,7 @@ public class Column {
isComputed = source.isComputed;
selectivity = source.selectivity;
primaryKey = source.primaryKey;
visible = source.visible;
}
}
......@@ -4915,6 +4915,22 @@ SELECT group_concat(name) FROM TEST group by id;
drop table test;
> ok
create table test(a int primary key, b int invisible, c int);
> ok
select * from test;
> A C
> - -
> rows: 0
select a, b, c from test;
> A B C
> - - -
> rows: 0
drop table test;
> ok
--- script drop ---------------------------------------------------------------------------------------------
create memory table test (id int primary key, im_ie varchar(10));
> ok
......@@ -6183,6 +6199,49 @@ SELECT * FROM TEST;
DROP TABLE TEST;
> ok
create table test(id int, name varchar invisible);
> ok
select * from test;
> ID
> --
> rows: 0
alter table test alter column name set visible;
> ok
select * from test;
> ID NAME
> -- ----
> rows: 0
alter table test add modify_date timestamp invisible before name;
> ok
select * from test;
> ID NAME
> -- ----
> rows: 0
alter table test alter column modify_date timestamp visible;
> ok
select * from test;
> ID MODIFY_DATE NAME
> -- ----------- ----
> rows: 0
alter table test alter column modify_date set invisible;
> ok
select * from test;
> ID NAME
> -- ----
> rows: 0
drop table test;
> ok
--- autoIncrement ----------------------------------------------------------------------------------------------
CREATE MEMORY TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR);
> ok
......
......@@ -697,7 +697,7 @@ vast vector vectors vendor venue verbatim verbose verification verified verifier
verifies verify verifying versa version versioned versioning versions versus
vertica vertical very verysmallint veto via vice victor videos view viewed viewer
viewport views vii viii violate violated violation violations virtual virus
viruses visible vision visit visited visitor visitors vista visual visualize
viruses visible visibility vision visit visited visitor visitors vista visual visualize
visualizer vividsolutions vladykin void volatile volume volunteer volunteers von
vpda vulnerabilities vulnerability wait waited waiting waits waives wake wakes
walk walker want wants war warehouse warehouses warn warned warning warnings
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论