2017 © Pedro Pelรกez
 

library platform-gnfdb

A Database library

image

ridibooks/platform-gnfdb

A Database library

  • Monday, February 13, 2017
  • by blu
  • Repository
  • 10 Watchers
  • 0 Stars
  • 7,065 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 13 Versions
  • 24 % Grown

The README.md

GnfDB

Scrutinizer Code Quality Latest Stable Version License, (*1)

์—ฌ๊ธฐ๋Š” ridi ๋ชจ๋…ธ๋ ˆํฌ์— Git Subrepo ๋ฅผ ํ†ตํ•ด์„œ ์ฝ”๋“œ๊ฐ€ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค., (*2)

Warning ์ง์ ‘ commitํ•˜์ง€ ๋งˆ์„ธ์š”., (*3)

0. ์„ค์น˜

composer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์น˜ ํ•  ์ˆ˜ ์žˆ๋‹ค., (*4)

ridibooks/platform-gnfdb ๋˜๋Š” gnf/gnfdb๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๋œ๋‹ค., (*5)

composer require ridibooks/platform-gnfdb

1. ์—ฐ๊ฒฐ ๋ฐฉ๋ฒ•

gnfdb ํŒจํ‚ค์ง€ ๋‚ด์— Gnf\db\PDO ๋ฅผ instanceํ™” ํ•˜๋ฉด ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค., (*6)

Gnf\db\PDO๋Š” \PDO๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. ์—ฐ๊ฒฐ์„ ์ƒ์„ฑํ•˜๊ณ  ์ธ์ž๋กœ ์ „๋‹ฌํ•˜์ž., (*7)

<?php
$pdo_dbh = new PDO('mysql:host={host}', $user, $password);
$db = new Gnf\db\PDO($pdo_dbh);

2. SELECT ๊ด€๋ จ

select์— ์‚ฌ์šฉํ•˜๋Š” method ๋ชฉ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค., (*8)

  • sqlDict / sqlDicts - ๊ฒฐ๊ณผ ๊ฐ’์„ array(PDO::FETCH_ASSOC)๋กœ ๋ฐ›๋Š”๋‹ค.
  • sqlObject / sqlObjects - ๊ฒฐ๊ณผ ๊ฐ’์„ object(PDO::FETCH_OBJ)๋กœ ๋ฐ›๋Š”๋‹ค.

์œ„ method์˜ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค., (*9)

  • sqlDict($sql, ...)

\PDO์˜ prepare์™€ ๋ฌธ๋ฒ•์ด ๋™์ผํ•˜์ง€๋งŒ, ":column:" ๊ณผ ๊ฐ™์€ bind๋Š” ์ง€์›ํ•˜์ง€ ์•Š๊ณ , "?" ๋กœ ์‚ฌ์šฉํ•˜๋Š” bind๋งŒ ์ง€์›ํ•œ๋‹ค., (*10)

ํ•˜๋‚˜๋งŒ ๊ฐ€์ ธ์˜ค๋Š” sqlDict / sqlObject๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ null์„ returnํ•˜๊ณ , ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” sqlDicts / sqlObjects๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ empty array๋ฅผ returnํ•œ๋‹ค.ย , (*11)

์‚ฌ์šฉ ์˜ˆ์ œ, (*12)

$db->sqlDict('SELECT * FROM tb_book WHERE id = ?', $b_id);
$db->sqlDicts(
    'SELECT *
    FROM tb_book
    INNER JOIN cpdp_books ON (tb_book.id = cpdp_books.b_id AND cpdp_books.approve_status = ?)
    WHERE tb_book.id = ? AND tb_book.pub_id = ?',
    ApproveStatus::OPEN,
    $b_id,
    $pud_ib
);

3. WHERE ์กฐ๊ฑด์˜ ํ™•์žฅ๋œ ์‚ฌ์šฉ๋ฒ•

where ์กฐ๊ฑด์„ ์ง์ ‘์ ์œผ๋กœ ์ง€์ •ํ•˜๊ณ  ?๋ฅผ bind ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ถˆํŽธํ•˜๊ธฐ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์ง€์›ํ•œ๋‹ค., (*13)

  • sqlWhere($where)

$where์€ column => condition ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด์ด๋‹ค., (*14)

3.1 ์กฐ๊ฑด ์ž‘์„ฑ ๋ฒ•

  • ๋‹จ์ˆœ ๋น„๊ต๋Š” ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
    • sqlLesser($value)
    • < $value
    • sqlLesserEqual($value)
    • <= $value
    • sqlGreater($value)
    • > $value
    • sqlGreaterEqual($value)
    • >= $value
  • ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž
    • sqlBetween($start, $end)
    • $start <= value <= $end
    • sqlRange($start, $end)
    • $start <= b < $end
  • like ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.
    • sqlLike($keyword)
    • like '%{$keyword}%'
    • sqlLikeBegin($keyword)
    • like '{$keyword}%'

์กฐ๊ฑด ์ž‘์„ฑ ์˜ˆ์ œ, (*15)

<?php
//๋‹จ์ˆœ ๋น„๊ต
$db->sqlDict(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['id' => $b_id])
); // => SELECT * FROM tb_book WHERE id = {$b_id}


//๋น„๊ต ์—ฐ์‚ฐ์ž
$db->sqlDict(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['pub_id' => sqlLesser($pub_id)])
); // => SELECT * FROM tb_book WHERE pub_id < {$pub_id}


//Between
$db->sqlDict(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['regdate' => sqlBetween('20160101000000', '20161231235959')])
); // => SELECT * FROM tb_book WHERE regdate BETWEEN '20160101000000' AND '20161231235959'


//like
$db->sqlDict(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['title' => sqlLike('์ฒดํ—˜ํŒ')])
); // => SELECT * FROM tb_book WHERE title like '%์ฒดํ—˜ํŒ%'
  • ๋‹จ์ˆœ ๋น„๊ต ์‹œ ๋น„๊ต ๋Œ€์ƒ์ด array์ธ ๊ฒฝ์šฐ IN ์ฟผ๋ฆฌ๋กœ ์ ์šฉ๋œ๋‹ค.

๋น„๊ต ๋Œ€์ƒ์ด array์ธ ์ฟผ๋ฆฌ ์˜ˆ์ œ, (*16)

<?php
$b_ids = ['101000940', '101000941', '101000945'];
$db->sqlDicts(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['id' => $b_ids])
); // => SELECT * FROM tb_book WHERE id IN ('101000940', '101000941', '101000945')

3.2 ๊ฐ™์ด ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜

  • sqlRaw($value)
    • ์ž…๋ ฅ ํ•œ ๊ฐ’ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
  • sqlLimit($count)
    • limit $count
  • sqlLimit($from, $count)
    • limit $from, $count
  • sqlNull()
    • null ๊ฐ’, ๊ทธ๋ƒฅ ๊ฐ’์œผ๋กœ null์„ ์‚ฌ์šฉํ•ด๋„ ๋™์ผํ•˜๋‹ค.
  • sqlNot($value)
    • Not์ด ์ ์šฉ ๋œ๋‹ค. sqlGreater ๋“ฑ ๋‹ค๋ฅธ ์—ฐ์‚ฐ๊ณผ ๋ณตํ•ฉ์ ์œผ๋กœ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

3.3 AND, OR ๋ณตํ•ฉ ์‚ฌ์šฉ

์•ž์„œ ์„ค๋ช…ํ–ˆ๋˜ sqlWhere์€ AND๋กœ ์ž‘์„ฑํ•˜๋Š” ํ•จ์ˆ˜๊ณ , sqlOr($where) ํ•จ์ˆ˜๋กœ OR๋ฌธ์„ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค., (*17)

์ด ๋‘ ํ•จ์ˆ˜๋ฅผ ์„ž์–ด ์‚ฌ์šฉํ•จ์œผ๋กœ ๋‹ค์–‘ํ•œ where๋ฌธ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค., (*18)

๋ณตํ•ฉ ์ฟผ๋ฆฌ, (*19)

<?php
$where = [
   'tb_book.pub_id' => sqlNot(101),
   sqlOr(
      [
         'tb_book.category' => sqlNot(
            [
               4001,
               4003
            ]
         )
      ],
      [
         'tb_book.serial_completed' => sqlNot('Y'),
         'tb_book.series_id' => sqlNot('')
      ],
      ['tb_category.genre' => sqlNot('comic')],
      ['tb_book.is_setbook' => 'Y']
   )
];
$db->sqlDicts(
    'SELECT tb_book.id
    FROM tb_book
    LEFT JOIN tb_book_comic ON (tb_book.id = tb_book_comic.b_id)
    LEFT JOIN tb_category ON (tb_book.category = tb_category.id)
    WHERE ?',
    sqlWhere($where)
);
/*
SELECT tb_book.id
FROM tb_book
LEFT JOIN tb_book_comic ON (tb_book.id = tb_book_comic.b_id)
LEFT JOIN tb_category ON (tb_book.category = tb_category.id)
WHERE tb_book.pub_id != 101
    AND (
        tb_book.category NOT IN (4001, 4003)
        OR (
            tb_book.serial_completed != 'Y'
                AND tb_book.series_id != ''
        )
        OR tb_category.genre != 'comic'
        OR tb_book.is_setbook = 'Y'
    )
*/

3.4 ํ™•์žฅ๋œ ํ…Œ์ด๋ธ” ๊ตฌ๋ฌธ

ํ…Œ์ด๋ธ”๋ช…์„ ์ง์ ‘์ ์œผ๋กœ ์ฟผ๋ฆฌ์— ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, JOIN์˜ ON ์กฐ๊ฑด์œผ๋กœ ์—ฐ๊ฒฐ๋˜๋Š” ์ปฌ๋Ÿผ๋งŒ ์ž‘์„ฑํ•จ์œผ๋กœ ๊ฐ„๋‹จํ•œ JOIN ์ฟผ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค., (*20)

์ง€์›๋˜๋Š” ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค., (*21)

  • sqlTable($table) - ๋‹จ์ผ ํ…Œ์ด๋ธ” ์‚ฌ์šฉ
  • sqlJoin($tables) - JOIN ์ฟผ๋ฆฌ
  • sqlLeftJoin($tables) - LEFT JOIN ์ฟผ๋ฆฌ
  • sqlInnerJoin($tables) - INNER JOIN ์ฟผ๋ฆฌ

์ž‘์„ฑ ๋ฐฉ๋ฒ•, (*22)

  • ํ…Œ์ด๋ธ” ๋ชฉ๋ก์€ array๋กœ ์ž‘์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.
  • key, value๋ชจ๋‘ ์„œ๋กœ ON์œผ๋กœ JOINํ•  ์ปฌ๋Ÿผ์„ ์ž‘์„ฑํ•œ๋‹ค.
  • ์•ž์„œ ์„ ์–ธ๋˜์–ด ์žˆ๋Š” ํ…Œ์ด๋ธ”์„ key๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.
  • ์ฒซ ์„ ์–ธ์€ key๊ฐ€ from ์ ˆ ํ…Œ์ด๋ธ”์ด๊ณ , value๊ฐ€ ํ›„์— ๋”ฐ๋ผ์˜ค๋Š” join ํ…Œ์ด๋ธ”์ด๋‹ค.
    • value์— ๋‹จ์ผ ํ…Œ์ด๋ธ”์„ ์ ์–ด์„œ ํ•˜๋‚˜๋งŒ JOIN ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋ฐฐ์—ด๋กœ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ์ž‘์„ฑํ•˜๋ฉด ์ˆœ์ฐจ์ ์œผ๋กœ JOIN๋ฌธ์ด ์ž‘์„ฑ๋œ๋‹ค.

ํ…Œ์ด๋ธ” ๊ตฌ๋ฌธ ์ž‘์„ฑ ์˜ˆ์ œ, (*23)

<?php
// ๋‹จ์ผ ํ…Œ์ด๋ธ”
$db->sqlDict('SELECT * FROM ?', sqlTable('tb_book'));
// => SELECT * FROM tb_book


// join ํ…Œ์ด๋ธ” - 1:1 ๊ด€๊ณ„๋งŒ
$tables = [
   'tb_book.id' => 'tb_book_comic.b_id',
   'tb_book.category' => 'tb_category.id'
];
$db->sqlDicts('SELECT * FROM ?', sqlLeftJoin($tables));
/*
SELECT *
FROM tb_book
LEFT JOIN tb_book_comic ON (tb_book.id = tb_book_comic.b_id)
LEFT JOIN tb_category ON (tb_book.category = tb_category.id)
*/


// join ํ…Œ์ด๋ธ” - ์ฒซ ํ…Œ์ด๋ธ”์— join์ด ์—ฌ๋Ÿฌ๊ฐœ ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ
$tables = [
   'tb_book_production.b_id' => ['tb_book.id', 'platform_withhold.b_id'],
   'tb_book.pub_id' => 'tb_publisher.id',
   'tb_publisher.id' => 'tb_publisher_manager.pub_id',
   'tb_book.id' => 'tb_book_search.b_id'
];
$db->sqlDicts('SELECT * FROM ?', sqlLeftJoin($tables));
/*
SELECT *
FROM tb_book_production
LEFT JOIN tb_book ON (tb_book_production.b_id = tb_book.id)
LEFT JOIN platform_withhold ON (tb_book_production.b_id = production_withhold.b_id)
LEFT JOIN tb_publisher ON (tb_book.pub_id = tb_publisher.id)
LEFT JOIN tb_publisher_manager ON (tb_publisher.id = tb_publisher_manager.pub_id)
LEFT JOIN tb_book_search ON (tb_book.id = tb_book_search.b_id)
*/

3.5 ๊ธฐํƒ€

  • sqlAdd($something) - column + $something ์ฟผ๋ฆฌ๋กœ ๋ณ€ํ™˜
  • sqlStrcat($something) - concat(colum, $something) ์ฟผ๋ฆฌ๋กœ ๋ณ€ํ™˜
  • sqlPassword($something) - sql password() function
  • sqlColumn($column) - ์ปฌ๋Ÿผ๋ช…์„ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ
  • sqlWhereWithClause($where) - sqlWhere()๊ณผ ๋™์ผํ•˜์ง€๋งŒ, 'WHERE '์ด ์กฐ๊ฑด ๋ฌธ ์•ž์— ์„ ํ–‰๋œ๋‹ค.
  • sqlRange($A, $B) - $a <= column < $b
  • sqlNow() - sql now() function

4. CRUD

CRUD ์ค‘ ๋‹ค์Œ method๋ฅผ ์ง€์›ํ•œ๋‹ค., (*24)

  • sqlInsert(table,data)
  • sqlUpdate(table, data, where);
  • sqlDelete(table, where)
  • sqlInsertOrUpdate(table, data, update_where = null) - INSERT INTO ~~ ON DUPLICATE KEY UPDATE ๊ตฌ๋ฌธ, (*25)

  • sqlInsertBulk(table, data_keys, data_valuess) - Bulk์šฉ sqlInsert, (*26)

  • sqlInsertOrUpdateBulk(table, data_keys, data_valuess) - Bulk์šฉ sqlInsertOrUpdate

๋ณ€์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค., (*27)

  • table - ๋‹จ์ผ ํ…Œ์ด๋ธ” ๋ช… (sqlTable์„ ์ ์šฉํ•˜์ง€ ์•Š์€)
  • data - ์ถ”๊ฐ€/์ˆ˜์ • ํ•  ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด, column => value ๋กœ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•จ
    • data_keys ์ถ”๊ฐ€/์ˆ˜์ • ํ•  ๋ฐ์ดํ„ฐ์˜ ํ‚ค ๋ฐฐ์—ด
    • data_valuess ์ถ”๊ฐ€/์ˆ˜์ • ํ•  ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด ๋‚˜์—ด, [[column => value], [column => value] ... ] ๋กœ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•จ
  • where, update_where - ์กฐ๊ฑด ๋ฌธ (sqlWhere์„ ์—†์–ด๋„ ๋จ)

5. transaction

transaction์€ ๋‹ค์Œ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ง€์›ํ•œ๋‹ค., (*28)

  • transactional, (*29)

    • php $success = $db->transactional(function($db) { $db->sqlDo($sql); });
  • sqlBegin-sqlEnd, (*30)

    • $db->sqlBegin();
      dbQueries($db);
      $success = $db->sqlEnd();
      
    • sqlEnd ๋Œ€์‹  sqlCommit, sqlRollback method๋„ ๊ฐ€๋Šฅ., (*31)

6. ๊ธฐํƒ€ ๋“ฑ๋“ฑ

์ž‘์„ฑ ๋˜์–ด ์žˆ๋Š” method ๋ชฉ๋ก, (*32)

  • sqlDo($sql, ...) - ๋‹จ์ˆœ ์‹คํ–‰
  • sqlCount($table, $where) - SELECT count(*) FROMย ~~ ์˜ ์ถ•์•ฝํ˜•
  • sqlData($sql, ...) - Dict/Object์™€ ๋‹ค๋ฅด๊ฒŒ SELECT์ ˆ์˜ ์ฒซ๋ฒˆ์งธ ์ปฌ๋Ÿผ๋งŒ ์ถ”์ถœํ•œ๋‹ค, ๊ฐ’์ด ์—†์œผ๋ฉด null
  • sqlDatas($sql, ...) - ์œ„์™€ ๋™์ผํ•˜๋ฉฐ, ๊ฐ’์ด ์—†์œผ๋ฉด empty array
  • sqlArray($sql, ...) - Dict์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, key ๊ฐ’์ด ์ˆซ์ž๋กœ ์ž…๋ ฅ๋จ (PDO::FETCH_NUM)
  • sqlArrays($sql, ...) - Dicts์™€ ์œ ์‚ฌํ•˜๊ณ , ์œ„์™€ ๋™์ผ
  • sqlLine($sql, ...) - sqlArray์™€ ๋™์ผ
  • sqlLines($sql, ...) - sqlArrays์™€ ๋™์ผ
  • sqlDump($sql, ...) - query parse ๊ฒฐ๊ณผ๊ฐ’์„ return
  • sqlDumpBegin(); sqlDo($sql, ...); sqlDumpEnd() - sqlDo ํ˜ธ์ถœ ์‹œ ์‹คํ–‰ ๋œ query๋ฅผ sqlDumpEnd์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

The Versions

13/02 2017

dev-master

9999999-dev

A Database library

  Sources   Download

MIT

The Requires

 

The Development Requires

by genesos

13/02 2017

v0.1.11

0.1.11.0

A Database library

  Sources   Download

MIT

The Requires

 

The Development Requires

by genesos

16/11 2016

v0.1.10

0.1.10.0

A Database library

  Sources   Download

MIT

The Requires

 

The Development Requires

by genesos

09/11 2016

v0.1.9

0.1.9.0

A Database library

  Sources   Download

MIT

The Requires

 

The Development Requires

by genesos

07/11 2016

v0.1.8

0.1.8.0

A Database library

  Sources   Download

MIT

The Requires

 

The Development Requires

by genesos

07/11 2016

v0.1.7

0.1.7.0

A Database library

  Sources   Download

MIT

The Requires

 

The Development Requires

by genesos

04/11 2016

v0.1.6

0.1.6.0

A Database library

  Sources   Download

MIT

The Requires

 

The Development Requires

by genesos

17/08 2016

v0.1.5

0.1.5.0

A Database library

  Sources   Download

MIT

by genesos

12/08 2016

v0.1.4

0.1.4.0

A Database library

  Sources   Download

MIT

by genesos

10/08 2016

v0.1.3

0.1.3.0

A Database library

  Sources   Download

MIT

by genesos

10/08 2016

v0.1.2

0.1.2.0

A Database library

  Sources   Download

MIT

by genesos

12/07 2016

v0.1.1

0.1.1.0

A Database library

  Sources   Download

MIT

by genesos

02/02 2016

v0.1

0.1.0.0

A Database library

  Sources   Download

MIT

by genesos