Zend - The PHP Company




Abstraction Layers

Add Code


fpatech - dbdriver MySQL abstraction layer class  

Type: class
Added by: fpapleux
Entered: 04/08/2003
Last modified: 09/12/2002
Rating: - (fewer than 3 votes)
Views: 6209
MySQL abstraction layer class. Easy to use. More functions than usual MySQL PHP functions to make it easier to port to another DB server platform.


<?php
    
/*
        ------------------------------------------------------------------------------------------

        dbdriver class
        version 1.0Beta
        ------------------------------------------------------------------------------------------
        
        the dbdriver class is a Database abstract class developed for MySQL.  Object is designed
        to work with the other PHP supported DB systems.  You need to adjust the functions.
        The reason for such many details in the methods is because not all functions are
        supported accross the bord and the SQL language can vary from a platform to another.
        Therefore, by being quite specific about each action, we help the developper of the main
        application to concentrate on building a generic code...
        
        
        
        License Information
        -------------------
        
        Copyright (C) 2003 Fabien Papleux, fpa Technology Consulting
        
        This program is free software; you can redistribute it and/or modify it under the terms
        of the GNU General Public License as published by the Free Software Foundation,
        version 2 of the License.

        This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
        without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
        See the GNU General Public License for more details.

        You should have received a copy of the GNU General Public License along with this program;
        if not, you can get a copy at http://www.gnu.org/licenses/gpl.txt
        
        
        
        Contact Information
        -------------------
        
        Should you have any questions / remarks / suggestions, please don't hesitate to
        contact me at dev_support@fpatech.com
        
        
        
        Synopsis
        --------
        
        Constants: Constants are defined outside the scope of the class and are global.
            DBDRIVER            =    1  : Indicates that the DBDRIVER Module is loaded
            CNT_AUTOINCREMENT    =     1    : Constraint -> Auto Increment
            CNT_PRIMARYKEY        =     2    : Constraint -> Primary Key
            CNT_UNIQUE            =     4    : Constraint -> Unique
            CNT_NOTNULL            =     8    : Constraint -> Not Null
            CNT_INDEXED            =    16    : Constraint -> Indexed
            
            
        Properties:
            $server            : server name, with port# if necessary
            $link_id        : ID for the current connection
            $user_name        : Name of the user
            $user_pwd        : User Password
            $current_db        : name of current database
            $current_table    : bane of the current table.  Usually used in conjunction w/
                              structural work.
            $result_id        : ID for the last result-set

        
        Methods:
        Constructor and structural methods:
            
            dbdriver($server_addr, $user, $pwd)
            classprint()
            connect($server_addr, $user, $pwd)
            close()
            
            db_create($database)
            db_select($database)
            db_exists($database)
            db_drop()
            
            table_create($table)
            table_select($table)
            table_exists($table)
            table_drop([$table])
            table_rename($table, $new_name)
            
            field_create($field_name, $type, $size, [$constraints], [$table_name])
            field_exists($field_name, [$table_name])
            field_drop($field_name, [$table_name])
                    
        All other methods:
            
            query($query)
            data_seek($rownumber)
            num_rows()
            fetch_row()
            errno()
            error()


        Known Bugs
        ----------
        
        
        Future Development
        ------------------
        
            1)     An export function that exports a result_set to a file and returns a filename
                to download.
            2)    An import function capable of loading a comma-separated file and/or an XML
                file and either create a table with it or update an existing one.
        
        
        ------------------------------------------------------------------------------------------
    */            

    //    CONSTANTS DEFINITIONS
    //    ------------------------------------------------------------------------------------------
        
define("DBDRIVER"1);
        
define("CNT_AUTOINCREMENT"1);
        
define("CNT_PRIMARYKEY"2);
        
define("CNT_UNIQUE"4);
        
define("CNT_NOTNULL"8);
        
define("CNT_INDEXED"16);
            


    class 
dbdriver {
    

    
//    PROPERTIES DECLARATION
    //    ------------------------------------------------------------------------------------------
        
var $server;
        var 
$link_id;
        var 
$user_name;
        var 
$user_pwd;
        var 
$current_db;
        var 
$current_table;
        var 
$result_id;



        
        
    
//    CONSTRUCTOR
    //    The constructor will initialize the properties and attempt to connect to the serer.
    //    Should it fail, $link_id will be Null
    //    ------------------------------------------------------------------------------------------
        
function dbdriver ($server_addr "localhost"$user ""$pwd "") {
            
$this->result_id 0;
            
$this->current_table "";
            
$this->current_db "";
            
$this->link_id 0;
            
$this->connect($server_addr$user$pwd);            
        }





    
//    CLASSPRINT
    //    The purpose of the Print method is to send to the client all the information about
    //    the object for testing and debugging purposes
    //    ------------------------------------------------------------------------------------------
        
function classprint() {
            echo(
"<b>printing information about dbdriver object</b><br><br>");
            echo(
"object properties:<br>");
            echo(
"Server: $this->server<br>");
            echo(
"link_id: {$this->link_id}<br>");
            echo(
"user_name: {$this->user_name}<br>");
            echo(
"user_pwd: {$this->user_pwd}<br>");
            echo(
"current_db: {$this->current_db}<br>");
            echo(
"current_table: {$this->current_table}<br>");
            echo(
"result_id: {$this->result_id}<br><br>");
            if (
$this->link_id) echo("driver is connected to server<br>");
            else echo(
"driver is not connected to server<br>");
        }

        
        
        
        
    
//    CONNECT
    //    The connect method connects to a server and returns the link_id upon successful
    //    connection.  Returns False on fail.
    //    ------------------------------------------------------------------------------------------
        
function connect ($server_addr$user$pwd) {
            
$this->server $server_addr;
            
$this->user_name $user;
            
$this->user_pwd $pwd;
            
$this->link_id mysql_connect($this->server$this->user_name$this->user_pwd);
            return 
$this->link_id;
        }





    
//    CLOSE
    //    The close method closes the server connection.  It always returns 1
    //    ------------------------------------------------------------------------------------------
        
function close() {
            if (
$this->link_id) {
                
$this->current_db "";
                
$this->current_table "";
                
$result_id 0;                
                
mysql_close($this->link_id);
            }
            return 
1;
        }


    
    
    
    
//    DB_CREATE
    //    Creates a database on current server
    //    ------------------------------------------------------------------------------------------
        
function db_create ($db_name) {
            
$result 0;
            if (
$this->link_id) {
                
$result mysql_query("CREATE DATABASE {$db_name}");
                if (
$result) return $this->db_select($db_name);
            }
            return 
$result;
        }





    
    
//    DB_SELECT
    //    Selects a database on the current server.  Returns False if DB cannot be found.
    //    ------------------------------------------------------------------------------------------
        
function db_select($database) {
            if (
$this->link_id) {
                if (
mysql_select_db($database$this->link_id)) {
                    
$this->current_db $database;
                    
$this->result_id 0;
                    return 
1;
                }
            }
            return 
0;
        }





    
//    DB_EXISTS
    //    Returns True if specified database exists on current server.  False if not.
    //    As for the other similar functions, this is useful to check on the version of a database
    //    in order to build auto-upgrading applications
    //    ------------------------------------------------------------------------------------------
        
function db_exists($database) {
            
$original_db $this->current_db;
            
$result 0;
            if (
$this->link_id) {
                
$result mysql_select_db($database$this->link_id);
                
$this->result_id 0;
                if (
$original_db != ""mysql_select_db($original_db$this->link_id);
            }
            return 
$result;
        }





    
//    DB_DROP
    //    Removes the current database from the server
    //    ------------------------------------------------------------------------------------------
        
function db_drop () {
            if ((
$this->link_id) && ($this->current_db != "")) {
                
$current_db "";
                
$result_id 0;
                return 
mysql_query("DROP DATABASE {$db_name}");
            }
            return 
0;
        }

    
    
    
    
    
//    TABLE_CREATE
    //    Creates an empty table in the current database and sets default table to newly created.
    //    Automatically creates a BIGINT PRIMARY KEY AUTO_INCREMENT field called 'ID' for ease.
    //    ------------------------------------------------------------------------------------------
        
function table_create($table) {
            if (
$this->link_id && ($this->current_db != "")) {
                
$result mysql_query("CREATE TABLE $table (ID BIGINT AUTO_INCREMENT
                                        PRIMARY KEY);"
$this->link_id);
                if (
$result) {
                    
$this->current_table $table;
                    return 
1;
                }
            }
            return 
0;
        }





    
//    TABLE_SELECT
    //    Selects a table as the current table in current DB.  Return False upon failure.
    //    ------------------------------------------------------------------------------------------
        
function table_select($table) {
            if ((
$this->link_id) && ($this->current_db != "") && ($this->table_exists($table))) {
                
$this->current_table $table;
                return 
1;
            }
            return 
0;
        }


    
    
    
    
//    TABLE_EXISTS
    //    Checks whether specified table exists in current database.
    //    ------------------------------------------------------------------------------------------
        
function table_exists($table) {
            
$found 0;
            if ((
$this->link_id) && ($this->current_db != "")) {
                
$result_id mysql_list_tables($this->current_db);
                while (
$record mysql_fetch_row($result_id)) {
                    if (
strtoupper($table) == strtoupper($record[0])) {
                        
$found 1;
                    }
                }
            }
            return 
$found;
        }





    
//    TABLE_DROP
    //    Drops the current table from the current database.  Returns False on failure.
    //    ------------------------------------------------------------------------------------------
        
function table_drop($table "") {
            if (
$this->link_id && ($this->current_db != "")) {
                if (
$table != ""$this->current_table $table;
                
$result mysql_query("DROP TABLE IF EXISTS $this->current_table");
                if (
$result$this->current_table "";
                return 
$result;
            } else return 
0;
        }

    
    
    
    
    
//    TABLE_RENAME
    //    Renames a table
    //    ------------------------------------------------------------------------------------------
        
function table_rename($table$new_name) {
            if (
$this->link_id && ($this->current_db != "")) {
                
$result mysql_query("ALTER TABLE $table RENAME TO $new_name");
                if (
$result$this->current_table $new_name;
                return 
$result;
            } else return 
0;
        }

    
    
    
    
    
//    FIELD_CREATE
    //    Creates a field in current table or any specified table.  constraints can be any
    //    sum of the CNT_ constants.  For instance, a UNIQUE and INDEXED field would be 
    //    sent a $constraints of CNT_UNIQUE+CNT_INDEXED.
    //    For fields which size is preset, use 0 for the $size parameter if you need to send
    //    any constraints.
    //    ------------------------------------------------------------------------------------------
        
function field_create($field_name$type$size 0$constraints 0$table_name "") {
            if (
$this->link_id && ($this->current_db != "") && (($this->current_table != "") ||
                (
$table_name != ""))) {
                if (
$table_name == ""$table_name $this->current_table;
                
                
$nullnotnull "NULL ";
                
$autoincrement " ";
                
$primarykey " ";
                
$unique " ";
                
$indexed " ";
                
$sql "";
                
                if (
$constraints >= 16) {
                    
$indexed "INDEX $field_name ";
                    
$constraints -= 16;
                }
                if (
$constraints >= 8) {
                    
$nullnotnull "NOT NULL ";
                    
$constraints -= 8;
                }
                if (
$constraints >= 4) {
                    
$unique "UNIQUE ";
                    
$constraints -= 4;
                }
                if (
$constraints >= 2) {
                    
$primarykey "PRIMARY KEY ";
                    
$constraints -= 2;
                }
                if (
$constraints == 1) {
                    
$autoincrement "AUTO_INCREMENT ";
                    
$constraints -= 1;
                }
                                                
                
$sql "ALTER TABLE $table_name ADD COLUMN $field_name $type ";
                if (
$size$sql $sql "(" $size ") ";
                
$sql $sql $nullnotnull $autoincrement $primarykey $unique;
                echo 
$sql "<br>";
                
$result mysql_query($sql$this->link_id);
                if (
$result) {
                    if (
$sql2 != ""mysql_query($sql2$this->link_id);
                    if (
$sql3 != ""mysql_query($sql3$this->link_id);
                }
                return 
$result;
            } else return 
0;
        }





    
//    FIELD_EXISTS
    //    Checks whether specified field exists in current or specified table.
    //    ------------------------------------------------------------------------------------------
        
function field_exists($field_name$table_name "") {
            
$found 0;
            
$t 0;
            if (
$this->link_id && ($this->current_db != "") && (($this->current_table != "") ||
               (
$table_name != ""))) {
                if (
$table_name != ""$this->current_table $table_name;
                
$result_id mysql_list_fields$this->current_db$this->current_table,
                                                
$this->link_id);
                for (
$t 0$t mysql_num_fields($result_id); $t++) {
                    if (
strtoupper($field_name) == 
                        
strtoupper(mysql_field_name($result_id$t))) {
                        
$found 1;
                    }    
                }
            }
            return 
$found;
        }





    
//    FIELD_DROP
    //    Drops the specified field from the current or specified table in current database
    //    ------------------------------------------------------------------------------------------
        
function field_drop($field_name$table_name "") {
            
$result 0;
            if (
$this->link_id && ($this->current_db != "") && (($this->current_table != "") ||
               (
$table_name != ""))) {
                if (
$table_name != ""$this->current_table $table_name;
                
$result mysql_query("ALTER TABLE $this->current_table 
                                       DROP INDEX 
$field_name"$this->link_id);
                
$result mysql_query("ALTER TABLE $this->current_table
                                       DROP COLUMN 
$field_name"$this->link_id);
            }
            return 
$result;
        }





    
//    QUERY
    //    Executes the specified query in current database
    //    ------------------------------------------------------------------------------------------
        
function query($sql) {
            
$this->return_id 0;
            if (
$this->link_id$this->result_id mysql_query($sql$this->link_id);
            return 
$this->result_id;
        }





    
//    DATA_SEEK
    //    Seeks a certain row within the current resultset.
    //    ------------------------------------------------------------------------------------------
        
function data_seek($rownumber) {
            if (
$this->result_id) return mysql_data_seek($this->result_id$rownumber);
            return 
0;
        }





    
//    NUM_ROWS
    //    Seeks a certain row within the current resultset.
    //    ------------------------------------------------------------------------------------------
        
function num_rows() {
            if (
$this->result_id) return mysql_num_rows($this->result_id);
            return 
0;
        }



    
    
    
//    FETCH_ROW
    //    Returns next row in current result set
    //    ------------------------------------------------------------------------------------------
        
function fetch_row() {
            if (
$this->result_id) return mysql_fetch_row($this->result_id);
            return 
0;
        }





    
//    ERROR
    //    Returns last MySQL error message
    //    ------------------------------------------------------------------------------------------
        
function error() { return mysql_error(); }
        
        
    
    
    
    
//    ERRNO
    //    Returns last MySQL error number
    //    ------------------------------------------------------------------------------------------
        
function errno() { return mysql_errno(); }
    
    }
?>


Usage Example


include ("dbdriver.cls.php");
set $db = new dbdriver("localhost", "manager", "xxczd");
$db->db_create("system");
$db->table_create("settings");


Rate This Script





Search



This Category All Categories