package test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import util.SqlUtil;
import util.StringJoin;

public class WhereTest {
   public static void main(String[] args) {
      testMysql();
      testOracle();
      testKingbase();
      testDm();
      testSqlserver();
      testPostgresql();
      testspark();
      testGauss();
      testGbase();
      testDB2();
      testhbase();
      testhive();
      tetES();
      testmongo();
   }

   static void testMysql() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "mysql");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------mysql--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testOracle() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "oracle");
      params.put("sql", "select (select id from tb2 where tb2.id=a.id) as id from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------oracle--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testKingbase() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "kingbase");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------kingbase--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testDm() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "dm");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------dm--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testspark() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "sparksql");
      params.put("sql", "select * from a where id>1 and exists(select * from tb1 where tb1.name='张三')");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------sparksql--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testSqlserver() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "sqlserver");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------sqlserver--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testDB2() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "db2");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------db2--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testPostgresql() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "postgresql");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------postgresql--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testGauss() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "gauss");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------gauss--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testGbase() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "gbase");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------gbase--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
      params.put("sql", "update tb set name=1 where id>1 and exists(select * from tb1 where tb1.name='张三')");
      strings = sqlUtil.parseWheres(params);
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testhive() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "hive");
      params.put("sql", "select * from a where id>1");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------hive--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testhbase() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "hbase");
      params.put("sql", "scan t1  { Filter=>\"ValueFilter(=,'123') and ValueFilter(<=,'456')\", Filter=>\"SubString('123')\", STARTROW=10,LIMIT=1000 }");
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------hbase--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void testmongo() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "mongodb");
      String sql = "{\"find\": \"emps\", \"filter\": {\"id\":\"123\"}, \"sort\": {}, \"projection\": {\"_id\": 0.0, \"name\": 1.0}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"ox8E0sqfQzW4ULnhWahC8A==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      params.put("sql", sql);
      SqlUtil sqlUtil = new SqlUtil();
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------mongodb--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
   }

   static void tetES() {
      Map<String, String> params = new HashMap();
      params.put("dbType", "es");
      params.put("sql", "get /crm/employee/_search\n{\n    \"query\": {\n        \"match\": {\n            \"_id\": 1\n        }\n    }\n}");
      SqlUtil sqlUtil = new SqlUtil();
      System.out.println((String)params.get("sql"));
      List<String> strings = sqlUtil.parseWheres(params);
      System.out.println("-----------------elastic--------------------------------");
      System.out.println(StringJoin.join(strings, "\n"));
   }
}
