WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server Click here to Update Your Picture
Ben Yacoub Hatem
Date : Jan 08th 2005
Grade : 3 of 5 (graded 8 times)
Viewed : 21397
File : 4057.php
Images : No Images for this code example.
Search : More code by Ben Yacoub Hatem
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This is a simple class that help you to convert SQL from Oracle, MySQL, MSSQL, SQLite and ODBC to SQL compatible and avoid compatibilites problem between specific databases.


DBConv.php
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author: Hatem Ben Yacoub <info@phptunisie.net>                       |
// +----------------------------------------------------------------------+
//
// $Id: DBConv.php,v 1.0.0 Sun Dec 05 2004 05:23:24 CET by hatem EXP $

/**
* DBConv class.  Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible.
*
* @author  Ben Yacoub Hatem <info@phptunisie.net>
* @todo   
*          - testing
*          - documentations
*          - modules
*
*/
class DBConv
{
   
// {{{ properties

    /**
     * @var  string  SQL data to convert
     */
   
var $_data;
   
   
/**
     * @var  string  Converted SQL data.
     */
   
var $_newdata;
   

   
/**
     * @var  string  SQL data type : Oracle,MySQL,MsSQL,SQLite,ODBC ...
     */
   
var $_type;
   

   
/**
     * @var  array  Replacments for each supported database.
     */
   
var $_subst = array();
   
   
// }}}
    // {{{ constructor
   
    /**
     * DBConv constructor
     *
     * @param mixed $type        Data source type
     *
     * @param mixed $source       Source data
     *
     * @access protected
     */
   
function DBConv($type, $source )
    {
       
$this->init();
        if (!
array_key_exists($type,$this->_subst)) {
            return
false;
        }
       
$this->_type = $type;
       
       
clearstatcache();
        if (
is_file($source) or ereg("^http://",$source)) {
           
           
$this->_data = implode('',@file($source));
           
        } elseif(
trim($source)!="") {
           
$this->_data = $source;
        }
    }
   
   
// }}}
    // {{{ init()
   
    /**
     * Initialize the substitution array
     *
     * @access protected
     */
   
function init(){
         
$this->_subst['mysql'] = array("int"=>"INTEGER","dec"=>"DECIMAL");
         
         
$this->_subst['oracle'] = array(
                 
"BFILE"=>"BLOB",
                 
"CLOB"=>"TEXT",
                 
"DATE"=>"DATETIME",
                 
"LONG"=>"BIGINT",
                 
"LONG RAW"=>"BIGINT",
                 
"NCHAR"=>"CHAR",
                 
"NCLOB"=>"TEXT",
                 
"NUMBER"=>"DECIMAL",
                 
"NVARCHAR2"=>"VARCHAR",
                 
"RAW"=>"BLOB",
                 
"VARCHAR2"=>"VARCHAR",
                 
"DOUBLE PRECISION"=>"DECIMAL",
                 
"\r\nLOGGING"=>"",
                 
"\r\nNOCACHE"=>"",
                 
"\r\nNOPARALLEL"=>"",
                 
" BYTE"=>""
               
);
       
$this->_subst['odbc'] = array("LONGCHAR"=>"BLOB");
       
       
$this->_subst['sqlite'] = array("INTEGER(10)"=>"INTEGER");
       
       
$this->_subst['mssql'] = array(
               
"nvarchar"=>"VARCHAR",
               
"nchar"=>"CHAR",
               
"int identity"=>"INTEGER",
               
"ntext"=>"TEXT",
               
"int"=>"INTEGER",
               
"image"=>"LONGBLOB",
               
"money"=>"FLOAT"
               
);
        return;
    }
   
   
// }}}
    // {{{ convert()
   
    /**
     * Convert data to compatible SQL
     *
     * @param mixed $save_to       File name to save conversion results.
     *
     * @access public
     */
   
function convert($save_to = ''){
       
       
       
$replacements = $this->_subst[$this->_type];           
       
$rep = array_keys($replacements);
       
$with = array_values($replacements);
                         
       
$this->_newdata = str_replace($rep,$with, $this->_data);
       

       
        if (
trim($save_to)!='') {
           
           
// Try to create file if not exist
           
if (!is_file($save_to)) {
                @
touch($save_to);
            }
           
           
// Try to save results
           
if (is_writable($save_to)) {
               
$handle = fopen($save_to, 'a');
               
fwrite($handle, $this->_newdata);
               
fclose($handle);
            } else {
                Echo
"Error : $save_to isn't writeable.<br>\n";
            }
        }
       
        return
$this->_newdata;
    }
   
// }}}   
}
?>




Usage Example
<?php
require_once "DBConv.php";
$conv = new DBConv("oracle","oracledb.sql");
$newsql = $conv->convert();
echo
$newsql;
?>



[PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more.
Categories : PHP, PHP Classes, Databases, MS SQL Server, MySQL
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
Recordset Class for MSSQL database
Categories : PHP Classes, Databases, PHP, MS SQL Server
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
 Abdoulaye Siby wrote :1260
Great example. Very interesting.