Name

QBit::Application::Model::DB - base class for DB.

Description

Base class for working with databases.

GitHub

https://github.com/QBitFramework/QBit-Application-Model-DB

Install

  • cpanm QBit::Application::Model::DB

  • cpanm https://github.com/QBitFramework/QBit-Application-Model-DB.git

Debug

$QBit::Application::Model::DB::DEBUG = TRUE;

Abstract methods

  • query

  • get_query_id

  • filter

  • _get_table_object

  • _create_sql_db

  • _connect

  • _is_connection_error

Package methods

meta

Arguments:

  • %meta - meta information about database

Example:

package Test::DB;

use qbit;

use base qw(QBit::Application::Model::DB);

my $meta = {
    tables => {
        users => {
            fields => [
                {name => 'id',        type => 'INT',      unsigned => 1, not_null => 1, autoincrement => 1,},
                {name => 'create_dt', type => 'DATETIME', not_null => 1,},
                {name => 'login',     type => 'VARCHAR',  length => 255, not_null => 1,},
            ],
            primary_key => [qw(id)],
            indexes     => [{fields => [qw(login)], unique => 1},],
        },

        fio => {
            fields => [
                {name => 'user_id'},
                {name => 'name',    type => 'VARCHAR', length => 255,},
                {name => 'midname', type => 'VARCHAR', length => 255,},
                {name => 'surname', type => 'VARCHAR', length => 255,},
            ],
            foreign_keys => [[[qw(user_id)] => 'users' => [qw(id)]]]
        },
    },
};

__PACKAGE__->meta($meta);

in Appplication.pm

use Test::DB accessor => 'db';

get_all_meta

Arguments:

  • $package - package object or name (optional)

Return values:

  • $meta - meta information about database

Example:

my $meta = $app->db->get_all_meta('Test::DB');

init

No arguments.

Method called from "new" before return object.

make_tables

It create "accessors" for accessing tables.

Arguments:

  • $tables - reference of a hash (required).

    {
        table_name => <string (table name also)>,
        table_name => <object (QBit::Application::Model::DB::Table)>,
        table_name => <hash (definition of a table)>,
    }

Example:

$app->db->make_tables({clients => 'users'});
# or
$app->db->make_tables({clients => $app->db->users});
# or
$app->db->make_tables({
    clients => {
       fields       => ...,
       primary_key  => ...,
       indexes      => ...,
       foreign_keys => ...,
       collate      => ...,
       engine       => ...,
    }
});

# after
$app->db->clients->create();
$app->db->clients->add_multi(...);
$app->db->drop();

set_dbh

Arguments:

  • $dbh - Database handle object (optional)

Return values:

  • $dbh - Database handle object or undef

Example:

my $dbh = DBI->connect(...);

# set
$app->db->set_dbh($dbh);

# clear
$app->db->set_dbh();

dbh

No arguments.

returns a database handle object or undef

Example:

my $dbh = $app->db->dbh;

quote

Arguments:

  • $name - string

Return values:

  • $quoted_name - quoted string

Example:

my $quoted_name = $app->db->quote('users'); # 'users'

quote_identifier

Arguments:

  • $name - string

Return values:

  • $quoted_name - quoted string

Example:

my $quoted_name = $app->db->quote_identifier('users'); # "users"

begin

No arguments.

start a new transaction or create new savepoint

Example:

$app->db->begin();

commit

No arguments.

commits the current transaction or release savepoint

Example:

$app->db->commit();

rollback

No arguments.

rolls back the current transaction or savepoint

Example:

$app->db->rollback();

transaction

Arguments:

  • $sub - reference to sub

Example:

$app->db->transaction(sub {
    # work with db
    ...
});

create_sql

Arguments:

  • @tables - table names (optional)

Return values:

  • $sql - sql

Example:

my $sql = $app->db->create_sql(qw(users));

init_db

Arguments:

  • @tables - table names (optional)

Example:

$app->db->init_db(qw(users));

post_run

No arguments.

Check that transaction closed

Example:

$app->db->post_run();

Internal packages

QBit::Application::Model::DB::Class - base class for DB modules;
QBit::Application::Model::DB::Field - base class for DB fields;
QBit::Application::Model::DB::Filter - base class for DB filters;
QBit::Application::Model::DB::Query - base class for DB queries;
QBit::Application::Model::DB::Table - base class for DB tables;
QBit::Application::Model::DB::VirtualTable - base class for DB virtual tables;