在前一节,已经测试了使用原始的PDO方式是可以访问达梦数据库。在这一节我们将尝试为Joomla写达梦的数据库接口类。测试使用Joomla提供的数据库API来访问达梦的数据库。
理论基础
Joomla对数据库层做了很好的抽象,为其进行移植预留了充分的可能性。
Joomla的底层数据库接口和实现在 网站的根目录 libraries\joomla\database 这个目录下面。最主要的文件有两个:driver.php和query.php。
- 其中driver.php文件中定义了各种数据库驱动层面的接口,如果需要让Joomla支持一种新的数据库驱动,只需要将新的数据库驱动继承JDatabaseDriver 这类,然后针对数据库的特有方法实现对应的接口方法即可。
- query.php文件中定义了数据库提供的一些基础的数据库操作方法。每一个数据库驱动都会自动加载一个与其同名的数据库查询类型。
在driver目录下,系统已经自带了几个常用的数据库驱动类的实现。如图:
因此,需要做的就比较明确了。只需要参考pdomysql.php这个文件,定义我们自己的数据库类即可。
准备文件架构
1,定义驱动类的名称
将达梦的数据库驱动类定义为pdodm.
2,实现达梦数据库驱动类
在libraries\joomla\database\driver目录下复制一份pdomysql.php文件,将其命名为pdodm.php文件.
编辑这个文件,将其类名修改为JDatabaseDriverPdokdb,且让其继承自JDatabaseDriverPdo。代码如下:
class JDatabaseDriverPdodm extends JDatabaseDriverPdo { /** * The name of the database driver. * * @var string * @since 3.4 */ public $name = 'pdodm';
这样我们就有一个达梦的数据库驱动实现类了。
3,实现达梦数据库查询类
在libraries\joomla\database\query目录下复制一份pdomysql.php文件,将其命名为pdodm.php文件.
编辑这个文件,将其类名修改为JDatabaseQueryPdodm.且让其继承自JDatabaseQueryPdomysql 代码如下:
class JDatabaseQueryPdodm extends JDatabaseQueryPdomysql {
这样我们就有一个达梦的数据库查询类了。
这样新增数据库驱动的文件结构就准备好了。
实现具体的达梦数据库链接
因为我们继承了 JDatabaseDriverPdo 这个类,因此,必须实现这个类中的定义的虚方法
public function __construct($options) public function connect() public function dropTable($tableName, $ifExists = true) public function select($database) public function getCollation() public function getConnectionCollation() public function getTableCreate($tables) public function getTableColumns($table, $typeOnly = true) public function getTableKeys($table) public function getTableList() public function lockTable($table) public function renameTable($oldTable, $newTable, $backup = null, $prefix = null) public function unlockTables()
最主要的一个方法是实现connect方法。代码如下:
/** * Connects to the database if needed. * * @return void Returns void if the database connected successfully. * * @since 3.0.0 * @throws RuntimeException */ public function connect() { if ($this->connection) { return; } // Make sure the PDO extension for PHP is installed and enabled. if (!self::isSupported()) { throw new JDatabaseExceptionUnsupported('PDO Extension is not available.', 1); } $replace = array(); $with = array(); // Find the correct PDO DSN Format to use: switch ($this->options['driver']) { case 'dm': $this->options['port'] = (isset($this->options['port'])) ? $this->options['port'] : 5236; $format = 'dm:host=#HOST#;port=#PORT#;dbname=#DBNAME#'; $replace = array('#HOST#', '#PORT#', '#DBNAME#'); $with = array($this->options['host'], $this->options['port'], $this->options['database']); break; } // Create the connection string: $connectionString = str_replace($replace, $with, $format); try { $this->connection = new PDO( $connectionString, $this->options['user'], $this->options['password'], $this->options['driverOptions'] ); } catch (PDOException $e) { throw new JDatabaseExceptionConnecting('Could not connect to PDO: ' . $e->getMessage(), 2, $e); } }
核心的一个思想就是按照数据库的特定要求来实现对应的方法,如果指定的方法在本数据库驱动中不被支持,支持方法false或者$this即可。
下面是一个初步的代码模板:
达梦数据库驱动类:pdodm.php
达梦数据库查询类:pdodm.php
特别注意,上面的代码只是一个文件的架构,具体的适配并没有全部完成,有兴趣的朋友可以在此基础上进行完成。
测试新的数据库驱动类
上面已经定义了数据库的驱动类型,下面就开始使用Joomla的方法来使用心得驱动类来访问数据库。代码如下:
try { $option = array(); //prevent problems $option['driver'] = 'pdodm'; // Database driver name $option['host'] = 'localhost'; // Database host name $option['user'] = 'SYSDBA'; // User for database authentication $option['password'] = 'Zmax99.com'; // Password for database authentication $option['database'] = 'SYSDBA'; // Database name $option['prefix'] = 'a3bav_'; // Database prefix (may be empty) //使用标准的Joomla数据库接口来查询数据 $db = JDatabaseDriver::getInstance( $option ); $query = $db->getQuery(true); $query->select("*")->from("STUDENT"); $db->setQuery($query); $items = $db->loadObjectList(); echo "<pre>"; print_r($items); echo "</pre>"; }catch(Exception $e){ print "Error: " . $e->getMessage()."<br/>"; }
执行结果正常,证明我们写的数据库驱动和数据库查询类成功。
做完这一步并不代表数据库的适配完成了,它只能说明已经具备了初步适配的可能性。接下来,就是迁移数据,进行SQL语法的调整适配。剩下的工作才是难点。
评论 (0)