/* 
   Copyright (c) 2000 Martin.Both
*/

module DB
{
	exception _Exception
	{
		long sqlcode;
		string <5> sqlstate;
		string <255> sqlmessage;
	};
	
	struct Warning
	{	
		long sqlcode;
		string <5> sqlstate;
		string <255> sqlmessage;
	};
	
	typedef sequence <Warning> Warnings;
	
	enum DataType
	{	TypeNull, TypeString,
		TypeShort, TypeInteger, TypeDouble, TypeDecimal,
		TypeTime, TypeDate, TypeDateTime,
		TypeText, TypeBinary
	};
	
	union ColumnData switch(DataType)
	{
		case TypeString:
			string stringValue;
		case TypeShort:
			short shortValue;
		case TypeInteger:
			long integerValue;
		case TypeDouble:
			double doubleValue;
		case TypeText:
			string textValue;
		case TypeBinary:
			sequence <octet> binaryValue;
	};
	
	typedef sequence <ColumnData> DataRow;
	typedef sequence <DataRow> DataRows;

	struct ColumnDescriptor
	{
		string <18> name;	// Column identifier (maybe equal to label)
		DataType type;
		short sqltype;
		short length;		// Length of the data type in bytes
		short scale;		// Scale for fixed-point decimal values or money
		short precision;	// Precision for decimal or money
		boolean nullable;
	};

	typedef sequence <ColumnDescriptor> DescriptorRow;
	
	// Cursor
	interface Cursor
	{
		// Opposite of Statement.declareCursor()
		void free(out Warnings warns) raises(_Exception);

		// "using" can be nothing
		void open(in DataRow using, out Warnings warns) raises(_Exception);

		// Opposite of open()
		void close(out Warnings warns) raises(_Exception);

		// Fetch one row
		long fetch(in long func, out DataRow row, out Warnings warns)
			raises(_Exception);

		// 
		long fetchSet(in long rowcnt, out DataRows rows,
			out Warnings warns) raises(_Exception);
	};

	// Statement
	interface Statement
	{
		void free(out Warnings warns) raises(_Exception);

		// returning type of statement, 0 is SELECT
		long describe(out DescriptorRow cols, out Warnings warns)
			raises(_Exception);
		
		// returning row counter
		long execute(in DataRow using, out DataRow row,
			out Warnings warns) raises(_Exception);
		
		Cursor declareCursor(in boolean hold, out Warnings warns)
			raises(_Exception);
	};

	// Connection
	interface Connection
	{
		void close(out Warnings warns) raises(_Exception);
		
		long execute(in string stmstr, out Warnings warns)
			raises(_Exception);
		Statement prepare(in string stmstr, out Warnings warns)
			raises(_Exception);
		
		void commit(out Warnings warns) raises(_Exception);
		void rollback(out Warnings warns) raises(_Exception);
	};

	// Manager
	interface Manager
	{
		Connection getConnection(in string url, in string user,
			in string password, out Warnings warns) raises(_Exception);

		oneway void shutdownServer();
	};
};
