1 June 2005

csopf on Mono - it works!

OK, I just had some spare time, and can gladly say that Ive managed to get csopf my pet project to compile with Mono in Linux! Fortunately Ive seperated the UI junk from the Core of the framework so I dont have to totally rely on the progress of Mono's System.Windows.Forms.

What Ive done is to compile the csopf.Core into csopf.dll, and the csopf.Data into csopf.data.dll. With these two dlls, I can write a simple app like so:



// 050601 yky Sample Console App to test out csopf.
// Saves a Customer, and loads it again.

/* command to compile it. Make sure the dlls are available too.
mcs Console.cs -r:../../build/csopf.dll -r:../../build/csopf.data.dll
*/

using csopf.Core;
using csopf.Data.SQL;

namespace SampleApp.Console
{

// 050601 yky This is the class which will be persistent.
public class Customer: PDObject
{
static Customer()
{
PDRegistry.RegisterPDObject( typeof(Customer), typeof(SQLDMObject), null );
}

public string CustomerName;
public string Designation;
public string Email;
}

public class RunMe
{
static RunMe()
{
// 050601 yky Initialize SQL persistence.
SQLDB.Init();
}
static public void Main( )
{
System.Console.Write("Hello\n");

// 050601 yky A new Customer, Joe Blow.
Customer vCust = new Customer();
vCust.CustomerName = "Joe";
vCust.Designation = "Blow";
vCust.Email = "joblo@gmail.com";

// 050601 yky Here's the magic... saves to database!
vCust.Save();
System.Console.WriteLine("Wrote Customer " + vCust.ToString() );
// 050601 yky And a new instance to test load it.
Customer vCust2 = new Customer();
// 050601 yky And load it up here...
vCust2.Load( vCust.ID );

System.Console.WriteLine("Read Customer " + vCust2.ToString() );
}
}
}


As you can see, its a very very simple application which saves "Joe"
into persistence and retrieves it out again. The code to do that is basically:
vCust.Save() and vCust2.Load(vCust.ID) ... very intuitive.

Also note that there are no Object to Data mapping files required.
All that meta information is already defined in... guess what? ... your class!

Anyway, Im pleased that this runs ok on Linux, and now I dont know
if I should develop in Gtk# or in Windows.Forms... sigh..

To see how this beast works, the debug.txt file is quite telling:

00001: 0: =================== Starting Debug ==================== 6/1/2005 2:35:52 PM ..Debugger
00002: 0: Created new Static SQLDB ..SQLDB
00003: 15: Creating Registry. .
00004: 0: Initializing SQLDB... .
00005: 10: Registering SampleApp.Console.Customer .
00006: 0: SQL Registering: SampleApp.Console.Customer .
00007: 5: Verifying Table Customer for SampleApp.Console.Customer ..SQLDMObject00008: 10: Registering csopf.Data.SQL.SQLConBase .
00009: 0: SQL Registering: csopf.Data.SQL.SQLConBase .
00011: 10: Registering csopf.Data.SQL.SQLCon .
00012: 0: SQL Registering: csopf.Data.SQL.SQLCon .
00014: 15: Opening the database to: csopftest ..SQLConnectionMySql
00015: -2: Either Database not created or User Login incorrect: Unknown database 'csopftest' ..SQLConnectionMySql
00016: 4: Attempting to create the database: csopftest ..SQLConnectionMySql
00017: 0: Succesfully Created the Database: csopftest ... reconnecting ..SQLConnectionMySql
00018: 4: Waiting for server: 0s ..SQLConnectionMySql
00019: 4: Waiting for server: 1s ..SQLConnectionMySql
00020: 4: Waiting for server: 2s ..SQLConnectionMySql
00021: 4: Waiting for server: 3s ..SQLConnectionMySql
00022: 4: Waiting for server: 4s ..SQLConnectionMySql
00023: 3: Server should be ready. ..SQLConnectionMySql
00024: 15: Opening the database to: csopftest ..SQLConnectionMySql
00025: -2: Customer not found. Attempting to create. Table 'csopftest.Customer' doesn't exist ..SQLDMObject
00026: 15: Attempting to create Table: CREATE TABLE Customer (
ID numeric(18,0) NOT NULL , CustomerName varchar(50) , Designation varchar(50) ,
Email varchar(50) , PRIMARY KEY (ID) ) TYPE = InnoDB ..SQLDMObject

00027: 15: SQL Exec: CREATE TABLE Customer (
ID numeric(18,0) NOT NULL , CustomerName varchar(50) , Designation varchar(50) ,
Email varchar(50) , PRIMARY KEY (ID) ) TYPE = InnoDB ..SQLDMObject

00028: 5: Created new table Customer ..SQLDMObject
00029: 0: Beginning a new Atom ..PDTransaction
00030: 15: Opening the database to: csopftest ..SQLConnectionMySql
00031: 15: Registering IDAllocator to check the table .
00032: 10: Registering csopf.Data.SQL.IDAllocator+IDAllocObj .
00033: 0: SQL Registering: csopf.Data.SQL.IDAllocator+IDAllocObj .
00034: 5: Verifying Table GenID for csopf.Data.SQL.IDAllocator+IDAllocObj ..SQLDMObject
00035: -2: GenID not found. Attempting to create. Table 'csopftest.GenID' doesn't exist ..SQLDMObject
00036: 15: Attempting to create Table: CREATE TABLE GenID (
TableName varchar(50) , LocationCode integer , CountryCode integer ,
CurrentID numeric(18,0) ) TYPE = InnoDB ..SQLDMObject

00037: 15: SQL Exec: CREATE TABLE GenID (
TableName varchar(50) , LocationCode integer , CountryCode integer ,
CurrentID numeric(18,0) ) TYPE = InnoDB ..SQLDMObject

00038: 5: Created new table GenID ..SQLDMObject
00039: 0: SQL Saving Object: 1000000001000 ..SQLDMObject
00040: 15: SQL Exec: INSERT INTO Customer
( Designation, Email, CustomerName, ID ) VALUES ( ?Designation, ?Email, ?CustomerName, ?ID ) ..SQLDMObject
00041: 20: Object Saved. ..SQLDMObject
00042: 0: Applying Atomic Commit. ..PDTransaction

csopf only kicks in when there are objects to be persisted, so this
occurs just when we call vCust.Save().
From 001 - 014 is pretty boring, registering stuff, setting things up.
At 015, the framework detected an error: Database was not created.
So it goes along and creates a database, and waits 5 seconds to reconnect (016-024).
At 024, the framework realises that the Customer Table does not exist,
so it creates one at 026-028.
Once the table is ready, the framework can start saving the object,
so it makes use of transactions (thus InnoDB for MySQL) at 029.
The framework allocates IDs 'manually' and doesnt just use an autoinc.
Thus the use of the IDAllocator and GenID Table (031-038).
Finally, at 039, the Framework saves the data into the database,
and 040 demonstrates that it uses Parameters so injection is not a problem.
Of course we Apply the transaction. 042.

Of course once the database and tables are created,
subsequent runs do not require them to be recreated.