|
|
Database Handling by DaoDataModelLimin Fu (phoolimin DaoDataModel is a module to map Dao classes to database tables (currently works only with MySQL). With such mapping, the handling of database with Dao becomes very simple. For example, there such class,
load DaoDataModel;
Here, the type INT_PRIMARY_KEY_AUTO_INCREMENT,VARCHAR100,TEXT
are defined by the DaoDataModel module, to allow the module
interprete properly the class fields and corresponding database record fields.
Those types provided by DaoDataModel are defined as aliases of
the built-in data types,
# class that can be mapped to a database table class Gene { my id : INT_PRIMARY_KEY_AUTO_INCREMENT; my name : VARCHAR100; my seq : TEXT; }
For MySQL database, one can connect to the database by,
# connect database
The prototype of DataModel() is,
model = DataModel( 'dbname', 'host', 'user', 'password' );
DataModel( name : string, host='', user='', pwd='' )=>DataModel
# create a table for class Gene
A table named Gene will be created.model.CreateTable( Gene ); Prototype of the method,
DataModel.CreateTable( klass )
If a class has a special constant string field named __TABLE_NAME__ , the name of database table corresponding to the class will be the value of __TABLE_NAME__ . A class may also contain another constant string field named __TABLE_PROPERTY__ , which indicates the property of the table or contains extra constraints on some fields.
gene = Gene{ 0, 'RAS', '...AATCCG...' };
The module knows which table to insert. After inserting,
if the class has a field with type INT_PRIMARY_KEY_AUTO_INCREMENT ,
that instance field (in this case it is gene.id ) will be filled
with id of the inserted record.# insert a record into the table model.Insert( gene ); One can also insert multiple records at once by pass a list of class instances to the Insert() method. This method will return a database handler, with which one can insert more records to avoid repeatedly compile SQL statements internally. Similarly there is Delete(),
DataModel.Insert( object ) => Handler
DataModel.Delete( object ) => Handler
# SELECT * FROM Gene WHERE name = 'RAS';
this module uses a series of method calls to contruct a SQL statement
and then compile it.
The following two methods can be use to create a handler for
thse two types of query,
hd = model.Select( Gene ).Where().EQ( 'name', 'RAS' ); # query and store the result into 'gene' object: hd.QueryOnce( gene );
DataModel.Select( object, ... ) => Handler
These two methods can take a class or a list of classes
as parameters, which indicate which database tables to be queried.
And after each of the class parameter, there can follow an integer,
which indicates how many fields to be queried for that class/table.
For example,
DataModel.Update( object, ... ) => Handler
hd = model.Select( Gene, 2 ).Where().EQ( 'name', 'RAS' );
this will generate such SQL statement,
# SELECT id,name FROM Gene WHERE name = 'RAS';
Then the handler can call the following methods to prepare the SQL statement for a query,
# WHERE
For the methods that take an optional parameter value ,
if it is omitted, a place holder variable will be used,
then data can be bind to these variables by,
Handler.Where( ) => Handler # SET field=value, or, SET field=? Handler.Set( field : string, value=nil ) => Handler # SET field=field+value, or, SET field=field+? Handler.Add( field : string, value=nil ) => Handler # field=value, or, field=? Handler.EQ( field : string, value=nil ) => Handler # field!=value, or, field!=? Handler.NE( field : string, value=nil ) => Handler Handler.GT( field : string, value=nil ) => Handler Handler.GE( field : string, value=nil ) => Handler Handler.LT( field : string, value=nil ) => Handler Handler.LE( field : string, value=nil ) => Handler # SET table.field=value, or, SET table.field=? Handler.Set( table, field : string, value=nil ) => Handler Handler.Add( table, field : string, value=nil ) => Handler Handler.EQ( table, field : string, value=nil ) => Handler Handler.NE( table, field : string, value=nil ) => Handler Handler.GT( table, field : string, value=nil ) => Handler Handler.GE( table, field : string, value=nil ) => Handler Handler.LT( table, field : string, value=nil ) => Handler Handler.LE( table, field : string, value=nil ) => Handler # field IN ( values ), or, field IN ? Handler.In( field : string, values={} ) => Handler Handler.In( table, field : string, values={} ) => Handler # OR Handler.Or( ) => Handler Handler.And( ) => Handler Handler.Not( ) => Handler # ( Handler.LBrace( ) => Handler # ) Handler.RBrace( ) => Handler # table1.field1=table2.field2 Handler.Match( table1, table2, field1='', field2='' ) => Handler # ORDER BY field ASC/DESC Handler.Sort( field : string, desc=0 ) => Handler # ORDER BY table.field ASC/DESC Handler.Sort( table, field : string, desc=0 ) => Handler # LIMIT limit, or, LIMIT limit OFFSET offset Handler.Range( limit : int, offset=0 ) => Handler
Handler.Bind( value, index=0 ) => Handler
A index can be given in the parameter list to indicate which to bind,
if there is multiple place-holder variables.
If no index parameter is given, the binds will be done sequentially.At last these two methods can be called to perform the query,
Handler.Query( ... ) => int
These two method will take class instance(s) as parameter,
and store the resulting data in the members of the class instances,
if the query is successful. These method will return 1 when query
is successful, otherwise return 0.
If the query hits multiple records, Handler.Query() can be called
repeatedly to get the result.
After calling Handler.Query(), it is necessary to call Handler.Done()
to reset the model. If Handler.QueryOnce() is called, there will be no
such necessary.Handler.QueryOnce( ... ) => int
DataModel.Query( sql : string ) => int
Perform a arbitrary query, return the status of the query.
Handler.sqlstring( ) => string
Return the SQL statemenet as a string.
Handler.Insert( object ) => int
Use the handler created by DataModel.Insert()
to insert more records.
view count 462 times
created at 2009-02-24, 18:49 GMT modified at 2009-06-08, 08:30 GMT |
fu: Many thanks (Jul.04,04:29) klabim: fixed Hi, great, now my test works now :- ). (Jun.30,17:51) Nightwalker: Few suggestions (Jul.03,14:37) |