/* Generated By:JJTree: Do not edit this line. OCreateDatabaseStatement.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=true,NODE_PREFIX=O,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
package com.orientechnologies.orient.core.sql.parser;

import com.orientechnologies.orient.core.command.OServerCommandContext;
import com.orientechnologies.orient.core.db.*;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.sql.executor.OInternalResultSet;
import com.orientechnologies.orient.core.sql.executor.OResultInternal;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import java.util.Locale;

public class OCreateDatabaseStatement extends OSimpleExecServerStatement {

  protected OIdentifier name;
  protected OIdentifier type;
  protected boolean ifNotExists = false;
  protected OJson config;

  public OCreateDatabaseStatement(int id) {
    super(id);
  }

  public OCreateDatabaseStatement(OrientSql p, int id) {
    super(p, id);
  }

  @Override
  public OResultSet executeSimple(OServerCommandContext ctx) {
    OrientDBInternal server = ctx.getServer();
    OResultInternal result = new OResultInternal();
    result.setProperty("operation", "create database");
    result.setProperty("name", name.getStringValue());

    ODatabaseType dbType;
    try {

      dbType = ODatabaseType.valueOf(type.getStringValue().toUpperCase(Locale.ENGLISH));
    } catch (IllegalArgumentException ex) {
      throw new OCommandExecutionException("Invalid db type: " + type.getStringValue());
    }
    if (ifNotExists && server.exists(name.getStringValue(), null, null)) {
      result.setProperty("created", false);
      result.setProperty("existing", true);
    } else {
      try {
        if (config == null) {
          server.create(name.getStringValue(), null, null, dbType);
        } else {
          server.create(name.getStringValue(), null, null, dbType, toOrientDBConfig(config, ctx));
        }
        result.setProperty("created", true);
      } catch (Exception e) {
        throw new OCommandExecutionException(
            "Could not create database " + type.getStringValue() + ":" + e.getMessage());
      }
    }

    OInternalResultSet rs = new OInternalResultSet();
    rs.add(result);
    return rs;
  }

  private OrientDBConfig toOrientDBConfig(OJson config, OServerCommandContext ctx) {
    OrientDBConfigBuilder builder = new OrientDBConfigBuilder();
    builder.fromMap(config.toMap(new OResultInternal(), ctx));
    return builder.build();
  }

  /** Accept the visitor. */
  public Object jjtAccept(OrientSqlVisitor visitor, Object data) {
    return visitor.visit(this, data);
  }
}
/* JavaCC - OriginalChecksum=99888a0f8bb929dce0904816cd51fefe (do not edit this line) */

@@ -376,16 +376,86 @@
 ontext;%0A
+import com.orientechnologies.orient.core.config.OGlobalConfiguration;%0A
 import c
@@ -807,16 +807,38 @@
 .Locale;
+%0Aimport java.util.Map;
 %0A%0Apublic
@@ -2620,24 +2620,40 @@
     
-builder.fromMap(
+Map%3CString, Object%3E configMap = 
 conf
@@ -2692,38 +2692,833 @@
 ctx)
-);%0A    return builder.build();
+;%0A    Object globalConfig = configMap.get(%22config%22);%0A    if (globalConfig != null && globalConfig instanceof Map) %7B%0A      ((Map%3CString, Object%3E) globalConfig)%0A          .entrySet().stream()%0A              .filter(x -%3E OGlobalConfiguration.findByKey(x.getKey()) != null)%0A              .forEach(%0A                  x -%3E builder.addConfig(OGlobalConfiguration.findByKey(x.getKey()), x.getValue()));%0A    %7D%0A    return builder.build();%0A  %7D%0A%0A  @Override%0A  public void toString(Map%3CObject, Object%3E params, StringBuilder builder) %7B%0A    builder.append(%22CREATE DATABASE %22);%0A    name.toString(params, builder);%0A    builder.append(%22 %22);%0A    type.toString(params, builder);%0A    if (ifNotExists) %7B%0A      builder.append(%22 IF NOT EXISTS%22);%0A    %7D%0A    if (config != null) %7B%0A      builder.append(%22 %22);%0A      config.toString(params, builder);%0A    %7D
 %0A  %7D

Implement passing of global configuration values in CREATE DATABASE via SQL