2022-07-17 18:16:00 +00:00
// Copyright (C) 2019-2022 Nicola Murino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-07-20 10:26:52 +00:00
package dataprovider
2020-06-07 21:30:18 +00:00
import (
"fmt"
"strconv"
"strings"
2022-07-24 14:18:54 +00:00
"github.com/drakkan/sftpgo/v2/internal/vfs"
2020-06-07 21:30:18 +00:00
)
2019-07-20 10:26:52 +00:00
const (
2022-11-16 18:04:50 +00:00
selectUserFields = "u.id,u.username,u.password,u.public_keys,u.home_dir,u.uid,u.gid,u.max_sessions,u.quota_size,u.quota_files," +
"u.permissions,u.used_quota_size,u.used_quota_files,u.last_quota_update,u.upload_bandwidth,u.download_bandwidth," +
"u.expiration_date,u.last_login,u.status,u.filters,u.filesystem,u.additional_info,u.description,u.email,u.created_at," +
"u.updated_at,u.upload_data_transfer,u.download_data_transfer,u.total_data_transfer," +
2022-12-11 16:15:34 +00:00
"u.used_upload_data_transfer,u.used_download_data_transfer,u.deleted_at,u.first_download,u.first_upload,r.name,u.last_password_change"
2021-03-21 18:15:47 +00:00
selectFolderFields = "id,path,used_quota_size,used_quota_files,last_quota_update,name,description,filesystem"
2022-11-16 18:04:50 +00:00
selectAdminFields = "a.id,a.username,a.password,a.status,a.email,a.permissions,a.filters,a.additional_info,a.description,a.created_at,a.updated_at,a.last_login,r.name"
2021-08-17 16:08:32 +00:00
selectAPIKeyFields = "key_id,name,api_key,scope,created_at,updated_at,last_use_at,expires_at,description,user_id,admin_id"
2021-11-06 13:13:20 +00:00
selectShareFields = "s.share_id,s.name,s.description,s.scope,s.paths,u.username,s.created_at,s.updated_at,s.last_use_at," +
"s.expires_at,s.password,s.max_tokens,s.used_tokens,s.allow_from"
2022-07-11 06:17:36 +00:00
selectGroupFields = "id,name,description,created_at,updated_at,user_settings"
selectEventActionFields = "id,name,description,type,options"
2022-11-16 18:04:50 +00:00
selectRoleFields = "id,name,description,created_at,updated_at"
2022-07-11 06:17:36 +00:00
selectMinimalFields = "id,name"
2019-07-20 10:26:52 +00:00
)
func getSQLPlaceholders ( ) [ ] string {
var placeholders [ ] string
2022-01-20 17:19:20 +00:00
for i := 1 ; i <= 50 ; i ++ {
2021-03-23 18:14:15 +00:00
if config . Driver == PGSQLDataProviderName || config . Driver == CockroachDataProviderName {
2022-06-24 11:16:45 +00:00
placeholders = append ( placeholders , fmt . Sprintf ( "$%d" , i ) )
2019-07-20 10:26:52 +00:00
} else {
placeholders = append ( placeholders , "?" )
}
}
return placeholders
}
2022-07-11 06:17:36 +00:00
func getSQLQuotedName ( name string ) string {
2022-06-10 15:36:26 +00:00
if config . Driver == MySQLDataProviderName {
2022-07-11 06:17:36 +00:00
return fmt . Sprintf ( "`%s`" , name )
2022-06-10 15:36:26 +00:00
}
2022-07-11 06:17:36 +00:00
return fmt . Sprintf ( ` "%s" ` , name )
}
func getSelectEventRuleFields ( ) string {
if config . Driver == MySQLDataProviderName {
return "id,name,description,created_at,updated_at,`trigger`,conditions,deleted_at"
}
return ` id,name,description,created_at,updated_at,"trigger",conditions,deleted_at `
2022-06-10 15:36:26 +00:00
}
2022-11-16 18:04:50 +00:00
func getCoalesceDefaultForRole ( role string ) string {
if role != "" {
return "0"
}
return "NULL"
}
2022-05-19 17:49:51 +00:00
func getAddSessionQuery ( ) string {
if config . Driver == MySQLDataProviderName {
return fmt . Sprintf ( "INSERT INTO %s (`key`,`data`,`type`,`timestamp`) VALUES (%s,%s,%s,%s) " +
"ON DUPLICATE KEY UPDATE `data`=VALUES(`data`), `timestamp`=VALUES(`timestamp`)" ,
sqlTableSharedSessions , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
return fmt . Sprintf ( ` INSERT INTO % s ( key , data , type , timestamp ) VALUES ( % s , % s , % s , % s ) ON CONFLICT ( key ) DO UPDATE SET data =
EXCLUDED . data , timestamp = EXCLUDED . timestamp ` ,
sqlTableSharedSessions , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
func getDeleteSessionQuery ( ) string {
if config . Driver == MySQLDataProviderName {
return fmt . Sprintf ( "DELETE FROM %s WHERE `key` = %s" , sqlTableSharedSessions , sqlPlaceholders [ 0 ] )
}
return fmt . Sprintf ( ` DELETE FROM %s WHERE key = %s ` , sqlTableSharedSessions , sqlPlaceholders [ 0 ] )
}
func getSessionQuery ( ) string {
if config . Driver == MySQLDataProviderName {
return fmt . Sprintf ( "SELECT `key`,`data`,`type`,`timestamp` FROM %s WHERE `key` = %s" , sqlTableSharedSessions ,
sqlPlaceholders [ 0 ] )
}
return fmt . Sprintf ( ` SELECT key,data,type,timestamp FROM %s WHERE key = %s ` , sqlTableSharedSessions ,
sqlPlaceholders [ 0 ] )
}
func getCleanupSessionsQuery ( ) string {
return fmt . Sprintf ( ` DELETE from %s WHERE type = %s AND timestamp < %s ` ,
sqlTableSharedSessions , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
2021-12-25 11:08:07 +00:00
func getAddDefenderHostQuery ( ) string {
if config . Driver == MySQLDataProviderName {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( "INSERT INTO %s (`ip`,`updated_at`,`ban_time`) VALUES (%s,%s,0) ON DUPLICATE KEY UPDATE `updated_at`=VALUES(`updated_at`)" ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO %s (ip,updated_at,ban_time) VALUES (%s,%s,0) ON CONFLICT (ip) DO UPDATE SET updated_at = EXCLUDED.updated_at RETURNING id ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getAddDefenderEventQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO %s (date_time,score,host_id) VALUES (%s,%s,(SELECT id from %s WHERE ip = %s)) ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderEvents , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlTableDefenderHosts , sqlPlaceholders [ 2 ] )
}
func getDefenderHostsQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT id,ip,ban_time FROM %s WHERE updated_at >= %s OR ban_time > 0 ORDER BY updated_at DESC LIMIT %s ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDefenderHostQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT id,ip,ban_time FROM %s WHERE ip = %s AND (updated_at >= %s OR ban_time > 0) ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDefenderEventsQuery ( hostIDS [ ] int64 ) string {
var sb strings . Builder
for _ , hID := range hostIDS {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( hID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
} else {
sb . WriteString ( "(0)" )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT host_id,SUM(score) FROM %s WHERE date_time >= %s AND host_id IN %s GROUP BY host_id ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderEvents , sqlPlaceholders [ 0 ] , sb . String ( ) )
}
func getDefenderIsHostBannedQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT id FROM %s WHERE ip = %s AND ban_time >= %s ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDefenderIncrementBanTimeQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET ban_time = ban_time + %s WHERE ip = %s ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDefenderSetBanTimeQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET ban_time = %s WHERE ip = %s ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDeleteDefenderHostQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE ip = %s ` , sqlTableDefenderHosts , sqlPlaceholders [ 0 ] )
2021-12-25 11:08:07 +00:00
}
func getDefenderHostsCleanupQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM % s WHERE ban_time < % s AND NOT EXISTS (
SELECT id FROM % s WHERE % s . host_id = % s . id AND % s . date_time > % s ) ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlTableDefenderEvents , sqlTableDefenderEvents , sqlTableDefenderHosts ,
sqlTableDefenderEvents , sqlPlaceholders [ 1 ] )
}
func getDefenderEventsCleanupQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE date_time < %s ` , sqlTableDefenderEvents , sqlPlaceholders [ 0 ] )
2021-12-25 11:08:07 +00:00
}
2022-11-16 18:04:50 +00:00
func getRoleByNameQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE name = %s ` , selectRoleFields , sqlTableRoles ,
sqlPlaceholders [ 0 ] )
}
func getRolesQuery ( order string , minimal bool ) string {
var fieldSelection string
if minimal {
fieldSelection = selectMinimalFields
} else {
fieldSelection = selectRoleFields
}
return fmt . Sprintf ( ` SELECT %s FROM %s ORDER BY name %s LIMIT %s OFFSET %s ` , fieldSelection ,
sqlTableRoles , order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getUsersWithRolesQuery ( roles [ ] Role ) string {
var sb strings . Builder
for _ , r := range roles {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( r . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
return fmt . Sprintf ( ` SELECT r.id, u.username FROM %s u INNER JOIN %s r ON u.role_id = r.id WHERE u.role_id IN %s ` ,
sqlTableUsers , sqlTableRoles , sb . String ( ) )
}
func getAdminsWithRolesQuery ( roles [ ] Role ) string {
var sb strings . Builder
for _ , r := range roles {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( r . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
return fmt . Sprintf ( ` SELECT r.id, a.username FROM %s a INNER JOIN %s r ON a.role_id = r.id WHERE a.role_id IN %s ` ,
sqlTableAdmins , sqlTableRoles , sb . String ( ) )
}
func getDumpRolesQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s ` , selectRoleFields , sqlTableRoles )
}
func getAddRoleQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % s ( name , description , created_at , updated_at )
VALUES ( % s , % s , % s , % s ) ` , sqlTableRoles , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] ,
sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
func getUpdateRoleQuery ( ) string {
return fmt . Sprintf ( ` UPDATE % s SET description = % s , updated_at = % s
WHERE name = % s ` , sqlTableRoles , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
func getDeleteRoleQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE name = %s ` , sqlTableRoles , sqlPlaceholders [ 0 ] )
}
2022-04-25 13:49:11 +00:00
func getGroupByNameQuery ( ) string {
2022-07-11 06:17:36 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE name = %s ` , selectGroupFields , getSQLQuotedName ( sqlTableGroups ) ,
sqlPlaceholders [ 0 ] )
2022-04-25 13:49:11 +00:00
}
func getGroupsQuery ( order string , minimal bool ) string {
var fieldSelection string
if minimal {
2022-07-11 06:17:36 +00:00
fieldSelection = selectMinimalFields
2022-04-25 13:49:11 +00:00
} else {
fieldSelection = selectGroupFields
}
2022-07-11 06:17:36 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s ORDER BY name %s LIMIT %s OFFSET %s ` , fieldSelection ,
getSQLQuotedName ( sqlTableGroups ) , order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2022-04-25 13:49:11 +00:00
}
func getGroupsWithNamesQuery ( numArgs int ) string {
var sb strings . Builder
for idx := 0 ; idx < numArgs ; idx ++ {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( sqlPlaceholders [ idx ] )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
} else {
sb . WriteString ( "('')" )
}
2022-07-11 06:17:36 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE name in %s ` , selectGroupFields , getSQLQuotedName ( sqlTableGroups ) , sb . String ( ) )
2022-04-25 13:49:11 +00:00
}
func getUsersInGroupsQuery ( numArgs int ) string {
var sb strings . Builder
for idx := 0 ; idx < numArgs ; idx ++ {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( sqlPlaceholders [ idx ] )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
} else {
sb . WriteString ( "('')" )
}
return fmt . Sprintf ( ` SELECT username FROM %s WHERE id IN (SELECT user_id from %s WHERE group_id IN (SELECT id FROM %s WHERE name IN (%s))) ` ,
2022-07-11 06:17:36 +00:00
sqlTableUsers , sqlTableUsersGroupsMapping , getSQLQuotedName ( sqlTableGroups ) , sb . String ( ) )
2022-04-25 13:49:11 +00:00
}
func getDumpGroupsQuery ( ) string {
2022-07-11 06:17:36 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s ` , selectGroupFields , getSQLQuotedName ( sqlTableGroups ) )
2022-04-25 13:49:11 +00:00
}
func getAddGroupQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % s ( name , description , created_at , updated_at , user_settings )
2022-07-11 06:17:36 +00:00
VALUES ( % s , % s , % s , % s , % s ) ` , getSQLQuotedName ( sqlTableGroups ) , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] ,
2022-04-25 13:49:11 +00:00
sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] )
}
func getUpdateGroupQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET description = % s , user_settings = % s , updated_at = % s
2022-07-11 06:17:36 +00:00
WHERE name = % s ` , getSQLQuotedName ( sqlTableGroups ) , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
2022-04-25 13:49:11 +00:00
sqlPlaceholders [ 3 ] )
}
func getDeleteGroupQuery ( ) string {
2022-07-11 06:17:36 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE name = %s ` , getSQLQuotedName ( sqlTableGroups ) , sqlPlaceholders [ 0 ] )
2022-04-25 13:49:11 +00:00
}
2021-01-17 21:29:08 +00:00
func getAdminByUsernameQuery ( ) string {
2022-11-16 18:04:50 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s a LEFT JOIN %s r on r.id = a.role_id WHERE a.username = %s ` ,
selectAdminFields , sqlTableAdmins , sqlTableRoles , sqlPlaceholders [ 0 ] )
2019-07-20 10:26:52 +00:00
}
2021-01-17 21:29:08 +00:00
func getAdminsQuery ( order string ) string {
2022-11-16 18:04:50 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s a LEFT JOIN %s r on r.id = a.role_id ORDER BY a.username %s LIMIT %s OFFSET %s ` ,
selectAdminFields , sqlTableAdmins , sqlTableRoles , order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2019-07-20 10:26:52 +00:00
}
2021-01-17 21:29:08 +00:00
func getDumpAdminsQuery ( ) string {
2022-11-16 18:04:50 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s a LEFT JOIN %s r on r.id = a.role_id ` ,
selectAdminFields , sqlTableAdmins , sqlTableRoles )
2021-01-17 21:29:08 +00:00
}
2022-11-16 18:04:50 +00:00
func getAddAdminQuery ( role string ) string {
return fmt . Sprintf ( ` INSERT INTO % s ( username , password , status , email , permissions , filters , additional_info , description , created_at , updated_at , last_login , role_id )
VALUES ( % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , 0 , COALESCE ( ( SELECT id from % s WHERE name = % s ) , % s ) ) ` ,
sqlTableAdmins , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] ,
sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] ,
sqlTableRoles , sqlPlaceholders [ 10 ] , getCoalesceDefaultForRole ( role ) )
2021-01-17 21:29:08 +00:00
}
2022-11-16 18:04:50 +00:00
func getUpdateAdminQuery ( role string ) string {
return fmt . Sprintf ( ` UPDATE % s SET password = % s , status = % s , email = % s , permissions = % s , filters = % s , additional_info = % s , description = % s , updated_at = % s ,
role_id = COALESCE ( ( SELECT id from % s WHERE name = % s ) , % s ) WHERE username = % s ` , sqlTableAdmins , sqlPlaceholders [ 0 ] ,
sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] ,
sqlPlaceholders [ 7 ] , sqlTableRoles , sqlPlaceholders [ 8 ] , getCoalesceDefaultForRole ( role ) , sqlPlaceholders [ 9 ] )
2021-01-17 21:29:08 +00:00
}
func getDeleteAdminQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE username = %s ` , sqlTableAdmins , sqlPlaceholders [ 0 ] )
2021-01-17 21:29:08 +00:00
}
2021-11-06 13:13:20 +00:00
func getShareByIDQuery ( filterUser bool ) string {
if filterUser {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s s INNER JOIN %s u ON s.user_id = u.id WHERE s.share_id = %s AND u.username = %s ` ,
2021-11-06 13:13:20 +00:00
selectShareFields , sqlTableShares , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s s INNER JOIN %s u ON s.user_id = u.id WHERE s.share_id = %s ` ,
2021-11-06 13:13:20 +00:00
selectShareFields , sqlTableShares , sqlTableUsers , sqlPlaceholders [ 0 ] )
}
func getSharesQuery ( order string ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s s INNER JOIN %s u ON s.user_id = u.id WHERE u.username = %s ORDER BY s.share_id %s LIMIT %s OFFSET %s ` ,
2021-11-06 13:13:20 +00:00
selectShareFields , sqlTableShares , sqlTableUsers , sqlPlaceholders [ 0 ] , order , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
func getDumpSharesQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s s INNER JOIN %s u ON s.user_id = u.id ` ,
2021-11-06 13:13:20 +00:00
selectShareFields , sqlTableShares , sqlTableUsers )
}
func getAddShareQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( share_id , name , description , scope , paths , created_at , updated_at , last_use_at ,
expires_at , password , max_tokens , used_tokens , allow_from , user_id ) VALUES ( % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , % s ) ` ,
2021-11-06 13:13:20 +00:00
sqlTableShares , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] ,
sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] ,
sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] , sqlPlaceholders [ 10 ] , sqlPlaceholders [ 11 ] ,
2021-11-27 10:12:51 +00:00
sqlPlaceholders [ 12 ] , sqlPlaceholders [ 13 ] )
}
func getUpdateShareRestoreQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET name = % s , description = % s , scope = % s , paths = % s , created_at = % s , updated_at = % s ,
last_use_at = % s , expires_at = % s , password = % s , max_tokens = % s , used_tokens = % s , allow_from = % s , user_id = % s WHERE share_id = % s ` , sqlTableShares ,
2021-11-27 10:12:51 +00:00
sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] ,
sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] ,
sqlPlaceholders [ 10 ] , sqlPlaceholders [ 11 ] , sqlPlaceholders [ 12 ] , sqlPlaceholders [ 13 ] )
2021-11-06 13:13:20 +00:00
}
func getUpdateShareQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET name = % s , description = % s , scope = % s , paths = % s , updated_at = % s , expires_at = % s ,
password = % s , max_tokens = % s , allow_from = % s , user_id = % s WHERE share_id = % s ` , sqlTableShares ,
2021-11-06 13:13:20 +00:00
sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] ,
sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] ,
sqlPlaceholders [ 10 ] )
}
func getDeleteShareQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE share_id = %s ` , sqlTableShares , sqlPlaceholders [ 0 ] )
2021-11-06 13:13:20 +00:00
}
2021-08-17 16:08:32 +00:00
func getAPIKeyByIDQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE key_id = %s ` , selectAPIKeyFields , sqlTableAPIKeys , sqlPlaceholders [ 0 ] )
2021-08-17 16:08:32 +00:00
}
func getAPIKeysQuery ( order string ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s ORDER BY key_id %s LIMIT %s OFFSET %s ` , selectAPIKeyFields , sqlTableAPIKeys ,
2021-08-17 16:08:32 +00:00
order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDumpAPIKeysQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s ` , selectAPIKeyFields , sqlTableAPIKeys )
2021-08-17 16:08:32 +00:00
}
func getAddAPIKeyQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( key_id , name , api_key , scope , created_at , updated_at , last_use_at , expires_at , description , user_id , admin_id )
VALUES ( % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , % s ) ` , sqlTableAPIKeys , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] ,
2021-08-17 16:08:32 +00:00
sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] ,
sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] , sqlPlaceholders [ 10 ] )
}
func getUpdateAPIKeyQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET name = % s , scope = % s , expires_at = % s , user_id = % s , admin_id = % s , description = % s , updated_at = % s
WHERE key_id = % s ` , sqlTableAPIKeys , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
2021-08-17 16:08:32 +00:00
sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] )
}
func getDeleteAPIKeyQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE key_id = %s ` , sqlTableAPIKeys , sqlPlaceholders [ 0 ] )
2021-08-17 16:08:32 +00:00
}
func getRelatedUsersForAPIKeysQuery ( apiKeys [ ] APIKey ) string {
var sb strings . Builder
for _ , k := range apiKeys {
if k . userID == 0 {
continue
}
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( k . userID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
} else {
sb . WriteString ( "(0)" )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT id,username FROM %s WHERE id IN %s ` , sqlTableUsers , sb . String ( ) )
2021-08-17 16:08:32 +00:00
}
func getRelatedAdminsForAPIKeysQuery ( apiKeys [ ] APIKey ) string {
var sb strings . Builder
for _ , k := range apiKeys {
if k . adminID == 0 {
continue
}
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( k . adminID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
} else {
sb . WriteString ( "(0)" )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT id,username FROM %s WHERE id IN %s ` , sqlTableAdmins , sb . String ( ) )
2021-08-17 16:08:32 +00:00
}
2022-11-16 18:04:50 +00:00
func getUserByUsernameQuery ( role string ) string {
if role == "" {
return fmt . Sprintf ( ` SELECT %s FROM %s u LEFT JOIN %s r on r.id = u.role_id WHERE u.username = %s AND u.deleted_at = 0 ` ,
selectUserFields , sqlTableUsers , sqlTableRoles , sqlPlaceholders [ 0 ] )
}
return fmt . Sprintf ( ` SELECT % s FROM % s u LEFT JOIN % s r on r . id = u . role_id WHERE u . username = % s AND u . deleted_at = 0
AND u . role_id is NOT NULL AND r . name = % s ` ,
selectUserFields , sqlTableUsers , sqlTableRoles , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2021-01-17 21:29:08 +00:00
}
2022-11-16 18:04:50 +00:00
func getUsersQuery ( order , role string ) string {
if role == "" {
return fmt . Sprintf ( ` SELECT % s FROM % s u LEFT JOIN % s r on r . id = u . role_id WHERE
u . deleted_at = 0 ORDER BY u . username % s LIMIT % s OFFSET % s ` ,
selectUserFields , sqlTableUsers , sqlTableRoles , order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
return fmt . Sprintf ( ` SELECT % s FROM % s u LEFT JOIN % s r on r . id = u . role_id WHERE
u . deleted_at = 0 AND u . role_id is NOT NULL AND r . name = % s ORDER BY u . username % s LIMIT % s OFFSET % s ` ,
selectUserFields , sqlTableUsers , sqlTableRoles , sqlPlaceholders [ 0 ] , order , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
2019-07-20 10:26:52 +00:00
}
2022-01-20 17:19:20 +00:00
func getUsersForQuotaCheckQuery ( numArgs int ) string {
var sb strings . Builder
for idx := 0 ; idx < numArgs ; idx ++ {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( sqlPlaceholders [ idx ] )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
2022-01-30 10:42:36 +00:00
return fmt . Sprintf ( ` SELECT id , username , quota_size , used_quota_size , total_data_transfer , upload_data_transfer ,
2022-06-24 11:16:45 +00:00
download_data_transfer , used_upload_data_transfer , used_download_data_transfer , filters FROM % s WHERE username IN % s ` ,
2022-01-20 17:19:20 +00:00
sqlTableUsers , sb . String ( ) )
}
2021-08-20 07:35:06 +00:00
func getRecentlyUpdatedUsersQuery ( ) string {
2022-11-16 18:04:50 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s u LEFT JOIN %s r on r.id = u.role_id WHERE u.updated_at >= %s OR u.deleted_at > 0 ` ,
selectUserFields , sqlTableUsers , sqlTableRoles , sqlPlaceholders [ 0 ] )
2021-08-20 07:35:06 +00:00
}
2019-12-27 22:12:44 +00:00
func getDumpUsersQuery ( ) string {
2022-11-16 18:04:50 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s u LEFT JOIN %s r on r.id = u.role_id WHERE u.deleted_at = 0 ` ,
selectUserFields , sqlTableUsers , sqlTableRoles )
2020-06-07 21:30:18 +00:00
}
func getDumpFoldersQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s ` , selectFolderFields , sqlTableFolders )
2019-12-27 22:12:44 +00:00
}
2022-01-30 10:42:36 +00:00
func getUpdateTransferQuotaQuery ( reset bool ) string {
if reset {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET used_upload_data_transfer = % s , used_download_data_transfer = % s , last_quota_update = % s
WHERE username = % s ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2022-01-30 10:42:36 +00:00
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET used_upload_data_transfer = used_upload_data_transfer + % s ,
used_download_data_transfer = used_download_data_transfer + % s , last_quota_update = % s
WHERE username = % s ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2022-01-30 10:42:36 +00:00
}
2019-07-28 11:24:46 +00:00
func getUpdateQuotaQuery ( reset bool ) string {
if reset {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET used_quota_size = % s , used_quota_files = % s , last_quota_update = % s
WHERE username = % s ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2019-07-28 11:24:46 +00:00
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET used_quota_size = used_quota_size + % s , used_quota_files = used_quota_files + % s , last_quota_update = % s
WHERE username = % s ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2019-07-20 10:26:52 +00:00
}
2021-08-19 13:51:43 +00:00
func getSetUpdateAtQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET updated_at = %s WHERE username = %s ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2021-08-19 13:51:43 +00:00
}
2022-08-21 17:01:08 +00:00
func getSetFirstUploadQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %s SET first_upload = %s WHERE username = %s AND first_upload = 0 ` ,
sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getSetFirstDownloadQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %s SET first_download = %s WHERE username = %s AND first_download = 0 ` ,
sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
2019-11-13 10:36:21 +00:00
func getUpdateLastLoginQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET last_login = %s WHERE username = %s ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2019-11-13 10:36:21 +00:00
}
2021-08-19 13:51:43 +00:00
func getUpdateAdminLastLoginQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET last_login = %s WHERE username = %s ` , sqlTableAdmins , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2021-08-19 13:51:43 +00:00
}
2021-08-17 16:08:32 +00:00
func getUpdateAPIKeyLastUseQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET last_use_at = %s WHERE key_id = %s ` , sqlTableAPIKeys , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2021-08-17 16:08:32 +00:00
}
2021-11-06 13:13:20 +00:00
func getUpdateShareLastUseQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET last_use_at = %s, used_tokens = used_tokens +%s WHERE share_id = %s ` ,
2021-11-06 13:13:20 +00:00
sqlTableShares , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
2019-07-20 10:26:52 +00:00
func getQuotaQuery ( ) string {
2022-01-30 10:42:36 +00:00
return fmt . Sprintf ( ` SELECT used_quota_size , used_quota_files , used_upload_data_transfer ,
2022-06-24 11:16:45 +00:00
used_download_data_transfer FROM % s WHERE username = % s ` ,
2022-01-30 10:42:36 +00:00
sqlTableUsers , sqlPlaceholders [ 0 ] )
2019-07-20 10:26:52 +00:00
}
2022-11-16 18:04:50 +00:00
func getAddUserQuery ( role string ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( username , password , public_keys , home_dir , uid , gid , max_sessions , quota_size , quota_files , permissions ,
2020-01-19 06:41:05 +00:00
used_quota_size , used_quota_files , last_quota_update , upload_bandwidth , download_bandwidth , status , last_login , expiration_date , filters ,
2022-01-30 10:42:36 +00:00
filesystem , additional_info , description , email , created_at , updated_at , upload_data_transfer , download_data_transfer , total_data_transfer ,
2022-12-11 16:15:34 +00:00
used_upload_data_transfer , used_download_data_transfer , deleted_at , first_download , first_upload , role_id , last_password_change )
2022-11-16 18:04:50 +00:00
VALUES ( % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , 0 , 0 , 0 , % s , % s , % s , 0 , % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , 0 , 0 , 0 , 0 , 0 ,
2022-12-11 16:15:34 +00:00
COALESCE ( ( SELECT id from % s WHERE name = % s ) , % s ) , % s ) ` ,
2022-01-30 10:42:36 +00:00
sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] ,
sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] ,
sqlPlaceholders [ 10 ] , sqlPlaceholders [ 11 ] , sqlPlaceholders [ 12 ] , sqlPlaceholders [ 13 ] , sqlPlaceholders [ 14 ] ,
sqlPlaceholders [ 15 ] , sqlPlaceholders [ 16 ] , sqlPlaceholders [ 17 ] , sqlPlaceholders [ 18 ] , sqlPlaceholders [ 19 ] ,
2022-11-16 18:04:50 +00:00
sqlPlaceholders [ 20 ] , sqlPlaceholders [ 21 ] , sqlPlaceholders [ 22 ] , sqlPlaceholders [ 23 ] , sqlTableRoles ,
2022-12-11 16:15:34 +00:00
sqlPlaceholders [ 24 ] , getCoalesceDefaultForRole ( role ) , sqlPlaceholders [ 25 ] )
2019-07-20 10:26:52 +00:00
}
2022-11-16 18:04:50 +00:00
func getUpdateUserQuery ( role string ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET password = % s , public_keys = % s , home_dir = % s , uid = % s , gid = % s , max_sessions = % s , quota_size = % s ,
quota_files = % s , permissions = % s , upload_bandwidth = % s , download_bandwidth = % s , status = % s , expiration_date = % s , filters = % s , filesystem = % s ,
additional_info = % s , description = % s , email = % s , updated_at = % s , upload_data_transfer = % s , download_data_transfer = % s ,
2022-12-11 16:15:34 +00:00
total_data_transfer = % s , role_id = COALESCE ( ( SELECT id from % s WHERE name = % s ) , % s ) , last_password_change = % s WHERE id = % s ` ,
2022-01-30 10:42:36 +00:00
sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] ,
sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] ,
sqlPlaceholders [ 10 ] , sqlPlaceholders [ 11 ] , sqlPlaceholders [ 12 ] , sqlPlaceholders [ 13 ] , sqlPlaceholders [ 14 ] ,
sqlPlaceholders [ 15 ] , sqlPlaceholders [ 16 ] , sqlPlaceholders [ 17 ] , sqlPlaceholders [ 18 ] , sqlPlaceholders [ 19 ] ,
2022-11-16 18:04:50 +00:00
sqlPlaceholders [ 20 ] , sqlPlaceholders [ 21 ] , sqlTableRoles , sqlPlaceholders [ 22 ] , getCoalesceDefaultForRole ( role ) ,
2022-12-11 16:15:34 +00:00
sqlPlaceholders [ 23 ] , sqlPlaceholders [ 24 ] )
2019-07-20 10:26:52 +00:00
}
2022-04-02 20:20:21 +00:00
func getUpdateUserPasswordQuery ( ) string {
2022-12-11 16:15:34 +00:00
return fmt . Sprintf ( ` UPDATE %s SET password=%s WHERE username = %s ` ,
sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2022-04-02 20:20:21 +00:00
}
2022-07-17 16:48:41 +00:00
func getDeleteUserQuery ( softDelete bool ) string {
if softDelete {
return fmt . Sprintf ( ` UPDATE %s SET updated_at=%s,deleted_at=%s WHERE username = %s ` ,
sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE id = %s ` , sqlTableUsers , sqlPlaceholders [ 0 ] )
2020-06-07 21:30:18 +00:00
}
2022-11-01 11:53:08 +00:00
func getRemoveSoftDeletedUserQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE username = %s AND deleted_at > 0 ` , sqlTableUsers , sqlPlaceholders [ 0 ] )
}
2021-02-01 18:04:15 +00:00
func getFolderByNameQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE name = %s ` , selectFolderFields , sqlTableFolders , sqlPlaceholders [ 0 ] )
2020-06-07 21:30:18 +00:00
}
func getAddFolderQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( path , used_quota_size , used_quota_files , last_quota_update , name , description , filesystem )
VALUES ( % s , % s , % s , % s , % s , % s , % s ) ` , sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
2021-03-21 18:15:47 +00:00
sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] )
2021-02-01 18:04:15 +00:00
}
func getUpdateFolderQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET path=%s,description=%s,filesystem=%s WHERE name = %s ` , sqlTableFolders , sqlPlaceholders [ 0 ] ,
2021-03-21 18:15:47 +00:00
sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2020-06-07 21:30:18 +00:00
}
func getDeleteFolderQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE id = %s ` , sqlTableFolders , sqlPlaceholders [ 0 ] )
2020-06-07 21:30:18 +00:00
}
2022-04-28 12:49:57 +00:00
func getUpsertFolderQuery ( ) string {
if config . Driver == MySQLDataProviderName {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( "INSERT INTO %s (`path`,`used_quota_size`,`used_quota_files`,`last_quota_update`,`name`," +
"`description`,`filesystem`) VALUES (%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE " +
2022-04-28 12:49:57 +00:00
"`path`=VALUES(`path`),`description`=VALUES(`description`),`filesystem`=VALUES(`filesystem`)" ,
sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] ,
sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( path , used_quota_size , used_quota_files , last_quota_update , name , description , filesystem )
VALUES ( % s , % s , % s , % s , % s , % s , % s ) ON CONFLICT ( name ) DO UPDATE SET path = EXCLUDED . path , description = EXCLUDED . description ,
2022-04-28 12:49:57 +00:00
filesystem = EXCLUDED . filesystem ` , sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] )
}
2022-04-25 13:49:11 +00:00
func getClearUserGroupMappingQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE user_id = (SELECT id FROM %s WHERE username = %s) ` , sqlTableUsersGroupsMapping ,
2020-06-07 21:30:18 +00:00
sqlTableUsers , sqlPlaceholders [ 0 ] )
}
2022-04-25 13:49:11 +00:00
func getAddUserGroupMappingQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( user_id , group_id , group_type ) VALUES ( ( SELECT id FROM % s WHERE username = % s ) ,
( SELECT id FROM % s WHERE name = % s ) , % s ) ` ,
2022-07-11 06:17:36 +00:00
sqlTableUsersGroupsMapping , sqlTableUsers , sqlPlaceholders [ 0 ] , getSQLQuotedName ( sqlTableGroups ) ,
sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
2022-04-25 13:49:11 +00:00
}
2022-09-13 16:04:27 +00:00
func getClearAdminGroupMappingQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE admin_id = (SELECT id FROM %s WHERE username = %s) ` , sqlTableAdminsGroupsMapping ,
sqlTableAdmins , sqlPlaceholders [ 0 ] )
}
func getAddAdminGroupMappingQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % s ( admin_id , group_id , options ) VALUES ( ( SELECT id FROM % s WHERE username = % s ) ,
( SELECT id FROM % s WHERE name = % s ) , % s ) ` ,
sqlTableAdminsGroupsMapping , sqlTableAdmins , sqlPlaceholders [ 0 ] , getSQLQuotedName ( sqlTableGroups ) ,
sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
2022-04-25 13:49:11 +00:00
func getClearGroupFolderMappingQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE group_id = (SELECT id FROM %s WHERE name = %s) ` , sqlTableGroupsFoldersMapping ,
2022-07-11 06:17:36 +00:00
getSQLQuotedName ( sqlTableGroups ) , sqlPlaceholders [ 0 ] )
2022-04-25 13:49:11 +00:00
}
func getAddGroupFolderMappingQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( virtual_path , quota_size , quota_files , folder_id , group_id )
VALUES ( % s , % s , % s , ( SELECT id FROM % s WHERE name = % s ) , ( SELECT id FROM % s WHERE name = % s ) ) ` ,
2022-04-25 13:49:11 +00:00
sqlTableGroupsFoldersMapping , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlTableFolders ,
2022-07-11 06:17:36 +00:00
sqlPlaceholders [ 3 ] , getSQLQuotedName ( sqlTableGroups ) , sqlPlaceholders [ 4 ] )
2022-04-25 13:49:11 +00:00
}
func getClearUserFolderMappingQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE user_id = (SELECT id FROM %s WHERE username = %s) ` , sqlTableUsersFoldersMapping ,
2022-04-25 13:49:11 +00:00
sqlTableUsers , sqlPlaceholders [ 0 ] )
}
func getAddUserFolderMappingQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( virtual_path , quota_size , quota_files , folder_id , user_id )
VALUES ( % s , % s , % s , ( SELECT id FROM % s WHERE name = % s ) , ( SELECT id FROM % s WHERE username = % s ) ) ` ,
2022-04-28 12:49:57 +00:00
sqlTableUsersFoldersMapping , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlTableFolders ,
sqlPlaceholders [ 3 ] , sqlTableUsers , sqlPlaceholders [ 4 ] )
2020-06-07 21:30:18 +00:00
}
2022-04-25 13:49:11 +00:00
func getFoldersQuery ( order string , minimal bool ) string {
var fieldSelection string
if minimal {
2022-07-11 06:17:36 +00:00
fieldSelection = selectMinimalFields
2022-04-25 13:49:11 +00:00
} else {
fieldSelection = selectFolderFields
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT %s FROM %s ORDER BY name %s LIMIT %s OFFSET %s ` , fieldSelection , sqlTableFolders ,
2020-06-07 21:30:18 +00:00
order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getUpdateFolderQuotaQuery ( reset bool ) string {
if reset {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET used_quota_size = % s , used_quota_files = % s , last_quota_update = % s
WHERE name = % s ` , sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2020-06-07 21:30:18 +00:00
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE % s SET used_quota_size = used_quota_size + % s , used_quota_files = used_quota_files + % s , last_quota_update = % s
WHERE name = % s ` , sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2020-06-07 21:30:18 +00:00
}
func getQuotaFolderQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT used_quota_size,used_quota_files FROM %s WHERE name = %s ` , sqlTableFolders ,
2020-06-07 21:30:18 +00:00
sqlPlaceholders [ 0 ] )
}
2022-04-25 13:49:11 +00:00
func getRelatedGroupsForUsersQuery ( users [ ] User ) string {
var sb strings . Builder
for _ , u := range users {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( u . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT g . name , ug . group_type , ug . user_id FROM % s g INNER JOIN % s ug ON g . id = ug . group_id WHERE
2022-07-11 06:17:36 +00:00
ug . user_id IN % s ORDER BY ug . user_id ` , getSQLQuotedName ( sqlTableGroups ) , sqlTableUsersGroupsMapping , sb . String ( ) )
2022-04-25 13:49:11 +00:00
}
2022-09-13 16:04:27 +00:00
func getRelatedGroupsForAdminsQuery ( admins [ ] Admin ) string {
var sb strings . Builder
for _ , a := range admins {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( a . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
return fmt . Sprintf ( ` SELECT g . name , ag . options , ag . admin_id FROM % s g INNER JOIN % s ag ON g . id = ag . group_id WHERE
ag . admin_id IN % s ORDER BY ag . admin_id ` , getSQLQuotedName ( sqlTableGroups ) , sqlTableAdminsGroupsMapping , sb . String ( ) )
}
2020-06-07 21:30:18 +00:00
func getRelatedFoldersForUsersQuery ( users [ ] User ) string {
var sb strings . Builder
for _ , u := range users {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( u . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
2021-03-21 18:15:47 +00:00
return fmt . Sprintf ( ` SELECT f . id , f . name , f . path , f . used_quota_size , f . used_quota_files , f . last_quota_update , fm . virtual_path ,
2022-06-24 11:16:45 +00:00
fm . quota_size , fm . quota_files , fm . user_id , f . filesystem , f . description FROM % s f INNER JOIN % s fm ON f . id = fm . folder_id WHERE
fm . user_id IN % s ORDER BY fm . user_id ` , sqlTableFolders , sqlTableUsersFoldersMapping , sb . String ( ) )
2020-06-07 21:30:18 +00:00
}
func getRelatedUsersForFoldersQuery ( folders [ ] vfs . BaseVirtualFolder ) string {
var sb strings . Builder
for _ , f := range folders {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( f . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT fm . folder_id , u . username FROM % s fm INNER JOIN % s u ON fm . user_id = u . id
WHERE fm . folder_id IN % s ORDER BY fm . folder_id ` , sqlTableUsersFoldersMapping , sqlTableUsers , sb . String ( ) )
2022-04-25 13:49:11 +00:00
}
func getRelatedGroupsForFoldersQuery ( folders [ ] vfs . BaseVirtualFolder ) string {
var sb strings . Builder
for _ , f := range folders {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( f . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT fm . folder_id , g . name FROM % s fm INNER JOIN % s g ON fm . group_id = g . id
2022-07-11 06:17:36 +00:00
WHERE fm . folder_id IN % s ORDER BY fm . folder_id ` , sqlTableGroupsFoldersMapping , getSQLQuotedName ( sqlTableGroups ) ,
sb . String ( ) )
2022-04-25 13:49:11 +00:00
}
func getRelatedUsersForGroupsQuery ( groups [ ] Group ) string {
var sb strings . Builder
for _ , g := range groups {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( g . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` SELECT um . group_id , u . username FROM % s um INNER JOIN % s u ON um . user_id = u . id
WHERE um . group_id IN % s ORDER BY um . group_id ` , sqlTableUsersGroupsMapping , sqlTableUsers , sb . String ( ) )
2022-04-25 13:49:11 +00:00
}
2022-09-13 16:04:27 +00:00
func getRelatedAdminsForGroupsQuery ( groups [ ] Group ) string {
var sb strings . Builder
for _ , g := range groups {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( g . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
return fmt . Sprintf ( ` SELECT am . group_id , a . username FROM % s am INNER JOIN % s a ON am . admin_id = a . id
WHERE am . group_id IN % s ORDER BY am . group_id ` , sqlTableAdminsGroupsMapping , sqlTableAdmins , sb . String ( ) )
}
2022-04-25 13:49:11 +00:00
func getRelatedFoldersForGroupsQuery ( groups [ ] Group ) string {
var sb strings . Builder
for _ , g := range groups {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( g . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
return fmt . Sprintf ( ` SELECT f . id , f . name , f . path , f . used_quota_size , f . used_quota_files , f . last_quota_update , fm . virtual_path ,
fm . quota_size , fm . quota_files , fm . group_id , f . filesystem , f . description FROM % s f INNER JOIN % s fm ON f . id = fm . folder_id WHERE
2022-06-24 11:16:45 +00:00
fm . group_id IN % s ORDER BY fm . group_id ` , sqlTableFolders , sqlTableGroupsFoldersMapping , sb . String ( ) )
2019-07-20 10:26:52 +00:00
}
2020-02-08 13:44:25 +00:00
2022-01-30 10:42:36 +00:00
func getActiveTransfersQuery ( ) string {
return fmt . Sprintf ( ` SELECT transfer_id , connection_id , transfer_type , username , folder_name , ip , truncated_size ,
2022-06-24 11:16:45 +00:00
current_ul_size , current_dl_size , created_at , updated_at FROM % s WHERE updated_at > % s ` ,
2022-01-30 10:42:36 +00:00
sqlTableActiveTransfers , sqlPlaceholders [ 0 ] )
}
func getAddActiveTransferQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` INSERT INTO % s ( transfer_id , connection_id , transfer_type , username , folder_name , ip , truncated_size ,
current_ul_size , current_dl_size , created_at , updated_at ) VALUES ( % s , % s , % s , % s , % s , % s , % s , % s , % s , % s , % s ) ` ,
2022-01-30 10:42:36 +00:00
sqlTableActiveTransfers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] ,
sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] ,
sqlPlaceholders [ 9 ] , sqlPlaceholders [ 10 ] )
}
func getUpdateActiveTransferSizesQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET current_ul_size=%s,current_dl_size=%s,updated_at=%s WHERE connection_id = %s AND transfer_id = %s ` ,
2022-01-30 10:42:36 +00:00
sqlTableActiveTransfers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] )
}
func getRemoveActiveTransferQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE connection_id = %s AND transfer_id = %s ` ,
2022-01-30 10:42:36 +00:00
sqlTableActiveTransfers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getCleanupActiveTransfersQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` DELETE FROM %s WHERE updated_at < %s ` , sqlTableActiveTransfers , sqlPlaceholders [ 0 ] )
2022-01-30 10:42:36 +00:00
}
2022-07-11 06:17:36 +00:00
func getRelatedRulesForActionsQuery ( actions [ ] BaseEventAction ) string {
var sb strings . Builder
for _ , a := range actions {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( a . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
return fmt . Sprintf ( ` SELECT am . action_id , r . name FROM % s am INNER JOIN % s r ON am . rule_id = r . id
WHERE am . action_id IN % s ORDER BY r . name ASC ` , sqlTableRulesActionsMapping , sqlTableEventsRules , sb . String ( ) )
}
func getEventsActionsQuery ( order string , minimal bool ) string {
var fieldSelection string
if minimal {
fieldSelection = selectMinimalFields
} else {
fieldSelection = selectEventActionFields
}
return fmt . Sprintf ( ` SELECT %s FROM %s ORDER BY name %s LIMIT %s OFFSET %s ` , fieldSelection ,
sqlTableEventsActions , order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDumpEventActionsQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s ` , selectEventActionFields , sqlTableEventsActions )
}
func getEventActionByNameQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE name = %s ` , selectEventActionFields , sqlTableEventsActions ,
sqlPlaceholders [ 0 ] )
}
func getAddEventActionQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO %s (name,description,type,options) VALUES (%s,%s,%s,%s) ` ,
sqlTableEventsActions , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
func getUpdateEventActionQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %s SET description=%s,type=%s,options=%s WHERE name = %s ` , sqlTableEventsActions ,
sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
func getDeleteEventActionQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE name = %s ` , sqlTableEventsActions , sqlPlaceholders [ 0 ] )
}
func getEventRulesQuery ( order string ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE deleted_at = 0 ORDER BY name %s LIMIT %s OFFSET %s ` ,
getSelectEventRuleFields ( ) , sqlTableEventsRules , order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDumpEventRulesQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE deleted_at = 0 ` , getSelectEventRuleFields ( ) , sqlTableEventsRules )
}
func getRecentlyUpdatedRulesQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE updated_at >= %s OR deleted_at > 0 ` , getSelectEventRuleFields ( ) ,
sqlTableEventsRules , sqlPlaceholders [ 0 ] )
}
func getEventRulesByNameQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE name = %s AND deleted_at = 0 ` , getSelectEventRuleFields ( ) , sqlTableEventsRules ,
sqlPlaceholders [ 0 ] )
}
func getAddEventRuleQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % s ( name , description , created_at , updated_at , % s , conditions , deleted_at )
VALUES ( % s , % s , % s , % s , % s , % s , 0 ) ` ,
sqlTableEventsRules , getSQLQuotedName ( "trigger" ) , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] )
}
func getUpdateEventRuleQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %s SET description=%s,updated_at=%s,%s=%s,conditions=%s WHERE name = %s ` ,
sqlTableEventsRules , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , getSQLQuotedName ( "trigger" ) , sqlPlaceholders [ 2 ] ,
sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] )
}
func getDeleteEventRuleQuery ( softDelete bool ) string {
if softDelete {
return fmt . Sprintf ( ` UPDATE %s SET updated_at=%s,deleted_at=%s WHERE name = %s ` ,
sqlTableEventsRules , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
return fmt . Sprintf ( ` DELETE FROM %s WHERE name = %s ` , sqlTableEventsRules , sqlPlaceholders [ 0 ] )
}
2022-11-01 16:39:53 +00:00
func getRemoveSoftDeletedRuleQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE name = %s AND deleted_at > 0 ` , sqlTableEventsRules , sqlPlaceholders [ 0 ] )
}
2022-07-11 06:17:36 +00:00
func getClearRuleActionMappingQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE rule_id = (SELECT id FROM %s WHERE name = %s) ` , sqlTableRulesActionsMapping ,
sqlTableEventsRules , sqlPlaceholders [ 0 ] )
}
func getUpdateRulesTimestampQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %s SET updated_at=%s WHERE id IN (SELECT rule_id FROM %s WHERE action_id = %s) ` ,
sqlTableEventsRules , sqlPlaceholders [ 0 ] , sqlTableRulesActionsMapping , sqlPlaceholders [ 1 ] )
}
func getRelatedActionsForRulesQuery ( rules [ ] EventRule ) string {
var sb strings . Builder
for _ , r := range rules {
if sb . Len ( ) == 0 {
sb . WriteString ( "(" )
} else {
sb . WriteString ( "," )
}
sb . WriteString ( strconv . FormatInt ( r . ID , 10 ) )
}
if sb . Len ( ) > 0 {
sb . WriteString ( ")" )
}
return fmt . Sprintf ( ` SELECT a . id , a . name , a . description , a . type , a . options , am . options , am . % s ,
am . rule_id FROM % s a INNER JOIN % s am ON a . id = am . action_id WHERE am . rule_id IN % s ORDER BY am . % s ASC ` ,
getSQLQuotedName ( "order" ) , sqlTableEventsActions , sqlTableRulesActionsMapping , sb . String ( ) ,
getSQLQuotedName ( "order" ) )
}
func getAddRuleActionMappingQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % s ( rule_id , action_id , % s , options ) VALUES ( ( SELECT id FROM % s WHERE name = % s ) ,
( SELECT id FROM % s WHERE name = % s ) , % s , % s ) ` ,
sqlTableRulesActionsMapping , getSQLQuotedName ( "order" ) , sqlTableEventsRules , sqlPlaceholders [ 0 ] ,
sqlTableEventsActions , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
func getTaskByNameQuery ( ) string {
return fmt . Sprintf ( ` SELECT updated_at,version FROM %s WHERE name = %s ` , sqlTableTasks , sqlPlaceholders [ 0 ] )
}
func getAddTaskQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO %s (name,updated_at,version) VALUES (%s,%s,0) ` ,
sqlTableTasks , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getUpdateTaskQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %s SET updated_at=%s,version = version + 1 WHERE name = %s AND version = %s ` ,
sqlTableTasks , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
func getUpdateTaskTimestampQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %s SET updated_at=%s WHERE name = %s ` ,
sqlTableTasks , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDeleteTaskQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE name = %s ` , sqlTableTasks , sqlPlaceholders [ 0 ] )
}
2022-09-25 17:48:55 +00:00
func getAddNodeQuery ( ) string {
if config . Driver == MySQLDataProviderName {
return fmt . Sprintf ( "INSERT INTO %s (`name`,`data`,created_at,`updated_at`) VALUES (%s,%s,%s,%s) ON DUPLICATE KEY UPDATE " +
"`data`=VALUES(`data`), `created_at`=VALUES(`created_at`), `updated_at`=VALUES(`updated_at`)" ,
sqlTableNodes , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
return fmt . Sprintf ( ` INSERT INTO % s ( name , data , created_at , updated_at ) VALUES ( % s , % s , % s , % s ) ON CONFLICT ( name )
DO UPDATE SET data = EXCLUDED . data , created_at = EXCLUDED . created_at , updated_at = EXCLUDED . updated_at ` ,
sqlTableNodes , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
func getUpdateNodeTimestampQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %s SET updated_at=%s WHERE name = %s ` ,
sqlTableNodes , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getNodeByNameQuery ( ) string {
return fmt . Sprintf ( ` SELECT name,data,created_at,updated_at FROM %s WHERE name = %s AND updated_at > %s ` ,
sqlTableNodes , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getNodesQuery ( ) string {
return fmt . Sprintf ( ` SELECT name,data,created_at,updated_at FROM %s WHERE name != %s AND updated_at > %s ` ,
sqlTableNodes , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getCleanupNodesQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE updated_at < %s ` , sqlTableNodes , sqlPlaceholders [ 0 ] )
}
2020-02-08 13:44:25 +00:00
func getDatabaseVersionQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( "SELECT version from %s LIMIT 1" , sqlTableSchemaVersion )
2020-02-08 13:44:25 +00:00
}
func getUpdateDBVersionQuery ( ) string {
2022-06-24 11:16:45 +00:00
return fmt . Sprintf ( ` UPDATE %s SET version=%s ` , sqlTableSchemaVersion , sqlPlaceholders [ 0 ] )
2020-06-07 21:30:18 +00:00
}