GT::SQL - A database independent perl interface
use GT::SQL;
my $db = new GT::SQL '/path/to/def';
my $table = $db->table ('Links');
my $editor = $db->editor ('Links');
my $creator = $db->creator ('NewTable');
my $html = $db->html ('Links', new CGI);
GT::SQL is a database independent perl interface to relational databases. It has been developed around Mysql, but has drivers that will port any mysql specific queries into the native format.
The GT::SQL object provides the interface to the entire database. It's only use is to provide you with objects that do the bulk of the work. See the appropriate docs for information on how to use each created object.
There are two ways to get a GT::SQL object. First, you can simply provide the path to the def file directory where GT::SQL stores all it's information:
$db = new GT::SQL '/path/to/def';
or you can pass in a hash or hash ref and specify options:
$db = new GT::SQL {
def_path => '/path/to/def',
cache => 1,
debug => 1,
subclass => 1
);
You must specify def_path. Setting cache => 1, will result in all table or relation objects to be cached which can improve performance.
Setting subclass => 0 or subclass => 1 will enable or disable the ability to subclass any of the objects GT::SQL creates. The default behaviour is 1.
GT::SQL has advanced debugging, and setting it to 1 should be adequate for most operations.
Pass in a def
$obj = GT::SQL->new ('/path/to/def/directory');
This method of calling new is also supported however you cannot pass in the debug level doing it this way.
GT::SQL loads the database connection info from database.def which is located in the defs directory.
To create this file, you call set_connect() as follows:
$obj->set_connect ({
driver => "mysql",
host => "localhost",
port => 3243,
database => "mydatabase",
login => 'user',
password => 'password'
});
This will test the database information, and save it to the def file. All future connections will automatically use this connection information.
Not all of the arguments in this hash are necessary. Some of them have reasonable defaults for the connection.
GT::SQL will create the following objects:
my $table = $db->table ('TableName');
or for a joined table:
my $relation = $db->table ('Table1', 'Table2');
See the GT::SQL::Table manpage for more information on how to use a table object.
my $creator = $db->creator('Newtablename');
where Newtablename is the name of the table you want to create. See the GT::SQL::Creator manpage for more information on how to use a creator object.
my $editor = $db->editor('Tablename');
Note: you cannot use an editor on joins, one table at a time. See the GT::SQL::Editor manpage for more information on how to use an editor object.
my $html = $db->html ($table, $cgi);
The html object uses information found in CGI to set values, etc. See the GT::SQL::Display::HTML manpage for more information on how to use a html object.
GT::SQL supports the concepts of table prefixes. If you specify a prefix using
the accessor, it is saved in the database.def file and will be used in all
future calls to table(), editor() and creator().
To set a prefix:
$db->prefix("foo");
to get the current prefix:
my $prefix = $db->prefix;
What this will do is prepend 'foo' to the beginning of every table name.
To display a list of all raw SQL queries sent to the database you can use:
my @queries = $db->query_stack;
or to have them formatted try
print $db->query_stack_disp;
which will join them up, displayed nicely. This is also available as a class method:
print GT::SQL->query_stack_disp;
GT::SQL has been designed to work under persistent environments as well as regular environments. A number of optimizations have been made to achieve this.
If you are using mod_perl, no changes to the code are required. You simply load the modules in your startup file as:
use GT::SQL;
use GT::SQL::Admin;
use GT::SQL::Table;
use GT::SQL::Relation;
use GT::SQL::Driver;
use GT::SQL::Display::HTML;
use GT::SQL::Display::HTML::Table;
use GT::SQL::Display::HTML::Relation;
You can omit the Admin, Relation and HTML::Relation if you are not using them.
If you are under a persistent environment that is not mod_perl (i.e. SpeedyCGI
or FastCGI), you must call GT::SQL->reset_env() at the beginning of your
script. Also, under those environments you can not cache your table objects,
you must set cache to 0.
the GT::SQL::Display::HTML manpage
Copyright (c) 2004 Gossamer Threads Inc. All Rights Reserved. http://www.gossamer-threads.com/
Revision: $Id: SQL.pm,v 1.103 2004/02/28 02:31:46 jagerman Exp $