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
8479651d
Commit
8479651d
authored
Jan 02, 2018
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add key/value options to DbResult, other refactoring
parent
9cbf09d4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
17 deletions
+48
-17
DbConnection.php
qa-src/Database/DbConnection.php
+1
-1
DbResult.php
qa-src/Database/DbResult.php
+47
-16
No files found.
qa-src/Database/DbConnection.php
View file @
8479651d
...
...
@@ -42,7 +42,7 @@ class DbConnection
if
(
defined
(
'QA_FINAL_WORDPRESS_INTEGRATE_PATH'
))
{
// Wordpress allows setting port inside DB_HOST constant, like 127.0.0.1:3306
$hostAndPort
=
explode
(
':'
,
$
host
);
$hostAndPort
=
explode
(
':'
,
$
this
->
config
[
'host'
]
);
if
(
count
(
$hostAndPort
)
>=
2
)
{
$this
->
config
[
'host'
]
=
$hostAndPort
[
0
];
$this
->
config
[
'port'
]
=
$hostAndPort
[
1
];
...
...
qa-src/Database/DbResult.php
View file @
8479651d
...
...
@@ -30,35 +30,46 @@ class DbResult
$this
->
stmt
=
$stmt
;
}
public
function
fetchNextAssoc
()
/**
* Return the first row from the results as an array of [column name] => [column value].
* @param bool $allowempty If false, throw a fatal error if there is no result.
* @return array|null
*/
public
function
fetchNextAssoc
(
$allowempty
=
false
)
{
return
$this
->
stmt
->
fetch
(
PDO
::
FETCH_ASSOC
);
return
$this
->
handleResult
(
$this
->
stmt
->
fetch
(
PDO
::
FETCH_ASSOC
),
$allowempty
,
'Reading one assoc from invalid result'
);
}
public
function
fetchAllAssoc
()
/**
* Return the data as an associative array.
* @param string $key Unique field to use as the array key for each row.
* @param string $value Field to use as the value (for key=>value format instead of the default key=>row)
* @return array
*/
public
function
fetchAllAssoc
(
$key
=
null
,
$value
=
null
)
{
return
$this
->
stmt
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
$data
=
array
();
while
((
$row
=
$this
->
stmt
->
fetch
(
PDO
::
FETCH_ASSOC
))
!==
false
)
{
if
(
isset
(
$key
))
{
$data
[
$row
[
$key
]]
=
isset
(
$value
)
?
$row
[
$value
]
:
$row
;
}
else
{
$data
[]
=
isset
(
$value
)
?
$row
[
$value
]
:
$row
;
}
}
return
$data
;
}
/**
* Return a specific cell from the results. Typically used with (single-row, single-column) aggregate queries.
* @param $col 0-indexed column to select from (defaults to first column).
* @param bool $allowempty If false, throw a fatal error if there is no result.
* @return
mixed
|null
* @return
array
|null
*/
public
function
fetchOneValue
(
$col
=
0
,
$allowempty
=
false
)
{
$data
=
$this
->
stmt
->
fetchColumn
(
$col
);
if
(
$data
!==
false
)
{
return
$data
;
}
if
(
!
$allowempty
)
{
qa_fatal_error
(
'Reading one value from empty results'
);
}
return
null
;
return
$this
->
handleResult
(
$this
->
stmt
->
fetchColumn
(
$col
),
$allowempty
,
'Reading one value from empty results'
);
}
/**
...
...
@@ -72,6 +83,26 @@ class DbResult
}
/**
* Return data or optionally fail if no more rows.
* @param array $data
* @param bool $allowempty
* @param string $errorMsg
* @return mixed
*/
private
function
handleResult
(
$data
,
$allowempty
,
$errorMsg
)
{
if
(
$data
!==
false
)
{
return
$data
;
}
if
(
!
$allowempty
)
{
qa_fatal_error
(
$errorMsg
);
}
return
null
;
}
/**
* Number of rows found (SELECT queries) or rows affected (UPDATE/INSERT/DELETE queries).
* @return int
*/
...
...
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