diff --git a/pom.xml b/pom.xml index d9a642f..e73ae62 100644 --- a/pom.xml +++ b/pom.xml @@ -116,6 +116,11 @@ kotlin-stdlib ${kotlin.version} + + org.xerial + sqlite-jdbc + 3.28.0 + diff --git a/src/main/java/com/yutou/tools/ad/AdController.java b/src/main/java/com/yutou/tools/ad/AdController.java new file mode 100644 index 0000000..9c1f5dd --- /dev/null +++ b/src/main/java/com/yutou/tools/ad/AdController.java @@ -0,0 +1,10 @@ +package com.yutou.tools.ad; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("ad/") +public class AdController { + +} diff --git a/src/main/java/com/yutou/tools/ad/Datas/AppData.java b/src/main/java/com/yutou/tools/ad/Datas/AppData.java new file mode 100644 index 0000000..958b61f --- /dev/null +++ b/src/main/java/com/yutou/tools/ad/Datas/AppData.java @@ -0,0 +1,22 @@ +package com.yutou.tools.ad.Datas; + +import lombok.Data; + +@Data +public class AppData { + private String appName; + private String packageName; + private String appId; + + private String adSrc; + private String adAppId; + private String adAppKey; + private String adOpenKey; + private String adInterKey; + private String adBannerKey; + private String adVideoKey; + private String adNativeKey; +} +/* + {"file":"ad.db","table":[{"name":"app","item":[{"name":"id","type":"int","isNull":false,"isKey":true},{"name":"appName","type":"String","isNull":false,"isKey":false},{"name":"packageName","type":"String","isNull":false,"isKey":false},{"name":"appId","type":"String","isNull":false,"isKey":false}]},{"name":"app_ad","item":[{"name":"id","type":"int","isNull":false,"isKey":true},{"name":"appId","type":"String","isNull":false,"isKey":false},{"name":"adSrc","type":"String","isNull":false,"isKey":false},{"name":"adAppId","type":"String","isNull":false,"isKey":false},{"name":"adAppKey","type":"String","isNull":false,"isKey":false},{"name":"adOpenKey","type":"String","isNull":true,"isKey":false},{"name":"adInterKey","type":"String","isNull":true,"isKey":false},{"name":"adBannerKey","type":"String","isNull":true,"isKey":false},{"name":"adVideoKey","type":"String","isNull":true,"isKey":false},{"name":"adNativeKey","type":"String","isNull":true,"isKey":false}]},{"name":"ad_log","table":[{"name":"id","type":"int","isNull":false,"isKey":true},{"name":"appId","type":"String","isNull":false,"isKey":false},{"name":"adSrc","type":"String","isNull":false,"isKey":false},{"name":"adType","type":"String","isNull":false,"isKey":false},{"name":"model","type":"String","isNull":false,"isKey":false},{"name":"subtime","type":"String","isNull":false,"isKey":false}]}]} + */ \ No newline at end of file diff --git a/src/main/java/com/yutou/tools/sqlite/SQLiteManager.java b/src/main/java/com/yutou/tools/sqlite/SQLiteManager.java new file mode 100644 index 0000000..31654ac --- /dev/null +++ b/src/main/java/com/yutou/tools/sqlite/SQLiteManager.java @@ -0,0 +1,117 @@ +package com.yutou.tools.sqlite; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.yutou.tools.utils.ConfigTools; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class SQLiteManager { + private static SQLiteManager manager; + protected Connection conn; + private String url = "jdbc:sqlite:"; + private File sql; + + public static SQLiteManager getInstance() { + if (manager == null) { + manager = new SQLiteManager(); + } + return manager; + } + + public void startBatch() { + try { + conn.setAutoCommit(false); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void closeBatch() { + try { + conn.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void commit() { + try { + conn.commit(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + protected SQLiteManager() { + + } + + private void createSql(JSONObject json) { + JSONObject table = json.getJSONObject("table"); + String tableName = table.getString("name"); + try { + sql.mkdirs(); + sql.delete(); + conn = DriverManager.getConnection(url + sql.getAbsolutePath()); + Statement statement = conn.createStatement(); + JSONArray items = table.getJSONArray("item"); + StringBuilder sql = new StringBuilder(); + sql.append("CREATE TABLE `") + .append(tableName) + .append("` ("); + for (Object item : items) { + StringBuilder builder = new StringBuilder(); + JSONObject it = (JSONObject) item; + String type; + switch (it.getString("type")) { + case "int": + type = " INTEGER "; + break; + case "TIME": + type = " NUMERIC "; + break; + default: + type = " TEXT "; + break; + } + builder.append("`") + .append(it.getString("name")) + .append("`") + .append(type) + .append(it.getBoolean("isNull") ? "" : " NOT NULL ") + .append(it.getBoolean("isKey") ? " PRIMARY KEY AUTOINCREMENT " : "") + .append(","); + sql.append(builder.toString()); + } + sql.append(");"); + statement.execute(sql.toString().replace(",);", ");")); + ConfigTools.save(ConfigTools.SQLITE,json.getString("file"),json); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void build(JSONObject json) { + try { + Class.forName("org.sqlite.JDBC"); + sql = new File("db" + File.separator + json.getString("file")); + if (!sql.exists()) { + createSql(json); + } else { + conn = DriverManager.getConnection(url + sql.getAbsolutePath()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + JSONObject json = JSONObject.parseObject("{\"file\":\"ad.db\",\"table\":{\"name\":\"sql\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"ticket_tmp\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"ticket\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"scannIndex\",\"type\":\"int\",\"isNull\":false,\"isKey\":false}]}}"); + SQLiteManager.getInstance().build(json); + } +} diff --git a/src/main/java/com/yutou/tools/utils/ConfigTools.java b/src/main/java/com/yutou/tools/utils/ConfigTools.java index 78b9e68..e8589b1 100644 --- a/src/main/java/com/yutou/tools/utils/ConfigTools.java +++ b/src/main/java/com/yutou/tools/utils/ConfigTools.java @@ -10,6 +10,7 @@ import java.io.*; public class ConfigTools { public static final String CONFIG="config.json"; public static final String DATA="data.json"; + public static final String SQLITE="sqlite.json"; static { try { File file=new File(CONFIG); @@ -43,11 +44,12 @@ public class ConfigTools { public static boolean save(String type,String key,Object data){ File file=new File(type); String src=readFile(file); - if(src!=null){ - JSONObject json=JSONObject.parseObject(src); - json.put(key,data); - saveFile(file,json.toJSONString()); + if(src==null){ + src="{}"; } + JSONObject json=JSONObject.parseObject(src); + json.put(key,data); + saveFile(file,json.toJSONString()); return false; } public static boolean saveFile(File file,String data){