Skip to content

Commit

Permalink
Extract a common interface for DBLayer classes
Browse files Browse the repository at this point in the history
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 9e32824
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 14 deletions.
10 changes: 5 additions & 5 deletions include/dblayer/common_db.php
Expand Up @@ -11,32 +11,32 @@
exit; exit;




// Load the appropriate DB layer class // Load and create the appropriate DB adapter (and open/connect to/select db)
switch ($db_type) switch ($db_type)
{ {
case 'mysql': case 'mysql':
case 'mysqli': case 'mysqli':
require_once PUN_ROOT.'include/dblayer/mysqli.php'; require_once PUN_ROOT.'include/dblayer/mysqli.php';
$db = new MysqlDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
break; break;


case 'mysql_innodb': case 'mysql_innodb':
case 'mysqli_innodb': case 'mysqli_innodb':
require_once PUN_ROOT.'include/dblayer/mysqli_innodb.php'; 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; break;


case 'pgsql': case 'pgsql':
require_once PUN_ROOT.'include/dblayer/pgsql.php'; require_once PUN_ROOT.'include/dblayer/pgsql.php';
$db = new PgsqlDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);
break; break;


case 'sqlite': case 'sqlite':
require_once PUN_ROOT.'include/dblayer/sqlite.php'; require_once PUN_ROOT.'include/dblayer/sqlite.php';
$db = new SqliteDBLayer($db_name, $db_prefix, $p_connect);
break; break;


default: default:
error('\''.$db_type.'\' is not a valid database type. Please check settings in config.php.', __FILE__, __LINE__); error('\''.$db_type.'\' is not a valid database type. Please check settings in config.php.', __FILE__, __LINE__);
break; 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);
70 changes: 70 additions & 0 deletions include/dblayer/interface.php
@@ -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);
}
5 changes: 4 additions & 1 deletion include/dblayer/mysqli.php
Expand Up @@ -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.'); 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 $prefix;
var $link_id; var $link_id;
Expand Down
5 changes: 4 additions & 1 deletion include/dblayer/mysqli_innodb.php
Expand Up @@ -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.'); 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 $prefix;
var $link_id; var $link_id;
Expand Down
5 changes: 4 additions & 1 deletion include/dblayer/pgsql.php
Expand Up @@ -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.'); 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 $prefix;
var $link_id; var $link_id;
Expand Down
7 changes: 5 additions & 2 deletions include/dblayer/sqlite.php
Expand Up @@ -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.'); 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 $prefix;
var $link_id; var $link_id;
Expand All @@ -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 // Prepend $db_name with the path to the forum root directory
$db_name = PUN_ROOT.$db_name; $db_name = PUN_ROOT.$db_name;
Expand Down
9 changes: 5 additions & 4 deletions install.php
Expand Up @@ -474,32 +474,33 @@ function process_form(the_form)
} }
else else
{ {
// Load the appropriate DB layer class // Load and create the appropriate DB adapter (and open/connect to/select db)
switch ($db_type) switch ($db_type)
{ {
case 'mysqli': case 'mysqli':
require PUN_ROOT.'include/dblayer/mysqli.php'; require PUN_ROOT.'include/dblayer/mysqli.php';
$db = new MysqlDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, false);
break; break;


case 'mysqli_innodb': case 'mysqli_innodb':
require PUN_ROOT.'include/dblayer/mysqli_innodb.php'; require PUN_ROOT.'include/dblayer/mysqli_innodb.php';
$db = new MysqlInnodbDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, false);
break; break;


case 'pgsql': case 'pgsql':
require PUN_ROOT.'include/dblayer/pgsql.php'; require PUN_ROOT.'include/dblayer/pgsql.php';
$db = new PgsqlDBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, false);
break; break;


case 'sqlite': case 'sqlite':
require PUN_ROOT.'include/dblayer/sqlite.php'; require PUN_ROOT.'include/dblayer/sqlite.php';
$db = new SqliteDBLayer($db_name, $db_prefix, false);
break; break;


default: default:
error(sprintf($lang_install['DB type not valid'], $db_type)); 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 // Validate prefix
if (strlen($db_prefix) > 0 && (!preg_match('%^[a-zA-Z_][a-zA-Z0-9_]*$%', $db_prefix) || strlen($db_prefix) > 40)) 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)); error(sprintf($lang_install['Table prefix error'], $db->prefix));
Expand Down

0 comments on commit 9e32824

Please sign in to comment.