1
|
|
|
<?php |
|
|
|
|
2
|
|
|
jRequire("../JException/JException.php"); |
3
|
|
|
jRequire("../JConfig/JConfig.php"); |
4
|
|
|
jRequire("../Connection/Connection.php"); |
5
|
|
|
trait Query { |
|
|
|
|
6
|
|
|
public $connection; |
7
|
|
|
public $currentConnection; |
8
|
|
|
public function __construct() { |
9
|
|
|
$this->connection = []; |
10
|
|
|
$this->currentConnection = null; |
11
|
|
|
} |
12
|
|
|
public function addConnection( $_path, $_name = "default" ) { |
13
|
|
|
if(!is_string($_path)) |
14
|
|
|
throw new JException("Parameter must be a string.", 1); |
15
|
|
|
try { |
16
|
|
|
$jConfig = new JConfig($_path); |
17
|
|
|
if($jConfig->enable) { |
|
|
|
|
18
|
|
|
$connection = new Connection($jConfig); |
19
|
|
|
$this->addConnectionMan($connection, $_name); |
20
|
|
|
} |
21
|
|
|
} catch (Exception $e) { |
22
|
|
|
throw new JException($e->getMessage(), 1); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
public function addConnectionMan( $_connection, $_name = "default") { |
26
|
|
|
if(!is_object($_connection) || !is_a($_connection, "Connection")) |
27
|
|
|
throw new JException("Parameter must be a Connection object.", 1); |
28
|
|
|
try { |
29
|
|
|
$this->connection["$_name"] = $_connection; |
30
|
|
|
$this->currentConnection = $_connection; |
31
|
|
|
} catch (Exception $e) { |
32
|
|
|
throw new JException($e->getMessage(), 1); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
public function setConnection( $_name = "default" ) { |
36
|
|
|
if(!is_string($_name)) |
37
|
|
|
throw new JException("Parameter must be a string.", 1); |
38
|
|
|
if(!isset($this->connection["$_name"])) |
39
|
|
|
throw new JException("This connection name does not exist.", 1); |
40
|
|
|
$this->currentConnection = $this->connection["$_name"]; |
41
|
|
|
} |
42
|
|
View Code Duplication |
public function query( $_query ) { |
|
|
|
|
43
|
|
|
if(!is_string($_query)) |
44
|
|
|
throw new JException("Parameter must be a string.", 1); |
45
|
|
|
if($this->currentConnection == null) |
46
|
|
|
throw new JException("No connection selected.", 1); |
47
|
|
|
try { |
48
|
|
|
$temp = $this->currentConnection->database->query($_query); |
49
|
|
|
} catch (Exception $e) { |
50
|
|
|
throw new JException($e->getMessage(), 1); |
51
|
|
|
} |
52
|
|
|
return $temp; |
53
|
|
|
} |
54
|
|
View Code Duplication |
public function queryInsert( $_query ) { |
|
|
|
|
55
|
|
|
if(!is_string($_query)) |
56
|
|
|
throw new JException("Parameter must be a string.", 1); |
57
|
|
|
if($this->currentConnection == null) |
58
|
|
|
throw new JException("No connection selected.", 1); |
59
|
|
|
try { |
60
|
|
|
$temp = $this->currentConnection->database->queryInsert($_query); |
61
|
|
|
} catch (Exception $e) { |
62
|
|
|
throw new JException($e->getMessage(), 1); |
63
|
|
|
} |
64
|
|
|
return $temp; |
65
|
|
|
} |
66
|
|
View Code Duplication |
public function queryFetch( $_query ) { |
|
|
|
|
67
|
|
|
if(!is_string($_query)) |
68
|
|
|
throw new JException("Parameter must be a string.", 1); |
69
|
|
|
if($this->currentConnection == null) |
70
|
|
|
throw new JException("No connection selected.", 1); |
71
|
|
|
try { |
72
|
|
|
$temp = $this->currentConnection->database->queryFetch($_query); |
73
|
|
|
} catch (Exception $e) { |
74
|
|
|
throw new JException($e->getMessage(), 1); |
75
|
|
|
} |
76
|
|
|
return $temp; |
77
|
|
|
} |
78
|
|
View Code Duplication |
public function queryArray( $_query ) { |
|
|
|
|
79
|
|
|
if(!is_string($_query)) |
80
|
|
|
throw new JException("Parameter must be a string.", 1); |
81
|
|
|
if($this->currentConnection == null) |
82
|
|
|
throw new JException("No connection selected.", 1); |
83
|
|
|
try { |
84
|
|
|
$temp = $this->currentConnection->database->queryArray($_query); |
85
|
|
|
} catch (Exception $e) { |
86
|
|
|
throw new JException($e->getMessage(), 1); |
87
|
|
|
} |
88
|
|
|
return $temp; |
89
|
|
|
} |
90
|
|
View Code Duplication |
public function newTable( $_query ) { |
|
|
|
|
91
|
|
|
if(!is_string($_query)) |
92
|
|
|
throw new JException("Parameter must be a string.", 1); |
93
|
|
|
if($this->currentConnection == null) |
94
|
|
|
throw new JException("No connection selected.", 1); |
95
|
|
|
try { |
96
|
|
|
$temp = $this->currentConnection->database->newTable($_query); |
97
|
|
|
} catch (Exception $e) { |
98
|
|
|
throw new JException($e->getMessage(), 1); |
99
|
|
|
} |
100
|
|
|
return $temp; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
?> |
|
|
|
|
104
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.