Giter Site home page Giter Site logo

gonzie / pdoload Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 4.0 46 KB

Small PDO wrapper/abstraction layer to provide read/write endpoints and load balancing capabilities in one line of code

License: MIT License

PHP 100.00%
pdo-wrapper pdo-php load-balancer read-write-splitting read-write read-write-separation pdo load-balancing database-abstraction-layer database-abstraction

pdoload's Introduction

Travis CI build PHP from Packagist License from pugx

PDOLoad

PDOLoad is a small PDO wrapper/abstraction layer to provide read/write endpoints and load balancing capabilities in one line of code. Load balancing is usually include within most major PHP Frameworks however when working with large legacy projects it's hard to come by.

Main Features

  • Add multiple read and write endpoints.
  • One line implementation. No need to rewrite your PDO queries.
  • Transaction aware. Any reads while a transaction is active will be performed on the write connection.

Getting started

  • PHP >= 7.0 is required
  • Install PDOLoad using composer (recommmended) or manually
  • Configure your connections and you're ready to go!

Installing with Composer

composer require gonzie/pdoload

How to use

PDOLoad gives you as much choice as possible with little to no modification required. A normal PDO connection would be set like:

$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8mb4', 'username', 'password');

(most basic set up)

To use a minimalistic version of PDOLoad you would amend the code as follows:

$dbh = new Gonzie\PDOLoad\PDOLoad('mysql:host=localhost;dbname=testdb;charset=utf8mb4', 'username', 'password');

Alternatively, to use PDOLoad to it's full abilities, and for what it was actually made for, you can set up by passing an array with settings.

<?php
$settings = [
    'driver' => 'mysql',
    'reader' => [
        [
            'host' => 'mysuperduperdbreader_1.com',
            'dbname' =>  'test',
            'user' => 'gonzie',
            'password' => 'password',
            'port' => '3306'
            'charset' => 'utf8mb4',
        ],
        [
            'host' => 'mysuperduperdbreader_2.com',
            'dbname' =>  'test',
            'user' => 'gonzie',
            'password' => 'password',
            'charset' => 'utf8mb4',
        ],
    ],
    'writer' => [
        [
            'host' => 'mysuperduperdbwriter_1.com',
            'dbname' =>  'test',
            'user' => 'gonzie',
            'password' => 'password',
            'charset' => 'utf8mb4',
        ],
    ],
    'balancer' => 'round-robin',
];

$dbh = new Gonzie\PDOLoad\PDOLoad($settings);

Easy right? Here's a breakdown of the settings array:

Field Type Description
driver string Optional, default is mysql. Available options: pdo_mysql, drizzle_pdo_mysql, mysqli, pdo_sqlite, pdo_pgsql, pdo_oci, pdo_sqlsrv, sqlsrv, oci8, sqlanywhere
reader array[connection] Required. Array of connection arrays.
writer array[connection] Required. Array of connection arrays.
balancer string Optional, default is . Available options: round-robin - go from top to bottom of connection's array, fixed - PDOLoad will pick a connection at random and stick to it, random - each query will pick a random connection.
overwrite_allowed string Optional, default is false. Some driver options require additional options other than the ones in the connection array, set this value to true if you need to add custom options to the array otherwise they won't be allowed.

Connection array

Other options can be added to your liking as long as overwrite_allowed is set to true.

Field Type Description
host string Required.
dbname string Optional.
user string Required.
password string Required.
charset string Optional, default is utf8mb4.

Contributing

Any changes to the Project must abide by PSR Standards (PSR 2 - 12, PSR4 and others where applicable).

Please run phpcs before committing any code.

./vendor/bin/phpcs --standard=psr2 src/

pdoload's People

Contributors

gonzie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pdoload's Issues

advanced setting doesn't set passed charset setting or set any charset by default

Describe the bug
Advanced setting doesn't use charset set or set any charset by default

To Reproduce
Steps to reproduce the behavior:

  1. Go to PDOLoad class
  2. initPDO method doesn't set charset

Expected behavior
I want the charset set in the advanced setting to be part of the config passed to the PDO class when called within the initPDO method of PDOLoad class

Screenshots
Screen Shot 2022-02-09 at 4 17 39 PM

Desktop (please complete the following information):

  • OS: MacOS Catalina 10.15.7
  • Browser: Firefox, Chrome

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.