forked from dlsniper/Sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
171 lines (141 loc) · 4.84 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
<?php
/**
* Add automatic detection of our host and connect without messing with configs
* in the source files in the database
* @package TaskServer
* @version 0.1
* @author Florin Patan
* @copyright Florin Patan
*/
class database extends mysqli
{
/**
* Run the query and get a single result
* @param string Query
* @param boolean Fatal error in case query can't be executed
* @return object Query result
*/
public function getOne($query, $isFatal = false)
{
// Try and run the query
if ($results = $this->query($query)) {
// Fetch the result
$result = $results->fetch_object();
// Free the results
$results->close();
} else {
// Send the error to the admin
TaskServer::error('Failed to run query:' . "\r\n" . $query, 'query error', $isFatal);
}
// Return the results
return $result;
}
/**
* Run the query and all results
* @param string Query
* @param boolean Fatal error in case query can't be executed
* @return object Query results
*/
public function getAll($query, $isFatal = false)
{
// Try and run the query
if ($results = $this->query($query)) {
} else {
// Send the error to the admin
TaskServer::error('Failed to run query:' . "\r\n" . $query, 'query error', $isFatal);
}
// Return the results
return $results;
}
/**
* Insert values into database using query $query and $types for data type
* @param string Query
* @param array The values
* @param boolean Fatal error?
*/
public function insert($query, $values, $fatalError = false)
{
// Create a prepared statement
if ($stmt = $this->prepare($query)) {
// Get the type of paramteres
$types = array_shift($values);
// This will store the inserted ids
$ids = array();
// For each element of the values do some
foreach ($values as $no => $value) {
// This is our param placeholder
$params = array();
// Add the types
$params[] = $types;
// Bind params call_user_func_array style
foreach ($value as $key => $val) {
$params[] = &$values[$no][$key];
}
// Bind our params
call_user_func_array(array($stmt, 'bind_param'), $params);
// Execute the query
$stmt->execute();
// Get the inserted id
$ids[] = $this->insert_id;
// Clean up
unset($params);
}
// Close our query
$stmt->close();
// Clean up
unset($stmt);
// Return the inserted id
return $ids;
} else {
TaskServer::error('Could not create a prepared statement for query: ' . "\r\n" . $query, 'failed to do insert query', $fatalError);
}
}
/**
* Connect to the MySQL database automatically depending on the hostname of the current computer
* @return database
*/
public function __construct()
{
// Detect where are we
$hostname = php_uname('n');
// Find out where we are
switch (true) {
// We are on the main server
case strpos($hostname, 'rodb.ro') !== false :
$host = 'localhost';
break;
// We are on a virtual machine from home
case strpos($hostname, 'mirror') !== false :
$host = '192.168.0.199';
break;
// We are on a linux machine from home
case strpos($hostname, 'linux-monzi') !== false :
$host = 'localhost';
break;
// We are home!
case strpos($hostname, 'monzi') !== false :
$host = 'localhost';
break;
// This is a new place, let's leave it for the time being
default :
TaskServer::error('Failed to detect the host in order to connect to the database', 'failed to connect to database', true);
}
// Create our object
@parent::__construct($host, "root", "x", "florinp_sync");
// Check of the connection was possible or not
if ($this->connect_error) {
// This is a error
TaskServer::error('Connect Error: ' . $this->connect_error, 'failed to connect to database', true);
}
// Let's return ourselfs
return $this;
}
/**
* Close our database before we go bye bye
*/
public function __destruct()
{
// Attempt to close our existing connection
@$this->close();
}
}