From 39fe0cbe0a4cd89ac025473fe4016e288e6be31d Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Fri, 12 Jan 2018 14:57:05 -0700 Subject: [PATCH] Give up on MySQL timestamps & just use unix timestamps (int) Also change the way update "success" is evaluated for setting details/words. --- public/api/Dictionary.php | 32 +++++++++++++++++--------------- public/api/structure.sql | 10 +++++----- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/public/api/Dictionary.php b/public/api/Dictionary.php index 6ce890a..23e9c72 100644 --- a/public/api/Dictionary.php +++ b/public/api/Dictionary.php @@ -118,7 +118,7 @@ SET name=:name, sort_by_definition=:sort_by_definition, is_complete=:is_complete, is_public=:is_public, - last_updated=NOW() + last_updated=:last_updated WHERE user=$user AND id=$dictionary"; $result1 = $this->db->query($query1, array( @@ -130,6 +130,7 @@ WHERE user=$user AND id=$dictionary"; ':sort_by_definition' => $dictionary_object['settings']['sortByDefinition'], ':is_complete' => $dictionary_object['settings']['isComplete'], ':is_public' => $dictionary_object['settings']['isPublic'], + ':last_updated' => $dictionary_object['lastUpdated'], )); if ($result1->rowCount() > 0) { $linguistics = $dictionary_object['details']; @@ -140,14 +141,14 @@ SET parts_of_speech=:parts_of_speech, grammar_notes=:grammar_notes WHERE dictionary=$dictionary"; - $result2 = $this->db->query($query2, array( + $result2 = $this->db->execute($query2, array( ':parts_of_speech' => json_encode($dictionary_object['partsOfSpeech']), ':phonology' => json_encode($linguistics['phonology']), ':orthography_notes' => $linguistics['orthography']['notes'], ':grammar_notes' => $linguistics['grammar']['notes'], )); - if ($result2->rowCount() > 0) { + if ($result2) { return true; } } @@ -160,14 +161,14 @@ WHERE dictionary=$dictionary"; if ($results) { return array_map(function ($row) { return array( - 'id' => $result['word_id'], - 'name' => $result['name'], - 'pronunciation' => $result['pronunciation'], - 'partOfSpeech' => $result['part_of_speech'], - 'definition' => $result['definition'], - 'details' => $result['details'], - 'lastUpdated' => is_null($result['last_updated']) ? null : strtotime($result['last_updated']), - 'createdOn' => strtotime($result['created_on']), + 'id' => intval($row['word_id']), + 'name' => $row['name'], + 'pronunciation' => $row['pronunciation'], + 'partOfSpeech' => $row['part_of_speech'], + 'definition' => $row['definition'], + 'details' => $row['details'], + 'lastUpdated' => is_null($row['last_updated']) ? null : strtotime($row['last_updated']), + 'createdOn' => strtotime($row['created_on']), ); }, $results); } @@ -175,10 +176,11 @@ WHERE dictionary=$dictionary"; } public function setWords ($dictionary, $words = array()) { - $query = 'INSERT INTO words (word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES '; + $query = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES '; $params = array(); foreach($words as $word) { - $query .= "(?, ?, ?, ?, ?, ?, FROM_UNIXTIME(?), FROM_UNIXTIME(?)), "; + $query .= "(?, ?, ?, ?, ?, ?, ?, ?, ?), "; + $params[] = $dictionary; $params[] = $word['id']; $params[] = $word['name']; $params[] = $word['pronunciation']; @@ -196,7 +198,7 @@ definition=VALUES(definition), details=VALUES(details), last_updated=VALUES(last_updated)'; - $results = $this->db->query($query, $params); - return $results->rowCount(); + $results = $this->db->execute($query, $params); + return $results; } } \ No newline at end of file diff --git a/public/api/structure.sql b/public/api/structure.sql index 97f1885..21b9632 100644 --- a/public/api/structure.sql +++ b/public/api/structure.sql @@ -18,8 +18,8 @@ CREATE TABLE IF NOT EXISTS `dictionaries` ( `sort_by_definition` tinyint(1) NOT NULL DEFAULT '0', `is_complete` tinyint(1) NOT NULL DEFAULT '0', `is_public` tinyint(1) NOT NULL DEFAULT '0', - `last_updated` timestamp NULL DEFAULT NULL, - `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `last_updated` int(11) DEFAULT NULL, + `created_on` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=352 ; DROP TRIGGER IF EXISTS `delete_dictionary_parts`; @@ -65,7 +65,7 @@ CREATE TABLE IF NOT EXISTS `users` ( PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`), UNIQUE KEY `username` (`username`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=151 ; +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=152 ; DROP TRIGGER IF EXISTS `Delete_User_Dictionaries`; DELIMITER // CREATE TRIGGER `Delete_User_Dictionaries` AFTER DELETE ON `users` @@ -81,8 +81,8 @@ CREATE TABLE IF NOT EXISTS `words` ( `part_of_speech` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `definition` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `details` text COLLATE utf8_unicode_ci NOT NULL COMMENT 'Markdown', - `last_updated` timestamp NULL DEFAULT NULL, - `created_on` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `last_updated` int(11) DEFAULT NULL, + `created_on` int(11) NOT NULL, UNIQUE KEY `unique_index` (`dictionary`,`word_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;