1 module mysql.test_helper; 2 3 import std.stdio; 4 import std.process; 5 import mysql.mysql; 6 7 string test_mysql_host = "localhost"; 8 string test_mysql_user = "root"; 9 string test_mysql_password = ""; 10 string test_mysql_db = "mysql_d_testing"; 11 12 bool test_mysql_info_printed = false; 13 14 Mysql test_mysql_db_connection () { 15 16 if (environment.get("MYSQL_HOST")) { 17 test_mysql_host = environment["MYSQL_HOST"]; 18 } 19 20 if (environment.get("MYSQL_USER")) { 21 test_mysql_user = environment["MYSQL_USER"]; 22 } 23 24 if (environment.get("MYSQL_PASSWORD", "^^^") != "^^^") { 25 test_mysql_password = environment["MYSQL_PASSWORD"]; 26 } 27 28 if (environment.get("MYSQL_DB")) { 29 test_mysql_db = environment["MYSQL_DB"]; 30 } 31 32 if (!test_mysql_info_printed) { 33 writefln("Connecting to: %s:%s@%s/%s", test_mysql_user, test_mysql_password, test_mysql_host, test_mysql_db); 34 test_mysql_info_printed = true; 35 } 36 37 return new Mysql(test_mysql_host, test_mysql_user, test_mysql_password, "mysql"); 38 } 39 40 Mysql testing_db_init() { 41 auto mysql = test_mysql_db_connection(); 42 43 mysql.query("DROP DATABASE IF EXISTS " ~ test_mysql_db); 44 mysql.query("CREATE DATABASE " ~ test_mysql_db); 45 mysql.selectDb(test_mysql_db); 46 return mysql; 47 } 48 49 50 string[] listTables(Mysql mysql) { 51 auto q_res = mysql.query("show tables;"); 52 53 string[] tables; 54 string col = q_res.fieldNames[0]; 55 56 foreach (table; q_res) { 57 tables ~= table[q_res.fieldNames[0]]; 58 } 59 return tables; 60 }