Name

QBit::Application::Model::DB::Query

Description

Base class for DB queries.

Abstract methods

  • _found_rows

Package methods

init

No arguments.

Method called from "new" before return object.

select

Arguments:

  • %opts - options with keys

    • table - object

    • fields (optional, default: all fields)

    • filter (optional)

Return values:

  • $query - object

Example:

my $query = $app->db->query->select(
    table  => $app->db->users,
    fields => [qw(id login)],
    filter => {id => 3},
);

join

Arguments:

  • %opts - options with keys

    • table - object

    • alias (optional)

    • fields (optional, default: all fields)

    • filter (optional)

    • join_type (optional, default: 'INNER JOIN')

    • join_on (optional, default: use foreign keys)

Return values:

  • $query - object

Example:

my $join_query = $query->join(
    table     => $app->db->fio,
    fields    => [qw(name surname)],
    filter    => ['name' => 'LIKE' => \'Max'],
    join_type => 'INNER JOIN',
    join_on   => ['user_id' => '=' => {'id' => $app->db->users}],
);

left_join

join_type => 'LEFT JOIN'

right_join

join_type => 'RIGHT JOIN'

group_by

Arguments:

  • @fields

Return values:

  • $query - object

Example:

my $group_query = $query->group_by(qw(name surname));

having

Arguments:

  • $expression

Return values:

  • $query - object

Example:

my $query_with_having = $query->having([cnt => '>=' => \100]);

order_by

Arguments:

  • @fields - fields or reference to array

Return values:

  • $query - object

Example:

my $order_query = $query->order_by('id', ['login', 1]);

limit

Arguments:

  • @limit

Return values:

  • $query - object

Example:

my $limit_query = $query->limit(100, 200);

distinct

No arguments.

Return values:

  • $query - object

Example:

my $distinct_query = $query->distinct();

union

Arguments:

  • $query - object

  • %opts - options with keys

    • all - boolean (optional, default: FALSE)

Return values:

  • $query - object

Example:

my $union_query = $query->union(
    $app->db->query->select(
        table => $app->db->people,
        fields => [qw(id login name surname)]
    ),
    all => FALSE,
);

union_all

all => TRUE

calc_rows

Arguments:

  • $flag - boolean

Return values:

  • $query - object

Example:

my $calc_rows_query = $query->calc_rows(TRUE);

all_langs

Arguments:

  • $flag - boolean

Return values:

  • $query - object

Example:

my $all_langs_query = $query->all_langs(TRUE);

for_update

No arguments.

Return values:

  • $query - object

Example:

my $for_update_query = $query->for_update();

filter

get_sql_with_data

Arguments:

  • %opts - options with keys

    • offset - number (optional, default: 0)

Return values:

  • $sql - string

Example:

my $sql = $query->get_sql_with_data();

get_all

No arguments.

Return values:

  • $data - reference to array

Example:

my $data = $query->get_all();

update

Arguments:

  • %opts - options with keys

    • table - object

    • alias - alias for table (type: string)

    • data - data for update (type: reference to hash)

    • filter (optional)

Return values:

  • $query - object

Example:

my $query = $app->db->query->update(
    table  => $app->db->users,
    alias  => 'u',
    data   => {
        login => 'ChuckNorris',
        date  => {NOW => []},
    },
    filter => {id => 3},
);

do

Execute query and returns results

No arguments

Return values:

  • $res - count of a affected rows or -1

Example:

my $res = $query->do();

found_rows

No arguments.

Return values:

  • $bool

Example:

my $bool = $query->found_rows();

For more information see code and test.