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


Messages In This Thread
Extend the Database Driver Class (in DB_driver) - by Kaosweaver - 07-17-2017, 01:15 PM



Theme © iAndrew 2016 - Forum software by © MyBB