// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.db.oom;

import jodd.datetime.JDateTime;
import jodd.db.DbSession;
import jodd.db.oom.sqlgen.DbEntitySql;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class LiveMapperDbTest extends DbBaseTest {

	/**
	 * DATABASES TO TEST!
	 */
	DbAccess[] databases = new DbAccess[]{
			new MySql(),
			new PostgreSql(),
			new HsqlDb(),
	};

	/**
	 * MySql.
	 */
	public class MySql extends MySqlDbAccess {

		@Override
		public String getCreateTableSql() {
			return "create table TESTER2 (" +
						"ID			INT UNSIGNED NOT NULL AUTO_INCREMENT," +
						"NAME		VARCHAR(20)	not null," +
						"VALUE		INT NULL," +
						"TIME		TIMESTAMP," +
						"primary key (ID)" +
						')';
		}

		@Override
		public String getTableName() {
			return "TESTER2";
		}
	}

	/**
	 * PostgreSql.
	 */
	public class PostgreSql extends PostgreSqlDbAccess {

		@Override
		public String getCreateTableSql() {
			return "create table TESTER2 (" +
						"ID			SERIAL," +
						"NAME		varchar(20)	NOT NULL," +
						"VALUE		integer NULL," +
						"TIME		TIMESTAMP," +
						"primary key (ID)" +
						')';
		}

		@Override
		public String getTableName() {
			return "TESTER2";
		}
	}

	/**
	 * HsqlDB.
	 */
	public class HsqlDb extends HsqlDbAccess {

		@Override
		public String getCreateTableSql() {
			return "create table TESTER2 (" +
						"ID			integer GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY," +
						"NAME		varchar(20)	NOT NULL," +
						"VALUE		integer NULL," +
						"TIME		TIMESTAMP" +
						')';
		}

		@Override
		public String getTableName() {
			return "TESTER2";
		}
	}

	// ---------------------------------------------------------------- test

	@Test
	public void testLiveMapperDb() throws Exception {

		for (int i = 0; i < 2; i++) {
			boolean strict = i == 0;
			System.out.println("strict: " + strict);
			for (DbAccess db : databases) {
				System.out.println("\t" + db.getClass().getSimpleName());
				init(strict);
				db.initDb();
				connect();

				dboom.registerEntity(Tester2.class);

				db.createTables();

				try {
					Tester2 tester2 = insertEntry();
					loadEntry(tester2);
				} finally {
					db.close();
				}
			}
		}
	}


	protected Tester2 insertEntry() {
		DbSession session = new DbSession();

		Tester2 tester2 = new Tester2();
		tester2.id = 1;
		tester2.name = "Hello";
		tester2.value = Integer.valueOf(123);
		tester2.time = new JDateTime(2014, 1, 30, 10, 42, 34, 987).convertToSqlTimestamp();

		DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.insert(tester2));
		dbOomQuery.setGeneratedKey();
		int result = dbOomQuery.executeUpdate();

		assertEquals(1, result);
		session.closeSession();

		return tester2;
	}

	protected void loadEntry(Tester2 tester2) {
		DbSession session = new DbSession();

		DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.findById(Tester2.class, tester2.id));
		Tester2 tester21 = dbOomQuery.find(Tester2.class);

		assertNotNull(tester21);

		assertEquals(tester2.id, tester21.id);

		session.closeSession();
	}

}
@@ -2557,11 +2557,9 @@
 34, 
-987
+0
 ).co
@@ -3125,16 +3125,153 @@
 r21.id);
+%0A%09%09assertEquals(tester2.value, tester21.value);%0A%09%09assertEquals(tester2.name, tester21.name);%0A%09%09assertEquals(tester2.time, tester21.time);
 %0A%0A%09%09sess

test completed