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

  • apt-get install libqbit-application-model-db-perl (http://perlhub.ru/)

Debug

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

Abstract methods

  • query

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

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));

finish

No arguments.

Check that transaction closed

Example:

$app->db->finish();

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;