The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Nile::Database - SQL database manager.

SYNOPSIS

    # set database connection params to connect
    # $args{driver}, $args{host}, $args{dsn}, $args{port}, $args{attr}
    # $args{name}, $args{user}, $args{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(%args);
    

DESCRIPTION

Nile::Database - SQL database manager.

dbh()

    $app->db->dbh;

Get or set the current database connection handle.

connect()

    $dbh = $app->db->connect(%args);

Connect to the database. If %args empty, it will try to get args from the config object. Returns the database connection handle is success.

disconnect()

    $app->db->disconnect;

Disconnect from this connection handle.

run()

    $app->db->run($qry);

Run query using the DBI do command or abort if error.

do()

    $app->db->do($qry);

Run query using the DBI do command and ignore errors.

exec()

    $sth = $app->db->exec($qry);

Prepare and execute the query and return the statment handle.

begin_work()

    $app->db->begin_work;

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.

commit()

    $app->db->commit;

Commit (make permanent) the most recent series of database changes if the database supports transactions and AutoCommit is off.

rollback()

    $app->db->rollback;

Rollback (undo) the most recent series of uncommitted database changes if the database supports transactions and AutoCommit is off.

quote()

    $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.

col()

    # 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

row()

    # select id, email, fname, lname from users
    @row = $app->db->row($qry);

Returns one row as array.

rows()

    # 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.

hash()

    # 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

row_object()

    # 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()

    %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

colhash()

    # 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.

value()

    # select email from users where id=123. return one column value
    $value = $app->db->value($qry);

Returns one column value from one row.

insertid()

    $id = $app->db->insertid;

Returns the last insert id from auto increment.

db_error()

    $app->db->db_error;

Aborts the application and display the last database error message.

Bugs

This project is available on github at https://github.com/mewsoft/Nile.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Nile.

SOURCE

Source repository is at https://github.com/mewsoft/Nile.

SEE ALSO

See Nile for details about the complete framework.

AUTHOR

Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> Website: http://www.mewsoft.com

COPYRIGHT AND LICENSE

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.