Permalink
Browse files

Extract a common interface for DBLayer classes

Now, the constructors of the various adapter implementations do not
necessarily need to have the same signature anymore.
  • Loading branch information...
franzliedke committed Jan 5, 2019
1 parent 4c220e3 commit 9e32824488304d8f63f1e8a4c680a76cd455e490
@@ -11,32 +11,32 @@
exit;
// Load the appropriate DB layer class
// Load and create the appropriate DB adapter (and open/connect to/select db)
switch ($db_type)
{
case 'mysql':
case 'mysqli':
require_once PUN_ROOT.'include/dblayer/mysqli.php';
$db = new MysqlDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
break;
case 'mysql_innodb':
case 'mysqli_innodb':
require_once PUN_ROOT.'include/dblayer/mysqli_innodb.php';
$db = new MysqlInnodbDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
break;
case 'pgsql':
require_once PUN_ROOT.'include/dblayer/pgsql.php';
$db = new PgsqlDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
break;
case 'sqlite':
require_once PUN_ROOT.'include/dblayer/sqlite.php';
$db = new SqliteDBLayer($db_name, $db_prefix, $p_connect);
break;
default:
error('\''.$db_type.'\' is not a valid database type. Please check settings in config.php.', __FILE__, __LINE__);
break;
}
// Create the database adapter object (and open/connect to/select db)
$db = new DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
@@ -0,0 +1,70 @@
<?php
/**
* Copyright (C) 2008-2012 FluxBB
* based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
interface DBLayer
{
public function start_transaction();
public function end_transaction();
public function query($sql, $unbuffered = false);
public function result($query_id = 0, $row = 0, $col = 0);
public function fetch_assoc($query_id = 0);
public function fetch_row($query_id = 0);
public function has_rows($query_id);
public function affected_rows();
public function insert_id();
public function get_num_queries();
public function get_saved_queries();
public function free_result($query_id = false);
public function escape($str);
public function error();
public function close();
public function get_names();
public function set_names($names);
public function get_version();
public function table_exists($table_name, $no_prefix = false);
public function field_exists($table_name, $field_name, $no_prefix = false);
public function index_exists($table_name, $index_name, $no_prefix = false);
public function create_table($table_name, $schema, $no_prefix = false);
public function drop_table($table_name, $no_prefix = false);
public function rename_table($old_table, $new_table, $no_prefix = false);
public function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false);
public function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false);
public function drop_field($table_name, $field_name, $no_prefix = false);
public function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false);
public function drop_index($table_name, $index_name, $no_prefix = false);
public function truncate_table($table_name, $no_prefix = false);
}
@@ -11,7 +11,10 @@
exit('This PHP environment doesn\'t have Improved MySQL (mysqli) support built in. Improved MySQL support is required if you want to use a MySQL 4.1 (or later) database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
require_once PUN_ROOT.'include/dblayer/interface.php';
class MysqlDBLayer implements DBLayer
{
var $prefix;
var $link_id;
@@ -11,7 +11,10 @@
exit('This PHP environment doesn\'t have Improved MySQL (mysqli) support built in. Improved MySQL support is required if you want to use a MySQL 4.1 (or later) database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
require_once PUN_ROOT.'include/dblayer/interface.php';
class MysqlInnodbDBLayer implements DBLayer
{
var $prefix;
var $link_id;
@@ -11,7 +11,10 @@
exit('This PHP environment doesn\'t have PostgreSQL support built in. PostgreSQL support is required if you want to use a PostgreSQL database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
require_once PUN_ROOT.'include/dblayer/interface.php';
class PgsqlDBLayer implements DBLayer
{
var $prefix;
var $link_id;
@@ -11,7 +11,10 @@
exit('This PHP environment doesn\'t have SQLite support built in. SQLite support is required if you want to use a SQLite database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
require_once PUN_ROOT.'include/dblayer/interface.php';
class SqliteDBLayer implements DBLayer
{
var $prefix;
var $link_id;
@@ -31,7 +34,7 @@ class DBLayer
);
function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
function __construct($db_name, $db_prefix, $p_connect)
{
// Prepend $db_name with the path to the forum root directory
$db_name = PUN_ROOT.$db_name;
@@ -474,32 +474,33 @@ function process_form(the_form)
}
else
{
// Load the appropriate DB layer class
// Load and create the appropriate DB adapter (and open/connect to/select db)
switch ($db_type)
{
case 'mysqli':
require PUN_ROOT.'include/dblayer/mysqli.php';
$db = new MysqlDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, false);
break;
case 'mysqli_innodb':
require PUN_ROOT.'include/dblayer/mysqli_innodb.php';
$db = new MysqlInnodbDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, false);
break;
case 'pgsql':
require PUN_ROOT.'include/dblayer/pgsql.php';
$db = new PgsqlDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, false);
break;
case 'sqlite':
require PUN_ROOT.'include/dblayer/sqlite.php';
$db = new SqliteDBLayer($db_name, $db_prefix, false);
break;
default:
error(sprintf($lang_install['DB type not valid'], $db_type));
}
// Create the database object (and connect/select db)
$db = new DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, false);
// Validate prefix
if (strlen($db_prefix) > 0 && (!preg_match('%^[a-zA-Z_][a-zA-Z0-9_]*$%', $db_prefix) || strlen($db_prefix) > 40))
error(sprintf($lang_install['Table prefix error'], $db->prefix));

0 comments on commit 9e32824

Please sign in to comment.