-
Notifications
You must be signed in to change notification settings - Fork 345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sync client from server using last update timestamp #846
Changes from all commits
7e1e575
3946683
6a2667b
9c781c0
20f740b
e9e69f8
3071660
6235fcb
31ca9e6
1b820f7
21de209
a20e83b
85196bd
7fea6c9
795fccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,4 +229,37 @@ public function __construct() { | |
public function optimize() { | ||
@\F3::get('db')->exec('OPTIMIZE TABLE `'.\F3::get('db_prefix').'sources`, `'.\F3::get('db_prefix').'items`'); | ||
} | ||
|
||
|
||
/** | ||
* Ensure row values have the appropriate PHP type. This assumes we are | ||
* using buffered queries (sql results are in PHP memory). | ||
* | ||
* @param expectedRowTypes associative array mapping columns to PDO types | ||
* @param rows array of associative array representing row results | ||
* @return array of associative array representing row results having | ||
* expected types | ||
*/ | ||
public function ensureRowTypes($expectedRowTypes, $rows) { | ||
foreach($rows as $rowIndex => $row) { | ||
foreach($expectedRowTypes as $column => $type) { | ||
if( array_key_exists($column, $row) ) { | ||
switch($type) { | ||
case \PDO::PARAM_INT: | ||
$value = intval($row[$column]); | ||
break; | ||
case \PDO::PARAM_BOOL: | ||
if( $row[$column] == "1" ) | ||
$value = true; | ||
else | ||
$value = false; | ||
break; | ||
} | ||
// $row is only a reference, so we change $rows[$rowIndex] | ||
$rows[$rowIndex][$column] = $value; | ||
} | ||
} | ||
} | ||
return $rows; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you return the value though it is mutated in-place and the returned value is never used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it might be more handy sometimes to directly return the call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be fair, I would prefer not using reference at all, unless the performance benefits are significant. Ideally, PHP engine would be smart enough to optimize it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While working on this, I though about this being used on big SQL results and I red about PHP arrays passed by value being duplicated in memory by the PHP engine when modified within the function. I wanted to prevent this. It is trivial to remove though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, I would remove the returns, if we need them in the future, they can always be re-added. Though, in the long term I would like to drop this altogether and use some abstraction like https://nextras.org/orm/ |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -476,6 +476,42 @@ public function stats() { | |
'.$this->stmt->sumBool('unread').' AS unread, | ||
'.$this->stmt->sumBool('starred').' AS starred | ||
FROM '.\F3::get('db_prefix').'items;'); | ||
$res = $this->ensureRowTypes(array('total' => \PDO::PARAM_INT, | ||
'unread' => \PDO::PARAM_INT, | ||
'starred' => \PDO::PARAM_INT), | ||
$res); | ||
return $res[0]; | ||
} | ||
|
||
|
||
/** | ||
* returns the datetime of the last item update or user action in db | ||
* | ||
* @return timestamp | ||
*/ | ||
public function lastUpdate() { | ||
$res = \F3::get('db')->exec('SELECT | ||
MAX(updatetime) AS last_update_time | ||
FROM '.\F3::get('db_prefix').'items;'); | ||
return $res[0]['last_update_time']; | ||
} | ||
|
||
|
||
/** | ||
* returns the statuses of items last update | ||
* | ||
* @param date since to return item statuses | ||
* @return array of unread, starred, etc. status of specified items | ||
*/ | ||
public function statuses($since) { | ||
$res = \F3::get('db')->exec('SELECT id, unread, starred | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to use proper booleans? Otherwise the sync will mark all changed items as starred and unread since unlike in PHP $ php -r 'var_dump(!!"0");'
Command line code:1:
bool(false)
$ node -e 'console.log(!!"0")'
true
|
||
FROM '.\F3::get('db_prefix').'items | ||
WHERE '.\F3::get('db_prefix').'items.updatetime > :since;', | ||
array(':since' => array($since, \PDO::PARAM_STR))); | ||
$res = $this->ensureRowTypes(array('id' => \PDO::PARAM_INT, | ||
'unread' => \PDO::PARAM_BOOL, | ||
'starred' => \PDO::PARAM_BOOL), | ||
$res); | ||
return $res; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now this is almost like GraphQL. Would not it be better to create a GraphQL endpoint? It could also save a lot requests during initial connection.