GithubHelp home page GithubHelp logo

pwfoo / db Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dealnews/db

0.0 2.0 0.0 368 KB

Database Library providing a PDO factory and CRUD operations

License: BSD 3-Clause "New" or "Revised" License

PHP 98.30% Shell 1.70%

db's Introduction

DealNews Datbase Library

Factory

The factory creates PDO objects using \DealNews\GetConfigto read database settings and create a PDO database connection.

Supported Settings

Setting Description
type This may be one of pdo, mysql, or pgsql. All types return PDO connections.
dsn A valid PDO DSN. See each driver for specifics
db The name of the database. For PDO, this is usually in the DSN.
server One of more comma separated servers names. Not used by the pdo type.
port Server port. Not used by the pdo type.
user Database user name. Not all PDO drivers require one.
pass Database password. Not all PDO drivers require one.
charset Character set to use for mysql connections. The default is utf8mb4.
options A JSON encoded array of options to pass to the PDO constructor. These vary by driver

Usage

Example:

$mydb = \DealNews\DB\Factory::init("mydb");

CRUD

The CRUD class is a helper that wraps up common PDO logic for CRUD operations.

Basic Usage

$mydb = \DealNews\DB\Factory::init("mydb");
$crud = new \DealNews\DB\CRUD($mydb);

// Create
$result = $crud->create(
    // table name
    "test",
    // data to add
    [
        "name"        => $name,
        "description" => $description,
    ]
);

// Read
$rows = $crud->read(
    // table name
    "test",
    // where clause data
    ["id" => $id]
);

// Update
$result = $crud->update(
    // table name
    "test",
    // data to update
    ["name" => "Test"],
    // where clause data
    ["id" => $id]
);

// Delete
$result = $crud->delete(
    // table name
    "test",
    // where clause data
    ["id" => $row["id"]]
);

Advanced Usage

The class also exposes a run method which is used internally by the other methods. Complex queries can be run using this method by providing an SQL query and a parameter array which will be mapped to the prepared query. It returns a PDOStatement object.

// Run a select with no parameters
$stmt = $crud->run("select * from table limit 10");

// Run a select query with paramters
$stmt = $crud->run(
    "select * from table where foo = :foo"
    [
        ":foo" => $foo
    ]
);

Testing

By default, only unit tests are run. To run the functional tests the host machine will need to be a docker host. Also, the pdo_pgsql, pdo_mysql, and pdo_sqlite extensions must be installed on the host machine. PHPUnit will start and stop docker containers to test the MySQL and Postgres connections. Use --group functional when running PHPUnit to run these tests.

db's People

Contributors

brianlmoon avatar

Watchers

 avatar  avatar

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.