package org.sfm.jdbc.mysql;


import java.sql.*;


public class MysqlDbHelper {
	
	private static volatile boolean objectDb;
	
	public static Connection objectDb() throws SQLException {
		Connection c = newMysqlDbConnection();
		
		if (!objectDb) {
			Statement st = c.createStatement();
			
			try {
				createDbObject(st);

			} finally {
				st.close();
			}
		}
	
		objectDb = true;
		return c;
	}


	
	private static void createDbObject(Statement st) throws SQLException {
		st.execute("create table IF NOT EXISTS test_db_object("
				+ " id bigint primary key,"
				+ " name varchar(100), "
				+ " email varchar(100),"
				+ " creation_Time DATETIME, type_ordinal int, type_name varchar(10)  )");


		st.execute("create table IF NOT EXISTS test_db_object_autoinc("
				+ " id bigint AUTO_INCREMENT primary key,"
				+ " name varchar(100), "
				+ " email varchar(100),"
				+ " creation_Time DATETIME, type_ordinal int, type_name varchar(10)  )");

		st.execute("create table IF NOT EXISTS test_db_object_ckey("
				+ " id bigint,"
				+ " name varchar(100), "
				+ " email varchar(100),"
				+ " creation_Time DATETIME, type_ordinal int, type_name varchar(10), primary key(id, name)  )");
	}


	private static Connection newMysqlDbConnection() throws SQLException {
		String user = "sfm";

		if ("true".equals(System.getenv("TRAVISBUILD"))) {
			user = "travis";
		}

		return DriverManager.getConnection("jdbc:mysql://localhost:3306/sfm", user, null);
	}

	public static void main(String[] args) throws SQLException {
		Connection connection = MysqlDbHelper.objectDb();

		System.out.println("product name  = " + connection.getMetaData().getDatabaseProductName());
		System.out.println("product name  = " + connection.getMetaData().getDatabaseProductVersion());
		System.out.println("product name  = " + connection.getMetaData().getDatabaseMajorVersion());
		System.out.println("product name  = " + connection.getMetaData().getDatabaseMinorVersion());

	}


}

@@ -87,53 +87,8 @@
 %0A%09%0D%0A
-%09private static volatile boolean objectDb;%0D%0A%09
 %0D%0A%09p
@@ -195,29 +195,8 @@
 %09%09%0D%0A
-%09%09if (!objectDb) %7B%0D%0A%09
 %09%09St
@@ -234,14 +234,10 @@
 );%0D%0A
-%09%09%09
 %0D%0A
-%09
 %09%09tr
@@ -244,17 +244,16 @@
 y %7B%0D%0A%09%09%09
-%09
 createDb
@@ -269,17 +269,16 @@
 );%0D%0A%0D%0A%09%09
-%09
 %7D finall
@@ -282,17 +282,16 @@
 ally %7B%0D%0A
-%09
 %09%09%09st.cl
@@ -304,38 +304,11 @@
 %0D%0A%09%09
-%09
 %7D%0D%0A
-%09%09%7D%0D%0A%09%0D%0A%09%09objectDb = true;
 %0D%0A%09%09

fix mysql test