-
Notifications
You must be signed in to change notification settings - Fork 1
/
setters.php
143 lines (126 loc) · 4.08 KB
/
setters.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
<?php
require_once "assets\MysqliDb\MysqliDb.php";
require_once "config.php";
/**
* Description of RubricSetters
*
* @author Doug
*/
class Setters {
/*
* Updates multiple seperate rows
* @param $data The data formatted like $id => rowData
* @param $idColumnName The name of the id column ie. field_id
* @param $tableName The table name the rows are contained in
*/
public static function updateRows($data, $idColumnName, $tableName, $transaction){
$db = self::getDBInstance();
$rowsUpdated = 0;
if($transaction){
$db->startTransaction();
}
foreach($data as $id => $itemData){
if(self::updateRow($id, $idColumnName, $tableName, $itemData)){
$rowsUpdated++;
continue;
}
if($transaction){
$db->rollback();
}
return false;
}
if($transaction){
$db->commit();
}
return true;
}
public static function rowExists($tableName, $criteria){
$db = self::getDBInstance();
foreach($criteria as $column => $value){
$db->where($column, $value);
}
if($db->get($tableName, null, '*')){
return true;
} else {
return false;
}
}
public static function updateOrInsertRow($tableName, $data, $criteria){
$db = self::getDBInstance();
if(self::rowExists($tableName, $criteria)){
return self::updateRowMultipleCriteria($tableName, $data, $criteria);
} else {
return self::insertRow($tableName, $data);
}
}
/*
* Updates a single row
* @param string $tableName The name of the table
* @param $data The associative array of columnName => value
* @param $criteria The associative array of columnName => value for where clauses
*/
public static function updateRowMultipleCriteria($tableName, $data, $criteria){
$db = self::getDBInstance();
foreach($criteria as $column => $value){
$db->where($column, $value);
}
if($db->update($tableName, $data)){
return true;
} else {
return false;
}
}
/*
* Updates a single row
* @param int $id Primary key for item
* @param string $idColumnName The column name for the key
* @param string $tableName The name of the table
* @param $data The associative array of columnName => value
*/
public static function updateRow($id, $idColumnName, $tableName, $data){
$db = self::getDBInstance();
$db->where($idColumnName, $id);
if($db->update($tableName, $data)){
return true;
} else {
return false;
}
}
public static function insertRow($tableName, $data){
$db = self::getDBInstance();
$id = $db->insert($tableName, $data);
return $id;
}
public static function insertRows($tableName, $data){
$db = self::getDBInstance();
$ids = $db->insertMulti($tableName, $data);
return $ids;
}
public static function insertSimilarRows($tableName, $data, $keys){
$db = self::getDBInstance();
$ids = $db->insertMulti($tableName, $data, $keys);
return $ids;
}
public static function deleteThenInsertRows($table, $column, $condition, $data){
$db = self::getDBInstance();
$db->startTransaction();
try {
$db->where($column, $condition);
$db->delete($table);
self::insertRows($table, $data);
$db->commit();
return true;
} catch(Exception $e){
$db->rollback();
return false;
}
}
/* Prevents creating more DB connectons than needed */
private static function getDBInstance(){
$db = MysqliDb::getInstance();
if(!isset($db)){
$db = new MysqliDb(DB_HOST, DB_USER, DB_PASS, DB_NAME);
}
return $db;
}
}