/
example.php
123 lines (97 loc) · 2.6 KB
/
example.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
<?php
// Require the DB Class
require_once('classes/DB.php');
// Setup a Connection (saving it is optional)
DB::MySQL('localhost','root','')->saveConnectionAs('default');
DB::MySQL()->profilerStart();
// Find Using Primary Key
$a = DB::MySQL()->db('DBAL')->table('test')->find(1);
echo $a->name . '<hr />';
// Find Using Magic Find Method
$a = DB::MySQL()->db('DBAL')->table('test')->findByName('Bob');
echo $a[0]->name . '<hr />';
// Select a Database, Table, and Query It
$a = DB::MySQL()->db('DBAL')->table('test')->select()->where('1=1')->fetchOne(); // fetchOne retains state
// Show the Query SQL
echo $a->querySQL();
echo '<hr />';
// Get a Property
echo $a->name;
echo '<hr />';
// Change Value
$a->name = 'Mikes';
// Save
$a->save();
// Find out what changed
var_dump($a->changedData());
echo '<hr />';
// Get Old Data
var_dump($a->oldData());
echo '<hr />';
// Affected Rows
echo "Affected Rows: " . $a->affectedRows();
echo '<hr />';
// Switch Database, ReQuery
$b = $a->db('DBAL2')->table('cow')->select()->fetchOne();
// Get Property
echo $b->name;
// Edit Value
$b->name = 'Rogers';
// Save
$b->save();
// Here's what just happened:
echo $b->querySql();
echo '<hr />';
// Ok, let's get all the records from the cow database
$c = $b->limit(0)->fetchAll();
// And the count of $b
echo "Records: " . $b->count();
echo '<hr />';
// And Our Query :)
echo $b->querySQL();
echo '<hr />';
// Now let's loop through and add 's' to everyone's name and echo the query
foreach($c as $v) {
echo $v->name . '<br />';
$v->name .= 's';
$v->save();
echo $v->querySql();
echo '<hr />';
}
// Now let's add a name
DB::MySQL()->table('cow')->insert(array('name' => 'Mike'))->save();
DB::MySQL()->profilerEnd();
echo '<hr />';
echo 'LARGE QUERIES:<br />';
echo 'QUERY | TIME TAKEN | MEMORY USED<br />';
foreach(DB::MySQL()->profilerShowLargerThan(1000) as $profile) {
echo $profile->getQuery();
echo ' | ';
echo $profile->getExecutionTime();
echo ' | ';
echo $profile->getMemoryUsed();
echo '<br />';
}
echo '<hr />';
echo 'LONG QUERIES:<br />';
echo 'QUERY | TIME TAKEN | MEMORY USED<br />';
foreach(DB::MySQL()->profilerShowLongerThan(.01) as $profile) {
echo $profile->getQuery();
echo ' | ';
echo $profile->getExecutionTime();
echo ' | ';
echo $profile->getMemoryUsed();
echo '<br />';
}
echo '<hr />';
echo 'ALL QUERIES:<br />';
echo 'QUERY | TIME TAKEN | MEMORY USED<br />';
foreach(DB::MySQL()->profilerShow() as $profile) {
echo $profile->getQuery();
echo ' | ';
echo $profile->getExecutionTime();
echo ' | ';
echo $profile->getMemoryUsed();
echo '<br />';
}
?>