提交 f950d686 authored 作者: alexpaschenko's avatar alexpaschenko 提交者: Sergi Vladykin

Added WITH to CREATE SCHEMA to supply default TableEngine params (#437)

* Added WITH to CREATE SCHEMA to supply default TableEngine params

* Updated docs to reflect changes in CREATE SCHEMA

* Review fixes - docs; code de-duplication; test for table engine params in schema

* Setter for table engine params in Schema
上级 8cef94bb
...@@ -577,10 +577,13 @@ CREATE ROLE READONLY ...@@ -577,10 +577,13 @@ CREATE ROLE READONLY
"Commands (DDL)","CREATE SCHEMA"," "Commands (DDL)","CREATE SCHEMA","
CREATE SCHEMA [ IF NOT EXISTS ] name [ AUTHORIZATION ownerUserName ] CREATE SCHEMA [ IF NOT EXISTS ] name [ AUTHORIZATION ownerUserName ]
[ WITH tableEngineParamName [,...] ]
"," ","
Creates a new schema. If no owner is specified, the current user is used. The Creates a new schema. If no owner is specified, the current user is used. The
user that executes the command must have admin rights, as well as the owner. user that executes the command must have admin rights, as well as the owner.
Specifying the owner currently has no effect. Specifying the owner currently has no effect.
Optional table engine parameters are used when CREATE TABLE command
is run on this schema without having its engine params set.
This command commits an open transaction in this connection. This command commits an open transaction in this connection.
"," ","
......
...@@ -1683,6 +1683,15 @@ CREATE TABLE TEST(ID INT, NAME VARCHAR) ENGINE "acme.MyTableEngine" WITH "param1 ...@@ -1683,6 +1683,15 @@ CREATE TABLE TEST(ID INT, NAME VARCHAR) ENGINE "acme.MyTableEngine" WITH "param1
<p> <p>
In which case the parameters are passed down in the tableEngineParams field of the CreateTableData object. In which case the parameters are passed down in the tableEngineParams field of the CreateTableData object.
</p> </p>
<p>
It is also possible to specify default table engine params on schema creation:
</p>
<pre>
CREATE SCHEMA TEST_SCHEMA WITH "param1", "param2";
</pre>
<p>
Params from the schema are used when CREATE TABLE issued on this schema does not have its own engine params specified.
</p>
<h2 id="triggers">Triggers</h2> <h2 id="triggers">Triggers</h2>
<p> <p>
......
...@@ -4560,9 +4560,20 @@ public class Parser { ...@@ -4560,9 +4560,20 @@ public class Parser {
} else { } else {
command.setAuthorization(session.getUser().getName()); command.setAuthorization(session.getUser().getName());
} }
if (readIf("WITH")) {
command.setTableEngineParams(readTableEngineParams());
}
return command; return command;
} }
private ArrayList<String> readTableEngineParams() {
ArrayList<String> tableEngineParams = New.arrayList();
do {
tableEngineParams.add(readUniqueIdentifier());
} while (readIf(","));
return tableEngineParams;
}
private CreateSequence parseCreateSequence() { private CreateSequence parseCreateSequence() {
boolean ifNotExists = readIfNotExists(); boolean ifNotExists = readIfNotExists();
String sequenceName = readIdentifierWithSchema(); String sequenceName = readIdentifierWithSchema();
...@@ -6129,11 +6140,7 @@ public class Parser { ...@@ -6129,11 +6140,7 @@ public class Parser {
} }
} }
if (readIf("WITH")) { if (readIf("WITH")) {
ArrayList<String> tableEngineParams = New.arrayList(); command.setTableEngineParams(readTableEngineParams());
do {
tableEngineParams.add(readUniqueIdentifier());
} while (readIf(","));
command.setTableEngineParams(tableEngineParams);
} }
// MySQL compatibility // MySQL compatibility
if (readIf("AUTO_INCREMENT")) { if (readIf("AUTO_INCREMENT")) {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
package org.h2.command.ddl; package org.h2.command.ddl;
import java.util.ArrayList;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface; import org.h2.command.CommandInterface;
import org.h2.engine.Database; import org.h2.engine.Database;
...@@ -22,6 +23,7 @@ public class CreateSchema extends DefineCommand { ...@@ -22,6 +23,7 @@ public class CreateSchema extends DefineCommand {
private String schemaName; private String schemaName;
private String authorization; private String authorization;
private boolean ifNotExists; private boolean ifNotExists;
private ArrayList<String> tableEngineParams;
public CreateSchema(Session session) { public CreateSchema(Session session) {
super(session); super(session);
...@@ -49,6 +51,7 @@ public class CreateSchema extends DefineCommand { ...@@ -49,6 +51,7 @@ public class CreateSchema extends DefineCommand {
} }
int id = getObjectId(); int id = getObjectId();
Schema schema = new Schema(db, id, schemaName, user, false); Schema schema = new Schema(db, id, schemaName, user, false);
schema.setTableEngineParams(tableEngineParams);
db.addDatabaseObject(session, schema); db.addDatabaseObject(session, schema);
return 0; return 0;
} }
...@@ -61,6 +64,10 @@ public class CreateSchema extends DefineCommand { ...@@ -61,6 +64,10 @@ public class CreateSchema extends DefineCommand {
this.authorization = userName; this.authorization = userName;
} }
public void setTableEngineParams(ArrayList<String> tableEngineParams) {
this.tableEngineParams = tableEngineParams;
}
@Override @Override
public int getType() { public int getType() {
return CommandInterface.CREATE_SCHEMA; return CommandInterface.CREATE_SCHEMA;
......
...@@ -38,6 +38,7 @@ public class Schema extends DbObjectBase { ...@@ -38,6 +38,7 @@ public class Schema extends DbObjectBase {
private User owner; private User owner;
private final boolean system; private final boolean system;
private ArrayList<String> tableEngineParams;
private final ConcurrentHashMap<String, Table> tablesAndViews; private final ConcurrentHashMap<String, Table> tablesAndViews;
private final ConcurrentHashMap<String, Index> indexes; private final ConcurrentHashMap<String, Index> indexes;
...@@ -176,6 +177,23 @@ public class Schema extends DbObjectBase { ...@@ -176,6 +177,23 @@ public class Schema extends DbObjectBase {
return owner; return owner;
} }
/**
* Get table engine params of this schema.
*
* @return default table engine params
*/
public ArrayList<String> getTableEngineParams() {
return tableEngineParams;
}
/**
* Set table engine params of this schema.
* @param tableEngineParams default table engine params
*/
public void setTableEngineParams(ArrayList<String> tableEngineParams) {
this.tableEngineParams = tableEngineParams;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, SchemaObject> getMap(int type) { private Map<String, SchemaObject> getMap(int type) {
Map<String, ? extends SchemaObject> result; Map<String, ? extends SchemaObject> result;
...@@ -588,6 +606,9 @@ public class Schema extends DbObjectBase { ...@@ -588,6 +606,9 @@ public class Schema extends DbObjectBase {
} }
} }
if (data.tableEngine != null) { if (data.tableEngine != null) {
if (data.tableEngineParams == null)
data.tableEngineParams = this.tableEngineParams;
return database.getTableEngine(data.tableEngine).createTable(data); return database.getTableEngine(data.tableEngine).createTable(data);
} }
return new RegularTable(data); return new RegularTable(data);
......
...@@ -78,6 +78,7 @@ public class TestTableEngines extends TestBase { ...@@ -78,6 +78,7 @@ public class TestTableEngines extends TestBase {
testSubQueryInfo(); testSubQueryInfo();
testEarlyFilter(); testEarlyFilter();
testEngineParams(); testEngineParams();
testSchemaEngineParams();
testSimpleQuery(); testSimpleQuery();
testMultiColumnTreeSetIndex(); testMultiColumnTreeSetIndex();
testBatchedJoin(); testBatchedJoin();
...@@ -133,6 +134,23 @@ public class TestTableEngines extends TestBase { ...@@ -133,6 +134,23 @@ public class TestTableEngines extends TestBase {
deleteDb("tableEngine"); deleteDb("tableEngine");
} }
private void testSchemaEngineParams() throws SQLException {
deleteDb("tableEngine");
Connection conn = getConnection("tableEngine");
Statement stat = conn.createStatement();
stat.execute("CREATE SCHEMA s1 WITH \"param1\", \"param2\"");
stat.execute("CREATE TABLE s1.t1(id int, name varchar) ENGINE \"" + EndlessTableEngine.class.getName() + '\"');
assertEquals(2,
EndlessTableEngine.createTableData.tableEngineParams.size());
assertEquals("param1",
EndlessTableEngine.createTableData.tableEngineParams.get(0));
assertEquals("param2",
EndlessTableEngine.createTableData.tableEngineParams.get(1));
conn.close();
deleteDb("tableEngine");
}
private void testSimpleQuery() throws SQLException { private void testSimpleQuery() throws SQLException {
deleteDb("tableEngine"); deleteDb("tableEngine");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论