NAME
MPMinus::Store::DBI - Simple database interface based on CTK::DBI
VERSION
Version 1.01
SYNOPSIS
use
MPMinus::Store::DBI;
# MySQL connect
my
$mysql
= new MPMinus::Store::DBI (
-dsn
=>
'DBI:mysql:database=TEST;host=192.168.1.1'
,
-user
=>
'login'
,
-pass
=>
'password'
,
-connect_to
=> 5,
-request_to
=> 60
-attr
=> {
mysql_enable_utf8
=> 1,
RaiseError
=> 0,
PrintError
=> 0,
},
);
# See CTK::DBI
# MySQL connect (old style, without DSN)
my
$mysql
= new MPMinus::Store::DBI (
-m
=>
$m
,
# OPTIONAL
-driver
=>
'mysql'
,
# Driver name. See DBI module
# Available drivers:
# CSV, DBM, ExampleP, File, Gofer, ODBC, Oracle,
# Pg, Proxy, SQLite, Sponge, mysql
-host
=>
'192.168.1.1'
,
-port
=>
'3306'
,
# default
-database
=>
'TEST'
,
-user
=>
'login'
,
-pass
=>
'password'
,
-attr
=> {
mysql_enable_utf8
=> 1,
RaiseError
=> 0,
PrintError
=> 0,
},
);
my
$dbh
=
$mysql
->
connect
;
my
$pingstat
=
$mysql
->ping
if
$mysql
;
$mysql
->reconnect()
unless
$pingstat
;
# Table select (as array)
my
@result
=
$mysql
->table(
$sql
,
@inargs
);
# Table select (as hash)
my
%result
=
$mysql
->tableh(
$key
,
$sql
,
@inargs
);
# $key - primary index field name
# Record (as array)
my
@result
=
$mysql
->record(
$sql
,
@inargs
);
# Record (as hash)
my
%result
=
$mysql
->recordh(
$sql
,
@inargs
);
# Fiels (as scalar)
my
$result
=
$mysql
->field(
$sql
,
@inargs
);
# SQL/PL-SQL
my
$sth
=
$mysql
->execute(
$sql
,
@inargs
);
...
$sth
->finish;
DESCRIPTION
Simple database interface based on CTK::DBI
DEBUG
You can set $MPMinus::Store::DBI::DEBUG_FORCE = 1 to enable forced debugging
METHODS
- new
-
my
$mysql
= new MPMinus::Store::DBI (
-m
=>
$m
,
# OPTIONAL
-driver
=>
'mysql'
,
# Driver name. See DBI module
# Available drivers:
# CSV, DBM, ExampleP, File, Gofer, ODBC, Oracle,
# Pg, Proxy, SQLite, Sponge, mysql
-host
=>
'192.168.1.1'
,
-port
=>
'3306'
,
# default
-database
=>
'TEST'
,
-user
=>
'login'
,
-pass
=>
'password'
,
-attr
=> {
mysql_enable_utf8
=> 1,
RaiseError
=> 0,
PrintError
=> 0,
},
);
Returns MPMinus::Store::DBI object. See also CTK::DBI
- ping
-
my
$status
=
$mysql
->ping();
Returns connection's life status
- reconnect
-
$mysql
->reconnect
unless
$mysql
->ping();
- err, errstr, state
-
my
$err
=
$mysql
->err;
my
$errstr
=
$mysql
->errstr;
my
$state
=
$mysql
->state;
Methods returns DBI values: err, errstr and state.
EXAMPLES
- Handler example
-
package
MPM::Foo::Handlers;
use
strict;
use
MPMinus::Store::DBI;
sub
handler {
my
$r
=
shift
;
my
$m
= MPMinus->m;
...
# MySQL connect
$m
->set_node(
mysql
=> new MPMinus::Store::DBI (
-dsn
=>
'DBI:mysql:database=NAME;host=HOST'
,
-user
=>
'USER'
,
-pass
=>
'PASSWORD'
,
-attr
=> {
mysql_enable_utf8
=> 1,
RaiseError
=> 0,
PrintError
=> 0,
HandleError
=>
sub
{
$m
->log_error(
shift
||
''
) },
},
)
)
unless
$m
->mysql;
...
}
package
MPM::Foo::Test;
use
strict;
sub
response {
my
$m
=
shift
;
my
@data
=
$m
->mysql->table(
'select * from table'
);
...
return
Apache2::Const::OK;
}
- Handler example with reconnection
-
package
MPM::Foo::Handlers;
use
strict;
use
MPMinus::Store::DBI;
sub
handler {
my
$r
=
shift
;
my
$m
= MPMinus->m;
...
# MySQL connect/reconnect
if
(
$m
->mysql) {
$m
->mysql->reconnect
unless
$m
->mysql->ping;
}
else
{
# eval 'sub CTK::DBI::_error {1}'; # For supressing CTK::DBI errors
$m
->set_node(
mysql
=> new MPMinus::Store::DBI (
-dsn
=>
'DBI:mysql:database=NAME;host=HOST'
,
-user
=>
'USER'
,
-pass
=>
'PASSWORD'
,
-attr
=> {
mysql_enable_utf8
=> 1,
RaiseError
=> 0,
PrintError
=> 0,
HandleError
=>
sub
{
$m
->log_error(
shift
||
''
) },
},
)
);
}
...
}
package
MPM::Foo::Test;
use
strict;
sub
response {
my
$m
=
shift
;
my
@data
=
$m
->mysql->table(
'select * from table'
);
...
return
Apache2::Const::OK;
}
- Simple example
-
use
MPMinus::Store::DBI;
$MPMinus::Store::DBI::DEBUG_FORCE
= 1;
my
$dbi
= new MPMinus::Store::DBI (
-driver
=>
'mysql'
,
-name
=>
'mylocaldb'
,
-user
=>
'user'
,
-password
=>
'password'
);
...
my
@table
=
$dbi
->table(
"select * from tablename where date = ?"
,
"01.01.2000"
);
- Sponge example
-
use
MPMinus::Store::DBI;
use
Data::Dumper;
$MPMinus::Store::DBI::DEBUG_FORCE
= 1;
my
$o
= new MPMinus::Store::DBI(
-driver
=>
'Sponge'
,
-attr
=> {
RaiseError
=> 1 },
);
my
$dbh
=
$o
->
connect
();
my
$sth
=
$dbh
->prepare(
"select * from table"
, {
rows
=> [
[
qw/foo bar baz/
],
[
qw/qux quux corge/
],
[
qw/grault garply waldo/
],
],
NAME
=> [
qw/h1 h2 h3/
],
});
$sth
->execute();
my
$result
=
$sth
->fetchall_arrayref;
$sth
->finish;
print
Dumper(
$result
);
HISTORY
See CHANGES
file
DEPENDENCIES
TO DO
See TODO
file
BUGS
* none noted
SEE ALSO
AUTHOR
Serż Minus (Sergey Lepenkov) http://www.serzik.com <abalama@cpan.org>
COPYRIGHT
Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See LICENSE
file and https://dev.perl.org/licenses/