Nile::DBI - SQL database manager.
# set database connection params to connect # $arg{driver}, $arg{host}, $arg{dsn}, $arg{port}, $arg{attr} # $arg{name}, $arg{user}, $arg{pass} # if called without params, it will try to load from the default config vars. # get app context $app = $self->app; $dbh = $app->db->connect(%arg);
$app->db->dbh;
Get or set the current database connection handle.
$dbh = $app->db->connect(%arg);
Connect to the database. If %arg empty, it will try to get arg from the config object. Returns the database connection handle is success.
$app->db->disconnect;
Disconnect from this connection handle.
$app->db->run($qry);
Run query using the DBI do command or abort if error.
$app->db->do($qry);
Run query using the DBI do command and ignore errors.
$sth = $app->db->exec($qry);
Prepare and execute the query and return the statment handle.
$app->db->begin;
Enable transactions (by turning AutoCommit off) until the next call to commit or rollback. After the next commit or rollback, AutoCommit will automatically be turned on again.
$app->db->commit;
Commit (make permanent) the most recent series of database changes if the database supports transactions and AutoCommit is off.
$app->db->rollback;
Rollback (undo) the most recent series of uncommitted database changes if the database supports transactions and AutoCommit is off.
$app->db->quote($value); $app->db->quote($value, $data_type);
Quote a string literal for use as a literal value in an SQL statement, by escaping any special characters (such as quotation marks) contained within the string and adding the required type of outer quotation marks.
# select id from users. return one column array from all rows @cols = $app->db->col($qry); $cols_ref = $app->db->col($qry);
Return one column array from all rows
# select id, email, fname, lname from users @row = $app->db->row($qry);
Returns one row as array.
# select id, fname, lname, email from users @rows = $app->db->rows($qry); $rows_ref = $app->db->rows($qry);
Returns all matched rows as array or array ref.
# select * from users where id=$id limit 1 %user = $app->db->hash($qry); $user_ref = $app->db->hash($qry);
Returns one row as a hash or hash ref
# select * from users where id=$id limit 1 $row_obj = $app->db->row_object($qry); print $row_obj->email; print $row_obj->fname; print $row_obj->lname;
Returns one row as object with columns names as object properties.
%hashes = $app->db->hashes($qry, $col); $hashes_ref = $app->db->hashes($qry, $col);
Returns list or hashes of all rows. Each hash element is a hash of one row
# select id, user from users %hash = $app->db->colhash($qry);
Returns all rows as a hash of the first column as the keys and the second column as the values.
# select email from users where id=123. return one column value $value = $app->db->value($qry);
Returns one column value from one row.
$id = $app->db->insertid;
Returns the last insert id from auto increment.
# level: # 0x01=DBI, 0x02=!Statement ,0x04=!MethodName, 0x06=!Statement:!Method, # 0x08=!MethodClass, 0x10=!Caller2, 0=disable $app->db->profile($level); # this will generate the reports file app/log/dbi.prof # then run the command dbiprof to view it: # dbiprof --number 15 --sort count
Enable DBI profiling. See DBI::Profile and DBI::ProfileDumper.
$app->db->db_error;
Aborts the application and display the last database error message.
This project is available on github at https://github.com/mewsoft/Nile.
Please visit the project's homepage at https://metacpan.org/release/Nile.
Source repository is at https://github.com/mewsoft/Nile.
See Nile for details about the complete framework.
Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> Website: http://www.mewsoft.com
Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, https://github.com/mewsoft/Nile, http://www.mewsoft.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Nile, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Nile
CPAN shell
perl -MCPAN -e shell install Nile
For more information on module installation, please visit the detailed CPAN module installation guide.