KurtYoung commented on a change in pull request #10296: [FLINK-14691][table]Add 
use/create/drop/alter database operation and support it in flink/blink planner
URL: https://github.com/apache/flink/pull/10296#discussion_r353029579
 
 

 ##########
 File path: 
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/TableEnvironmentImpl.java
 ##########
 @@ -469,21 +479,84 @@ public void sqlUpdate(String stmt) {
                                createTableOperation.getCatalogTable(),
                                createTableOperation.getTableIdentifier(),
                                createTableOperation.isIgnoreIfExists());
+               } else if (operation instanceof CreateDatabaseOperation) {
+                       CreateDatabaseOperation createDatabaseOperation = 
(CreateDatabaseOperation) operation;
+                       Catalog catalog = 
getCatalogOrThrowException(createDatabaseOperation.getCatalogName());
+                       String exMsg = getDDLOpExecuteErrorMsg("CREATE 
DATABASE", createDatabaseOperation.getCatalogName());
+                       try {
+                               catalog.createDatabase(
+                                               
createDatabaseOperation.getDatabaseName(),
+                                               
createDatabaseOperation.getCatalogDatabase(),
+                                               
createDatabaseOperation.isIgnoreIfExists());
+                       } catch (DatabaseAlreadyExistException e) {
+                               throw new ValidationException(exMsg, e);
+                       } catch (Exception e) {
+                               throw new TableException(exMsg, e);
+                       }
                } else if (operation instanceof DropTableOperation) {
                        DropTableOperation dropTableOperation = 
(DropTableOperation) operation;
                        catalogManager.dropTable(
                                dropTableOperation.getTableIdentifier(),
                                dropTableOperation.isIfExists());
-               } else if (operation instanceof UseCatalogOperation) {
+               } else if (operation instanceof DropDatabaseOperation) {
+                       DropDatabaseOperation dropDatabaseOperation = 
(DropDatabaseOperation) operation;
+                       Catalog catalog = 
getCatalogOrThrowException(dropDatabaseOperation.getCatalogName());
+                       String exMsg = getDDLOpExecuteErrorMsg("DROP DATABASE", 
dropDatabaseOperation.getCatalogName());
+                       try {
+                               catalog.dropDatabase(
+                                               
dropDatabaseOperation.getDatabaseName(),
+                                               
dropDatabaseOperation.isIfExists(),
+                                               
dropDatabaseOperation.isCascade());
+                       } catch (DatabaseNotExistException | 
DatabaseNotEmptyException e) {
+                               throw new ValidationException(exMsg, e);
+                       } catch (Exception e) {
+                               throw new TableException(exMsg, e);
+                       }
+               } else if (operation instanceof AlterDatabaseOperation) {
+                       AlterDatabaseOperation alterDatabaseOperation = 
(AlterDatabaseOperation) operation;
+                       Catalog catalog = 
getCatalogOrThrowException(alterDatabaseOperation.getCatalogName());
+                       String exMsg = getDDLOpExecuteErrorMsg("ALTER 
DATABASE", alterDatabaseOperation.getCatalogName());
+                       try {
+                               catalog.alterDatabase(
+                                               
alterDatabaseOperation.getDatabaseName(),
+                                               
alterDatabaseOperation.getCatalogDatabase(),
+                                               false);
+                       } catch (DatabaseNotExistException e) {
+                               throw new ValidationException(exMsg, e);
+                       } catch (Exception e) {
+                               throw new TableException(exMsg, e);
+                       }
+               } else if (operation instanceof UseOperation) {
+                       applyUseOperation((UseOperation) operation);
+               } else {
+                       throw new 
TableException(UNSUPPORTED_QUERY_IN_SQL_UPDATE_MSG);
+               }
+       }
+
+       /**Apply use operation to current table environment. */
+       private void applyUseOperation(UseOperation operation) {
+               if (operation instanceof UseCatalogOperation) {
                        UseCatalogOperation useCatalogOperation = 
(UseCatalogOperation) operation;
                        
catalogManager.setCurrentCatalog(useCatalogOperation.getCatalogName());
+               } else if (operation instanceof UseDatabaseOperation) {
+                       UseDatabaseOperation useDatabaseOperation = 
(UseDatabaseOperation) operation;
+                       
catalogManager.setCurrentCatalog(useDatabaseOperation.getCatalogName());
+                       
catalogManager.setCurrentDatabase(useDatabaseOperation.getDatabaseName());
                } else {
-                       throw new TableException(
-                               "Unsupported SQL query! sqlUpdate() only 
accepts a single SQL statements of " +
-                                       "type INSERT, CREATE TABLE, DROP TABLE, 
USE CATALOG");
+                       throw new 
TableException(UNSUPPORTED_QUERY_IN_SQL_UPDATE_MSG);
 
 Review comment:
   don't have to throw unsupported operation exception here again. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to