-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.php
89 lines (76 loc) · 2.14 KB
/
db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace ZDB ;
/**
* Синглетон инкапсулирующий конект к БД
*/
class DB
{
private static $db = null;
private static $driver = null;
private static $dbhost;
private static $dbname;
private static $dbuser;
private static $dbpassword;
private $conn = null;
private function __construct() {
}
/**
* Установка параметров коннекта
*
* @param mixed $dbhost
* @param mixed $dbname
* @param mixed $dbuser
* @param mixed $dbpassword
* @param string $driver
*/
public static function config($dbhost, $dbname, $dbuser, $dbpassword, $driver = "mysqli") {
global $ADODB_FETCH_MODE, $ADODB_QUOTE_FIELDNAMES;
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$ADODB_QUOTE_FIELDNAMES = true;
self::$dbhost = $dbhost;
self::$dbname = $dbname;
self::$dbuser = $dbuser;
self::$dbpassword = $dbpassword;
self::$driver = $driver;
}
/**
* Открывает конект к БД и возвращает соотаветствующий ресурс
*
*/
public static function getConnect() {
$db = DB::getDB();
$db->open();
return $db->conn;
}
/**
* Возвращает инстанс
*
*/
public static function getDB() {
if (self::$db == null) {
self::$db = new DB();
}
return self::$db;
}
private function open() {
if ($this->conn instanceof \ADOConnection) {
return;
}
$this->conn = \ADONewConnection(self::$driver);
$this->conn->Connect(self::$dbhost, self::$dbuser, self::$dbpassword, self::$dbname);
if(DB::$driver == "mysqli") {
$this->conn->Execute("SET NAMES 'utf8'");
$this->conn->Execute("SET SQL_BIG_SELECTS=1");
}
}
/**
* Закрывает конект к БД
*
*/
public static function Close() {
$db = DB::getDB();
if ($db->conn instanceof \ADOConnection) {
$db->conn->Close();
}
}
}