-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbTransactions.php
More file actions
172 lines (162 loc) · 4.7 KB
/
dbTransactions.php
File metadata and controls
172 lines (162 loc) · 4.7 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
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
<?php
namespace Database\Transactions;
include_once "db.php";
include_once "Log.php";
include_once "utils.php";
abstract class Transaction
{
private $committed;
private $properties;
protected function __construct()
{
$this->committed=false;
$this->properties=array();
}
public function __set($property,$value)
{
if($property!='committed' && array_key_exists($property,$this->properties))
{
if(is_array($this->properties[$property]) && !is_array($value))
{
array_push($this->properties[$property],$value);
}
else
{
$this->properties[$property]=$value;
}
}
}
public function __get($property)
{
if($property!='committed' && array_key_exists($property,$this->properties))
{
return $this->properties[$property];
}
return null;
}
public function commit()
{
if(!$this->committed)
{
$this->exec_commit();
$this->committed=true;
}
}
public function rollback()
{
if($this->committed)
{
$this->exec_rollback();
$this->committed=false;
}
}
abstract protected function exec_commit();
abstract protected function exec_rollback();
protected function create_prop($name,$value)
{
$this->properties[$name]=$value;
}
}
class CreateBook extends Transaction
{
public function __construct()
{
parent::__construct();
$this->create_prop('filename',null);
$this->create_prop('bookid',null);
$this->create_prop('hash',null);
$this->create_prop('size',null);
$this->create_prop('descr',null);
$this->create_prop('year',null);
$this->create_prop('title',null);
$this->create_prop('author',null);
$this->create_prop('img',null);
$this->create_prop('vendor',null);
$this->create_prop('subjects',array());
$this->create_prop('file_id',null);
$this->create_prop('file_size',null);
}
protected function exec_commit()
{
$dbase = \Database\odbc()->connect();
if($dbase)
{
$con = $dbase->con();
$title=mysqli_real_escape_string($con,$this->title);
$descr=mysqli_real_escape_string($con,$this->descr);
$author=mysqli_real_escape_string($con,$this->author);
$size=$this->size;
$year=$this->year;
$hash=$this->hash;
$img=mysqli_real_escape_string($con,$this->img);
$file_id=$this->file_id;
$file_size=$this->file_size;
$vendor=$this->vendor;
$filename=mysqli_real_escape_string($con,$this->filename);
$sql="INSERT INTO BOOKS (TITLE,DESCR,AUTHORS,SIZE,YEAR,HASH,IMG_PATH) VALUES ('$title','$descr','$author',$size,'$year','$hash','$img')";
$res=$dbase->query($sql);
if(!$res)
{
$dbase->close();
throw new \Exception('database error: '.$sql);
}
$id=$this->bookid = $res;
foreach($this->subjects as $subject)
{
if(strlen($subject)>0)
{
$subject_id=\utils\selectOrCreateSubject($dbase,$subject);
if($subject_id>0)
{
$dbase->query("INSERT INTO BOOKS_SUBJECTS_ASSOC (SUBJECT_ID,BOOK_ID) VALUES ($subject_id,$id)");
}
else
{
\Logs\logDebug('cannot get subjects id for \'' . $subject . '\'');
}
}
}
$res=$dbase->query("SELECT ID FROM FILE_STORE WHERE VENDOR_CODE='$vendor'");
$store_id=null;
if($res->next())
{
$store_id=$res->field_value('ID');
}
else
{
$dbase->close();
throw new \Exception('cannot get file store id.');
}
$dbase->query("INSERT BOOKS_LINKS(BOOK_ID,STORE_ID,FILE_ID,FILE_SIZE,FILE_NAME) VALUES($id,$store_id,'$file_id',$file_size,'$filename')");
$dbase->close();
}
}
public function exec_rollback()
{
}
}
class DeleteBook extends Transaction
{
public function __construct()
{
parent::__construct();
$this->create_prop('bookid',null);
}
protected function exec_commit()
{
$dbase = \Database\odbc()->connect();
if($dbase)
{
$bookid=$this->bookid;
$dbase->query("DELETE FROM BOOKS WHERE ID=$bookid");
$dbase->query("DELETE FROM BOOKS_LINKS WHERE BOOK_ID=$bookid");
$dbase->query("DELETE FROM BOOKS_SUBJECTS_ASSOC WHERE BOOK_ID=$bookid");
$dbase->query("DELETE FROM IT_SUBJECT WHERE ID NOT IN (SELECT SUBJECT_ID FROM BOOKS_SUBJECTS_ASSOC) AND PERMANENT=0");
$dbase->close();
}
}
public function exec_rollback()
{
}
}
?>