add sqlite support (refactoring)

This commit is contained in:
Miroslav Šedivý 2020-06-11 01:47:02 +02:00
parent ebce135ebf
commit 938b95691c
2 changed files with 34 additions and 6 deletions

View file

@ -20,6 +20,10 @@ class DB
return self::$_instance;
}
public static function connection() {
return Config::get_safe('db_connection', 'mysql');
}
// Initialise PDO object
private final function __construct(){
$host = Config::get_safe('mysql_host', false);
@ -154,7 +158,11 @@ class DB
$query .= '(';
foreach($field as $key => $value){
if($value === "NOW()"){
$query .= 'NOW()';
if(DB::connection() === 'sqlite') {
$query .= "datetime('now', 'localtime')";
} else {
$query .= "NOW()";
}
} else {
$query .= '?';
$prepared_data[] = $value;
@ -209,7 +217,11 @@ class DB
$set_data = null;
foreach($fields as $key => $value){
if($value === "NOW()"){
$set_data .="`{$key}` = NOW()";
if(DB::connection() === 'sqlite') {
$set_data .="`{$key}` = datetime('now', 'localtime')";
} else {
$set_data .="`{$key}` = NOW()";
}
} else {
$set_data .= "`{$key}` = ?";
$prepared_data[] = $value;

View file

@ -201,8 +201,14 @@ class Post
public static function get_date($r){
self::login_protected();
if (DB::connection() === 'sqlite') {
$datetime = "strftime('%Y %m %d %H %M', `posts`.`datetime`)";
} else {
$datetime = "DATE_FORMAT(`datetime`,'%Y %c %e %k %i')";
}
$date = DB::get_instance()->query("
SELECT DATE_FORMAT(`datetime`,'%Y %c %e %k %i') AS `date_format`
SELECT $datetime AS `date_format`
FROM `posts`
WHERE `id` = ?
AND `status` <> 5
@ -216,7 +222,12 @@ class Post
self::login_protected();
$d = $r["date"];
$datetime = "{$d[0]}/{$d[1]}/{$d[2]} {$d[3]}:{$d[4]}";
if (DB::connection() === 'sqlite') {
$datetime = vsprintf("%04d-%02d-%02d %02d:%02d", $d);
} else {
$datetime = vsprintf("%04d/%02d/%02d %02d:%02d", $d);
}
DB::get_instance()->query("
UPDATE `posts`
SET `datetime` = ?
@ -411,11 +422,16 @@ class Post
$person = $r["filter"]["person"];
}
if (DB::connection() === 'sqlite') {
$datetime = "strftime('%d %m %Y %H:%M', `posts`.`datetime`)";
} else {
$datetime = "DATE_FORMAT(`posts`.`datetime`,'%d %b %Y %H:%i')";
}
return DB::get_instance()->query("
SELECT
`id`, `text`, `feeling`, `persons`, `location`, `privacy`, `content_type`, `content`,
DATE_FORMAT(`posts`.`datetime`,'%d %b %Y %H:%i') AS `datetime`,
(`status` <> 1) AS `is_hidden`
$datetime AS `datetime`, (`status` <> 1) AS `is_hidden`
FROM `posts`
WHERE ".
(!User::is_logged_in() ? (User::is_visitor() ? "`privacy` IN ('public', 'friends') AND " : "`privacy` = 'public' AND ") : "").