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