Completed
Pull Request — patch_1-1-4 (#3202)
by Spuds
15:49
created

DbSearch_PostgreSQL   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 183
Duplicated Lines 4.37 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 8
loc 183
ccs 0
cts 123
cp 0
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A search_support() 0 6 1
B membersTableInfo() 8 56 7
A create_word_search() 0 18 1
A search_query() 0 56 4
A db_search() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This class handles database search. (PostgreSQL)
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * @version 1.1
11
 *
12
 */
13
14
/**
15
 * PostgreSQL implementation of DbSearch
16
 */
17
class DbSearch_PostgreSQL implements DbSearch
18
{
19
	/**
20
	 * This instance of the search
21
	 * @var DbSearch_PostgreSQL
22
	 */
23
	private static $_search = null;
24
25
	/**
26
	 * This function will tell you whether this database type supports this search type.
27
	 *
28
	 * @param string $search_type
29
	 */
30
	public function search_support($search_type)
31
	{
32
		$supported_types = array('custom');
33
34
		return in_array($search_type, $supported_types);
35
	}
36
37
	/**
38
	 * Compute and execute the correct query for search.
39
	 * Returns the result of executing the query.
40
	 *
41
	 * @param string $identifier
42
	 * @param string $db_string
43
	 * @param mixed[] $db_values default array()
44
	 * @param resource|null $connection
45
	 */
46
	public function search_query($identifier, $db_string, $db_values = array(), $connection = null)
47
	{
48
		$db = database();
49
50
		$replacements = array(
51
			'create_tmp_log_search_topics' => array(
52
				'~mediumint\(\d\)~i' => 'int',
53
				'~unsigned~i' => '',
54
				'~ENGINE=MEMORY~i' => '',
55
			),
56
			'create_tmp_log_search_messages' => array(
57
				'~mediumint\(\d\)~i' => 'int',
58
				'~unsigned~i' => '',
59
				'~ENGINE=MEMORY~i' => '',
60
			),
61
			'drop_tmp_log_search_topics' => array(
62
				'~IF\sEXISTS~i' => '',
63
			),
64
			'drop_tmp_log_search_messages' => array(
65
				'~IF\sEXISTS~i' => '',
66
			),
67
			'insert_into_log_messages_fulltext' => array(
68
				'~LIKE~i' => 'iLIKE',
69
				'~NOT\sLIKE~i' => '~NOT iLIKE',
70
				'~NOT\sRLIKE~i' => '!~*',
71
				'~RLIKE~i' => '~*',
72
			),
73
			'insert_log_search_results_subject' => array(
74
				'~LIKE~i' => 'iLIKE',
75
				'~NOT\sLIKE~i' => 'NOT iLIKE',
76
				'~NOT\sRLIKE~i' => '!~*',
77
				'~RLIKE~i' => '~*',
78
			),
79
		);
80
81
		if (isset($replacements[$identifier]))
82
			$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string);
83
		elseif (preg_match('~^\s*INSERT\sIGNORE~i', $db_string) != 0)
84
		{
85
			$db_string = preg_replace('~^\s*INSERT\sIGNORE~i', 'INSERT', $db_string);
86
			// Don't error on multi-insert.
87
			$db->skip_next_error();
88
		}
89
90
		// @deprecated since 1.1 - temporary measure until a proper skip_error is implemented
91
		if (!empty($db_values['db_error_skip']))
92
		{
93
			$db->skip_next_error();
94
		}
95
96
		$return = $db->query('', $db_string,
97
			$db_values, $connection
98
		);
99
100
		return $return;
101
	}
102
103
	/**
104
	 * Returns some basic info about the {db_prefix}messages table
105
	 * Used in ManageSearch.controller.php in the page to select the index method
106
	 */
107
	public function membersTableInfo()
108
	{
109
		global $db_prefix, $txt;
110
111
		$db = database();
112
		$db_table = db_table();
113
114
		$table_info = array();
115
116
		// In order to report the sizes correctly we need to perform vacuum (optimize) on the tables we will be using.
117
		$db_table->optimize('{db_prefix}messages');
118
		if ($db_table->table_exists('{db_prefix}log_search_words'))
119
			$db_table->optimize('{db_prefix}log_search_words');
120
121
		// PostGreSql has some hidden sizes.
122
		$request = $db->query('', '
123
			SELECT relname, relpages * 8 *1024 AS "KB" FROM pg_class
124
			WHERE relname = {string:messages} OR relname = {string:log_search_words}
125
			ORDER BY relpages DESC',
126
			array(
127
				'messages' => $db_prefix . 'messages',
128
				'log_search_words' => $db_prefix . 'log_search_words',
129
			)
130
		);
131
132
		if ($request !== false && $db->num_rows($request) > 0)
133
		{
134
			while ($row = $db->fetch_assoc($request))
135
			{
136
				if ($row['relname'] == $db_prefix . 'messages')
137
				{
138
					$table_info['data_length'] = (int) $row['KB'];
139
					$table_info['index_length'] = (int) $row['KB'];
140
141
					// Doesn't support fulltext
142
					$table_info['fulltext_length'] = $txt['not_applicable'];
143
				}
144
				elseif ($row['relname'] == $db_prefix . 'log_search_words')
145
				{
146
					$table_info['index_length'] = (int) $row['KB'];
147
					$table_info['custom_index_length'] = (int) $row['KB'];
148
				}
149
			}
150
			$db->free_result($request);
151
		}
152 View Code Duplication
		else
153
			// Didn't work for some reason...
154
			$table_info = array(
155
				'data_length' => $txt['not_applicable'],
156
				'index_length' => $txt['not_applicable'],
157
				'fulltext_length' => $txt['not_applicable'],
158
				'custom_index_length' => $txt['not_applicable'],
159
			);
160
161
		return $table_info;
162
	}
163
164
	/**
165
	 * Make a custom word index.
166
	 *
167
	 * @param string $size
168
	 */
169
	public function create_word_search($size)
170
	{
171
		$db = database();
172
173
		$size = 'int';
174
175
		$db->query('', '
176
			CREATE TABLE {db_prefix}log_search_words (
177
				id_word {raw:size} NOT NULL default {string:string_zero},
178
				id_msg int NOT NULL default {string:string_zero},
179
				PRIMARY KEY (id_word, id_msg)
180
			)',
181
			array(
182
				'size' => $size,
183
				'string_zero' => '0',
184
			)
185
		);
186
	}
187
188
	/**
189
	 * Static method that allows to retrieve or create an instance of this class.
190
	 */
191
	public static function db_search()
192
	{
193
		if (is_null(self::$_search))
194
		{
195
			self::$_search = new self();
196
		}
197
		return self::$_search;
198
	}
199
}