package util.sqlparse.visitor.mongo.test;

import bean.DataBase;
import bean.Schema;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import util.JdbcUtil;
import util.SqlUtil;
import util.getdata.DatabaseCache;

public class MongoValueReplaceTest {
   public static void main(String[] args) throws Exception {
      String content = "{\n      \"ip_port\": \"192.168.2.118:27017\",\n      \"url\": \"jdbc:mongodb://192.168.2.118:27017/test\",\n      \"username\": \"\",\n      \"isIgnorecase\":\"0\",\n      \"password\": \"\",\n      \"dbType\": \"mongodb\",\n      \"servername\": \"test\",\n      \"excludeSchema\": [\"encryption_test2\",\"ghca_static_develop\",\"ghca_static_develop_test\",\"pg_catalog\",\"information_schema\",\"sysaudit\",\"sysmac\",\"sys\",\"sysmac\",\"sysmac\",\"sysmac\",\"ry\"]\n    }";
      JdbcUtil.initData(content);
      Thread.sleep(3000L);
      MongoValueReplaceTest myTest = new MongoValueReplaceTest();
      myTest.test();
   }

   public void test() throws Exception {
      DataBase dataBase = (DataBase)JdbcUtil.dataBaseList.get(0);
      DatabaseCache simpleCache = dataBase.simpleCache;
      Schema ry = simpleCache.getSchema("ry");
      System.out.println(ry == null ? "" : ry.getName());
      Map<String, String> params = new HashMap();
      params.put("dbType", "mongodb");
      params.put("schema", "test");
      params.put("url", "jdbc:mongodb://192.168.2.118:27017/test");
      params.put("replaceSchema", ".*");
      params.put("replaceNewTable", "people_bk");
      params.put("replaceTable", ".*");
      params.put("replaceColumn", ".*");
      params.put("expression", "concat(substr(${value},1,1),'**')");
      params.put("limit", "3");
      String sql = "{\"_id\": 10.0, \"item\": \"box\", \"qty\": 20.0}\r\n\r\n{\"insert\": \"products\", \"ordered\": true, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"tTKUMMVNT0yaK7+e5nlT7A==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"_id\": 11.0, \"item\": \"pencil\", \"qty\": 50.0, \"type\": \"no.2\"}\r\n\r\n{\"_id\": {\"$oid\": \"63e9abc504f19fe3b33d3f9b\"}, \"item\": \"pen\", \"qty\": 20.0}\r\n\r\n{\"_id\": {\"$oid\": \"63e9abc504f19fe3b33d3f9c\"}, \"item\": \"eraser\", \"qty\": 25.0}\r\n\r\n{\"insert\": \"products\", \"ordered\": false, \"writeConcern\": {\"w\": 2.0, \"wtimeout\": 5000.0}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"tTKUMMVNT0yaK7+e5nlT7A==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"find\": \"inventory\", \"filter\": {\"item\": \"pencil\"}, \"sort\": {\"item\": 1}, \"projection\": {\"_id\": 1.0, \"item\": 1.0,\"item2\": 1.0}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"ox8E0sqfQzW4ULnhWahC8A==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"find\": \"inventory\", \"filter\": {\"item\": {$eq: \"pencil\"}}, \"sort\": {\"item\": 1}, \"projection\": {\"_id\": 1.0, \"item\": 1.0,\"item2\": 1.0}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"ox8E0sqfQzW4ULnhWahC8A==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"find\": \"inventory\", \"filter\": {\"tags\": [\"red\", \"blank\"]}, \"lsid\": {\"id\": {\"$binary\": \"0SpwXHPUTqiYDd7FXdddcw==\", \"$type\": \"04\"}}, \"$db\": \"test\"}";
      sql = "{\"find\": \"inventory\", \"filter\": { \"status\": { $in: [ \"A\", \"D\" ] } }, \"lsid\": {\"id\": {\"$binary\": \"0SpwXHPUTqiYDd7FXdddcw==\", \"$type\": \"04\"}}, \"$db\": \"test\"}";
      sql = "{\"find\": \"inventory\", \"filter\": { \"status\": \"A\", \"qty\": { $lt: 30 } }, \"lsid\": {\"id\": {\"$binary\": \"0SpwXHPUTqiYDd7FXdddcw==\", \"$type\": \"04\"}}, \"$db\": \"test\"}";
      sql = "{\"find\": \"inventory\", \"filter\": { \"status\": \"A\", \"qty\": { $lt: \"sd\" } }, \"lsid\": {\"id\": {\"$binary\": \"0SpwXHPUTqiYDd7FXdddcw==\", \"$type\": \"04\"}}, \"$db\": \"test\"}";
      sql = "{\"find\": \"inventory\", \"filter\": { $or: [ { \"status\": \"A\" }, { \"qty\": { $lt: 30 } } ] }, \"lsid\": {\"id\": {\"$binary\": \"0SpwXHPUTqiYDd7FXdddcw==\", \"$type\": \"04\"}}, \"$db\": \"test\"}";
      sql = "{\"q\": {\"stock\": {\"$lte\": 10.0}}, \"u\": {\"$set\": {\"reorder\": true}}, \"multi\": true, \"upsert\": false,\"hint\": {\"status\": 1.0}}\r\n\r\n{\"update\": \"books\", \"ordered\": true,\"writeConcern\": {\"w\": 2.0, \"wtimeout\": 5000.0},\"collation\": {\"locale\": \"fr\", \"strength\": 1.0},\"let\": {\"targetFlavor\": \"strawberry\"},\"lsid\": {\"id\": {\"$binary\": {\"base64\": \"tTKUMMVNT0yaK7+e5nlT7A==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"q\": {\"stock\": {\"$eq\": \"dd\"}}, \"u\": {\"$set\": {\"reorder\": \"dd\"}}, \"multi\": true, \"upsert\": false,\"hint\": {\"status\": 1.0}}\r\n\r\n{\"update\": \"books\", \"ordered\": true,\"writeConcern\": {\"w\": 2.0, \"wtimeout\": 5000.0},\"collation\": {\"locale\": \"fr\", \"strength\": 1.0},\"let\": {\"targetFlavor\": \"strawberry\"},\"lsid\": {\"id\": {\"$binary\": {\"base64\": \"tTKUMMVNT0yaK7+e5nlT7A==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"findandmodify\": \"grades\", \"query\": {\"name\": \"A. MacDyver\"}, \"update\": {\"$inc\": {\"points\": 5.0}}, \"sort\": {\"points\": 1.0}, \"fields\": {\"assignment\": 1.0, \"points\": 1.0}, \"upsert\": false, \"new\": false, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"m5cjtkg4QjWvZFJyXZ+9Cw==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"findandmodify\": \"grades\", \"query\": {\"name\": \"A. MacDyver\"}, \"update\": {\"points\": \"dd\"}, \"sort\": {\"points\": 1.0}, \"fields\": {\"assignment\": 1.0, \"points\": 1.0}, \"upsert\": false, \"new\": false, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"m5cjtkg4QjWvZFJyXZ+9Cw==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"findandmodify\": \"scores\", \"query\": {\"name\": \"A. MacDyver\"}, \"remove\": true, \"sort\": {\"points\": 1.0}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"m5cjtkg4QjWvZFJyXZ+9Cw==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"findandmodify\": \"scores\", \"query\": {\"score\": {\"$lt\": 20000.0}}, \"update\": {\"team\": \"Observant Badgers\", \"score\": 20000.0}, \"sort\": {\"score\": 1.0}, \"upsert\": false, \"new\": false, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"m5cjtkg4QjWvZFJyXZ+9Cw==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"q\": {\"name\": \"gengxinliele\"}, \"limit\": 0.0}\r\n\r\n{\"delete\": \"aa\", \"ordered\": true, \"lsid\": {\"id\": {\"$binary\": \"q2yQnpXtTTuJkH+WzDtzZw==\", \"$type\": \"04\"}}, \"$db\": \"test\"}";
      sql = "{\"count\": \"collection\", \"query\": {\"a\": \"dd\", \"b\": {\"$in\": [\"d\",\"c\"]}}, \"skip\": 1.0, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"m5cjtkg4QjWvZFJyXZ+9Cw==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"distinct\": \"inventory\", \"key\": \"item.sku\", \"query\": {\"dept\": \"A\" }, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"IfAqJ48vTwuDvlIYAoCgSQ==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"q\": {\"_id\": 100.0}, \"u\": {\"_id\": 100.0, \"item\": \"water\", \"qty\": 30.0}, \"multi\": false, \"upsert\": true}\r\n\r\n{\"update\": \"products\", \"ordered\": true, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"IfAqJ48vTwuDvlIYAoCgSQ==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"aggregate\": \"people\", \"pipeline\": [{\"$match\": {\"name\": \"Tanya Jordan\"}}, {\"$graphLookup\": {\"from\": \"people\", \"startWith\": \"$friends\", \"connectFromField\": \"friends\", \"connectToField\": \"name\", \"as\": \"golfers\", \"restrictSearchWithMatch\": {\"hobbies\": \"golf\"}}}, {\"$project\": {\"name\": 1.0, \"friends\": 1.0, \"connections who play golf\": \"$golfers.name\"}}], \"cursor\": {}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"W2ELMo9UTSaFRcE6KrD7cA==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"aggregate\": \"students\", \"pipeline\": [{\"$unwind\": \"$grades\"}, {\"$match\": {\"grades.grade\": {\"$eq\": \"90.0\"}}}, {\"$replaceRoot\": {\"newRoot\": \"$grades\"}}], \"cursor\": {}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"W2ELMo9UTSaFRcE6KrD7cA==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"aggregate\": \"artwork\", \"pipeline\": [{\"$facet\": {\"categorizedByTags\": [{\"$unwind\": \"$tags\"}, {\"$sortByCount\": \"$tags\"}], \"categorizedByPrice\": [{\"$match\": {\"price\": {\"$exists\": \"1.0\"}}}, {\"$bucket\": {\"groupBy\": \"$price\", \"boundaries\": [0.0, 150.0, 200.0, 300.0, 400.0], \"default\": \"Other\", \"output\": {\"count\": {\"$sum\": 1.0}, \"titles\": {\"$push\": \"$title\"}}}}], \"categorizedByYears(Auto)\": [{\"$bucketAuto\": {\"groupBy\": \"$year\", \"buckets\": 4.0}}]}}], \"cursor\": {}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"W2ELMo9UTSaFRcE6KrD7cA==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"aggregate\": \"orders\", \"pipeline\": [{\"$lookup\": {\"from\": \"warehouses\", \"let\": {\"order_item\": \"$item\", \"order_qty\": \"$ordered\"}, \"pipeline\": [{\"$match\": {\"$expr\": {\"$and\": [{\"$eq\": [\"$stock_item\", \"$$order_item\"]}, {\"$gte\": [\"$instock\", \"$$order_qty\"]}]}}}, {\"$project\": {\"stock_item\": 0.0, \"_id\": 0.0}}], \"as\": \"stockdata\"}}], \"cursor\": {}, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"W2ELMo9UTSaFRcE6KrD7cA==\", \"subType\": \"04\"}}}, \"$db\": \"test\"}";
      sql = "{\"aggregate\":\"orders\",\"pipeline\":[{\"$match\":{\"$expr\":{\"$eq\":[\"$status\",\"completed\"]}}}],\"cursor\":{},\"lsid\":{\"id\":{\"data\":\"QlMJ1CcJRzS3brSAqkwlPw==\",\"type\":4}},\"$db\":\"test\"}";
      sql = "{\"aggregate\":\"orders\",\"pipeline\":[{\"$match\":{\"$expr\":{\"$eq\":[\"status\",\"$completed\"]}}}],\"cursor\":{},\"lsid\":{\"id\":{\"data\":\"QlMJ1CcJRzS3brSAqkwlPw==\",\"type\":4}},\"$db\":\"test\"}";
      sql = "{\"aggregate\":\"orders\",\"pipeline\":[{\"$match\":{\"$expr\":{\"$and\":[{\"$gt\":[\"$price\",100]},{\"$gte\":[\"$quantity\",\"5\"]}]}}}],\"cursor\":{},\"lsid\":{\"id\":{\"data\":\"QlMJ1CcJRzS3brSAqkwlPw==\",\"type\":4}},\"$db\":\"test\"}";
      sql = "{\"_id\": {\"$oid\": \"64d5d6f897def9f00877283a\"}, \"item\": \"journal\", \"qty\": 25.0, \"tags\": [\"blank\", \"red\"], \"dim_cm\": [14.0, 21.0]}\r\n\r\n{\"insert\":\"inventory\",\"ordered\":true,\"lsid\":{\"id\":{\"data\":\"4hUZuiIhRDuwS3zoijiVsg==\",\"type\":4}},\"$db\":\"test\"}";
      sql = "{\"_id\": {\"$oid\": \"64d5ddf997def9f008772844\"}, \"item\": \"journal\", \"qty\": 25.0, \"size\": {\"h\": 14.0, \"w\": 21.0, \"uom\": \"cm\"}, \"status\": \"A\"}\r\n\r\n{\"insert\": \"inventory\", \"ordered\": true, \"lsid\": {\"id\": {\"$binary\": \"4hUZuiIhRDuwS3zoijiVsg==\", \"$type\": \"04\"}}, \"$db\": \"test\"}";
      sql = "{\"find\":\"inventory\",\"filter\":{\"size\":{\"h\":14.0,\"w\":21.0,\"uom\":\"cm\"}},\"lsid\":{\"id\":{\"data\":\"4hUZuiIhRDuwS3zoijiVsg==\",\"type\":4}},\"$db\":\"test\"}";
      params.put("sql", sql);
      SqlUtil sqlUtil = new SqlUtil();
      Map<String, Object> s2 = sqlUtil.parseSqlStructure(params);
      System.out.println("\n\n====sh-sql====");
      System.out.println(s2.get("sh-sql"));
      System.out.println("\n\n====whereList====");
      List<Map<String, Object>> whereList = (List)s2.get("whereList");
      System.out.println(whereList);
      System.out.println("\n\n====whereList====");
      System.out.println("\n\n====whereMap====");
      Map<String, Object> whereMap = (Map)s2.get("whereMap");
      System.out.println(whereMap);
      System.out.println("\n\n====whereMap====");
      List<Map> repalceMap = new ArrayList();

      for(Map<String, Object> item : whereList) {
         Map map = new HashMap();
         map.put("columnValue", "加密");
         repalceMap.add(map);
      }

      String s = sqlUtil.pareSqlReplaceValue2(params, repalceMap);
      System.out.println("new sql:" + s);
      System.out.println("---------------------/parse structure------------------------------------------------");
   }
}
