tomrf/seminorm

pdo sql query builder and executor

0.0.7 2023-03-20 00:05 UTC

This package is auto-updated.

Last update: 2024-04-23 17:58:22 UTC


README

PHP Version Require Latest Stable Version License

PHP SQL query builder and executor, using PDO.

📔 Go to documentation

Installation

Installation via composer:

composer require tomrf/seminorm

Usage

$db = new \Tomrf\Seminorm\Seminorm(
   new PdoConnection(
       PdoConnection::dsn(
           'mysql',
           'my_database',
           'localhost',
       ),
       'username',
       'password',
       []           // array of PDO options, ATTR_PERSISTENT, ATTR_ERRMODE etc..
   ),
   new Factory(QueryBuilder::class),
   new Factory(PdoQueryExecutor::class),
   null,            // optional row class, defaults to array
   null,            // optional data value class, defaults to string
);

$rows = $db->execute(
    'SELECT * FROM `my_table` WHERE id = :id',
    [ 'id' => 1 ]
)->findMany();

$rowId = $db->execute(
    $db->query()->insertInto(
       'my_table', 
       [ 'name' => 'My Name', 'email' => 'mail@example.com' ]
    )
)->getLastInsertId();

...

Testing

composer test

License

This project is released under the MIT License (MIT). See LICENSE for more information.

Documentation

📂 Tomrf\Seminorm\Seminorm::class

__construct()

public function __construct(
    Tomrf\Seminorm\Pdo\PdoConnection $connection,
    Tomrf\Seminorm\Factory\Factory $queryBuilderFactory,
    Tomrf\Seminorm\Factory\Factory $queryExecutorFactory,
    ?string $rowClass = '',
    ?string $valueClass = ''
): void

getConnection()

Return the active connection.

public function getConnection(): Tomrf\Seminorm\Pdo\PdoConnection

query()

public function query(): Tomrf\Seminorm\QueryBuilder\QueryBuilder

execute()

public function execute(
    Tomrf\Seminorm\Interface\QueryBuilderInterface|string $query,
    array $parameters = []
): Tomrf\Seminorm\Pdo\PdoQueryExecutor

@param    array<int|string,mixed> $parameters

setLogger()

Sets a logger.

public function setLogger(
    Psr\Log\LoggerInterface $logger
): void

@param    \Tomrf\Seminorm\LoggerInterface $logger

📂 Tomrf\Seminorm\Data\ImmutableArrayObject::class

__get()

public function __get(
    string $name
): mixed

__isset()

public function __isset(
    mixed $name
): void

offsetSet()

public function offsetSet(
    mixed $key,
    mixed $value
): void

@SuppressWarnings (PHPMD.UnusedFormalParameter)

offsetUnset()

public function offsetUnset(
    mixed $key
): void

@SuppressWarnings (PHPMD.UnusedFormalParameter)

offsetGet()

public function offsetGet(
    mixed $key
): mixed

offsetExists()

public function offsetExists(
    mixed $key
): bool

__construct()

public function __construct(
    object|array $array = [],
    int $flags = 0,
    string $iteratorClass = 'ArrayIterator'
): void

append()

public function append(
    mixed $value
): void

getArrayCopy()

public function getArrayCopy(): void

count()

public function count(): void

getFlags()

public function getFlags(): void

setFlags()

public function setFlags(
    int $flags
): void

asort()

public function asort(
    int $flags = 0
): void

ksort()

public function ksort(
    int $flags = 0
): void

uasort()

public function uasort(
    callable $callback
): void

uksort()

public function uksort(
    callable $callback
): void

natsort()

public function natsort(): void

natcasesort()

public function natcasesort(): void

unserialize()

public function unserialize(
    string $data
): void

serialize()

public function serialize(): void

__serialize()

public function __serialize(): void

__unserialize()

public function __unserialize(
    array $data
): void

getIterator()

public function getIterator(): void

exchangeArray()

public function exchangeArray(
    object|array $array
): void

setIteratorClass()

public function setIteratorClass(
    string $iteratorClass
): void

getIteratorClass()

public function getIteratorClass(): void

__debugInfo()

public function __debugInfo(): void

📂 Tomrf\Seminorm\Data\Row::class

toArray()

public function toArray(): array

@return   array <string,mixed>

toJson()

public function toJson(): string

__get()

public function __get(
    string $name
): mixed

__isset()

public function __isset(
    mixed $name
): void

offsetSet()

public function offsetSet(
    mixed $key,
    mixed $value
): void

@SuppressWarnings (PHPMD.UnusedFormalParameter)

offsetUnset()

public function offsetUnset(
    mixed $key
): void

@SuppressWarnings (PHPMD.UnusedFormalParameter)

offsetGet()

public function offsetGet(
    mixed $key
): mixed

offsetExists()

public function offsetExists(
    mixed $key
): bool

__construct()

public function __construct(
    object|array $array = [],
    int $flags = 0,
    string $iteratorClass = 'ArrayIterator'
): void

append()

public function append(
    mixed $value
): void

getArrayCopy()

public function getArrayCopy(): void

count()

public function count(): void

getFlags()

public function getFlags(): void

setFlags()

public function setFlags(
    int $flags
): void

asort()

public function asort(
    int $flags = 0
): void

ksort()

public function ksort(
    int $flags = 0
): void

uasort()

public function uasort(
    callable $callback
): void

uksort()

public function uksort(
    callable $callback
): void

natsort()

public function natsort(): void

natcasesort()

public function natcasesort(): void

unserialize()

public function unserialize(
    string $data
): void

serialize()

public function serialize(): void

__serialize()

public function __serialize(): void

__unserialize()

public function __unserialize(
    array $data
): void

getIterator()

public function getIterator(): void

exchangeArray()

public function exchangeArray(
    object|array $array
): void

setIteratorClass()

public function setIteratorClass(
    string $iteratorClass
): void

getIteratorClass()

public function getIteratorClass(): void

__debugInfo()

public function __debugInfo(): void

📂 Tomrf\Seminorm\Data\Value::class

__construct()

public function __construct(
    string|int|float|bool|null $data
): void

__toString()

public function __toString(): string

asString()

public function asString(): string

asInt()

public function asInt(): int

asFloat()

public function asFloat(): float

asBool()

public function asBool(): bool

isNumeric()

public function isNumeric(): bool

isInt()

public function isInt(): bool

isString()

public function isString(): bool

isBool()

public function isBool(): bool

isNull()

public function isNull(): bool

getType()

public function getType(): string

📂 Tomrf\Seminorm\Factory\Factory::class

__construct()

public function __construct(
    string $class
): void

@param    class-string $class

make()

public function make(
    mixed $params
): mixed

📂 Tomrf\Seminorm\Pdo\PdoConnection::class

__construct()

public function __construct(
    PDO|string $dsnOrPdo,
    ?string $username = '',
    ?string $password = '',
    ?array $options = []
): void

@param    \PDO|string $dsnOrPdo DSN string or an existing PDO object
@param    null|array<int,int> $options PDO options array

getPdo()

Get the PDO resource object for this connection.

public function getPdo(): ?PDO

getOptions()

Get PDO options array for this connection.

public function getOptions(): ?array

@return   null|array<int,int>

isConnected()

Returns true if database connection has been established.

public function isConnected(): bool

getDsn()

Get the value of DSN.

public function getDsn(): ?string

getUsername()

Get the value of username.

public function getUsername(): ?string

dsn()

Static helper function to build DSN string for PDO.

public static function dsn(
    string $driver,
    string $dbname,
    ?string $host = '',
    int $port = 3306,
    string $charset = 'utf8mb4'
): string

connect()

Connect to the database if not already connected.

public function connect(): void

@throws   \RuntimeException

disconnect()

Disconnect from the database.

public function disconnect(): void

__debugInfo()

Mask password when dumping.

public function __debugInfo(): array

@return   array<string,string|null|array<int,int>>

📂 Tomrf\Seminorm\Pdo\PdoQueryExecutor::class

__construct()

public function __construct(
    Tomrf\Seminorm\Pdo\PdoConnection $connection,
    ?string $rowClass = '',
    ?string $valueClass = ''
): void

getRowCount()

Returns the number of rows affected by the last SQL statement.

public function getRowCount(): int

getLastInsertId()

Returns the last inserted row ID as string.

public function getLastInsertId(): string|false

execute()

Prepare and execute PDOStatement from an instance of QueryBuilderInterface.

public function execute(
    Tomrf\Seminorm\Interface\QueryBuilderInterface|string $query,
    array $parameters = []
): static

@throws   \PDOException

findOne()

Fetch next row from the result set as Row.

public function findOne(): object|array|null

@return   null|(null|object|string)[]|object

findMany()

Fetch all rows from query result set.

public function findMany(): object|array

@return   array<int,(null|object|string)[]|object>

📂 Tomrf\Seminorm\QueryBuilder\QueryBuilder::class

__toString()

public function __toString(): string

selectFrom()

public function selectFrom(
    string $table,
    string $columns
): static

insertInto()

Insert a row into a table

public function insertInto(
    string $table,
    array $values = []
): static

@param    string $table
@param    array<string,int|string|float|null> $values
@throws   \InvalidArgumentException

update()

public function update(
    string $table,
    array $values = []
): static

@param    array<string,int|string|float|null> $values
@throws   \InvalidArgumentException

deleteFrom()

public function deleteFrom(
    string $table
): static

set()

public function set(
    string $column,
    string|int|float $value
): static

setRaw()

public function setRaw(
    string $column,
    string $expression
): static

setFromArray()

public function setFromArray(
    array $values
): static

@param    array<string,null|float|int|string> $values
@throws   \InvalidArgumentException

alias()

public function alias(
    string $expression,
    string $alias
): static

join()

public function join(
    string $table,
    string $joinCondition,
    ?string $joinType = ''
): static

limit()

public function limit(
    int $limit,
    ?int $offset = null
): static

offset()

public function offset(
    int $offset
): static

onDuplicateKey()

public function onDuplicateKey(
    string $expression
): static

getQuery()

public function getQuery(): string

getQueryParameters()

public function getQueryParameters(): array

@return   (null|bool|float|int|string)[]

orderByAsc()

public function orderByAsc(
    string $column
): static

orderByDesc()

public function orderByDesc(
    string $column
): static

select()

public function select(
    string $columns
): static

selectAs()

public function selectAs(
    string $expression,
    string $alias
): static

selectRaw()

public function selectRaw(
    string $expressions
): static

selectRawAs()

public function selectRawAs(
    string $expression,
    string $alias
): static

where()

public function where(
    string $column,
    string $operator,
    string|int|float $value
): static

whereRaw()

public function whereRaw(
    string $expression
): static

whereColumnRaw()

public function whereColumnRaw(
    string $column,
    string $expression
): static

whereEqual()

public function whereEqual(
    string $column,
    string|int|float $value
): static

whereNotEqual()

public function whereNotEqual(
    string $column,
    string|int|float $value
): static

whereNull()

public function whereNull(
    string $column
): static

whereNotNull()

public function whereNotNull(
    string $column
): static

📂 Tomrf\Seminorm\Sql\SqlCompiler::class

getQuery()

public function getQuery(): string

getQueryParameters()

public function getQueryParameters(): array

@return   (null|bool|float|int|string)[]

Generated 2022-11-11T01:32:36+01:00 using 📚tomrf/readme-gen