forked from gebeto/cydia-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.php
138 lines (116 loc) · 3.45 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
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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
class DB {
public static $connection;
public static function connect() {
$host = 'localhost';
$user = 'c97006xu_1';
$pass = 'password';
$dbname = 'c97006xu_1';
try {
DB::$connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch(PDOException $e) {
echo $e->getMessage();
exit();
}
}
public static function select($query, $data = array()) {
$STH = DB::$connection->prepare($query);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$STH->execute($data);
$res = $STH->fetchAll();
return $res;
}
public static function exec($query, $data = array()) {
$STH = DB::$connection->prepare($query);
$res = $STH->execute($data);
return $res;
}
public static function insert($table_name, $data = array()) {
$table_keys = array();
$prepare_keys = array();
$sql = 'INSERT INTO `';
$sql .= $table_name . '` (';
foreach ($data as $key => $value) {
$table_keys[] = '`' . $key . '`';
$prepare_keys[] = ':' . $key;
}
$sql .= implode(', ', $table_keys);
$sql .= ') VALUES (';
$sql .= implode(', ', $prepare_keys);
$sql .= ');';
DB::exec($sql, $data);
return '';
}
public static function create_table($table_name, $cols=array()) {
$sql = 'CREATE TABLE `';
$sql .= $table_name;
$sql .= '` (';
$sql .= implode(', ', $cols);
$sql .= ') ENGINE=InnoDB DEFAULT CHARSET=utf8;';
DB::exec($sql);
}
public static function alter_table($table_name, $action) {
$sql = 'ALTER TABLE `' . $table_name . '` ' . $action . ';';
DB::exec($res);
}
public static function drop_table($table_name) {
$sql = 'DROP TABLE `' . $table_name . '`;';
DB::exec($sql);
}
// public static function init() {
// DB::create_table('repo_info', array(
// '`id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)',
// '`title` varchar(64) NOT NULL',
// '`description` varchar(256) NOT NULL',
// '`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
// ));
// DB::insert('repo_info', array(
// 'id' => 1,
// 'title' => 'Danik repo',
// 'description' => 'Daniel repository'
// ));
// DB::create_table('repo_users', array(
// '`id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)',
// '`name` varchar(50) NOT NULL',
// '`udid` varchar(40) NOT NULL',
// '`is_active` tinyint(1) NOT NULL DEFAULT 1'
// ));
// DB::create_table('repo_tweaks', array(
// '`id` INT NOT NULL AUTO_INCREMENT',
// '`name` VARCHAR(32) NOT NULL',
// '`description` VARCHAR(256) NOT NULL',
// '`version` VARCHAR(11) NOT NULL',
// 'PRIMARY KEY (`id`)',
// 'UNIQUE `tweak_unique` (`name`)'
// ));
// DB::create_table('repo_purchases', array(
// '`id` INT NOT NULL AUTO_INCREMENT',
// '`user_id` INT NOT NULL AUTO_INCREMENT',
// '`tweak_id` INT NOT NULL AUTO_INCREMENT',
// '`purchase_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP',
// 'PRIMARY KEY (`id`)'
// ));
// }
// public static function uninit() {
// DB::drop_table('repo_info');
// DB::drop_table('repo_users');
// DB::drop_table('repo_tweaks');
// DB::drop_table('repo_purchases');
// }
// public static function action() {
// if (isset($_GET['act'])) {
// call_user_func_array(array('DB', $_GET['act']), array());
// }
// }
}
DB::connect();
// print_r(
// DB::select("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME=:table_name", array(
// 'table_name' => 'repo_info'
// )
// )
// );
// DB::action();