Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pax.testyadb2;
- import ch.vorburger.exec.ManagedProcessException;
- import ch.vorburger.mariadb4j.DB;
- import ch.vorburger.mariadb4j.DBConfigurationBuilder;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.*;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- public class Main {
- private static String DB_PATH = "";
- private static String DB_NAME = "db10";
- private static Boolean FIRST_START = true;
- private static int USERS_COUNT = 10;
- private static int START_RECORDS = 10;
- private static int QUERY_PERIOD = 1000;
- private static DBConfigurationBuilder configBuilder;
- private static Connection conn;
- private static Timer usersTimer;
- private static Timer messageTimer;
- public static void main(String[] args) throws SQLException, IOException, ManagedProcessException {
- DB_PATH = args[0];
- DB_NAME = args[1];
- System.out.println("Welcome!");
- System.out.println("This is test MariaDB");
- System.out.println("first time?(y or n)");
- System.out.println("Enter 'h' for get help");
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- // String line = in.readLine();
- String line = "";
- while (line.equalsIgnoreCase("quit") == false) {
- line = in.readLine();
- switch (line) {
- case "y":
- initialize();
- break;
- case "n":
- configBuilder = DBConfigurationBuilder.newBuilder();
- configBuilder.setPort(3306); // OR, default: setPort(0); => autom. detect free port
- configBuilder.setDataDir(DB_PATH + DB_NAME); // just an example
- DB db = DB.newEmbeddedDB(configBuilder.build());
- db.start();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
- break;
- case "au":
- addNewUser(conn);
- break;
- case "h":
- help();
- break;
- case "su":
- showUsers(conn);
- break;
- case "sup":
- showUserPoints(conn);
- break;
- case "nm":
- addNewMessage(conn);
- break;
- case "sm":
- showMessages(conn);
- break;
- case "stm":
- messageTimer.cancel();
- break;
- case "stu":
- usersTimer.cancel();
- break;
- }
- if (line.contains("mau")) {
- String[] number = line.split(" ");
- int iMax = Integer.parseInt(number[1]);
- for (int i = 0; i < iMax; i++) {
- addNewUser(conn);
- }
- }
- if (line.contains("tnm")) {
- int countInteger;
- int period;
- String[] count = line.split(" ");
- if (count.length < 2) {
- countInteger = 1;
- period = 1000;
- } else {
- countInteger = Integer.parseInt(count[1]);
- period = Integer.parseInt(count[2]);
- }
- class message extends TimerTask {
- public void run() {
- for (int i = 0; i < countInteger; i++) {
- try {
- addNewMessage(conn);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- messageTimer = new Timer();
- messageTimer.schedule(new message(), 0, period);
- }
- if (line.contains("tnu")) {
- int countInteger;
- int period;
- String[] count = line.split(" ");
- if (count.length < 2) {
- countInteger = 1;
- period = 1000;
- } else {
- countInteger = Integer.parseInt(count[1]);
- period = Integer.parseInt(count[2]);
- }
- class user extends TimerTask {
- public void run() {
- for (int i = 0; i < countInteger; i++) {
- addNewUser(conn);
- }
- }
- }
- usersTimer = new Timer();
- usersTimer.schedule(new user(), 0, period);
- }
- }
- // String[] values = line.split(" ");
- // FIRST_START = values[0].equals("y");
- // START_RECORDS = Integer.parseInt(values[1]);
- // USERS_COUNT = Integer.parseInt(values[2]);
- // QUERY_PERIOD = Integer.parseInt(values[3]);
- // if (FIRST_START) {
- // System.out.println("y");
- // initialize();
- // } else {
- //// configBuilder = DBConfigurationBuilder.newBuilder();
- //// configBuilder.setPort(3306); // OR, default: setPort(0); => autom. detect free port
- //// configBuilder.setDataDir("/Volumes/secondary/projects/java/yadb/db"); // just an example
- //// DB db = DB.newEmbeddedDB(configBuilder.build());
- //// db.start();
- //// try {
- //// Class.forName("com.mysql.jdbc.Driver");
- //// } catch (ClassNotFoundException e) {
- //// e.printStackTrace();
- //// }
- //// conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
- //
- // }
- // if (START_RECORDS > 0) {
- //// setStartRecords(conn);
- // } else {
- //// randomInsert(conn);
- // }
- }
- private static void showMessages(Connection connection) throws SQLException {
- final Statement statement = connection.createStatement();
- int i = 1;
- String q = ("SELECT * FROM chat_history");
- ResultSet rs = statement.executeQuery(q);
- while (rs.next()) {
- System.out.print("(" + i + ") - (");
- System.out.print(rs.getString("id"));
- System.out.print(",");
- System.out.print(rs.getString("origin"));
- System.out.print(",");
- System.out.print(rs.getString("sender"));
- System.out.print(",");
- System.out.print(rs.getString("message"));
- System.out.print(",");
- System.out.print(rs.getString("timestamp"));
- System.out.print(")");
- i++;
- System.out.println();
- }
- }
- private static void addNewMessage(Connection connection) throws SQLException {
- Random random = new Random();
- String origin = "";
- String sender = "";
- String message = "";
- Timestamp timestamp = new Timestamp(System.currentTimeMillis());
- // System.out.println(timestamp);
- final Statement statement = connection.createStatement();
- int i = 0;
- String q = ("SELECT * FROM user_stats");
- ResultSet rs = statement.executeQuery(q);
- while (rs.next()) {
- i++;
- }
- int userId = random.nextInt() % i;
- if (userId < 0) {
- userId = userId * (-1);
- }
- String q2 = ("SELECT * FROM user_stats WHERE id=" + userId);
- ResultSet rs2 = statement.executeQuery(q2);
- while (rs2.next()) {
- origin = rs2.getString("origin");
- sender = rs2.getString("username");
- message = String.valueOf(random.nextDouble() % 1000000.0);
- // System.out.println("new Message:" + timestamp + " " + origin + "-" + sender + ":" + message);
- }
- connection.createStatement().
- execute("INSERT INTO chat_history " +
- "(origin, sender, " +
- "message, timestamp) VALUES (" +
- "'" + origin + "'," +
- "'" + sender + "'," +
- "'" + message + "'," +
- "'" + timestamp + "');");
- }
- private static void showUserPoints(Connection connection) throws SQLException {
- Random random = new Random();
- final Statement statement = connection.createStatement();
- int i = 0;
- String q = ("SELECT * FROM user_stats");
- ResultSet rs = statement.executeQuery(q);
- while (rs.next()) {
- i++;
- }
- int userId = random.nextInt() % i;
- if (userId < 0) {
- userId = userId * (-1);
- }
- String q2 = ("SELECT * FROM user_stats WHERE id =" + userId);
- ResultSet rs2 = statement.executeQuery(q2);
- System.out.println(userId);
- while (rs2.next()) {
- System.out.println(rs2.getString("points"));
- }
- }
- private static void showUsers(Connection connection) throws SQLException {
- final Statement statement = connection.createStatement();
- int i = 1;
- String q = ("SELECT * FROM user_stats");
- ResultSet rs = statement.executeQuery(q);
- while (rs.next()) {
- System.out.print("(" + i + ") - (");
- System.out.print(rs.getString("id"));
- System.out.print(",");
- System.out.print(rs.getString("origin"));
- System.out.print(",");
- System.out.print(rs.getString("username"));
- System.out.print(",");
- System.out.print(rs.getString("level"));
- System.out.print(",");
- System.out.print(rs.getString("points"));
- System.out.print(",");
- System.out.print(rs.getString("position"));
- System.out.print(",");
- System.out.print(rs.getString("online"));
- System.out.print(",");
- System.out.print(rs.getString("total_points"));
- System.out.print(")");
- i++;
- System.out.println();
- }
- // System.out.println("count:" + (i - 1));
- }
- private static void help() {
- System.out.println("au - add new user");
- System.out.println("ac - add new chat config N'\\'A");
- System.out.println("ach - add new chat history N'\\'A");
- System.out.println("acc - add new chat command N'\\'A");
- System.out.println("asm - add new scheduled message N'\\'A");
- System.out.println("---------------------------------");
- System.out.println("su - show users");
- System.out.println("sc - show chat configs N'\\'A");
- System.out.println("sch - show chat histories N'\\'A");
- System.out.println("scc - show chat commands N'\\'A");
- System.out.println("sm - show messages");
- System.out.println("ssm - show scheduled messages N'\\'A");
- System.out.println("---------------------------------");
- System.out.println("mau - multi adding users");
- System.out.println("mac - multi adding new chat configs N'\\'A");
- System.out.println("mach - multi adding new chat histories N'\\'A");
- System.out.println("macc - multi adding new chat commands N'\\'A");
- System.out.println("masm - multi adding new scheduled messages N'\\'A");
- System.out.println("---------------------------------");
- System.out.println("sup - show user points");
- System.out.println("nw - new message");
- System.out.println("tnm - timer new messages (two values count and period)");
- System.out.println("tnu - timer new users (two values count and period)");
- System.out.println("stm - stop timer messages");
- System.out.println("stu - stop timer users");
- }
- private static void addNewUser(Connection connection) {
- Random random = new Random();
- try {
- connection.createStatement().
- execute("INSERT INTO user_stats " +
- "(origin, username, " +
- "level, points," +
- " position, online, " +
- "total_points ) VALUES (" +
- "'" + getRandomOrigin(random.nextBoolean()) + "'," +
- "'" + random.nextDouble() % 1000000 + "'," +
- "'" + Math.abs(random.nextInt() % 10) + "'," +
- "'" + random.nextFloat() % 10000.0 + "'," +
- "'" + Math.abs(random.nextInt() % 3) + "'," +
- "'" + Math.abs(random.nextInt() % 2) + "'," +
- "'" + Math.abs(random.nextFloat() % 10000.0) + "');");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private static String getRandomOrigin(boolean b) {
- if (b) {
- return "twitch";
- } else {
- return "goodgame";
- }
- }
- private static void initialize() throws ManagedProcessException, SQLException {
- configBuilder = DBConfigurationBuilder.newBuilder();
- configBuilder.setPort(3306); // OR, default: setPort(0); => autom. detect free port
- configBuilder.setDataDir(DB_PATH + DB_NAME); // just an example
- DB db = DB.newEmbeddedDB(configBuilder.build());
- db.start();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
- try {
- String chatCommandsSql = "CREATE TABLE chat_commands(\n" +
- "id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,\n" +
- "command VARCHAR(32) ,\n" +
- "description VARCHAR(255) ,\n" +
- "target BOOLEAN ,\n" +
- "kind BOOLEAN DEFAULT 0 ,\n" +
- "type TINYINT(2) ,\n" +
- "personal BOOLEAN,\n" +
- "action VARCHAR(255),\n" +
- "cost FLOAT,\n" +
- "enabled BOOLEAN,\n" +
- "error_cost VARCHAR(255),\n" +
- "error_command VARCHAR(255),\n" +
- "PRIMARY KEY (id)\n" +
- ") ENGINE=InnoDB;";
- String chatHistorySql = "CREATE TABLE chat_history(\n" +
- "id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,\n" +
- "origin VARCHAR(32) ,\n" +
- "sender VARCHAR(255) ,\n" +
- "message TEXT ,\n" +
- "timestamp DATETIME,\n" +
- "PRIMARY KEY (id)\n" +
- ") ENGINE=InnoDB;";
- String configSql = "CREATE TABLE config(\n" +
- "`key` VARCHAR(255),\n" +
- "`value` VARCHAR(255),\n" +
- "PRIMARY KEY (`key`)\n" +
- ") ENGINE=InnoDB;";
- String scheduledMessagesSql = "CREATE TABLE scheduled_messages(\n" +
- "id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,\n" +
- "target VARCHAR(32) ,\n" +
- "schedule TIME ,\n" +
- "starter_schedule TIME ,\n" +
- "text TEXT ,\n" +
- "enabled BOOLEAN,\n" +
- "PRIMARY KEY (id)\n" +
- ") ENGINE=InnoDB;";
- String userStatsSql = "CREATE TABLE user_stats(\n" +
- "id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,\n" +
- "origin VARCHAR(32) NOT NULL,\n" +
- "username VARCHAR(255) NOT NULL,\n" +
- "level INT(3) NOT NULL,\n" +
- "points FLOAT NOT NULL DEFAULT 0.0,\n" +
- "position INT(2) NOT NULL DEFAULT 2,\n" +
- "online BOOLEAN NOT NULL DEFAULT 0,\n" +
- "total_points FLOAT NOT NULL DEFAULT 0.0,\n" +
- "PRIMARY KEY (id)\n" +
- ") ENGINE=InnoDB;";
- // String sql3 = "CREATE TABLE users2(\n" +
- // "userid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,\n" +
- // "name VARCHAR(80) NOT NULL,\n" +
- // "surname VARCHAR(80) NOT NULL,\n" +
- // "PRIMARY KEY (userid)\n" +
- // ") ENGINE=InnoDB;";
- // String sql = "PRAGMA foreign_keys = false;\n" +
- // "CREATE TABLE \"chat_commands\"(\n" +
- // "\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
- // "\"command\" TEXT(32),\n" +
- // "\"description\" TEXT(256),\n" +
- // "\"target\" INTEGER,\n" +
- // "\"kind\" INTEGER DEFAULT 0,\n" +
- // "\"type\" INTEGER,\n" +
- // "\"personal\" INTEGER,\n" +
- // "\"action\" TEXT(256),\n" +
- // "\"cost\" INTEGER,\n" +
- // "\"enabled\" INTEGER,\n" +
- // "\"error_cost\" TEXT(256),\n" +
- // "\"error_cost_test\" TEXT(256),\n" +
- // "\"error_command\" TEXT(256)\n" +
- // ")\n" +
- // " ENGINE=InnoDB;" +
- // "CREATE TABLE \"chat_history\" (\n" +
- // "\t \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
- // "\t \"origin\" TEXT(16,0) NOT NULL,\n" +
- // "\t \"sender\" TEXT(64,0) NOT NULL,\n" +
- // "\t \"message\" TEXT(1024,0) NOT NULL,\n" +
- // "\t \"timestamp\" integer\n" +
- // ")\n" +
- // " ENGINE=InnoDB;" +
- // "CREATE TABLE \"config\" (\n" +
- // "\t \"key\" TEXT(255,0) NOT NULL,\n" +
- // "\t \"value\" TEXT(65536,0) NOT NULL,\n" +
- // "\tPRIMARY KEY(\"key\")\n" +
- // ")\n" +
- // " ENGINE=InnoDB;" +
- // "CREATE TABLE \"scheduled_messages\" (\n" +
- // "\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
- // "\"target\" TEXT(16),\n" +
- // "\"schedule\" TEXT(32),\n" +
- // "\"starter_schedule\" TEXT(32),\n" +
- // "\"text\" TEXT(128),\n" +
- // "\"enabled\" INTEGER\n" +
- // ")" +
- // " ENGINE=InnoDB;\n" +
- // "CREATE TABLE \"user_stats\" (\n" +
- // "\t \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
- // "\t \"origin\" TEXT(32,0) NOT NULL,\n" +
- // "\t \"username\" TEXT(128,0) NOT NULL,\n" +
- // "\t \"level\" integer NOT NULL,\n" +
- // "\t \"points\" real NOT NULL DEFAULT 0,\n" +
- // "\t \"position\" integer NOT NULL DEFAULT 2,\n" +
- // "\t \"online\" integer NOT NULL DEFAULT 0,\n" +
- // "\t \"total_points\" real NOT NULL DEFAULT 0\n" +
- // ")\n" +
- // " ENGINE=InnoDB;" +
- // "INSERT INTO config (key, value) VALUES (\"version\", \"1\")\n" +
- // "PRAGMA foreign_keys = true;";
- // conn.createStatement().execute(
- // "CREATE TABLE users(\n" +
- // "userid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,\n" +
- // "name VARCHAR(80) NOT NULL,\n" +
- // "surname VARCHAR(80) NOT NULL,\n" +
- // "PRIMARY KEY (userid)\n" +
- // ") ENGINE=InnoDB;");
- conn.createStatement().execute(
- chatCommandsSql);
- conn.createStatement().execute(
- chatHistorySql);
- conn.createStatement().execute(
- configSql);
- conn.createStatement().execute(
- scheduledMessagesSql);
- conn.createStatement().execute(
- userStatsSql);
- // conn.createStatement().execute(
- // sql3);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private static void randomInsert(Connection connection) {
- try {
- connection.createStatement().execute("INSERT INTO user_stats VALUES ('" + getRandomNumber() + "');");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private static void setStartRecords(Connection conn) {
- for (int i = 0; i < START_RECORDS; i++) {
- randomInsert(conn);
- }
- }
- private static void randomSelect(Connection connection) throws Exception {
- int i = 0;
- final Statement statement = connection.createStatement();
- String q = ("SELECT code FROM sk");
- ResultSet rs = statement.executeQuery(q);
- while (rs.next()) {
- i++;
- System.out.println(rs.getString("code"));
- }
- System.out.println("count:" + (i - 1));
- }
- private static int getRandomNumber() {
- Random random = new Random();
- return Math.abs(random.nextInt() % 1000 + 1);
- }
- private static int getRandomOfUserCountNumber() {
- Random random = new Random();
- return Math.abs(random.nextInt() % USERS_COUNT + 1);
- }
- private static void randomTask(Connection connection) {
- Random random = new Random();
- if (random.nextBoolean()) {
- System.out.println("============INSERT=============");
- randomInsert(connection);
- } else {
- try {
- System.out.println("============SELECT=============");
- randomSelect(connection);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement