molajo/database

This package is abandoned and no longer maintained. No replacement package was suggested.

Molajo Database: platform independent multi-database support for PHP Applications

Installs: 1 044

Dependents: 0

Suggesters: 0

Security: 0

Watchers: 4

Open Issues: 0

Type:molajo-package

v0.4 2014-04-07 15:46 UTC

This package is not auto-updated.

Last update: 2015-08-05 12:45:08 UTC


README

[ALPHA] Database

Build Status

Simple, uniform Database Services API for PHP applications enabling interaction with multiple Database types and packages. Currently supported are MySqli, Postgresql, and MS SQL Server.

Instantiate the Database Adapter

Before using the Database Adapter, instantiate the desired database Handler and pass that object into the Database Adapter constructor as a dependency.

    // 1. Instantiate the Handler Desired
    $options                    = array();
    $options['db_type']         = $configuration->db_type;
    $options['db_host']         = $configuration->db_host;
    $options['db_user']         = $configuration->db_user;
    $options['db_password']     = $configuration->db_password;
    $options['db_name']         = $configuration->db;
    $options['db_prefix']       = $configuration->db_prefix;
    $options['process_events'] = $configuration->process_events;
    $options['select']          = true;

    $class = 'Molajo\\Database\\Handler\\MySql';
    $handler = new $class($options);

    // 2. Instantiate the Molajo Database Adapter, passing in the instantiated Handler
    $class = 'Molajo\\Database\\Adapter';
    $adapter = new $class($handler);

Interacting with Databases

Once instantiated as defined above, the database $adapter can be used to execute database queries.

Escape and Filter

It is an important security precaution to escape strings and filter numeric data before sending it to the database.

    $secure_string_data = $adapter->escape("this isn't that hard.");

    $secure_numeric_data = (int) $integer;

Quote and Namequote

String data must be quoted. Use namequote for table names, columns, etc.

    $quoted_fieldname = $adapter->quotename('Fieldname');

    $where = ' where ' . $adapter->quotename('Fieldname')
        . ' = ' . $adapter->quote($adapter->escape("don't forget to escape"));

Querying the Database

Query Object

First, obtain a Query Object which you will use to build queries:

    $query_object = $adapter->getQueryObject();

Molajo has three possible query options:

  1. Return a single value
  2. Return an array containing one or more rows of data where the row is defined as an object
  3. Directly interacting with the database using an object.

Select: Single Result

    $query_object = $adapter->getQueryObject();

    $query_object->select('count(*)');
    $query_object->from('#__actions');

    $result = $this->product_result->loadResult();

    echo $result;

Select Row(s)

    $query_object = $adapter->getQueryObject();

    $query_object->select('*');
    $query_object->from('#__actions');
    $query_object->order('id');

    $results = $this->product_result->loadObjectList();

    if (count($results) > 0) {
        foreach ($results as $row) {
            $id = $row->id;
            $title = $row->title;
            //etc.
        }
    }

Offset and Limit

To limit the number of rows returned and/or to start at a certain row, add offset and limit to the loadObjectList parameters.

    $query_object = $adapter->getQueryObject();

    $query_object->select('*');
    $query_object->from('#__actions');
    $query_object->order('id');

    $query_offset = 0;
    $query_count = 5;
    $results = $adapter->loadObjectList($query_offset, $query_count);

Update

Get a query object, write the query, execute the query.

   $query_object = $adapter->getQueryObject();

   $query_object->update('#__table');
   $query_object->set('x = y');
   $query_object->where('id = 1');

   $results = $adapter->execute();

Delete

   $query_object = $adapter->getQueryObject();

   $query_object->delete('#__table');
   $query_object->where('id = 1');

   $results = $adapter->execute();

Other SQL

For other database functions, like creating a table or executing a stored procedure, use execute.

   // connect to $adapter, as described above
   $query_object = $adapter->getQueryObject();

   $sql = 'build this or whatever sql is needed';
   $results = $adapter->execute($sql);

   // that's it.

Install using Composer from Packagist

Step 1: Install composer in your project

    curl -s https://getcomposer.org/installer | php

Step 2: Create a composer.json file in your project root

{
    "require": {
        "Molajo/Database": "1.*"
    }
}

Step 3: Install via composer

    php composer.phar install

Requirements and Compliance