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

Elk_Testing_psql   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

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