Skip to content
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

Merged
merged 15 commits into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions daos/mysql/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 ensureRowTypes().

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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/

}
}
8 changes: 8 additions & 0 deletions daos/mysql/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ public function stats() {
'.$this->stmt->sumBool('unread').' AS unread,
'.$this->stmt->sumBool('starred').' AS starred
FROM '.\F3::get('db_prefix').'items;');
$this->ensureRowTypes(array('total' => \PDO::PARAM_INT,
'unread' => \PDO::PARAM_INT,
'starred' => \PDO::PARAM_INT),
$res);
return $res[0];
}

Expand Down Expand Up @@ -504,6 +508,10 @@ public function statuses($since) {
FROM '.\F3::get('db_prefix').'items
WHERE '.\F3::get('db_prefix').'items.updatetime > :since;',
array(':since' => array($since, \PDO::PARAM_STR)));
$this->ensureRowTypes(array('id' => \PDO::PARAM_INT,
'unread' => \PDO::PARAM_BOOL,
'starred' => \PDO::PARAM_BOOL),
$res);
return $res;
}
}
14 changes: 14 additions & 0 deletions daos/pgsql/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,18 @@ public function __construct() {
public function optimize() {
\F3::get('db')->exec("VACUUM ANALYZE");
}


/**
* 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) {
return $rows; // pgsql returns correct PHP types
}
}