手册 >> 数据模型 >> 多数据库/数据表

多数据库/数据表

从SpeedPHP第三版开始,支持多种类型的数据库类型,并且通过对数据库句柄的操作,可以在多个数据库/数据表直接进行切换,也可以做到一定程度的“主从数据库读写分离”。

多数据库切换

主要是通过对spModel子类的_db成员变量进行切换操作。_db变量是数据库驱动类的实例,我们可以先直接实例化多个数据库类,然后再对_db变量进行赋值,就可以达到切换数据库的目的。

// MSSQL驱动实例
$dsn_mssql = spClass('db_mssql',array( 'MSSQL的配置'), SP_PATH.'/Drivers/mssql.php', TRUE);
// MYSQL驱动实例
$dsn_mysql = spClass('db_mysql',array( 'MYSQL的配置'), SP_PATH.'/Drivers/mysql.php', TRUE); // MYSQL

// 实例化
$g = spClass('m_guestbook');
// 切换到MSSQL
$g->_db = $dsn_mssql;
/*****
* 对MSSQL的操作
****/

// 切换回MYSQL
$g->_db = $dsn_mysql; 
/*****
* MySQL的操作
****/

本文所述的“库配置”,指的是spConfig配置中“db”节点的配置(数组)。

	库配置 = array(  // 数据库连接配置
		'driver' => 'mysql',   // 驱动类型
		'host' => 'localhost', // 数据库地址
		'port' => 3306,        // 端口
		'login' => 'root',     // 用户名
		'password' => '',      // 密码
		'database' => '',      // 库名称
		'prefix' => '',           // 表前缀
		'persistent' => FALSE,    // 是否使用长链接
	);

主从数据库读写分离

主从库读写分离主要是通过数据库中间层实现的,当然通过SpeedPHP框架,我们也可以在一定程度上做到读写分库。

<?php
class spModelNew extends spModel {
	var $_db_write = null;
	var $_db_read = null;
	
	var $db_master = array(
		'driver' => 'mysqli',   // 驱动类型
		'host' => 'localhost', // 数据库地址
		'port' => 3306,        // 端口
		'login' => 'root',     // 用户名
		'password' => '',      // 密码
		'database' => 'dbmaster',      // 库名称
	);
	
	var $db_slave = array(
		'driver' => 'mysqli',   // 驱动类型
		'host' => 'localhost', // 数据库地址
		'port' => 3306,        // 端口
		'login' => 'root',     // 用户名
		'password' => '',      // 密码
		'database' => 'dbslave',      // 库名称
	);
	
	public function __construct(){
		if( null == $this->tbl_name )$this->tbl_name = $GLOBALS['G_SP']['db']['prefix'] . $this->table;
		// 主库(写入库)实例化驱动
		$this->_db_write = spClass('db_mysqli', array($this->db_master), SP_PATH.'/Drivers/mysqli.php', true);
		// 从库(读取库)实例化驱动
		$this->_db_read = spClass('db_mysqli', array($this->db_slave), SP_PATH.'/Drivers/mysqli.php',true);
	}


	public function find($conditions = null, $sort = null, $fields = null){
		$this->_db = $this->_db_read;
		return parent::find($conditions, $sort, $fields);
	}
	public function findAll($conditions = null, $sort = null, $fields = null, $limit = null){
		$this->_db = $this->_db_read;
		return parent::findAll($conditions, $sort, $fields, $limit);
	}
	public function findBy($field, $value){
		$this->_db = $this->_db_read;
		return parent::findBy($field, $value);
	}
	public function findSql($sql){
		$this->_db = $this->_db_read;
		return parent::findSql($sql);
	}
	public function findCount($conditions = null){
		$this->_db = $this->_db_read;
		return parent::findCount($conditions);
	}


	public function create($row){
		$this->_db = $this->_db_write;
		return parent::create($row);
	}
	public function createAll($rows){
		$this->_db = $this->_db_write;
		return parent::createAll($row);
	}
	public function delete($conditions){
		$this->_db = $this->_db_write;
		return parent::delete($conditions);
	}
	public function updateField($conditions, $field, $value){
		$this->_db = $this->_db_write;
		return parent::updateField($conditions, $field, $value);
	}
	public function runSql($sql){
		$this->_db = $this->_db_write;
		return parent::runSql($sql);
	}
	public function affectedRows(){
		$this->_db = $this->_db_write;
		return parent::affectedRows();
	}
	public function update($conditions, $row){
		$this->_db = $this->_db_write;
		return parent::update($conditions, $row);
	}
	public function replace($conditions, $row){
		$this->_db = $this->_db_write;
		return parent::replace($conditions, $row);
	}
	public function incrField($conditions, $field, $optval = 1){
		$this->_db = $this->_db_write;
		return parent::incrField($conditions, $field, $optval);
	}
	public function decrField($conditions, $field, $optval = 1){
		$this->_db = $this->_db_write;
		return parent::decrField($conditions, $field, $optval);
	}
	public function deleteByPk($pk){
		$this->_db = $this->_db_write;
		return parent::deleteByPk($pk);
	}
}

从上面的类,我们继承了spModel的大部分操作,并且区分开“读”与“写”的操作,然后在构造函数中,我们分别实例化了“读库”和“写库”的数据库驱动。最后我们在每个“读操作”中,将“读库”的实例赋值给spModel的_db变量,那么这些“读操作”就都会去读取“读库”的数据;而每个“写操作”也通过将“写库”的实例赋值给_db后,“写操作”就会对“写库”进行操作了。

在应用程序中,其他的数据库模型类都应该继承于spModelNew(而不是spModel),这样就可以无缝地使用“读写分库”的功能了。