
package com.exedio.cope.lib.database;

import com.exedio.cope.lib.SystemException;
import com.exedio.cope.lib.Type;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Database
{
	
	public Database()
	{
	}
	
	public char getNameDelimiterStart()
	{
		return '"';
	}
	
	public char getNameDelimiterEnd()
	{
		return '"';
	}
	
	public void createTable(final Type type)
	{
		executeSQL(getCreateTableStatement(type));
	}
	
	public void dropTable(final Type type)
	{
		executeSQL(getDropTableStatement(type));
	}
	
	private void executeSQL(final String sql)
	{
		final String driver = "oracle.jdbc.driver.OracleDriver";
		final String url = "jdbc:oracle:thin:@database3.exedio.com:1521:DB3";
		final String user = "wiebicke";
		final String password = "wiebicke1234";
		
		try
		{
			Class.forName(driver);
		}
		catch(ClassNotFoundException e)
		{
			throw new SystemException(e);
		}
		
		Connection connection = null;
		Statement statement = null;
		try
		{
			connection = DriverManager.getConnection(url, user, password);
			statement = connection.createStatement();
			statement.execute(sql);
		}
		catch(SQLException e)
		{
			throw new SystemException(e, sql);
		}
		finally
		{
			if(connection!=null)
			{
				try
				{
					connection.close();
				}
				catch(SQLException e)
				{
					// exception is already thrown
				}
			}
			if(statement!=null)
			{
				try
				{
					statement.close();
				}
				catch(SQLException e)
				{
					// exception is already thrown
				}
			}
		}
	}
	
	private String getTableName(final Type type)
	{
		final String className = type.getJavaClass().getName();
		final int pos = className.lastIndexOf('.');
		return className.substring(pos+1);
	}
	
	private String getCreateTableStatement(final Type type)
	{
		final char delimiterStart = getNameDelimiterStart();
		final char delimiterEnd = getNameDelimiterEnd();

		final StringBuffer bf = new StringBuffer();
		bf.append("create table ").
			append(delimiterStart).
			append(getTableName(type)).
			append(delimiterEnd).
			append('(');
		bf.append("\"code\" varchar2(5),");
		bf.append("\"name\" varchar2(20)");
		bf.append(')');
		return bf.toString();
	}
	
	private String getDropTableStatement(final Type type)
	{
		final char delimiterStart = getNameDelimiterStart();
		final char delimiterEnd = getNameDelimiterEnd();

		final StringBuffer bf = new StringBuffer();
		bf.append("drop table ").
			append(delimiterStart).
			append(getTableName(type)).
			append(delimiterEnd);
		return bf.toString();
	}
	
}

@@ -33,16 +33,54 @@
 abase;%0A%0A
+import com.exedio.cope.lib.Attribute;%0A
 import c
@@ -263,16 +263,43 @@
 atement;
+%0Aimport java.util.Iterator;
 %0A%0Apublic
@@ -1819,32 +1819,136 @@
 ng(pos+1);%0A%09%7D%0A%09%0A
+%09private String getPersistentQualifier(final Attribute attribute)%0A%09%7B%0A%09%09return attribute.getName();%0A%09%7D%0A%09%0A
 %09private String 
@@ -1980,32 +1980,32 @@
 inal Type type)%0A
-
 %09%7B%0A%09%09final char 
@@ -2279,44 +2279,217 @@
 ;%0A%09%09
-bf.append(%22%5C%22code%5C%22 varchar2(5),%22
+%0A%09%09boolean first = true;%0A%09%09for(Iterator i = type.getAttributes().iterator(); i.hasNext(); )%0A%09%09%7B%0A%09%09%09if(first)%0A%09%09%09%09first = false;%0A%09%09%09else%0A%09%09%09%09bf.append(',');%0A%09%09%09final Attribute attribute = (Attribute)i.next(
 );%0A
+%09
 %09%09bf
@@ -2500,18 +2500,127 @@
 end(
-%22%5C%22name%5C%22 
+delimiterStart).%0A%09%09%09%09append(getPersistentQualifier(attribute)).%0A%09%09%09%09append(delimiterEnd).%0A%09%09%09%09append(%22 %22).%0A%09%09%09%09append(%22
 varc
@@ -2628,15 +2628,18 @@
 ar2(
-20
+5
 )%22);%0A
+%09%09%7D%0A
 %09%09bf

create table rows