ID: 3002 - Mini OOP wrapper for Mysql PHP functions
Posted by nebiros on Fri Feb 26 16:51:47 UTC 2010. Language php

<?php

class App_Db_Adapter_Mysql
{
    protected $_options = array(
        "host" => "localhost",
        "username" => "root",
        "password" => "",
        "dbname" => ""
    );

    protected $_resource = null;
    
    public function __construct( Array $options = array() )
    {
        if ( false === empty( $options ) )
        {
            $this->setOptions( $options );
        }
    }

    public function setOptions( Array $options )
    {
        $this->_options = $options;
        return $this;
    }

    public function getOptions()
    {
        return $this->_options;
    }

    public function setOption( $key, $value = null )
    {
        $this->_options[$key] = $value;
        return $this;
    }

    public function getOption( $key, $default = null )
    {
        if ( true === isset( $this->_options[$key] ) )
        {
            return $this->_options[$key];
        }

        return $default;
    }

    public function connect()
    {
        try
        {
            if ( false === (
                $this->_resource = mysql_connect(
                    $this->getOption( "host" ),
                    $this->getOption( "username" ),
                    $this->getOption( "password" )
            ) ) )
            {
                throw new Exception( "mysql_connect() function error (" . mysql_error() . ")" );
            }

            if ( false === mysql_select_db( $this->getOption( "dbname" ) ) )
            {
                throw new Exception( "mysql_select_db() function error (" . mysql_error() . ")" );
            }
        }
        catch ( Exception $e )
        {
            throw new Exception( "Can't connect ({$e->getMessage()})" );
        }

        return $this;
    }

    public function getConection()
    {
        return $this->_resource;
    }

    public function disconnect()
    {
        try
        {
            if ( false === mysql_close( $this->_resource ) )
            {
                throw new Exception( "mysql_close() function error (" . mysql_error() . ")" );
            }
        }
        catch ( Exception $e )
        {
            throw new Exception( "Can't disconnect ({$e->getMessage()})" );
        }
    }
    
    public function query( $query )
    {
        try
        {
            if ( false === ( $resource = mysql_query( $query, $this->_resource ) ) )
            {
                throw new Exception( "mysql_query() function error (" . mysql_error() . ")" );
            }
        }
        catch ( Exception $e )
        {
            throw new Exception( "Can't query this database ({$e->getMessage()})" );
        }

        return $resource;
    }

    public function insert( $table = null, Array $data = array() )
    {
        try
        {
            if ( false !== empty( $table ) )
            {
                return null;
            }

            if ( false !== empty( $data ) )
            {
                return null;
            }

            foreach ( $data AS $column => $value )
            {
                if ( $value === null || strtolower( $value ) === "null" || $value === "" )
                {
                    $data[$column] = "NULL";
                }
                else
                {
                    $data[$column] = "'" . mysql_real_escape_string( $value, $this->_resource ) . "'";
                }
            }

            $query = "INSERT INTO
                {$table}
                ( " . implode( ", ", array_keys( $data ) ) . " )
                VALUES
                ( " . implode( ", ", $data ) . " )
            ";
        }
        catch ( Exception $e )
        {
            throw new Exception( "Can't insert data ({$e->getMessage()})" );
        }

        return $this->query( $query );
    }

    public function update( $table = null, Array $data = array(), $where = null )
    {
        try
        {
            if ( false !== empty( $table ) )
            {
                return null;
            }

            if ( false !== empty( $data ) )
            {
                return null;
            }

            $set = array();

            foreach ( $data AS $column => $value )
            {
                if ( $value === null || strtolower( $value ) == "null" || $value == "" )
                {
                    $set[] = $column . " = NULL";
                }
                else
                {
                    $set[] = $column . " = '" . mysql_real_escape_string( $value, $this->_resource ) . "'";
                }
            }

            $query = "UPDATE
                {$table}
                SET " . implode( ", ", $set ) .
                ( ( false === empty( $where ) ) ? "\n WHERE {$where}" : null );
        }
        catch ( Exception $e )
        {
            throw new Exception( "Can't update data ({$e->getMessage()})" );
        }

        return $this->query( $query );
    }

    public function delete( $table, $where = null )
    {
        try
        {
            if ( false !== empty( $table ) )
            {
                return null;
            }

            if ( false === empty( $where ) )
            {
                $where = "WHERE " . $where;
            }

            $query = "DELETE FROM {$table} {$where}";
        }
        catch ( Exception $e )
        {
            throw new Exception( "Can't delete data ({$e->getMessage()})" );
        }

        return $this->query( $query );
    }

    public function lastInsertId( $table = null )
    {
        try
        {
            $tableQuery = null;

            if ( false === empty( $table ) )
            {
                $tableQuery = "FROM {$table}";
            }

            $query = trim( "SELECT LAST_INSERT_ID() AS last_insertd_id {$tableQuery}" );
            $result =  $this->query( $query );

            if ( false === $result )
            {
                throw new Exception( mysql_error() . " (" . $query . ")" );
            }

            $fetch = $this->fetchAssoc( $result );
        }
        catch ( Exception $e )
        {
            throw new Exception( "Can't get last insert id ({$e->getMessage()})" );
        }

        return ( int ) $fetch["last_insertd_id"];
    }

    public function fetchAssoc( $resource )
    {
        return mysql_fetch_assoc( $resource );
    }

    public function fetchObject( $resource, $className = null, Array $params = null )
    {
        return mysql_fetch_object( $resource );
    }
}