Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mappi-cursor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan Kuchař
mappi-cursor
Commits
755f6d36
Commit
755f6d36
authored
8 years ago
by
Jan Kuchař
Browse files
Options
Downloads
Patches
Plain Diff
CursorDriver: polished
parent
2c76cd15
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/PostgresDriver/CursorDriver.php
+39
-17
39 additions, 17 deletions
src/PostgresDriver/CursorDriver.php
tests/Store/PostgresDriver/CursorDriverTest.phpt
+15
-15
15 additions, 15 deletions
tests/Store/PostgresDriver/CursorDriverTest.phpt
with
54 additions
and
32 deletions
src/PostgresDriver/CursorDriver.php
+
39
−
17
View file @
755f6d36
...
...
@@ -6,14 +6,14 @@
namespace
Grifart\Mappi\Store\PostgresDriver
;
use
Dibi\Connection
;
use
Dibi\Row
;
/**
* PostgreSQL cursor driver
*
* Basic envelope over PostgreSQL's cursor. This cursor envelope does not know
* cursor position, thus cannot do boundary checks. If you reach end, you
* simply
* get no more values. You can check if you reached the end by
* simply get no more values. You can check if you reached the end by
* `isHeadOnRecord()`.
*
* @package Grifart\Mappi\Store\PostgresDriver
...
...
@@ -22,10 +22,12 @@ final class CursorDriver implements ICursorDriver
{
/** @var Connection */
private
$connection
;
/** @var string */
private
$name
;
/** @var bool */
private
$headOnRecord
=
false
;
private
$headOnRecord
=
FALSE
;
/**
* Warning: this class itself does not check if cursor is really valid in
...
...
@@ -41,7 +43,7 @@ final class CursorDriver implements ICursorDriver
{
$this
->
connection
=
$connection
;
$this
->
name
=
$name
;
// todo: close; for now automatically closed on transaction end
// todo: close; for now automatically closed on transaction end
; maybe in destructor? Ignore errors?
}
public
function
getConnection
()
:
Connection
...
...
@@ -118,40 +120,60 @@ final class CursorDriver implements ICursorDriver
if
(
$rows
===
0
)
{
$this
->
headOnRecord
=
count
(
$recs
)
===
1
;
}
else
{
}
else
{
// todo: test this properly!!
$this
->
headOnRecord
=
count
(
$recs
)
===
abs
(
$rows
);
}
// todo: DibiRecord to array
return
$recs
;
return
$this
->
convertRows
(
$recs
)
;
}
public
function
fetchOneAt
(
int
$index
)
:
array
{
$r
ecord
=
$this
->
connection
->
query
(
$r
ow
=
$this
->
connection
->
query
(
"FETCH ABSOLUTE %i FROM %n"
,
$index
,
$this
->
getName
()
)
->
fetch
();
$this
->
headOnRecord
=
$r
ecord
!==
false
;
if
(
$r
ecord
===
false
)
{
return
null
;
$this
->
headOnRecord
=
$r
ow
!==
FALSE
;
if
(
$r
ow
===
FALSE
)
{
return
NULL
;
}
return
$
record
->
toArray
(
);
return
$
this
->
convertRow
(
$row
);
}
public
function
fetchOneBy
(
int
$rows
)
:
array
{
$r
ecord
=
$this
->
connection
->
query
(
$r
ow
=
$this
->
connection
->
query
(
"FETCH RELATIVE %i FROM %n"
,
$rows
,
$this
->
getName
()
)
->
fetch
();
$this
->
headOnRecord
=
$record
!==
false
;
if
(
$record
===
false
)
{
return
null
;
$this
->
headOnRecord
=
$row
!==
FALSE
;
if
(
$row
===
FALSE
)
{
return
NULL
;
}
return
$this
->
convertRow
(
$row
);
}
// ----------- DATA CONVERSION -----------
private
function
convertRow
(
Row
$row
)
:
array
{
return
$row
->
toArray
();
}
/**
* @param Row[] $rows
* @return array
*/
private
function
convertRows
(
array
$rows
)
:
array
{
// TODO: TrackedCursor should return correct index values in array indexes
$new
=
[];
foreach
(
$rows
as
$row
)
{
$new
[]
=
$this
->
convertRow
(
$row
);
}
return
$
record
->
toArray
()
;
return
$
new
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/Store/PostgresDriver/CursorDriverTest.phpt
+
15
−
15
View file @
755f6d36
...
...
@@ -87,7 +87,7 @@ class CursorDriverTest extends BaseTest
$result
=
$this
->
uut
->
fetchRange
(
0
);
// rows backwards + read current
Assert
::
count
(
1
,
$result
);
Assert
::
same
(
3
,
current
(
$result
[
0
]
->
toArray
()
));
Assert
::
same
(
3
,
current
(
$result
[
0
]));
}
// fetch( x > 0 )
...
...
@@ -106,8 +106,8 @@ class CursorDriverTest extends BaseTest
// todo: split into fetch decorator for those special fetch* methods
Assert
::
count
(
2
,
$result
);
Assert
::
same
(
4
,
current
(
$result
[
0
]
->
toArray
()
));
Assert
::
same
(
5
,
current
(
$result
[
1
]
->
toArray
()
));
Assert
::
same
(
4
,
current
(
$result
[
0
]));
Assert
::
same
(
5
,
current
(
$result
[
1
]));
Assert
::
same
(
5
,
$this
->
helper_fetchCurrentSingle
());
}
...
...
@@ -127,8 +127,8 @@ class CursorDriverTest extends BaseTest
// todo: split into fetch decorator for those special fetch* methods
Assert
::
count
(
2
,
$result
);
Assert
::
same
(
2
,
current
(
$result
[
0
]
->
toArray
()
));
Assert
::
same
(
1
,
current
(
$result
[
1
]
->
toArray
()
));
Assert
::
same
(
2
,
current
(
$result
[
0
]));
Assert
::
same
(
1
,
current
(
$result
[
1
]));
Assert
::
same
(
1
,
$this
->
helper_fetchCurrentSingle
());
}
...
...
@@ -223,11 +223,11 @@ class CursorDriverTest extends BaseTest
$result
=
$this
->
uut
->
fetchRange
(
ICursorDriver
::
FETCH_REMAINING
);
Assert
::
count
(
5
,
$result
);
Assert
::
same
(
996
,
current
(
$result
[
0
]
->
toArray
()
));
Assert
::
same
(
997
,
current
(
$result
[
1
]
->
toArray
()
));
Assert
::
same
(
998
,
current
(
$result
[
2
]
->
toArray
()
));
Assert
::
same
(
999
,
current
(
$result
[
3
]
->
toArray
()
));
Assert
::
same
(
1000
,
current
(
$result
[
4
]
->
toArray
()
));
Assert
::
same
(
996
,
current
(
$result
[
0
]));
Assert
::
same
(
997
,
current
(
$result
[
1
]));
Assert
::
same
(
998
,
current
(
$result
[
2
]));
Assert
::
same
(
999
,
current
(
$result
[
3
]));
Assert
::
same
(
1000
,
current
(
$result
[
4
]));
Assert
::
null
(
$this
->
helper_fetchCurrentSingle
());
Assert
::
equal
(
1000
,
$this
->
helper_fetchPrevSingle
());
...
...
@@ -243,11 +243,11 @@ class CursorDriverTest extends BaseTest
$result
=
$this
->
uut
->
fetchRange
(
ICursorDriver
::
FETCH_FOREGOING
);
Assert
::
count
(
5
,
$result
);
Assert
::
same
(
5
,
current
(
$result
[
0
]
->
toArray
()
));
Assert
::
same
(
4
,
current
(
$result
[
1
]
->
toArray
()
));
Assert
::
same
(
3
,
current
(
$result
[
2
]
->
toArray
()
));
Assert
::
same
(
2
,
current
(
$result
[
3
]
->
toArray
()
));
Assert
::
same
(
1
,
current
(
$result
[
4
]
->
toArray
()
));
Assert
::
same
(
5
,
current
(
$result
[
0
]));
Assert
::
same
(
4
,
current
(
$result
[
1
]));
Assert
::
same
(
3
,
current
(
$result
[
2
]));
Assert
::
same
(
2
,
current
(
$result
[
3
]));
Assert
::
same
(
1
,
current
(
$result
[
4
]));
Assert
::
null
(
$this
->
helper_fetchCurrentSingle
());
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment