NAME
HDB - Hybrid Database - Handles multiple databases with the same interface.
DESCRIPTION
HDB is an easy, fast and powerfull way to access any type of database. With it you don't need to know SQL, DBI, or the type of the database that you are using. HDB will make all the work around between the differences of any database. From HDB you still can use DBI and SQL commands if needed, but this won't be portable between database types. If you use only HDB querys (not DBI) you can change the database that you are using, Server and OS without change your code! For example, you can test/develope your code in your desktop (let's say Win32) with SQLite, and send your code to the Server (Linux), where you use MySQL.
HDB borns like a module to access in a easy way MySQL databases from HPL Documents. From HPL6, when it was fully rewrited, the module MYSQL was rewrited too and called HDB, where the resources were expanded for other DB types, like SQLite. Also HDB was turned into a Perl module, that can run independent of HPL when not called from it.
USAGE
use HDB ;
my $HDB = HDB->new(
type => 'sqlite' ,
file => 'test.db' ,
) ;
... or ...
my $HDB = HDB->new(
type => 'mysql' ,
host => 'some.domain.com' ,
user => 'foo' ,
pass => 'bar' ,
) ;
$HDB->create('users',[
'user' => 100 ,
'name' => 100 ,
'age' => 'int(200)' ,
'more' => 1024*4 ,
]);
$HDB->insert( 'users' , {
user => 'joe' ,
name => 'joe tribianny' ,
age => '30' ,
} ) ;
... or ...
$HDB->users->insert( {
user => 'joe' ,
name => 'joe tribianny' ,
age => '30' ,
} ) ;
...
my @sel = $HDB->select('users' , 'name =~ joe' , '@%' ) ;
foreach my $sel_i ( @sel ) {
my %cols = %$sel_i ;
...
}
...
my $hdbhandle = $HDB->select('users' , '<%>');
while( my %cols = <$hdbhandle> ) {
foreach my $Key ( keys %cols ) { print "$Key = $cols{$Key}\n" ;}
...
}
...
$HDB->update('users' , 'user eq joe' , { user => 'JoeT' } )
...
$HDB->disconnect ;
METHODS
new
Create the HDB object connected to the DB.
Arguments:
- FILE
-
File for flat databases, like SQLite.
- DB
-
The database name.
- HOST
-
The host of the database server. If omited and needed will use "localhost".
- USER
-
The user for connection.
- PASS
-
The password for connection.
- TYPE
-
The database type. See modules inside HDB/MOD for installed names.
- CACHE (boolean)
-
Turn on/off the cache of sth and col names.
- ID
-
Set a global ID for dynamic access.
- DYNAMIC (boolean)
-
Enable/disable the dynamic acess.
- WARNING (boolean)
-
Turn on/off errors/warnings.
open
Alias for new.
connect
Connect to the database. Note, no arguments are needed, they will be get from the object.
** Password will be hidden inside the class, not int the object.
disconnect
Disconnect the database and free any sth or cache.
reconnect
Disconnect, if needed, and reconnect the database.
dbh
Use to access in the DBI way:
$HDB->dbh->do("select * from users") ;
...
my $sth = $HDB->dbh->prepare("INSERT INTO users VALUES (?, ?, ?)") ;
$sth->execute("joe", "Joe Tribiany", "joe\friends.com") ;
You can use dbi too:
$HDB->dbi->do("select * from users") ;
** Try to avoid this way to keep your code portable between database types and OS.
COMMANDS METHODS
** See HDB::CMDS for usage.
Commands: select, insert, update, delete, create, drop, cmd, names, tables, table_columns...
DYNAMIC ACCESS
You can access the database connection without have the object variable. With this you have a global access to any connection with the ID set.
The Dynamic Access allow the use of a table as a method too:
$HDB->users->select('name == joe');
Since the table is always the first argument, HDB will paste the fake method (table name) to the CMD (select) as the fisrt argument.
** Note that the name of the table can't be a HDB method.
USAGE
my $HDB = HDB->new(
type => 'mysql' ,
db => 'test' ,
user => 'joe' ,
pass => '12345' ,
id => 'TEST' ;
) ;
...
HDB->TEST->select(users , 'user == joe');
...
my $HDB = HDB->TEST ;
$HDB->users->update('name == joe',{name => 'Jeff'}) ;
... or ...
my $table_users = $HDB->TABLE(users) ;
$table_users->update('name == joe',{name => 'Jeff'}) ;
... or ...
HDB->TEST->users->update('name == joe',{name => 'Jeff'}) ;
...
** Dynamic access is used for global connections in HPL (connections set in the config file: .conf.hpl).
SEE ALSO
HDB::CMDS, HDB::Encode, HDB::sqlite, HDB::mysql.
HPL.
AUTHOR
Graciliano M. P. <gm@virtuasites.com.br>
I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P
COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.