-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBoperation.php
More file actions
63 lines (62 loc) · 2.25 KB
/
DBoperation.php
File metadata and controls
63 lines (62 loc) · 2.25 KB
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
<?php namespace Raymondoor;
class DBoperation extends DBconnection{
public static function makeTableIfNot(string $table, string $schema){
if(empty($table) || empty($schema)){
throw new \Exception('Table name and schema cannot be empty.');
}
return static::exec("CREATE TABLE IF NOT EXISTS {$table} ({$schema})");
}
public static function dropTableIfIs(string $table){
try{
$statement = 'DROP TABLE IF EXISTS '.$table;
return static::exec($statement);
}catch(\Exception $e){
return array('No such table');
}
}
public static function allFrom(string $table){
try{
$statement = 'SELECT * FROM '.$table.' ORDER BY id ASC';
return static::run($statement);
}catch(\Exception $e){
return array('error' => $e->getMessage());
}
}
public static function fetchOne(string $stmt, array $param=[]){
try{
if(strpos(trim($stmt), 'SELECT') !== 0){
throw new \Exception('fetchOne() must be used for SELECT only');
}
return static::run($stmt, $param)[0];
}catch(\Exception $e){
return $e->getMessage();
}
}
// useful column presets. make sure to add ',' before/after calling these methods if needed.
public static function create_id(){
switch(static::getType()){
case static::TYPE_SQLITE:
return 'id INTEGER PRIMARY KEY AUTOINCREMENT';
break;
case static::TYPE_MYSQL:
return 'id INT AUTO_INCREMENT PRIMARY KEY';
break;
case static::TYPE_PGSQL:
return 'id SERIAL PRIMARY KEY';
break;
}
}
public static function create_time_record(){
switch(static::getType()){
case static::TYPE_SQLITE:
return 'created_at DATETIME DEFAULT (DATETIME(\'now\', \'localtime\'))';
break;
case static::TYPE_MYSQL:
return 'created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP';
break;
case static::TYPE_PGSQL:
return 'created_at TIMESTAMP DEFAULT NOW()';
break;
}
}
}