@ -24,6 +24,9 @@ import (
const (
const (
mySQLErrDuplicateKey = 1062
mySQLErrDuplicateKey = 1062
driverMySQL = "mysql"
driverSQLite = "sqlite3"
)
)
type writestore interface {
type writestore interface {
@ -103,6 +106,13 @@ type datastore struct {
driverName string
driverName string
}
}
func ( db * datastore ) now ( ) string {
if db . driverName == driverSQLite {
return "strftime('%Y-%m-%d %H-%M-%S','now')"
}
return "NOW()"
}
func ( db * datastore ) CreateUser ( u * User , collectionTitle string ) error {
func ( db * datastore ) CreateUser ( u * User , collectionTitle string ) error {
// New users get a `users` and `collections` row.
// New users get a `users` and `collections` row.
t , err := db . Begin ( )
t , err := db . Begin ( )
@ -572,12 +582,7 @@ func (db *datastore) CreatePost(userID, collID int64, post *SubmittedPost) (*Pos
}
}
}
}
timeFunction := "NOW()"
stmt , err := db . Prepare ( "INSERT INTO posts (id, slug, title, content, text_appearance, language, rtl, privacy, owner_id, collection_id, created, updated, view_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + db . now ( ) + ", ?)" )
if db . driverName == "sqlite3" {
timeFunction = "strftime('%Y-%m-%d %H-%M-%S','now')"
}
stmt , err := db . Prepare ( "INSERT INTO posts (id, slug, title, content, text_appearance, language, rtl, privacy, owner_id, collection_id, created, updated, view_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + timeFunction + ", ?)" )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -674,13 +679,7 @@ func (db *datastore) UpdateOwnedPost(post *AuthenticatedPost, userID int64) erro
return ErrPostNoUpdatableVals
return ErrPostNoUpdatableVals
}
}
timeFunction := "NOW()"
queryUpdates += sep + "updated = " + db . now ( )
if db . driverName == "sqlite3" {
timeFunction = "strftime('%Y-%m-%d %H-%M-%S','now')"
}
queryUpdates += sep + "updated = " + timeFunction
res , err := db . Exec ( "UPDATE posts SET " + queryUpdates + " WHERE id = ? AND " + authCondition , params ... )
res , err := db . Exec ( "UPDATE posts SET " + queryUpdates + " WHERE id = ? AND " + authCondition , params ... )
if err != nil {
if err != nil {
@ -995,11 +994,7 @@ func (db *datastore) GetPostsCount(c *CollectionObj, includeFuture bool) {
var count int64
var count int64
timeCondition := ""
timeCondition := ""
if ! includeFuture {
if ! includeFuture {
timeCondition = "AND created <= NOW()"
timeCondition = "AND created <= " + db . now ( )
if db . driverName == "sqlite3" {
timeCondition = "AND created <= strftime('%Y-%m-%d %H-%M-%S','now')"
}
}
}
err := db . QueryRow ( "SELECT COUNT(*) FROM posts WHERE collection_id = ? AND pinned_position IS NULL " + timeCondition , c . ID ) . Scan ( & count )
err := db . QueryRow ( "SELECT COUNT(*) FROM posts WHERE collection_id = ? AND pinned_position IS NULL " + timeCondition , c . ID ) . Scan ( & count )
switch {
switch {
@ -1038,11 +1033,7 @@ func (db *datastore) GetPosts(c *Collection, page int, includeFuture, forceRecen
}
}
timeCondition := ""
timeCondition := ""
if ! includeFuture {
if ! includeFuture {
timeCondition = "AND created <= NOW()"
timeCondition = "AND created <= " + db . now ( )
if db . driverName == "sqlite3" {
timeCondition = "AND created <= strftime('%Y-%m-%d %H-%M-%S','now')"
}
}
}
rows , err := db . Query ( "SELECT " + postCols + " FROM posts WHERE collection_id = ? AND pinned_position IS NULL " + timeCondition + " ORDER BY created " + order + limitStr , collID )
rows , err := db . Query ( "SELECT " + postCols + " FROM posts WHERE collection_id = ? AND pinned_position IS NULL " + timeCondition + " ORDER BY created " + order + limitStr , collID )
if err != nil {
if err != nil {
@ -1099,11 +1090,7 @@ func (db *datastore) GetPostsTagged(c *Collection, tag string, page int, include
}
}
timeCondition := ""
timeCondition := ""
if ! includeFuture {
if ! includeFuture {
timeCondition = "AND created <= NOW()"
timeCondition = "AND created <= " + db . now ( )
if db . driverName == "sqlite3" {
timeCondition = "AND created <= strftime('%Y-%m-%d %H-%M-%S','now')"
}
}
}
rows , err := db . Query ( "SELECT " + postCols + " FROM posts WHERE collection_id = ? AND LOWER(content) RLIKE ? " + timeCondition + " ORDER BY created " + order + limitStr , collID , "#" + strings . ToLower ( tag ) + "[[:>:]]" )
rows , err := db . Query ( "SELECT " + postCols + " FROM posts WHERE collection_id = ? AND LOWER(content) RLIKE ? " + timeCondition + " ORDER BY created " + order + limitStr , collID , "#" + strings . ToLower ( tag ) + "[[:>:]]" )
if err != nil {
if err != nil {
@ -2169,13 +2156,7 @@ func (db *datastore) GetDynamicContent(id string) (string, *time.Time, error) {
}
}
func ( db * datastore ) UpdateDynamicContent ( id , content string ) error {
func ( db * datastore ) UpdateDynamicContent ( id , content string ) error {
timeFunction := "NOW()"
_ , err := db . Exec ( "INSERT INTO appcontent (id, content, updated) VALUES (?, ?, " + db . now ( ) + ") ON DUPLICATE KEY UPDATE content = ?, updated = " + db . now ( ) , id , content , content )
if db . driverName == "sqlite3" {
timeFunction = "strftime('%Y-%m-%d %H-%M-%S','now')"
}
_ , err := db . Exec ( "INSERT INTO appcontent (id, content, updated) VALUES (?, ?, " + timeFunction + ") ON DUPLICATE KEY UPDATE content = ?, updated = " + timeFunction , id , content , content )
if err != nil {
if err != nil {
log . Error ( "Unable to INSERT appcontent for '%s': %v" , id , err )
log . Error ( "Unable to INSERT appcontent for '%s': %v" , id , err )
}
}