package com.orientechnologies.orient.core.command;

import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by luigidellaquila on 10/02/17.
 */
public class OSqlScriptExecutorTest {

  @Test
  public void testPlain() {
    OrientDB factory = OrientDB.fromUrl("embedded:./", OrientDBConfig.defaultConfig());
    String dbName = getClass().getSimpleName() + "test";
    factory.create(dbName, "root", "root", OrientDB.DatabaseType.MEMORY);
    ODatabaseDocument db = factory.open(dbName, "admin", "admin");

    String script = "insert into V set name ='a';";
    script += "insert into V set name ='b';";
    script += "insert into V set name ='c';";
    script += "insert into V set name ='d';";
    script += "select from v;";

    OResultSet result = db.execute("sql", script);
    List<Object> list = result.stream().map(x -> x.getProperty("name")).collect(Collectors.toList());
    result.close();

    Assert.assertTrue(list.contains("a"));
    Assert.assertTrue(list.contains("b"));
    Assert.assertTrue(list.contains("c"));
    Assert.assertTrue(list.contains("d"));
    Assert.assertEquals(4, list.size());

    db.close();
    factory.drop(dbName, "root", "root");
  }

  @Test
  public void testWithPositionalParams() {
    OrientDB factory = OrientDB.fromUrl("embedded:./", OrientDBConfig.defaultConfig());
    String dbName = getClass().getSimpleName() + "test";
    factory.create(dbName, "root", "root", OrientDB.DatabaseType.MEMORY);
    ODatabaseDocument db = factory.open(dbName, "admin", "admin");

    String script = "insert into V set name ='a';";
    script += "insert into V set name ='b';";
    script += "insert into V set name ='c';";
    script += "insert into V set name ='d';";
    script += "select from v where name = ?;";

    OResultSet result = db.execute("sql", script, "a");
    List<Object> list = result.stream().map(x -> x.getProperty("name")).collect(Collectors.toList());
    result.close();

    Assert.assertTrue(list.contains("a"));

    Assert.assertEquals(1, list.size());

    db.close();
    factory.drop(dbName, "root", "root");
  }

  @Test
  public void testWithNamedParams() {
    OrientDB factory = OrientDB.fromUrl("embedded:./", OrientDBConfig.defaultConfig());
    String dbName = getClass().getSimpleName() + "test";
    factory.create(dbName, "root", "root", OrientDB.DatabaseType.MEMORY);
    ODatabaseDocument db = factory.open(dbName, "admin", "admin");

    String script = "insert into V set name ='a';";
    script += "insert into V set name ='b';";
    script += "insert into V set name ='c';";
    script += "insert into V set name ='d';";
    script += "select from v where name = :name;";

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", "a");
    OResultSet result = db.execute("sql", script, params);
    List<Object> list = result.stream().map(x -> x.getProperty("name")).collect(Collectors.toList());
    result.close();

    Assert.assertTrue(list.contains("a"));

    Assert.assertEquals(1, list.size());

    db.close();
    factory.drop(dbName, "root", "root");
  }
}

@@ -45,16 +45,75 @@
 mmand;%0A%0A
+import com.orientechnologies.orient.core.db.ODatabaseType;%0A
 import c
@@ -653,32 +653,36 @@
 ientDB factory =
+ new
  OrientDB.fromUr
@@ -670,32 +670,24 @@
 new OrientDB
-.fromUrl
 (%22embedded:.
@@ -681,32 +681,48 @@
 B(%22embedded:./%22,
+ %22root%22, %22root%22,
  OrientDBConfig.
@@ -731,32 +731,117 @@
 faultConfig());%0A
+    if (!factory.exists(%22test%22))%0A      factory.create(%22test%22, ODatabaseType.MEMORY);%0A
     String dbNam
@@ -912,33 +912,9 @@
 me, 
-%22root%22, %22root%22, OrientDB.
+O
 Data
@@ -1649,32 +1649,16 @@
 p(dbName
-, %22root%22, %22root%22
 );%0A  %7D%0A%0A
@@ -1722,32 +1722,36 @@
 ientDB factory =
+ new
  OrientDB.fromUr
@@ -1739,32 +1739,24 @@
 new OrientDB
-.fromUrl
 (%22embedded:.
@@ -1750,32 +1750,48 @@
 B(%22embedded:./%22,
+ %22root%22, %22root%22,
  OrientDBConfig.
@@ -1800,32 +1800,117 @@
 faultConfig());%0A
+    if (!factory.exists(%22test%22))%0A      factory.create(%22test%22, ODatabaseType.MEMORY);%0A
     String dbNam
@@ -1981,33 +1981,9 @@
 me, 
-%22root%22, %22root%22, OrientDB.
+O
 Data
@@ -2610,32 +2610,16 @@
 p(dbName
-, %22root%22, %22root%22
 );%0A  %7D%0A%0A
@@ -2686,16 +2686,20 @@
 actory =
+ new
  OrientD
@@ -2703,16 +2703,8 @@
 ntDB
-.fromUrl
 (%22em
@@ -2714,16 +2714,32 @@
 ded:./%22,
+ %22root%22, %22root%22,
  OrientD
@@ -2760,24 +2760,109 @@
 tConfig());%0A
+    if (!factory.exists(%22test%22))%0A      factory.create(%22test%22, ODatabaseType.MEMORY);%0A
     String d
@@ -2937,33 +2937,9 @@
 me, 
-%22root%22, %22root%22, OrientDB.
+O
 Data
@@ -3666,32 +3666,16 @@
 p(dbName
-, %22root%22, %22root%22
 );%0A  %7D%0A%7D

Fix compile errors after DB API refactoring