@@ -3,94 +3,94 @@ |
||
3 | 3 | jRequire("../JConfig/JConfig.php"); |
4 | 4 | jRequire("../Connection/Connection.php"); |
5 | 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 | - foreach ($this->modules as &$module) |
|
32 | - if(isset($this->currentConnection)) |
|
33 | - $module->addConnectionMan($this->currentConnection, $_name); |
|
34 | - } catch (Exception $e) { |
|
35 | - throw new JException($e->getMessage(), 1); |
|
36 | - } |
|
37 | - } |
|
38 | - public function setConnection( $_name = "default" ) { |
|
39 | - if(!is_string($_name)) |
|
40 | - throw new JException("Parameter must be a string.", 1); |
|
41 | - if(!isset($this->connection["$_name"])) |
|
42 | - throw new JException("This connection name does not exist.", 1); |
|
43 | - $this->currentConnection = $this->connection["$_name"]; |
|
44 | - } |
|
45 | - public function query( $_query ) { |
|
46 | - if(!is_string($_query)) |
|
47 | - throw new JException("Parameter must be a string.", 1); |
|
48 | - try { |
|
49 | - $temp = $this->currentConnection->database->query($_query); |
|
50 | - } catch (Exception $e) { |
|
51 | - throw new JException($e->getMessage(), 1); |
|
52 | - } |
|
53 | - return $temp; |
|
54 | - } |
|
55 | - public function queryInsert( $_query ) { |
|
56 | - if(!is_string($_query)) |
|
57 | - throw new JException("Parameter must be a string.", 1); |
|
58 | - try { |
|
59 | - $temp = $this->currentConnection->database->queryInsert($_query); |
|
60 | - } catch (Exception $e) { |
|
61 | - throw new JException($e->getMessage(), 1); |
|
62 | - } |
|
63 | - return $temp; |
|
64 | - } |
|
65 | - public function queryFetch( $_query ) { |
|
66 | - if(!is_string($_query)) |
|
67 | - throw new JException("Parameter must be a string.", 1); |
|
68 | - try { |
|
69 | - $temp = $this->currentConnection->database->queryFetch($_query); |
|
70 | - } catch (Exception $e) { |
|
71 | - throw new JException($e->getMessage(), 1); |
|
72 | - } |
|
73 | - return $temp; |
|
74 | - } |
|
75 | - public function queryArray( $_query ) { |
|
76 | - if(!is_string($_query)) |
|
77 | - throw new JException("Parameter must be a string.", 1); |
|
78 | - try { |
|
79 | - $temp = $this->currentConnection->database->queryArray($_query); |
|
80 | - } catch (Exception $e) { |
|
81 | - throw new JException($e->getMessage(), 1); |
|
82 | - } |
|
83 | - return $temp; |
|
84 | - } |
|
85 | - public function newTable( $_query ) { |
|
86 | - if(!is_string($_query)) |
|
87 | - throw new JException("Parameter must be a string.", 1); |
|
88 | - try { |
|
89 | - $temp = $this->currentConnection->database->newTable($_query); |
|
90 | - } catch (Exception $e) { |
|
91 | - throw new JException($e->getMessage(), 1); |
|
92 | - } |
|
93 | - return $temp; |
|
94 | - } |
|
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 | + foreach ($this->modules as &$module) |
|
32 | + if(isset($this->currentConnection)) |
|
33 | + $module->addConnectionMan($this->currentConnection, $_name); |
|
34 | + } catch (Exception $e) { |
|
35 | + throw new JException($e->getMessage(), 1); |
|
36 | + } |
|
37 | + } |
|
38 | + public function setConnection( $_name = "default" ) { |
|
39 | + if(!is_string($_name)) |
|
40 | + throw new JException("Parameter must be a string.", 1); |
|
41 | + if(!isset($this->connection["$_name"])) |
|
42 | + throw new JException("This connection name does not exist.", 1); |
|
43 | + $this->currentConnection = $this->connection["$_name"]; |
|
44 | + } |
|
45 | + public function query( $_query ) { |
|
46 | + if(!is_string($_query)) |
|
47 | + throw new JException("Parameter must be a string.", 1); |
|
48 | + try { |
|
49 | + $temp = $this->currentConnection->database->query($_query); |
|
50 | + } catch (Exception $e) { |
|
51 | + throw new JException($e->getMessage(), 1); |
|
52 | + } |
|
53 | + return $temp; |
|
54 | + } |
|
55 | + public function queryInsert( $_query ) { |
|
56 | + if(!is_string($_query)) |
|
57 | + throw new JException("Parameter must be a string.", 1); |
|
58 | + try { |
|
59 | + $temp = $this->currentConnection->database->queryInsert($_query); |
|
60 | + } catch (Exception $e) { |
|
61 | + throw new JException($e->getMessage(), 1); |
|
62 | + } |
|
63 | + return $temp; |
|
64 | + } |
|
65 | + public function queryFetch( $_query ) { |
|
66 | + if(!is_string($_query)) |
|
67 | + throw new JException("Parameter must be a string.", 1); |
|
68 | + try { |
|
69 | + $temp = $this->currentConnection->database->queryFetch($_query); |
|
70 | + } catch (Exception $e) { |
|
71 | + throw new JException($e->getMessage(), 1); |
|
72 | + } |
|
73 | + return $temp; |
|
74 | + } |
|
75 | + public function queryArray( $_query ) { |
|
76 | + if(!is_string($_query)) |
|
77 | + throw new JException("Parameter must be a string.", 1); |
|
78 | + try { |
|
79 | + $temp = $this->currentConnection->database->queryArray($_query); |
|
80 | + } catch (Exception $e) { |
|
81 | + throw new JException($e->getMessage(), 1); |
|
82 | + } |
|
83 | + return $temp; |
|
84 | + } |
|
85 | + public function newTable( $_query ) { |
|
86 | + if(!is_string($_query)) |
|
87 | + throw new JException("Parameter must be a string.", 1); |
|
88 | + try { |
|
89 | + $temp = $this->currentConnection->database->newTable($_query); |
|
90 | + } catch (Exception $e) { |
|
91 | + throw new JException($e->getMessage(), 1); |
|
92 | + } |
|
93 | + return $temp; |
|
94 | + } |
|
95 | 95 | } |
96 | 96 | ?> |
@@ -1,49 +1,49 @@ |
||
1 | 1 | <?php |
2 | 2 | requireComponents("Adapters"); |
3 | 3 | class Connection { |
4 | - public $database; |
|
5 | - public $info; |
|
6 | - public function __construct( $_object ) { |
|
7 | - if(!is_object($_object)) |
|
8 | - throw new JException("Parameter must be an object."); |
|
9 | - $this->setConnection( |
|
10 | - $_object->server, |
|
11 | - $_object->database, |
|
12 | - $_object->user, |
|
13 | - $_object->password, |
|
14 | - $this->getConnectionType($_object->engine) |
|
15 | - ); |
|
16 | - } |
|
17 | - protected function setConnection ( $_srv, $_db, $_usr, $_pass, $_type ) { |
|
18 | - switch ($_type) { |
|
19 | - case "mysqli": |
|
20 | - $this->database = new MysqliAdapter($_srv, $_db, $_usr, $_pass); |
|
21 | - break; |
|
22 | - case "postgresql": |
|
23 | - $this->database = new PostgresqlAdapter($_srv, $_db, $_usr, $_pass); |
|
24 | - break; |
|
25 | - case "pdo-sqlite-memory": |
|
26 | - $this->database = new PdoAdapterSqLiteMemory($_srv, $_db, $_usr, $_pass); |
|
27 | - break; |
|
28 | - case "pdo-mysql": |
|
29 | - default: |
|
30 | - $this->database = new PdoAdapterMysql($_srv, $_db, $_usr, $_pass); |
|
31 | - break; |
|
32 | - } |
|
33 | - $this->info = [ |
|
34 | - "srv" => $_srv, |
|
35 | - "db" => $_db, |
|
36 | - "usr" => $_usr, |
|
37 | - "pass" => $_pass, |
|
38 | - "type" => $_type |
|
39 | - ]; |
|
40 | - } |
|
41 | - protected function getConnectionType( $_type ) { |
|
42 | - $array = (array)$_type; |
|
43 | - foreach ($array as $key => $value) |
|
44 | - if($value) |
|
45 | - return $key; |
|
46 | - return "pdo"; |
|
47 | - } |
|
4 | + public $database; |
|
5 | + public $info; |
|
6 | + public function __construct( $_object ) { |
|
7 | + if(!is_object($_object)) |
|
8 | + throw new JException("Parameter must be an object."); |
|
9 | + $this->setConnection( |
|
10 | + $_object->server, |
|
11 | + $_object->database, |
|
12 | + $_object->user, |
|
13 | + $_object->password, |
|
14 | + $this->getConnectionType($_object->engine) |
|
15 | + ); |
|
16 | + } |
|
17 | + protected function setConnection ( $_srv, $_db, $_usr, $_pass, $_type ) { |
|
18 | + switch ($_type) { |
|
19 | + case "mysqli": |
|
20 | + $this->database = new MysqliAdapter($_srv, $_db, $_usr, $_pass); |
|
21 | + break; |
|
22 | + case "postgresql": |
|
23 | + $this->database = new PostgresqlAdapter($_srv, $_db, $_usr, $_pass); |
|
24 | + break; |
|
25 | + case "pdo-sqlite-memory": |
|
26 | + $this->database = new PdoAdapterSqLiteMemory($_srv, $_db, $_usr, $_pass); |
|
27 | + break; |
|
28 | + case "pdo-mysql": |
|
29 | + default: |
|
30 | + $this->database = new PdoAdapterMysql($_srv, $_db, $_usr, $_pass); |
|
31 | + break; |
|
32 | + } |
|
33 | + $this->info = [ |
|
34 | + "srv" => $_srv, |
|
35 | + "db" => $_db, |
|
36 | + "usr" => $_usr, |
|
37 | + "pass" => $_pass, |
|
38 | + "type" => $_type |
|
39 | + ]; |
|
40 | + } |
|
41 | + protected function getConnectionType( $_type ) { |
|
42 | + $array = (array)$_type; |
|
43 | + foreach ($array as $key => $value) |
|
44 | + if($value) |
|
45 | + return $key; |
|
46 | + return "pdo"; |
|
47 | + } |
|
48 | 48 | } |
49 | 49 | ?> |
@@ -1,21 +1,21 @@ |
||
1 | 1 | <?php |
2 | 2 | jRequire("PdoAdapterMysql.php"); |
3 | 3 | class PdoAdapterSqLiteMemory extends PdoAdapterMysql { |
4 | - public $connection; |
|
5 | - public function __construct( $_srv, $_db, $_usr, $_pass ) { |
|
6 | - try { |
|
7 | - $this->connection = new PDO("sqlite::memory:"); |
|
8 | - } catch( Exception $e ) { |
|
9 | - throw new JException($e->getMessage()); |
|
10 | - } |
|
11 | - } |
|
12 | - public function newTable( $_sql ) { |
|
13 | - try { |
|
14 | - $temp = $this->connection->exec($_sql); |
|
15 | - } catch (Exception $e) { |
|
16 | - throw new JException($e->getMessage()); |
|
17 | - } |
|
18 | - return $temp; |
|
19 | - } |
|
4 | + public $connection; |
|
5 | + public function __construct( $_srv, $_db, $_usr, $_pass ) { |
|
6 | + try { |
|
7 | + $this->connection = new PDO("sqlite::memory:"); |
|
8 | + } catch( Exception $e ) { |
|
9 | + throw new JException($e->getMessage()); |
|
10 | + } |
|
11 | + } |
|
12 | + public function newTable( $_sql ) { |
|
13 | + try { |
|
14 | + $temp = $this->connection->exec($_sql); |
|
15 | + } catch (Exception $e) { |
|
16 | + throw new JException($e->getMessage()); |
|
17 | + } |
|
18 | + return $temp; |
|
19 | + } |
|
20 | 20 | } |
21 | 21 | ?> |
@@ -1,44 +1,44 @@ |
||
1 | 1 | <?php |
2 | 2 | jRequire("ConnectionInterface.php"); |
3 | 3 | class PdoAdapterMysql implements ConnectionAdapterInterface { |
4 | - public $connection; |
|
5 | - public function __construct( $_srv, $_db, $_usr, $_pass ) { |
|
6 | - try { |
|
7 | - $connection = "mysql:host=$_srv;dbname=$_db"; |
|
8 | - $this->connection = new PDO( $connection, $_usr, $_pass, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"] ); |
|
9 | - } catch( Exception $e ) { |
|
10 | - throw new JException($e->getMessage()); |
|
11 | - } |
|
12 | - } |
|
13 | - public function query( $_query ) { |
|
14 | - $this->stdQuery($_query); |
|
15 | - return true; |
|
16 | - } |
|
17 | - public function queryInsert( $_query ) { |
|
18 | - $this->stdQuery($_query); |
|
19 | - return $this->connection->lastInsertId(); |
|
20 | - } |
|
21 | - public function queryFetch( $_query ) { |
|
22 | - $temp = $this->stdQuery($_query); |
|
23 | - return $temp->fetchAll(PDO::FETCH_ASSOC); |
|
24 | - } |
|
25 | - public function queryArray( $_query ) { |
|
26 | - $temp = $this->stdQuery($_query); |
|
27 | - return $temp->fetchAll(PDO::FETCH_COLUMN, 0); |
|
28 | - } |
|
29 | - protected function stdQuery( $_query ) { |
|
30 | - $database = $this->connection; |
|
31 | - $query = $database->prepare($_query); |
|
32 | - $result = $query->execute(); |
|
33 | - if(!$result) |
|
34 | - throw new JException(json_encode([ |
|
35 | - "query" => $_query, |
|
36 | - "error" => [ |
|
37 | - $query->errorInfo(), |
|
38 | - $database->errorInfo() |
|
39 | - ] |
|
40 | - ])); |
|
41 | - return $query; |
|
42 | - } |
|
4 | + public $connection; |
|
5 | + public function __construct( $_srv, $_db, $_usr, $_pass ) { |
|
6 | + try { |
|
7 | + $connection = "mysql:host=$_srv;dbname=$_db"; |
|
8 | + $this->connection = new PDO( $connection, $_usr, $_pass, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"] ); |
|
9 | + } catch( Exception $e ) { |
|
10 | + throw new JException($e->getMessage()); |
|
11 | + } |
|
12 | + } |
|
13 | + public function query( $_query ) { |
|
14 | + $this->stdQuery($_query); |
|
15 | + return true; |
|
16 | + } |
|
17 | + public function queryInsert( $_query ) { |
|
18 | + $this->stdQuery($_query); |
|
19 | + return $this->connection->lastInsertId(); |
|
20 | + } |
|
21 | + public function queryFetch( $_query ) { |
|
22 | + $temp = $this->stdQuery($_query); |
|
23 | + return $temp->fetchAll(PDO::FETCH_ASSOC); |
|
24 | + } |
|
25 | + public function queryArray( $_query ) { |
|
26 | + $temp = $this->stdQuery($_query); |
|
27 | + return $temp->fetchAll(PDO::FETCH_COLUMN, 0); |
|
28 | + } |
|
29 | + protected function stdQuery( $_query ) { |
|
30 | + $database = $this->connection; |
|
31 | + $query = $database->prepare($_query); |
|
32 | + $result = $query->execute(); |
|
33 | + if(!$result) |
|
34 | + throw new JException(json_encode([ |
|
35 | + "query" => $_query, |
|
36 | + "error" => [ |
|
37 | + $query->errorInfo(), |
|
38 | + $database->errorInfo() |
|
39 | + ] |
|
40 | + ])); |
|
41 | + return $query; |
|
42 | + } |
|
43 | 43 | } |
44 | 44 | ?> |