Sunday, October 20, 2013

tdbo - Tcl DataBase Object

TDBO - Tcl DataBase Object

TDBO is a Tcl package, named tdbo, that provides an object oriented access to records (or tuples) in a table/view of a Relational Database Management Systems. It provides a framework to abstract schema definition of a table/view in a data-access-object class (e.g., Employee, PurchaseItem etc.). At any given point in time, a data-access-object instance maps to a record in a table. Data-access-objects can make use of addgetsave and delete methods to perform CRUD operations on records of a table/view.
TDBO supports connectivity to SQLiteMySQL / MariaDB and PostgreSQL database systems. However, it is designed to plugin support for other database systems easily. Support for other databases will be added in future.
TDBO package can be obtained from GitHub: https://github.com/nagarajanchinnasamy/tdbo. If you would like to contribute by adding support to a new database, please send in your pull request.

Dependency

TDBO is based on IncrTcl, an object oriented extension of Tcl. TDBO depends on installation of following Tcl packages:

Getting Started

Simple steps to use this package are:
  1. Load tdbo package using: package require tdbo
  2. Define your application object by inheriting tdbo::DBObject
    # Employee Definition
    
    itcl::class Employee {
        inherit tdbo::DBObject
    
        # db argument is an instance of tdbo::SQLite class
        constructor {db args} {tdbo::DBObject::constructor $db} {
            configure {*}$args
        }
    
        # Implementing this proc is mandatory to return
        # name of the table/view.
        proc schema_name {} {
            return "employeeTable"
        }
    
        # public variables correspond to fields in the database table
        public variable id
        public variable name
        public variable rollno
    
        # define primary key, unique and sequence/autoincrement fields
        protected method _define_primarykey {} {
            define_primarykey id  
        }
    
        protected method _define_autoincrement {} {
            define_autoincrement id
        }
        protected method _define_unique {} {
            define_unique {rollno}
        }
    }
    
  3. Now in your main application, instantiate a database connectivity implementation:
    set db [tdbo::SQLite #auto -location mytest/test.db]
    $db open
    
  4. Create instances of Employee and start using it transparently:
    Employee emp $db -name "new employee" -rollno "MK12345" 
    
    # insert the record. After addition the id is automatically populated.
    emp1 add
    
    # modify the record
    emp1 configure -name "new updated employee"
    emp1 save
    
    # delete the record
    emp1 delete 
    
    # query about another employee with rollno "MK67890"
    emp clear
    emp configure -rollno "MK67890"
    emp get
    puts [emp cget]
    
For more details please refer to the documentation in doc folder and examples in demo folder of the package.

No comments:

Post a Comment