2017 © Pedro Peláez
 

library glpdo2

The easy to use and better PDO Wrapper.

image

geeklab/glpdo2

The easy to use and better PDO Wrapper.

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 8 % Grown

The README.md

License: BSD phpstan enabled Build Status Scrutinizer Code Quality Code Coverage, (*1)

GeekLab\GLPDO2

Easy to use PDO Wrapper for PHP >= 8.1, (*2)

Latest

2022-11-25 (4.0.9) * Style fixes., (*3)

Features

  • Bind value by the correct type. E.g. Don't bind as a string where an integer bind should be.
  • Bindings are injected, so you can create your own!
  • Help prevent injections.
  • PSR1/2/4 Compliant.

Installation

composer require geeklab/glpdo2, (*4)

Todo

  • More tests, since we can test at many levels and implementations.
  • Better schema for testing.
  • Better docs.
  • More of a real wold example.
  • Reduce the complexity?

Basic Usage (Quick-N-Dirty)

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

define('DS', DIRECTORY_SEPARATOR);
require_once '..' . DS . '..' . DS . 'vendor' . DS . 'autoload.php';

$dbConn    = new PDO('mysql:host=localhost;dbname=playground', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$db        = new \GeekLab\GLPDO2\GLPDO2($dbConn);
$Statement = new \GeekLab\GLPDO2\Statement(GLPDO2\Bindings\MySQL\MySQLBindingFactory::build());
$start     = 0;
$limit     = 5;

$Statement->sql('SELECT *')
          ->sql('FROM (')
          ->sql('          SELECT *')
          ->sql('          FROM   `mock_data`')
          ->sql('          LIMIT  ?, ?')->bInt($start)->bInt($limit)
          ->sql('     ) SUBQ')
          ->sql('ORDER BY `id` DESC;');

// Show computedSQL statement
print_r($Statement->getComputed());

$res = $db->selectRows($Statement);

print_r($res);

Data Binding Methods

Statement->bBool($value, optional boolean $null)br/ Binds a value as bool(true, false), with optional NULL value return., (*5)

Statement->bBoolInt($value, optional boolean $null)br/ Binds a value as int(0, 1), with optional NULL value return., (*6)

Statement->bDate($value, optional boolean $null)br/ Binds a value as a date (string - validated for YYYY-MM-DD), with optional NULL return., (*7)

Statement->bDateTime($value, optional boolean $null)br/ Binds a value as a date time (string - validated for YYYY-MM-DD HH:MM:SS), with optional NULL return., (*8)

Statement->bFloat($value, optional integer $decimals, optional boolean $null)br/ Binds a value aa a float, with decimal place (default of 3) and optional NULL return. Use '%%' instead of '?'., (*9)

Statement->bInt($value, optional boolean $null)br/ Bind a value as an integer, whith optional NULL return., (*10)

Statement->bIntArray(array $data, integer $default)br/ Converts an array of integers to a comma separated values. Will output $default (which is 0) if $data is not an array. Used with IN() statements. Use '%%' instead of '?'., (*11)

Statement->bJSON($data, optional boolean $null)br/ Binds a JSON object or string as a string, with optional NULL value. Throws JsonException., (*12)

Statement->bLike($value, boolean $ends, boolean $starts)br/ Binds a value as a string for LIKE queries. $ends = "ends with", $starts = "starts with", (*13)

Statement->bStr($value, optional boolean $null, optional \PDO::PARAM_* $type)br/ Binds a value as a string, with optional NULL value return and optional PDO binding type (default \PDO::PARAM_STR)., (*14)

Statement->bStrArr(array $values, optional string $default)br/ Binds a string converted array for use with IN statements. $default is used when value isn't an array, which the default is NULL. Use '%%' instead of '?'., (*15)

Statement->bind($name, $value, \PDO::PARAM_* $type)br/ Binds a value to a named parameter with option PDO binding type (default \PDO::PARAM_STR), (*16)

Statement->bRaw($value)br/ Binds a raw value to '%%' in the sql statement. This is unquoted and unescaped. Good for tables names and functions. Can be dangerous if not handled correctly., (*17)

Query Methods

GLPDO->queryDelete(Statement $SQL)br/ Runs a delete query and returns numbers of affected rows., (*18)

GLPDO->queryInsert(Statement $SQL)br/ Runs an insert query and returns the primary ID., (*19)

GLPDO->queryUpdate(Statement $SQL)br/ Runs an update query and returns number of affect rows, (*20)

GLPDO->selectRows(Statement $SQL)br/ Run a normal query, returns multiple rows as an array of associative arrays, or false., (*21)

GLPDO->selectRow(Statement $SQL)br/ Runs a normal query, returns a single row as an array, or false., (*22)

GLPDO->selectValue(Statement $SQL, $column, $default)br/ Runs a normal query, returns a single column ($column) and can return a default (mixed $default = null) value is no value is in the column., (*23)

GLPDO->selectRow(Statement $SQL)br/ Runs a normal query, returns a single row as an array., (*24)

GLPDO->beginTransaction()br/ Begins an SQL Transaction., (*25)

GLPDO->commit()br/ Commits an SQL Transaction., (*26)

GLPDO->inTransaction()br/ Is there a transaction in progress, returns bool., (*27)

Misc Methods

Statement->sql(string $text)br/ Used to build up the SQL parameterized statement., (*28)

Statement->reset()br/ Used to reset Statement private variables. Usefully for creating multiple queries without having to create a new Statement object., (*29)

Statement->execute(\PDO $PDO)br/ Prepares and executes the statement, (*30)

Statement->getComputed()br/ Returns what the compiled SQL query string might look like for debugging purposes., (*31)

Alternative Packages

  • https://github.com/nkt/flame

The Versions

03/07 2018

dev-master

9999999-dev

The easy to use and better PDO Wrapper.

  Sources   Download

MIT

The Development Requires

geeklab

03/07 2018

1.1.0

1.1.0.0

The easy to use and better PDO Wrapper.

  Sources   Download

MIT

The Development Requires

geeklab

12/03 2018

1.0.4

1.0.4.0

The easy to use and better PDO Wrapper.

  Sources   Download

MIT

The Development Requires

geeklab

06/12 2017

1.0.3

1.0.3.0

The easy to use and better PDO Wrapper.

  Sources   Download

MIT

The Development Requires

geeklab

06/12 2017

1.0.2

1.0.2.0

The easy to use and better PDO Wrapper.

  Sources   Download

MIT

The Development Requires

geeklab

03/12 2017

1.0.1

1.0.1.0

The easy to use and better PDO Wrapper.

  Sources   Download

MIT

The Development Requires

geeklab

19/11 2017

1.0.0

1.0.0.0

The easy to use and better PDO Wrapper.

  Sources   Download

MIT

The Development Requires

geeklab