-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection transact class.php
308 lines (258 loc) · 7.24 KB
/
connection transact class.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
class TransactionDemo{
const DB_HOST = 'localhost';
const DB_NAME = 'classicmodels';
const DB_USER = 'root';
const DB_PASSWORD = '';
private $conn = null;
private $message = '';
/**
* get message
* @return string the message of transferring process
*/
public function getMessage() {
return $this->message;
}
/**
* transfer money from the $from account to $to account
* @param int $from
* @param int $to
* @param float $amount
* @return true on success or false on failure. The message is logged in the
* $message
*/
public function transfer($from,$to,$amount) {
try {
$this->conn->beginTransaction();
// get available amount of the transferred account
$sql = 'SELECT amount FROM accounts WHERE id=:from';
$stmt = $this->conn->prepare($sql);
$stmt->execute(array(":from" => $from));
$availableAmount = (int)$stmt->fetchColumn();
$stmt->closeCursor();
if($availableAmount < $amount){
$this->message = 'Insufficient amount to transfer';
return false;
}
// deduct from the transferred account
$sql_update_from = 'UPDATE accounts
SET amount = amount - :amount
WHERE id = :from';
$stmt = $this->conn->prepare($sql_update_from);
$stmt->execute(array(":from"=> $from, ":amount" => $amount));
$stmt->closeCursor();
// add to the receiving account
$sql_update_to = 'UPDATE accounts
SET amount = amount + :amount
WHERE id = :to';
$stmt = $this->conn->prepare($sql_update_to);
$stmt->execute(array(":to" => $to, ":amount" => $amount));
// commit the transaction
$this->conn->commit();
$this->message = 'The amount has been transferred successfully';
return true;
} catch (Exception $e) {
$this->message = $e->getMessage();
$this->conn->rollBack();
}
}
/**
* Open the database connection
*/
public function __construct(){
// open database connection
$connectionString = sprintf("mysql:host=%s;dbname=%s",
TransactionDemo::DB_HOST,
TransactionDemo::DB_NAME);
try {
$this->conn = new PDO($connectionString,
TransactionDemo::DB_USER,
TransactionDemo::DB_PASSWORD);
} catch (PDOException $pe) {
die($pe->getMessage());
}
}
/**
* close the database connection
*/
public function __destruct() {
// close the database connection
$this->conn = null;
}
}
// working with procedures in php //
function call_sp($customerNumber)
{
try {
$conn = new PDO("mysql:host=localhost;dbname=classicmodels", 'root', '');
// execute the stored procedure
$sql = 'CALL get_order_by_cust(:no,@shipped,@canceled,@resolved,@disputed)';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':no', $customerNumber, PDO::PARAM_INT);
$stmt->execute();
$stmt->closeCursor();
// execute the second query to get values from OUT parameter
$r = $conn->query("SELECT @shipped,@canceled,@resolved,@disputed")
->fetch(PDO::FETCH_ASSOC);
if ($r) {
printf('Shipped: %d, Canceled: %d, Resolved: %d, Disputed: %d',
$r['@shipped'],
$r['@canceled'],
$r['@resolved'],
$r['@disputed']);
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
}
call_sp(141);
//in mysql - creating a proc //
DELIMITER $$
CREATE PROCEDURE get_order_by_cust(
IN cust_no INT,
OUT shipped INT,
OUT canceled INT,
OUT resolved INT,
OUT disputed INT)
BEGIN
-- shipped
SELECT
count(*) INTO shipped
FROM
orders
WHERE
customerNumber = cust_no
AND status = 'Shipped';
-- canceled
SELECT
count(*) INTO canceled
FROM
orders
WHERE
customerNumber = cust_no
AND status = 'Canceled';
-- resolved
SELECT
count(*) INTO resolved
FROM
orders
WHERE
customerNumber = cust_no
AND status = 'Resolved';
-- disputed
SELECT
count(*) INTO disputed
FROM
orders
WHERE
customerNumber = cust_no
AND status = 'Disputed';
END
//calling a proc //
CALL get_order_by_cust(141,@shipped,@canceled,@resolved,@disputed);
SELECT @shipped,@canceled,@resolved,@disputed;
//creating triggers //
//using conditions in mysql //
SELECT
customerName, state, country
FROM
customers
ORDER BY (CASE
WHEN state IS NULL THEN country
ELSE state
END);
2
3
4
5
6
7
SELECT
customerNumber,
customerName,
IF(state IS NULL, 'N/A', state) state,
country
FROM
customers;
//using concat //
SELECT
productname,
CONCAT('$',
FORMAT(quantityInStock * buyPrice, 2)) stock_value
FROM
products;
//pdo persistent connection //
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass, array(
PDO::ATTR_PERSISTENT => true));
//triggers in ms sql //
now.column = inserted
old.column = deleted
//trigger sample for ms sql server //
USE MSSQLTips;
GO
CREATE TABLE dbo.SampleTable (
SampleTableID INT NOT NULL IDENTITY(1,1),
SampleTableInt INT NOT NULL,
SampleTableChar CHAR(5) NOT NULL,
SampleTableVarChar VARCHAR(30) NOT NULL,
CONSTRAINT PK_SampleTable PRIMARY KEY CLUSTERED (SampleTableID)
);
GO
CREATE TABLE dbo.SampleTable_Audit (
SampleTableID INT NOT NULL,
SampleTableInt INT NOT NULL,
SampleTableChar CHAR(5) NOT NULL,
SampleTableVarChar VARCHAR(30) NOT NULL,
Operation CHAR(1) NOT NULL,
TriggerTable CHAR(1) NOT NULL,
AuditDateTime smalldatetime NOT NULL DEFAULT GETDATE(),
);
CREATE INDEX IDX_SampleTable_Audit_AuditDateTime ON dbo.SampleTable_Audit (AuditDateTime DESC);
GO
CREATE TRIGGER dbo.SampleTable_InsertTrigger
ON dbo.SampleTable
FOR INSERT
AS
BEGIN
INSERT INTO dbo.SampleTable_Audit
(SampleTableID, SampleTableInt, SampleTableChar, SampleTableVarChar, Operation, TriggerTable)
SELECT SampleTableID, SampleTableInt, SampleTableChar, SampleTableVarChar, 'I', 'I'
FROM inserted;
END;
GO
CREATE TRIGGER dbo.SampleTable_DeleteTrigger
ON dbo.SampleTable
FOR DELETE
AS
BEGIN
INSERT INTO dbo.SampleTable_Audit
(SampleTableID, SampleTableInt, SampleTableChar, SampleTableVarChar, Operation, TriggerTable)
SELECT SampleTableID, SampleTableInt, SampleTableChar, SampleTableVarChar, 'D', 'D'
FROM deleted;
END;
GO
CREATE TRIGGER dbo.SampleTable_UpdateTrigger
ON dbo.SampleTable
FOR UPDATE
AS
BEGIN
INSERT INTO dbo.SampleTable_Audit
(SampleTableID, SampleTableInt, SampleTableChar, SampleTableVarChar, Operation, TriggerTable)
SELECT SampleTableID, SampleTableInt, SampleTableChar, SampleTableVarChar, 'U', 'D'
FROM deleted;--remember deleted is the old data --
INSERT INTO dbo.SampleTable_Audit
(SampleTableID, SampleTableInt, SampleTableChar, SampleTableVarChar, Operation, TriggerTable)
SELECT SampleTableID, SampleTableInt, SampleTableChar, SampleTableVarChar, 'U', 'I'
FROM inserted;
END;
GO
//asp parameter //
' Open Connection Conn
set ccmd = CreateObject("ADODB.Command")
ccmd.Activeconnection= Conn
ccmd.CommandText="SPWithParam"
ccmd.commandType = 4 'adCmdStoredProc
ccmd.parameters.Item("InParam").value = "hello world" ' input parameter
ccmd.execute()
' Access ccmd.parameters(0) as return value of stored procedure
' Access ccmd.parameters(2) or ccmd.parameters("OutParam") as the output parameter.