-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.php
173 lines (149 loc) · 3.9 KB
/
database.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* @author Stormbreaker
*/
class Database extends PDO
{
const MYSQL_TIMESTAMP = '%Y-%m-%d %H:%M:%S';
const MYSQL_DATE = '%Y-%m-%d';
public function __construct($dsn, $username = null, $password = null, $driver_options = null)
{
parent::__construct($dsn, $username, $password, $driver_options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array ('StormPDOStatement', array($this)));
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
}
/**
* Execute SQL and return num of affected rows
*
* @param string Query
* @param string Parameters
* @return int Num affected rows
*/
public function exec($statement, $values = null)
{
if ( is_array($values) )
{
$stmt = $this->prepare($statement);
$stmt->execute($values);
return $stmt->rowCount();
}
else
return parent::exec($statement);
}
/**
* Execute query and return StormPDOStatement
*
* @param string $statement
* @param mixed $var
* @param mixed $obj1
* @param mixed $obj2
* @return
*/
public function query($statement, $var = null, $obj1 = null, $obj2 = null)
{
if ( is_array($var) )
{
$stmt = $this->prepare($statement);
$stmt->execute($var);
return $stmt;
}
else
return parent::query($statement);
}
/**
* Get the time difference and display in current language
*
* @param int $time UNIX timestamp of the date and time
* @param array $lang Array with language constants
* @return string
*/
public static function TimeElapsed($time, $lang)
{
$t = strtotime('now') - $time;
$y = (int)strftime('%Y', $t) - 1970;
if ( $y > 0 )
return ( $y - 1970 ) . ' ' . ( $y == 1 ? $lang['years_1'] : $lang['years'] );
$m = (int)strftime('%m', $t) - 1;
if ( $m > 0 )
return $m . ' ' . ( $m == 1 ? $lang['months_1'] : $lang['months'] );
$w = (int)strftime('%W', $t);
if ( $w > 0 )
return $w . ' ' . ( $w == 1 ? $lang['weeks_1'] : $lang['weeks'] );
$d = (int)strftime('%d', $t) - 1;
if ( $d > 0 )
return $d . ' ' . ( $d == 1 ? $lang['days_1'] : $lang['days'] );
$H = (int)strftime('%H', $t) - (int)strftime('%H', 0);
if ( $H > 0 )
return $H . ' ' . ( $H == 1 ? $lang['hours_1'] : $lang['hours'] );
$M = (int)strftime('%M', $t);
if ( $M > 0 )
return $M . ' ' . ( $M == 1 ? $lang['minutes_1'] : $lang['minutes'] );
$S = (int)strftime('%S', $t);
if ( $S > 0 )
return $S . ' ' . ( $S == 1 ? $lang['seconds_1'] : $lang['seconds'] );
}
}
class StormPDOStatement extends PDOStatement
{
protected $pdo;
/**
* PSPDOStatement::__construct()
*
* @param mixed $pdo
* @return
*/
protected function __construct($pdo)
{
$this->pdo = $pdo;
}
/**
* PSPDOStatement::execute()
*
* @param mixed $params
* @return
*/
public function execute($params = array())
{
if ( is_array($params) )
foreach ( $params as $key => $value )
{
if ( is_int($key) )
$key += 1;
else
$key = ':'.$key;
if ( is_array($value) )
$this->bindValue($key, $value[0], $value[1]);
elseif ( is_int($value) )
$this->bindValue($key, $value, PDO::PARAM_INT);
elseif ( is_bool($value) )
$this->bindValue($key, $value, PDO::PARAM_BOOL);
elseif ( is_null($value) )
$this->bindValue($key, $value, PDO::PARAM_NULL);
else
$this->bindValue($key, $value);
}
parent::execute();
return $this;
}
/**
* PSPDOStatement::fetch()
*
* @param mixed $fetch_style
* @param mixed $cursor_orientation
* @param integer $cursor_offset
* @return
*/
public function fetch( $fetch_style = PDO::FETCH_OBJ, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0 )
{
return parent::fetch($fetch_style, $cursor_orientation, $cursor_offset);
}
public function fetchAllColumn($index = 0)
{
$ar = array();
while ( $c = $this->fetchColumn($index) )
$ar[] = $c;
return $ar;
}
}
?>