Giter VIP home page Giter VIP logo

sway-dblayer's Introduction

DBLayer

Wrapper for PHP' pdo driver.
This packages comes from swayengine 2.

Installation

composer require rev/sway-dblayer

This will install package at latest version.

Requirements

This packages requires PDO driver (for default, bundled with PHP).

Usage

  • Create connection handler
<?php
/**
* Helps to create connection credentials
 */
$dblayerConnector = new DBLayerConnector();
$dblayerConnector->setDatabaseHostName('localhost');
$dblayerConnector->setDatabaseListenerPort(3306);
$dblayerConnector->setDatabaseName('my_db');
$dblayerConnector->setDatabaseUserName('john_sky');
$dblayerConnector->setUserPassword('my_secret_password');
$dblayerConnector->setSchemaPrefix('dev');
/**
* Sets to 'pdo_mysql'
 */
$dblayerConnector->useMysqlDriver();

/**
* Connects to database and get DBLayer instance
 */
$db = $dblayerConnector->connect();
?>
  • Fetching rows
<?php

/**
* Returns all rows with given type as associative array
 */
$db->Run("SELECT * FROM %pr%my_table WHERE type = ?", 
    [ 'eg_type', PDO::PARAM_STR])->assoc();

/**
* Returns first fetched row as associative array
 */
$db->Run("SELECT * FROM %pr%my_table WHERE type = ?",
    [ 'eg_type', PDO::PARAM_STR ])->assoc(0);

/**
* Returns value under column 'column1' at first fetched row.
 */
$db->Run("SELECT column1, column_2 FROM %pr%my_table WHERE type = ?",
    [ 'eg_type', PDO::PARAM_STR ])->assoc(0, 'column1');

/**
* Executes an query and pass array with fetched rows to given anonymous function.
* Anonymous function must returns a value.
 */
$db->Run("SELECT * FROM %pr%my_table WHERE type = ?",
    [ 'eg_type', PDO::PARAM_STR ])->assoc(function($entries) {
        //do something with fetched entries       
        return $entries;
    });
?>
  • Inserting, updating, removing rows
<?php

$db = (new DBLayerConnector())->connect();

/**
* Returns True if rows have been updated,
 * returns false if no rows have been updated.
 */
$db->Run("UPDATE %pr%my_table SET column1 = ? WHERE type = ?",
    [ 'value1', PDO::PARAM_STR ],
    [ 'eg_type', PDO::PARAM_STR ])->assoc();

?>

sway-dblayer's People

Contributors

reverze avatar

Watchers

James Cloos 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.