2017 © Pedro Peláez
 

library database

Database library for LiftKit

image

liftkit/database

Database library for LiftKit

  • Friday, July 6, 2018
  • by rwstream9
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,053 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 59 Versions
  • 4 % Grown

The README.md

LiftKit database library

Installation

composer require liftkit/database

Let's jump right in to some examples., (*1)

Connection

Establish a connection

use LiftKit\Database\Connection\MySql;
use LiftKit\DependencyInjection\Container\Container;
use LiftKit\Database\Cache\Cache;
use PDO;

$connection = new MySql(
  new Container,
    new Cache,
    new PDO('connectionString', 'username', 'password')
);

Run a raw SQL query

$results = $connection->query(
  "
    SELECT *
    FROM tbl
  "
);

Using placeholders

$connection->query(
  "
    SELECT *
    FROM tbl
    WHERE col1 = ?
      AND col2 = ?
  ",
  [
    'val1',
    'val2',
  ]
);

Result objects

Loop through results

// NOTE: 
// Results are not loaded into memory. Instead they are
// wrapped by an object of the class
// \LiftKit\Database\Result\Result

$results = $connection->query(
    "
        SELECT *
        FROM tbl
    "
);

foreach ($results as $row) {
  echo 'column "name" = ' . $row['name'] . PHP_EOL;
  echo 'column "id" = ' . $row['id'] . PHP_EOL;
}

Fetch a single column as an array

foreach ($results->fetchColumn('id') as $id) {
    echo $id . PHP_EOL;
}
// '1'
// '2'
// ...

Fetch all rows as an array of entities

foreach ($results->fetchAll() as $row) {
    // Do something with $row['column']
}

Fetch all rows as an array of associative arrays

foreach ($results->flatten() as $row) {
    // Do something with $row['column']
}

Query builder

New query

use LiftKit\Database\Query\Query;

/**
 * @var Query $query
 */
$query = $connection->createQuery();

Simple select query

// SELECT field1, field2
// FROM tbl
// WHERE field1 = 'val1'

$results = $query->select('field1', 'field2')
  ->from('tbl')
  ->whereEqual('field1', 'val1')
  ->execute();

More complicated select query

Note that the method $connection->quoteIdentifier() is called on the right parameters. That's because the right parameter is expected to be a value. If it is instead a SQL identifier, it must be quoted., (*2)

This example shows the query with MySQL style identifier quotes to illustrate the point. Note the difference between the JOIN conditions and the WHERE conditions., (*3)

use LiftKit\Database\Query\Condition\Condition;

// SELECT `field1`, `field2`
// FROM `tbl`
// LEFT JOIN `other_tbl` ON (
//  `tbl`.`field1` = `other_tbl`.`field1`
//  OR `tbl`.`field2` > `other_tbl`.field2`
// )
// WHERE `tbl`.`field1` = 'val1'
//    OR `other_tbl`.`field2` = 'val2'
// GROUP BY `tbl`.`field3`, `tbl`.`field4`
// HAVING `tbl`.`field1` < 1
// ORDER BY `tbl`.`field5` ASC, `tbl`.`field6` DESC

$results = $query->select('field1', 'field2')
  ->from('tbl')
  ->leftJoin(
    'other_tbl',
    $connection->createCondition()
        ->equal(
            'tbl.field1',
            $connection->quoteIdentifier('other_tbl.field1')
        )
        ->orGreaterThan(
            'tbl.field2',
            $connection->quoteIdentifier('other_tbl.field2')
        )
  )
  ->whereEqual('tbl1.field1', 'val1')
  ->orWhereEqual('other_tbl.field2', 'val2')
  ->groupBy('tbl.field3')
  ->groupBy('tbl.field4')
  ->havingLessThan('tbl.field1', 1)
  ->orderBy('tbl.field5', Query::ORDER_ASC)
  ->orderBy('tbl.field6', Query::ORDER_DESC)
  ->execute();

Update query

Note that update queries can utilize conditions the same as select statements., (*4)

// UPDATE tbl
// SET field2 = 'val2', field3 = 'val3'
// WHERE tbl.id = 2

$query->update()
  ->table('tbl')
  ->set(
    [
      'field2' => 'val2',
      'field3' => 'val3',
    ]
  )
  ->whereEqual('tbl.id', 2)
  ->execute();

Insert query

Insert queries return their insert ID., (*5)

// INSERT INTO tbl
// SET field2 = 'val2', field3 = 'val3'

$id = $query->insert()
  ->into('tbl')
  ->set(
    [
      'field2' => 'val2',
      'field3' => 'val3',
    ]
  )
  ->execute();

Delete query

Note that delete queries can use conditions the same as select queries., (*6)

// DELETE tbl.*
// FROM tbl
// WHERE id = 1

$query->delete()
  ->from('tbl')
  ->whereEqual('id', 1)
  ->execute();

Subqueries

Subqueries can be substituted pretty much anywhere a value or identifier can be., (*7)

Note: This is also an example of how to use raw SQL instead of escaped values in your queries using the method $connection->createRaw(). Select arguments, like the left had side of conditions, will be quoted as an identifier unless otherwise specified., (*8)

// SELECT *
// FROM tbl1
// WHERE
// ( SELECT COUNT(*)
//   FROM tbl2
//   WHERE tbl1.id = tbl2.tbl1_id
// ) = 1

$results = $query->select('*')
  ->from('tbl1')
  ->whereEqual(
    $connection->createQuery()
      ->select($connection->createRaw('COUNT(*)'))
      ->from('tbl2')
      ->whereEqual(
        'tbl1.id', 
        $connection->quoteIdentifier('tb2.tbl1_id')
      ),
    1
  )
  ->execute();

Composing parts of queries

This comes in hand for extracting away parts of queries you use often, while retaining the ability to combine them with other queries., (*9)

Let's say you have a function that returns all of the rows from tbl., (*10)

function getAllTblRows ()
{
  return $connection->createQuery()
    ->select('*')
    ->from('tbl')
    ->execute();
}

// SELECT *
// FROM tbl

$results = getActiveTblRows();

Now you need another query which select only records which are active from tbl. Notice the additions to getAllTblRows., (*11)

function getAllTblRows (Query $inputQuery = null)
{
  return $connection->createQuery()
    ->select('*')
    ->from('tbl')
    ->composeWith($inputQuery)
    ->execute();
}

function getActiveTblRows ()
{
  $query = $connection->createQuery()
    ->whereEqual('active', 1);

  return getAllTblRows($query);
}

// SELECT *
// FROM tbl
// WHERE active = 1

$results = getActiveTblRows();

Table objects

Table objects are meant to reduce the boilerplate you need to place in your query builder queries., (*12)

Fetching multiple rows

use LiftKit\Database\Schema\Schema;
use LiftKit\Database\Schema\Table\Table;

// We'll get back to schemas in a moment

$table = new Table(
    $connection,
    new Schema($connection),
    'tbl'
);

// SELECT *
// FROM tbl

$results = $tbl->getRows();

Fetching multiple rows with composed query

// SELECT *
// FROM tbl
// WHERE active = 1

$results = $table->getRows(
    $connection->createQuery()
        ->whereEqual('active', 1)
);

Fetching a single row

// SELECT *
// FROM tbl
// WHERE id = 1
// LIMIT 1

$row = $table->getRow(1);

// 'val1'
echo $row['field1'];

// 'val2'
echo $row['field2'];

Inserting a new row

````php // INSERT INTO tbl // SET field1 = 'val1', field2 = 'val2', (*13)

$id = $table->insertRow( [ 'field1' => 'val1', 'field2' => 'val2', ] );, (*14)


### Updating a row NOTE: The library will auto-detect the primary key column and create an equal condition on that column. ```php // UPDATE tbl // SET field1 = 'val1', field2 = 'val2' // WHERE id = 1 $table->updateRow( [ 'id' => 1, 'field1' => 'val1', 'field2' => 'val2', ] );

Deleting a row

// DELETE FROM tbl
// WHERE id = 1

$table->deleteRow(1);

More info on table objects, relations, and entities coming soon!, (*15)

The Versions

06/07 2018

dev-master

9999999-dev

Database library for LiftKit

  Sources   Download

LGP-2.1 LGPL-2.1-only

The Requires

 

The Development Requires

06/07 2018

v2.17.7

2.17.7.0

Database library for LiftKit

  Sources   Download

LGPL-2.1-only

The Requires

 

The Development Requires

25/04 2018

v2.17.6

2.17.6.0

Database library for LiftKit

  Sources   Download

LGPL-2.1-only

The Requires

 

The Development Requires

25/04 2018

v2.17.5

2.17.5.0

Database library for LiftKit

  Sources   Download

LGPL-2.1-only

The Requires

 

The Development Requires

16/03 2018

v2.17.4

2.17.4.0

Database library for LiftKit

  Sources   Download

LGPL-2.1-only

The Requires

 

The Development Requires

16/03 2018

v2.17.3

2.17.3.0

Database library for LiftKit

  Sources   Download

LGPL-2.1-only

The Requires

 

The Development Requires

29/01 2018

v2.17.2

2.17.2.0

Database library for LiftKit

  Sources   Download

LGPL-2.1-only

The Requires

 

The Development Requires

13/07 2017

v2.17.1

2.17.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

20/06 2017

v2.17.0

2.17.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

03/05 2017

v2.16.0

2.16.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

13/03 2017

v2.15.0

2.15.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

06/02 2017

v2.14.1

2.14.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

10/10 2016

v2.14.0

2.14.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

06/06 2016

v2.13.2

2.13.2.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

01/06 2016

v2.13.1

2.13.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

25/03 2016

v2.13.0

2.13.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

24/03 2016

v2.12.0

2.12.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

19/01 2016

v2.11.2

2.11.2.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

18/01 2016

v2.11.1

2.11.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

01/12 2015

v2.11.0

2.11.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

25/08 2015

v2.10.0-alpha

2.10.0.0-alpha

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

25/08 2015

v2.10.0

2.10.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

19/08 2015

v2.9.0

2.9.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

05/08 2015

v2.9.0-alpha10

2.9.0.0-alpha10

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

04/08 2015

v2.9.0-alpha9

2.9.0.0-alpha9

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

04/08 2015

v2.9.0-alpha8

2.9.0.0-alpha8

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

03/08 2015

v2.9.0-alpha7

2.9.0.0-alpha7

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

03/08 2015

v2.9.0-alpha6

2.9.0.0-alpha6

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

03/08 2015

v2.9.0-alpha5

2.9.0.0-alpha5

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

03/08 2015

v2.9.0-alpha4

2.9.0.0-alpha4

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

31/07 2015

v2.9.0-alpha3

2.9.0.0-alpha3

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

31/07 2015

v2.9.0-alpha.2

2.9.0.0-alpha2

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

31/07 2015

v2.9.0-alpha.1

2.9.0.0-alpha1

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

22/05 2015

v2.8.0

2.8.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

20/05 2015

v2.7.1

2.7.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

20/05 2015

v2.7.0

2.7.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

19/05 2015

v2.6.5

2.6.5.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

19/05 2015

v2.6.4

2.6.4.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

19/05 2015

v2.6.3

2.6.3.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

07/05 2015

v2.6.2

2.6.2.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

05/05 2015

v2.6.1

2.6.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

22/04 2015

v2.6.0

2.6.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

22/04 2015

v2.5.1

2.5.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

22/04 2015

v2.5.0

2.5.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

09/04 2015

v2.4.2

2.4.2.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

09/04 2015

v2.4.1

2.4.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

09/04 2015

v2.4.0

2.4.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

08/04 2015

v2.3.4

2.3.4.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

06/04 2015

v2.3.3

2.3.3.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

31/03 2015

v2.3.2

2.3.2.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

20/03 2015

v2.3.1

2.3.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

20/03 2015

v2.3.0

2.3.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

13/03 2015

v2.2.0

2.2.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

13/03 2015

v2.1.1

2.1.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

13/03 2015

v2.1.0

2.1.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

28/02 2015

v2.0.0

2.0.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

28/02 2015

v1.1.0-alpha

1.1.0.0-alpha

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

26/02 2015

v1.0.1

1.0.1.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires

26/02 2015

v1.0

1.0.0.0

Database library for LiftKit

  Sources   Download

LGP-2.1

The Requires

 

The Development Requires