-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #377 from mudhoney/feature/save_state
client_state database table and endpoints to create and read them
- Loading branch information
Showing
10 changed files
with
303 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE IF NOT EXISTS `client_states` ( | ||
`id` CHAR(64) PRIMARY KEY, | ||
`state` JSON NOT NULL DEFAULT '{}', | ||
`created` DATETIME DEFAULT CURRENT_TIMESTAMP, | ||
`updated` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | ||
) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ | ||
/** | ||
* Client State class for managing client_states table | ||
* | ||
* @category Database | ||
* @package Helioviewer | ||
* @author Kasim Necdet Percinel <[email protected]> | ||
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1 | ||
* @link https://github.com/Helioviewer-Project/ | ||
*/ | ||
|
||
require_once HV_ROOT_DIR.'/../src/Database/DbConnection.php'; | ||
|
||
class ClientState extends Database_DbConnection | ||
{ | ||
/** | ||
* This function creates or updates the state in database. | ||
* | ||
* @return string | ||
*/ | ||
public function upsert(array $state): string | ||
{ | ||
$state_json = json_encode($state); | ||
$state_key = hash('sha256',$state_json); | ||
|
||
$create_sql = "REPLACE INTO client_states(id, state) VALUES ('%s','%s')"; | ||
$create_state_sql = sprintf($create_sql, $state_key, $this->link->real_escape_string($state_json)); | ||
|
||
// intentionally let exception thrown | ||
$result = $this->query($create_state_sql); | ||
|
||
return $state_key; | ||
} | ||
|
||
/** | ||
* This function finds the client state in db, with our given id | ||
* @param string, state_key is the id of the client state in database | ||
* @return array? | ||
*/ | ||
public function find(string $state_key): ?array | ||
{ | ||
$find_sql = "SELECT * FROM client_states WHERE id = '%s' LIMIT 1"; | ||
|
||
$find_state_sql = sprintf($find_sql, $this->link->real_escape_string($state_key)); | ||
|
||
$query_result = $this->query($find_state_sql); | ||
|
||
$res = null; | ||
|
||
while ($row = $query_result->fetch_array(MYSQLI_ASSOC)) { | ||
$res = json_decode($row['state'], true); | ||
} | ||
|
||
$query_result->close(); | ||
|
||
return $res; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.