SqlSpeller.java 2.4 KB
Newer Older
Z
zyyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
package com.taosdata.example.jdbcTaosdemo.utils;

import java.util.Random;

public class SqlSpeller {
    private static final Random random = new Random(System.currentTimeMillis());
    private static final String[] locations = {
            "Beijing", "Shanghai", "Guangzhou", "Shenzhen",
            "HangZhou", "Tianjin", "Wuhan", "Changsha", "Nanjing", "Xian"
    };

    public static String createDatabaseSQL(String dbName, int keep, int days) {
        return "create database if not exists " + dbName + " keep " + keep + " days " + days;
    }

    public static String dropDatabaseSQL(String dbName) {
        return "drop database if exists " + dbName;
    }

    public static String useDatabaseSQL(String dbName) {
        return "use " + dbName;
    }

    public static String createSuperTableSQL(String superTableName) {
        return "create table if not exists " + superTableName + "(ts timestamp, current float, voltage int, phase float) tags(location binary(64), groupId int)";
    }

    public static String dropSuperTableSQL(String dbName, String superTableName) {
        return "drop table if exists " + dbName + "." + superTableName;
    }

    public static String createTableSQL(int tableIndex, String dbName, String superTableName) {
        String location = locations[random.nextInt(locations.length)];
        return "create table d" + tableIndex + " using " + dbName + "." + superTableName + " tags('" + location + "'," + tableIndex + ")";
    }

    public static String insertOneRowSQL(String dbName, String tbPrefix, int tableIndex, long ts) {
        float current = 10 + random.nextFloat();
        int voltage = 200 + random.nextInt(20);
        float phase = random.nextFloat();
        String sql = "insert into " + dbName + "." + tbPrefix + "" + tableIndex + " " + "values(" + ts + ", " + current + ", " + voltage + ", " + phase + ")";
        return sql;
    }

    public static String insertBatchSizeRowsSQL(String dbName, String tbPrefix, int tbIndex, long ts, int valuesCount) {
        float current = 10 + random.nextFloat();
        int voltage = 200 + random.nextInt(20);
        float phase = random.nextFloat();
        StringBuilder sb = new StringBuilder();
        sb.append("insert into " + dbName + "." + tbPrefix + "" + tbIndex + " " + "values");
        for (int i = 0; i < valuesCount; i++) {
            sb.append("(" + (ts + i) + ", " + current + ", " + voltage + ", " + phase + ") ");
        }
        return sb.toString();
    }


}