Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
outils
question2answer
Commits
3d604bff
Commit
3d604bff
authored
Dec 30, 2017
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add docBlock and use statements
parent
ffb29023
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
5 deletions
+41
-5
DbConnection.php
qa-src/Database/DbConnection.php
+41
-5
No files found.
qa-src/Database/DbConnection.php
View file @
3d604bff
...
@@ -18,6 +18,9 @@
...
@@ -18,6 +18,9 @@
namespace
Q2A\Database
;
namespace
Q2A\Database
;
use
PDO
;
use
PDOException
;
class
DbConnection
class
DbConnection
{
{
protected
$pdo
;
protected
$pdo
;
...
@@ -47,11 +50,21 @@ class DbConnection
...
@@ -47,11 +50,21 @@ class DbConnection
}
}
}
}
/**
* Indicates to the Q2A database layer that database connections are permitted from this point forwards (before
* this point, some plugins may not have had a chance to override some database access functions).
*/
public
function
allowConnect
()
public
function
allowConnect
()
{
{
$this
->
allowConnect
=
true
;
$this
->
allowConnect
=
true
;
}
}
/**
* Connect to the Q2A database, optionally install the $failhandler (and call it if necessary). Uses PDO as of Q2A
* 1.9.
* @param string $failhandler
* @return mixed|void
*/
public
function
connect
(
$failHandler
=
null
)
public
function
connect
(
$failHandler
=
null
)
{
{
if
(
!
$this
->
allowConnect
)
{
if
(
!
$this
->
allowConnect
)
{
...
@@ -71,20 +84,28 @@ class DbConnection
...
@@ -71,20 +84,28 @@ class DbConnection
$dsn
.=
';port='
.
$this
->
config
[
'port'
];
$dsn
.=
';port='
.
$this
->
config
[
'port'
];
}
}
$options
=
array
(
\PDO
::
ATTR_ERRMODE
=>
\
PDO
::
ERRMODE_EXCEPTION
);
$options
=
array
(
PDO
::
ATTR_ERRMODE
=>
PDO
::
ERRMODE_EXCEPTION
);
if
(
QA_PERSISTENT_CONN_DB
)
{
if
(
QA_PERSISTENT_CONN_DB
)
{
$options
[
\
PDO
::
ATTR_PERSISTENT
]
=
true
;
$options
[
PDO
::
ATTR_PERSISTENT
]
=
true
;
}
}
try
{
try
{
$this
->
pdo
=
new
\
PDO
(
$dsn
,
$this
->
config
[
'username'
],
$this
->
config
[
'password'
],
$options
);
$this
->
pdo
=
new
PDO
(
$dsn
,
$this
->
config
[
'username'
],
$this
->
config
[
'password'
],
$options
);
}
catch
(
\
PDOException
$ex
)
{
}
catch
(
PDOException
$ex
)
{
$this
->
failError
(
'connect'
,
$ex
->
getCode
(),
$ex
->
getMessage
());
$this
->
failError
(
'connect'
,
$ex
->
getCode
(),
$ex
->
getMessage
());
}
}
qa_report_process_stage
(
'db_connected'
);
qa_report_process_stage
(
'db_connected'
);
}
}
/**
* If a DB error occurs, call the installed fail handler (if any) otherwise report error and exit immediately.
* @param $type
* @param int $errno
* @param string $error
* @param string $query
* @return mixed
*/
public
function
failError
(
$type
,
$errno
=
null
,
$error
=
null
,
$query
=
null
)
public
function
failError
(
$type
,
$errno
=
null
,
$error
=
null
,
$query
=
null
)
{
{
@
error_log
(
'PHP Question2Answer MySQL '
.
$type
.
' error '
.
$errno
.
': '
.
$error
.
(
isset
(
$query
)
?
(
' - Query: '
.
$query
)
:
''
));
@
error_log
(
'PHP Question2Answer MySQL '
.
$type
.
' error '
.
$errno
.
': '
.
$error
.
(
isset
(
$query
)
?
(
' - Query: '
.
$query
)
:
''
));
...
@@ -100,6 +121,12 @@ class DbConnection
...
@@ -100,6 +121,12 @@ class DbConnection
}
}
}
}
/**
* Prepare and execute a SQL query, handling any failures. In debugging mode, track the queries and resources used.
* @param string $query
* @param array $params
* @return PDOStatement
*/
public
function
query
(
$query
,
$params
=
array
())
public
function
query
(
$query
,
$params
=
array
())
{
{
try
{
try
{
...
@@ -122,11 +149,20 @@ class DbConnection
...
@@ -122,11 +149,20 @@ class DbConnection
}
else
{
}
else
{
$stmt
=
$this
->
execute
(
$query
,
$params
);
$stmt
=
$this
->
execute
(
$query
,
$params
);
}
}
}
catch
(
\PDOException
$ex
)
{
return
$stmt
;
}
catch
(
PDOException
$ex
)
{
$this
->
failError
(
'query'
,
$ex
->
getCode
(),
$ex
->
getMessage
(),
$query
);
$this
->
failError
(
'query'
,
$ex
->
getCode
(),
$ex
->
getMessage
(),
$query
);
}
}
}
}
/**
* Lower-level function to prepare and execute a SQL query. Automatically retries if there is a MySQL deadlock
* error.
* @param string $query
* @param array $params
* @return PDOStatement
*/
protected
function
execute
(
$query
,
$params
=
array
())
protected
function
execute
(
$query
,
$params
=
array
())
{
{
$stmt
=
$this
->
pdo
->
prepare
(
$query
);
$stmt
=
$this
->
pdo
->
prepare
(
$query
);
...
...
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