Welcome Guest, Not a member yet? Register   Sign In
Extend the Database Driver Class (in DB_driver)
#1

(This post was last modified: 07-17-2017, 01:17 PM by Kaosweaver. Edit Reason: copy-paste error typo fixed )

I need replication to be automatic in the framework (someone does a write function, we need it to automatically select the master table and perform the query with a set comment in union with the PHP plug in we're using to accomplish the correct database to query from)

I've modified the DB_driver.php file in CI 3.1.5, however, I'd like to really do this outside of the core file.

Quick notes on what we did:

The config database.php file has two lines in it in the $db['default'] array
PHP Code:
'replication' => TRUE,
 
'replication_text' => '/*ms=master*/' 

The DB_driver.php file has an added function (swipped from CI4's DB files)
PHP Code:
//--------------------------------------------------------------------

 /**
 * Determines if the statement is a write-type query or not.
 *
 * @return bool
 */
 
public function isWriteType($sql): bool
 
{
     return (bool)
preg_match(
     
'/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s/i',
 
$sql);
 } 

And then I modified the simple_query function like so:
PHP Code:
public function simple_query($sql)
    {
        if ( ! 
$this->conn_id)
        {
            if ( ! 
$this->initialize())
            {
                return 
FALSE;
            }
        }
        if (
$this->replication) {
            if (
$this->isWriteType($sql)) {
                
$sql $this->replication_text."
                "
.$sql;                
            }
        }
        return 
$this->_execute($sql);
    } 

I added a MY_DB_drive.php file in the core folder hoping it would work (pulling out the above changes and making it in to an extended class) like so:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
MY_DB_drive extends DB_driver {
    
/**
     * Constructor
     */
    
public function __construct()
    {
        
parent::__construct();
    }
    
    public function 
simple_query($sql)
    {
        if ( ! 
$this->conn_id)
        {
            if ( ! 
$this->initialize())
            {
                return 
FALSE;
            }
        }
        if (
$this->replication) {
            if (
$this->isWriteType($sql)) {
                
$sql $this->replication_text."
                "
.$sql;                
            }
        }
        return 
$this->_execute($sql);
    }
    
    
//--------------------------------------------------------------------

    /**
     * Determines if the statement is a write-type query or not.
     *
     * @return bool
     */
    
public function isWriteType($sql): bool
    
{
        return (bool)
preg_match(
            
'/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s/i',
            
$sql);
    }


But that didn't work.  Any pointers on getting what I'd like done without editing the core files?
Reply
#2

As far as I know, this has always been too difficult. I've been using CI since 2009, and there's never been an easy way to extend the database classes. You can't just extend the DB_driver class like you expect. The database classes are special, and it's because of the way drivers are applied. Search around the forums and you will eventually find some people doing it, but it's almost as hacky as just hacking system files. I just hack the files, and make really good notes about what I did in a file called CI_UPGRADE_WARNING.txt.
Reply
#3

(This post was last modified: 07-20-2017, 05:45 AM by spjonez.)

You can overload the core loader to allow you to extend DB drivers the usual way. Code is available here: https://forum.codeigniter.com/thread-685...#pid345512
Reply




Theme © iAndrew 2016 - Forum software by © MyBB