Skip to content

Commit

Permalink
Merge pull request #532 from yDelouis/restUpdatedItems
Browse files Browse the repository at this point in the history
REST API : Get only items updated since given time
  • Loading branch information
SSilence committed Jul 4, 2014
2 parents af32244 + b257414 commit 259fd53
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 13 deletions.
44 changes: 41 additions & 3 deletions daos/mysql/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct() {
foreach($table as $key=>$value)
$tables[] = $value;

if(!in_array(\F3::get('db_prefix').'items', $tables))
if(!in_array(\F3::get('db_prefix').'items', $tables)) {
\F3::get('db')->exec('
CREATE TABLE '.\F3::get('db_prefix').'items (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
Expand All @@ -56,9 +56,25 @@ public function __construct() {
source INT NOT NULL ,
uid VARCHAR(255) NOT NULL,
link TEXT NOT NULL,
updatetime DATETIME NOT NULL,
INDEX (source)
) ENGINE = MYISAM DEFAULT CHARSET=utf8;
');
\F3::get('db')->exec('
CREATE TRIGGER insert_updatetime_trigger
BEFORE INSERT ON items FOR EACH ROW
BEGIN
SET NEW.updatetime = NOW();
END;
');
\F3::get('db')->exec('
CREATE TRIGGER update_updatetime_trigger
BEFORE UPDATE ON items FOR EACH ROW
BEGIN
SET NEW.updatetime = NOW();
END;
');
}

$isNewestSourcesTable = false;
if(!in_array(\F3::get('db_prefix').'sources', $tables)) {
Expand All @@ -85,7 +101,7 @@ public function __construct() {
');

\F3::get('db')->exec('
INSERT INTO '.\F3::get('db_prefix').'version (version) VALUES (3);
INSERT INTO '.\F3::get('db_prefix').'version (version) VALUES (4);
');

\F3::get('db')->exec('
Expand All @@ -105,14 +121,36 @@ public function __construct() {
$version = @\F3::get('db')->exec('SELECT version FROM '.\F3::get('db_prefix').'version ORDER BY version DESC LIMIT 0, 1');
$version = $version[0]['version'];

if($version == "2"){
if(strnatcmp($version, "3") < 0){
\F3::get('db')->exec('
ALTER TABLE '.\F3::get('db_prefix').'sources ADD lastupdate INT;
');
\F3::get('db')->exec('
INSERT INTO '.\F3::get('db_prefix').'version (version) VALUES (3);
');
}
if(strnatcmp($version, "4") < 0){
\F3::get('db')->exec('
ALTER TABLE '.\F3::get('db_prefix').'items ADD updatetime DATETIME;
');
\F3::get('db')->exec('
CREATE TRIGGER insert_updatetime_trigger
BEFORE INSERT ON items FOR EACH ROW
BEGIN
SET NEW.updatetime = NOW();
END;
');
\F3::get('db')->exec('
CREATE TRIGGER update_updatetime_trigger
BEFORE UPDATE ON items FOR EACH ROW
BEGIN
SET NEW.updatetime = NOW();
END;
');
\F3::get('db')->exec('
INSERT INTO '.\F3::get('db_prefix').'version (version) VALUES (4);
');
}
}

// just initialize once
Expand Down
10 changes: 8 additions & 2 deletions daos/mysql/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ public function get($options = array()) {
$params[':source'] = array($options['source'], \PDO::PARAM_INT);
$where .= " AND items.source=:source ";
}


// update time filter
if(isset($options['updatedsince']) && strlen($options['updatedsince'])>0) {
$params[':updatedsince'] = array($options['updatedsince'], \PDO::PARAM_STR);
$where .= " AND items.updatetime > :updatedsince ";
}

// set limit
if(!is_numeric($options['items']) || $options['items']>200)
$options['items'] = \F3::get('items_perpage');
Expand All @@ -238,7 +244,7 @@ public function get($options = array()) {

// get items from database
return \F3::get('db')->exec('SELECT
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, sources.title as sourcetitle, sources.tags as tags
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, updatetime, sources.title as sourcetitle, sources.tags as tags
FROM '.\F3::get('db_prefix').'items AS items, '.\F3::get('db_prefix').'sources AS sources
WHERE items.source=sources.id '.$where.'
ORDER BY items.datetime '.$order.'
Expand Down
47 changes: 43 additions & 4 deletions daos/pgsql/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,29 @@ public function __construct() {
starred BOOLEAN NOT NULL,
source INTEGER NOT NULL,
uid TEXT NOT NULL,
link TEXT NOT NULL
link TEXT NOT NULL,
updatetime TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
');

\F3::get('db')->exec('
CREATE INDEX source ON items (
source
);
');
\F3::get('db')->exec('
CREATE OR REPLACE FUNCTION update_updatetime_procedure()
RETURNS TRIGGER AS $$
BEGIN
NEW.updatetime = NOW();
RETURN NEW;
END;
$$ LANGUAGE "plpgsql";
');
\F3::get('db')->exec('
CREATE TRIGGER update_updatetime_trigger
BEFORE UPDATE ON items FOR EACH ROW EXECUTE PROCEDURE
update_updatetime_procedure();
');
}

$isNewestSourcesTable = false;
Expand Down Expand Up @@ -98,7 +112,7 @@ public function __construct() {
');

\F3::get('db')->exec('
INSERT INTO version (version) VALUES (3);
INSERT INTO version (version) VALUES (4);
');

\F3::get('db')->exec('
Expand All @@ -118,14 +132,39 @@ public function __construct() {
$version = @\F3::get('db')->exec('SELECT version FROM version ORDER BY version DESC LIMIT 1');
$version = $version[0]['version'];

if($version == "2"){
if(strnatcmp($version, "3") < 0){
\F3::get('db')->exec('
ALTER TABLE sources ADD lastupdate INT;
');
\F3::get('db')->exec('
INSERT INTO version (version) VALUES (3);
');
}
if(strnatcmp($version, "4") < 0){
\F3::get('db')->exec('
ALTER TABLE items ADD updatetime TIMESTAMP WITH TIME ZONE;
');
\F3::get('db')->exec('
ALTER TABLE items ALTER COLUMN updatetime SET DEFAULT CURRENT_TIMESTAMP;
');
\F3::get('db')->exec('
CREATE OR REPLACE FUNCTION update_updatetime_procedure()
RETURNS TRIGGER AS $$
BEGIN
NEW.updatetime = NOW();
RETURN NEW;
END;
$$ LANGUAGE "plpgsql";
');
\F3::get('db')->exec('
CREATE TRIGGER update_updatetime_trigger
BEFORE UPDATE ON items FOR EACH ROW EXECUTE PROCEDURE
update_updatetime_procedure();
');
\F3::get('db')->exec('
INSERT INTO version (version) VALUES (4);
');
}
}

// just initialize once
Expand Down
8 changes: 7 additions & 1 deletion daos/pgsql/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public function get($options = array()) {
$params[':source'] = array($options['source'], \PDO::PARAM_INT);
$where .= " AND items.source=:source ";
}

// update time filter
if(isset($options['updatedsince']) && strlen($options['updatedsince'])>0) {
$params[':updatedsince'] = array($options['updatedsince'], \PDO::PARAM_STR);
$where .= " AND items.updatetime > :updatedsince ";
}

// set limit
if(!is_numeric($options['items']) || $options['items']>200)
Expand All @@ -65,7 +71,7 @@ public function get($options = array()) {

// get items from database
return \F3::get('db')->exec('SELECT
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, sources.title as sourcetitle, sources.tags as tags
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, updatetime, sources.title as sourcetitle, sources.tags as tags
FROM items, sources
WHERE items.source=sources.id '.$where.'
ORDER BY items.datetime '.$order.'
Expand Down
42 changes: 39 additions & 3 deletions daos/sqlite/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public function __construct() {
starred BOOL NOT NULL,
source INT NOT NULL,
uid VARCHAR(255) NOT NULL,
link TEXT NOT NULL
link TEXT NOT NULL,
updatetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
');

Expand All @@ -70,6 +71,15 @@ public function __construct() {
source
);
');
\F3::get('db')->exec('
CREATE TRIGGER update_updatetime_trigger
AFTER UPDATE ON items FOR EACH ROW
BEGIN
UPDATE items
SET updatetime = CURRENT_TIMESTAMP
WHERE id = NEW.id;
END;
');
}

$isNewestSourcesTable = false;
Expand Down Expand Up @@ -97,7 +107,7 @@ public function __construct() {
');

\F3::get('db')->exec('
INSERT INTO version (version) VALUES (3);
INSERT INTO version (version) VALUES (4);
');

\F3::get('db')->exec('
Expand All @@ -117,14 +127,40 @@ public function __construct() {
$version = @\F3::get('db')->exec('SELECT version FROM version ORDER BY version DESC LIMIT 0, 1');
$version = $version[0]['version'];

if($version == "2"){
if(strnatcmp($version, "3") < 0){
\F3::get('db')->exec('
ALTER TABLE sources ADD lastupdate INT;
');
\F3::get('db')->exec('
INSERT INTO version (version) VALUES (3);
');
}
if(strnatcmp($version, "4") < 0){
\F3::get('db')->exec('
ALTER TABLE items ADD updatetime DATETIME;
');
\F3::get('db')->exec('
CREATE TRIGGER insert_updatetime_trigger
AFTER INSERT ON items FOR EACH ROW
BEGIN
UPDATE items
SET updatetime = CURRENT_TIMESTAMP
WHERE id = NEW.id;
END;
');
\F3::get('db')->exec('
CREATE TRIGGER update_updatetime_trigger
AFTER UPDATE ON items FOR EACH ROW
BEGIN
UPDATE items
SET updatetime = CURRENT_TIMESTAMP
WHERE id = NEW.id;
END;
');
\F3::get('db')->exec('
INSERT INTO version (version) VALUES (4);
');
}
}

// just initialize once
Expand Down

0 comments on commit 259fd53

Please sign in to comment.