Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
PHP-MySQLi-Database-Class
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
PHP-MySQLi-Database-Class
Commits
97c09d9f
Commit
97c09d9f
authored
May 10, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Query execution tracing functions added
parent
6f810c66
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
2 deletions
+74
-2
MysqliDb.php
MysqliDb.php
+41
-0
readme.md
readme.md
+28
-0
tests.php
tests.php
+5
-2
No files found.
MysqliDb.php
View file @
97c09d9f
...
...
@@ -112,6 +112,15 @@ class MysqliDb
*/
protected
$isSubQuery
=
false
;
/**
* Variables for query execution tracing
*
*/
protected
$traceStartQ
;
protected
$traceEnabled
;
protected
$traceStripPrefix
;
public
$trace
=
array
();
/**
* @param string $host
* @param string $username
...
...
@@ -192,6 +201,9 @@ class MysqliDb
*/
protected
function
reset
()
{
if
(
$this
->
traceEnabled
)
$this
->
trace
[]
=
array
(
$this
->
_lastQuery
,
(
microtime
(
true
)
-
$this
->
traceStartQ
)
,
$this
->
_traceGetCaller
());
$this
->
_where
=
array
();
$this
->
_join
=
array
();
$this
->
_orderBy
=
array
();
...
...
@@ -968,6 +980,9 @@ class MysqliDb
if
(
!
$stmt
=
$this
->
_mysqli
->
prepare
(
$this
->
_query
))
{
trigger_error
(
"Problem preparing query (
$this->_query
) "
.
$this
->
_mysqli
->
error
,
E_USER_ERROR
);
}
if
(
$this
->
traceEnabled
)
$this
->
traceStartQ
=
microtime
(
true
);
return
$stmt
;
}
...
...
@@ -1204,5 +1219,31 @@ class MysqliDb
return
;
$this
->
rollback
();
}
/**
* Query exection time tracking switch
*
* @param bool $enabled Enable execution time tracking
* @param string $stripPrefix Prefix to strip from the path in exec log
**/
public
function
setTrace
(
$enabled
,
$stripPrefix
=
null
)
{
$this
->
traceEnabled
=
$enabled
;
$this
->
traceStripPrefix
=
$stripPrefix
;
return
$this
;
}
/**
* Get where and what function was called for query stored in MysqliDB->trace
*
* @return string with information
*/
private
function
_traceGetCaller
()
{
$dd
=
debug_backtrace
();
$caller
=
next
(
$dd
);
while
(
isset
(
$caller
)
&&
$caller
[
"file"
]
==
__FILE__
)
$caller
=
next
(
$dd
);
return
__CLASS__
.
"->"
.
$caller
[
"function"
]
.
"() >> file
\"
"
.
str_replace
(
$this
->
traceStripPrefix
,
''
,
$caller
[
"file"
]
)
.
"
\"
line #"
.
$caller
[
"line"
]
.
" "
;
}
}
// END class
?>
readme.md
View file @
97c09d9f
...
...
@@ -474,3 +474,31 @@ if (!$db->insert ('myTable', $insertData)) {
$db
->
commit
();
}
```
### Query exectution time benchmarking
To track query execution time setTrace() function should be called.
```
php
$db
->
setTrace
(
true
);
// As a second parameter it is possible to define prefix of the path which should be striped from filename
// $db->setTrace (true, $_SERVER['SERVER_ROOT']);
$db
->
get
(
"users"
);
$db
->
get
(
"test"
);
print_r
(
$db
->
trace
);
```
```
[0] => Array
(
[0] => SELECT * FROM t_users ORDER BY `id` ASC
[1] => 0.0010669231414795
[2] => MysqliDb->get() >> file "/avb/work/PHP-MySQLi-Database-Class/tests.php" line #151
)
[1] => Array
(
[0] => SELECT * FROM t_test
[1] => 0.00069189071655273
[2] => MysqliDb->get() >> file "/avb/work/PHP-MySQLi-Database-Class/tests.php" line #152
)
```
tests.php
View file @
97c09d9f
...
...
@@ -5,6 +5,9 @@ error_reporting(E_ALL);
$db
=
new
Mysqlidb
(
'localhost'
,
'root'
,
''
,
'testdb'
);
if
(
!
$db
)
die
(
"Database error"
);
$mysqli
=
new
mysqli
(
'localhost'
,
'root'
,
''
,
'testdb'
);
$db
=
new
Mysqlidb
(
$mysqli
);
$db
=
new
Mysqlidb
(
Array
(
'host'
=>
'localhost'
,
'username'
=>
'root'
,
...
...
@@ -13,11 +16,10 @@ $db = new Mysqlidb(Array (
'charset'
=>
null
));
if
(
!
$db
)
die
(
"Database error"
);
$mysqli
=
new
mysqli
(
'localhost'
,
'root'
,
''
,
'testdb'
);
$db
=
new
Mysqlidb
(
$mysqli
);
$prefix
=
't_'
;
$db
->
setPrefix
(
$prefix
);
$db
->
setTrace
(
true
);
$tables
=
Array
(
'users'
=>
Array
(
...
...
@@ -360,4 +362,5 @@ echo "All done";
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
print_r
(
$db
->
trace
);
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment