Name

QBit::Application::Model::DB::mysql::Table - Class for MySQL tables.

Description

Implements methods for MySQL tables.

Package methods

add

Arguments:

  • $data - reference to hash or object (QBit::Application::Model::DB::Query)

  • %opts - additional options

    • replace - boolean (uses 'REPLACE' instead 'INSERT')

    • fields - reference to array (order for fields)

Return values:

  • $id - ID new record (returns array if primary key has more than one columns)

Example:

my $id = $app->db->users->add({login => 'Login'});

$id = $app->db->users->add({
    login => 'Login',
    name => 'Name',
    phone => '3-56-54'
  },
  fields => [qw(login name)]
);
# insert only login and name in this order

$app->db->users->add(
  $app->db->query->select(
      table => $app->db->logs,
      fields => [qw(login name)],
      filter => {date => '2017-09-19'}
  ),
  fields => [qw(login name)]
);

# mysql
INSERT INTO `users` (`login`, `name`) SELECT
    `logs`.`login` AS `login`,
    `logs`.`name` AS `name`
FROM `logs`
WHERE (
    `logs`.`date` = '2017-09-19'
)

add_multi

ADD_CHUNK (records number in one statement; default: 1000)

$QBit::Application::Model::DB::mysql::ADD_CHUNK = 500;

Arguments:

  • $data - reference to array

  • %opts - additional options

    • replace - boolean

    • fields - reference to array (order for fields)

    • identical_rows - boolean (true: get field names from first row, false: Unites all fields from all rows; default: false)

    • ignore_extra_fields - boolean (true: ignore field names that not exists in table, false: throw exception; default: false)

    • ignore - boolean (true: adds 'IGNORE' after 'INSERT/REPLACE', false: without 'IGNORE'; default: false)

Return values:

  • $count - records number

Example:

my $count = $app->db->users->add_multi([{login => 'Login 1'}, {login => 'Login 2'}]); # $count = 2

$count = $app->db->users->add_multi([
      {
        login => 'Login 1',
        name => 'Name 1',
        phone => '3-56-54'
      },
      {
        login => 'Login 2',
        name => 'Name 2',
        phone => '2-54-56'
      }
  ],
  fields => [qw(login name)]
);
# $count = 2, insert only date and hits in this order

create_sql

returns sql for create table.

No arguments.

Return values:

  • $sql - string

Example:

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

delete

Arguments:

  • $pkeys_or_filter - perl variables or object (QBit::Application::Model::DB::filter)

Example:

$app->db->users->delete(1);
$app->db->users->delete([1]);
$app->db->users->delete({id => 1});
$app->db->users->delete($app->db->filter({login => 'Login'}));

replace

Same as

$app->db->users->add($data, replace => TRUE);