Passed
Branch development (7a3bc2)
by Elk
06:00
created

ElkTestingMysql   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 4
1
<?php
2
3
/**
4
 * Handles the mysql / mariadb db actions
5
 * Called by setup-database.sh as part of the install
6
 *
7
 * @package   ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
10
 *
11
 * @version 2.0 dev
12
 *
13
 */
14
15
use ElkArte\Database\Mysqli\Connection;
16
use ElkArte\Database\Mysqli\Table;
17
18
/**
19
 * Sets up a Database_MySQL object
20
 */
21
class DbTable_MySQL_Install extends Table
22
{
23
	public static $_tbl_inst = null;
24
25
	/**
26
	 * DbTable_MySQL::construct
27
	 *
28
	 * @param object $db - A Database_MySQL object
29
	 */
30
	public function __construct($db, $db_prefix)
31
	{
32
		global $db_prefix;
33
34
		// We are installing, of course we want to do any remove on these
35
		$this->_reservedTables = array();
36
37
		foreach ($this->_reservedTables as $k => $table_name)
38
		{
39
			$this->_reservedTables[$k] = strtolower($db_prefix . $table_name);
40
		}
41
42
		// let's be sure.
43
		$this->_package_log = array();
44
45
		// This executes queries and things
46
		$this->_db = $db;
47
		$this->_db_prefix = $db_prefix;
48
	}
49
50
	/**
51
	 * Static method that allows to retrieve or create an instance of this class.
52
`	 *
53
	 * @param object $db - A Database_MySQL object
54
	 *
55
	 * @return object - A DbTable_MySQL object
56
	 */
57
	public static function db_table($db, $db_prefix)
58
	{
59
		if (is_null(self::$_tbl_inst))
60
		{
61
			self::$_tbl_inst = new DbTable_MySQL_Install($db, $db_prefix);
62
		}
63
64
		return self::$_tbl_inst;
65
	}
66
}
67
68
class DbTable_MySQLi_Install extends DbTable_MySQL_Install
69
{
70
}
71
72
/**
73
 * Extend ElkTestingSetup with MySql values
74
 *
75
 * return int 0|1
76
 */
77
class ElkTestingMysql extends ElkTestingSetup
78
{
79
	public function init()
80
	{
81
		global $db_name, $db_prefix, $db_type, $boardurl, $db_server, $db_user, $db_passwd;
82
83
		$boardurl = $this->_boardurl = 'http://127.0.0.1';
84
		$db_server = $this->_db_server = '127.0.0.1';
85
		$db_type = $this->_db_type = 'mysql';
86
		$db_name = $this->_db_name = 'elkarte_test';
87
		$db_user = $this->_db_user = 'root';
88
		$db_passwd = $this->_db_passwd = '';
89
		$db_prefix = $this->_db_prefix = 'elkarte_';
90
91
		$link = mysqli_connect($this->_db_server, $this->_db_user, $this->_db_passwd);
92
		if (!$link)
93
		{
94
			echo 'Could not connect: ' . mysqli_error($link);
95
			return 1;
96
		}
97
98
		printf("MySQL server version: %s\n", mysqli_get_server_info($link));
99
100
		try
101
		{
102
			// Start the database interface
103
			$this->_db = Connection::initiate($this->_db_server, $this->_db_name, $this->_db_user, $this->_db_passwd, $this->_db_prefix, ['select_db' => true]);
104
			$this->_db_table = DbTable_MySQL_Install::db_table($this->_db, $this->_db_prefix);
105
		}
106
		catch (Exception $e)
107
		{
108
			echo $e->getMessage();
109
			return 1;
110
		}
111
112
		// Load the mysql install queries
113
		$this->load_queries(BOARDDIR . '/install/install_' . DB_SCRIPT_VERSION . '.php');
114
115
		$result = $this->run_queries();
116
117
		if (empty($result))
118
			return 1;
119
120
		// Prepare Settings.php, add a member, set time
121
		$this->prepare();
122
		return 0;
123
	}
124
}
125