2019-07-20 10:26:52 +00:00
package dataprovider
2020-06-07 21:30:18 +00:00
import (
"fmt"
"strconv"
"strings"
2021-06-26 05:31:41 +00:00
"github.com/drakkan/sftpgo/v2/vfs"
2020-06-07 21:30:18 +00:00
)
2019-07-20 10:26:52 +00:00
const (
2019-12-30 17:37:50 +00:00
selectUserFields = "id,username,password,public_keys,home_dir,uid,gid,max_sessions,quota_size,quota_files,permissions,used_quota_size," +
2021-02-24 18:40:29 +00:00
"used_quota_files,last_quota_update,upload_bandwidth,download_bandwidth,expiration_date,last_login,status,filters,filesystem," +
2022-01-30 10:42:36 +00:00
"additional_info,description,email,created_at,updated_at,upload_data_transfer,download_data_transfer,total_data_transfer," +
"used_upload_data_transfer,used_download_data_transfer"
2021-03-21 18:15:47 +00:00
selectFolderFields = "id,path,used_quota_size,used_quota_files,last_quota_update,name,description,filesystem"
2021-08-19 13:51:43 +00:00
selectAdminFields = "id,username,password,status,email,permissions,filters,additional_info,description,created_at,updated_at,last_login"
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-04-25 13:49:11 +00:00
selectGroupFields = "id,name,description,created_at,updated_at,user_settings"
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 {
2019-07-20 10:26:52 +00:00
placeholders = append ( placeholders , fmt . Sprintf ( "$%v" , i ) )
} else {
placeholders = append ( placeholders , "?" )
}
}
return placeholders
}
2021-12-25 11:08:07 +00:00
func getAddDefenderHostQuery ( ) string {
if config . Driver == MySQLDataProviderName {
return fmt . Sprintf ( "INSERT INTO %v (`ip`,`updated_at`,`ban_time`) VALUES (%v,%v,0) ON DUPLICATE KEY UPDATE `updated_at`=VALUES(`updated_at`)" ,
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
return fmt . Sprintf ( ` INSERT INTO %v (ip,updated_at,ban_time) VALUES (%v,%v,0) ON CONFLICT (ip) DO UPDATE SET updated_at = EXCLUDED.updated_at RETURNING id ` ,
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getAddDefenderEventQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO %v (date_time,score,host_id) VALUES (%v,%v,(SELECT id from %v WHERE ip = %v)) ` ,
sqlTableDefenderEvents , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlTableDefenderHosts , sqlPlaceholders [ 2 ] )
}
func getDefenderHostsQuery ( ) string {
2022-03-16 13:53:00 +00:00
return fmt . Sprintf ( ` SELECT id,ip,ban_time FROM %v WHERE updated_at >= %v OR ban_time > 0 ORDER BY updated_at DESC LIMIT %v ` ,
2021-12-25 11:08:07 +00:00
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDefenderHostQuery ( ) string {
2022-03-16 17:22:08 +00:00
return fmt . Sprintf ( ` SELECT id,ip,ban_time FROM %v WHERE ip = %v AND (updated_at >= %v 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)" )
}
return fmt . Sprintf ( ` SELECT host_id,SUM(score) FROM %v WHERE date_time >= %v AND host_id IN %v GROUP BY host_id ` ,
sqlTableDefenderEvents , sqlPlaceholders [ 0 ] , sb . String ( ) )
}
func getDefenderIsHostBannedQuery ( ) string {
return fmt . Sprintf ( ` SELECT id FROM %v WHERE ip = %v AND ban_time >= %v ` ,
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDefenderIncrementBanTimeQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %v SET ban_time = ban_time + %v WHERE ip = %v ` ,
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDefenderSetBanTimeQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %v SET ban_time = %v WHERE ip = %v ` ,
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDeleteDefenderHostQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE ip = %v ` , sqlTableDefenderHosts , sqlPlaceholders [ 0 ] )
}
func getDefenderHostsCleanupQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM % v WHERE ban_time < % v AND NOT EXISTS (
SELECT id FROM % v WHERE % v . host_id = % v . id AND % v . date_time > % v ) ` ,
sqlTableDefenderHosts , sqlPlaceholders [ 0 ] , sqlTableDefenderEvents , sqlTableDefenderEvents , sqlTableDefenderHosts ,
sqlTableDefenderEvents , sqlPlaceholders [ 1 ] )
}
func getDefenderEventsCleanupQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE date_time < %v ` , sqlTableDefenderEvents , sqlPlaceholders [ 0 ] )
}
2022-04-25 13:49:11 +00:00
func getGroupByNameQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE name = %s ` , selectGroupFields , sqlTableGroups , sqlPlaceholders [ 0 ] )
}
func getGroupsQuery ( order string , minimal bool ) string {
var fieldSelection string
if minimal {
fieldSelection = "id,name"
} else {
fieldSelection = selectGroupFields
}
return fmt . Sprintf ( ` SELECT %s FROM %s ORDER BY name %s LIMIT %v OFFSET %v ` , fieldSelection , sqlTableGroups ,
order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
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 ( "('')" )
}
return fmt . Sprintf ( ` SELECT %s FROM %s WHERE name in %s ` , selectGroupFields , sqlTableGroups , sb . String ( ) )
}
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))) ` ,
sqlTableUsers , sqlTableUsersGroupsMapping , sqlTableGroups , sb . String ( ) )
}
func getDumpGroupsQuery ( ) string {
return fmt . Sprintf ( ` SELECT %s FROM %s ` , selectGroupFields , sqlTableGroups )
}
func getAddGroupQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % s ( name , description , created_at , updated_at , user_settings )
VALUES ( % v , % v , % v , % v , % v ) ` , sqlTableGroups , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] ,
sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] )
}
func getUpdateGroupQuery ( ) string {
return fmt . Sprintf ( ` UPDATE % s SET description = % v , user_settings = % v , updated_at = % v
WHERE name = % s ` , sqlTableGroups , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
sqlPlaceholders [ 3 ] )
}
func getDeleteGroupQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %s WHERE name = %s ` , sqlTableGroups , sqlPlaceholders [ 0 ] )
}
2021-01-17 21:29:08 +00:00
func getAdminByUsernameQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v WHERE username = %v ` , selectAdminFields , sqlTableAdmins , sqlPlaceholders [ 0 ] )
2019-07-20 10:26:52 +00:00
}
2021-01-17 21:29:08 +00:00
func getAdminsQuery ( order string ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v ORDER BY username %v LIMIT %v OFFSET %v ` , selectAdminFields , sqlTableAdmins ,
order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2019-07-20 10:26:52 +00:00
}
2021-01-17 21:29:08 +00:00
func getDumpAdminsQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v ` , selectAdminFields , sqlTableAdmins )
}
func getAddAdminQuery ( ) string {
2021-08-19 13:51:43 +00:00
return fmt . Sprintf ( ` INSERT INTO % v ( username , password , status , email , permissions , filters , additional_info , description , created_at , updated_at , last_login )
VALUES ( % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , 0 ) ` , sqlTableAdmins , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] ,
sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] ,
sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] )
2021-01-17 21:29:08 +00:00
}
func getUpdateAdminQuery ( ) string {
2021-08-19 13:51:43 +00:00
return fmt . Sprintf ( ` UPDATE % v SET password = % v , status = % v , email = % v , permissions = % v , filters = % v , additional_info = % v , description = % v , updated_at = % v
2021-01-17 21:29:08 +00:00
WHERE username = % v ` , sqlTableAdmins , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
2021-08-19 13:51:43 +00:00
sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] )
2021-01-17 21:29:08 +00:00
}
func getDeleteAdminQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE username = %v ` , sqlTableAdmins , sqlPlaceholders [ 0 ] )
}
2021-11-06 13:13:20 +00:00
func getShareByIDQuery ( filterUser bool ) string {
if filterUser {
return fmt . Sprintf ( ` SELECT %v FROM %v s INNER JOIN %v u ON s.user_id = u.id WHERE s.share_id = %v AND u.username = %v ` ,
selectShareFields , sqlTableShares , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
return fmt . Sprintf ( ` SELECT %v FROM %v s INNER JOIN %v u ON s.user_id = u.id WHERE s.share_id = %v ` ,
selectShareFields , sqlTableShares , sqlTableUsers , sqlPlaceholders [ 0 ] )
}
func getSharesQuery ( order string ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v s INNER JOIN %v u ON s.user_id = u.id WHERE u.username = %v ORDER BY s.share_id %v LIMIT %v OFFSET %v ` ,
selectShareFields , sqlTableShares , sqlTableUsers , sqlPlaceholders [ 0 ] , order , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
func getDumpSharesQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v s INNER JOIN %v u ON s.user_id = u.id ` ,
selectShareFields , sqlTableShares , sqlTableUsers )
}
func getAddShareQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % v ( share_id , name , description , scope , paths , created_at , updated_at , last_use_at ,
2021-11-27 10:12:51 +00:00
expires_at , password , max_tokens , used_tokens , allow_from , user_id ) VALUES ( % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , % v ) ` ,
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 {
return fmt . Sprintf ( ` UPDATE % v SET name = % v , description = % v , scope = % v , paths = % v , created_at = % v , updated_at = % v ,
last_use_at = % v , expires_at = % v , password = % v , max_tokens = % v , used_tokens = % v , allow_from = % v , user_id = % v WHERE share_id = % v ` , 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 ] , sqlPlaceholders [ 12 ] , sqlPlaceholders [ 13 ] )
2021-11-06 13:13:20 +00:00
}
func getUpdateShareQuery ( ) string {
return fmt . Sprintf ( ` UPDATE % v SET name = % v , description = % v , scope = % v , paths = % v , updated_at = % v , expires_at = % v ,
password = % v , max_tokens = % v , allow_from = % v , user_id = % v WHERE share_id = % v ` , sqlTableShares ,
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 {
return fmt . Sprintf ( ` DELETE FROM %v WHERE share_id = %v ` , sqlTableShares , sqlPlaceholders [ 0 ] )
}
2021-08-17 16:08:32 +00:00
func getAPIKeyByIDQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v WHERE key_id = %v ` , selectAPIKeyFields , sqlTableAPIKeys , sqlPlaceholders [ 0 ] )
}
func getAPIKeysQuery ( order string ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v ORDER BY key_id %v LIMIT %v OFFSET %v ` , selectAPIKeyFields , sqlTableAPIKeys ,
order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getDumpAPIKeysQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v ` , selectAPIKeyFields , sqlTableAPIKeys )
}
func getAddAPIKeyQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % v ( key_id , name , api_key , scope , created_at , updated_at , last_use_at , expires_at , description , user_id , admin_id )
VALUES ( % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , % v ) ` , sqlTableAPIKeys , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] ,
sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] ,
sqlPlaceholders [ 7 ] , sqlPlaceholders [ 8 ] , sqlPlaceholders [ 9 ] , sqlPlaceholders [ 10 ] )
}
func getUpdateAPIKeyQuery ( ) string {
return fmt . Sprintf ( ` UPDATE % v SET name = % v , scope = % v , expires_at = % v , user_id = % v , admin_id = % v , description = % v , updated_at = % v
WHERE key_id = % v ` , sqlTableAPIKeys , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] , sqlPlaceholders [ 7 ] )
}
func getDeleteAPIKeyQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE key_id = %v ` , sqlTableAPIKeys , sqlPlaceholders [ 0 ] )
}
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)" )
}
return fmt . Sprintf ( ` SELECT id,username FROM %v WHERE id IN %v ` , sqlTableUsers , sb . String ( ) )
}
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)" )
}
return fmt . Sprintf ( ` SELECT id,username FROM %v WHERE id IN %v ` , sqlTableAdmins , sb . String ( ) )
}
2021-01-17 21:29:08 +00:00
func getUserByUsernameQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v WHERE username = %v ` , selectUserFields , sqlTableUsers , sqlPlaceholders [ 0 ] )
}
func getUsersQuery ( order string ) string {
2020-06-07 21:30:18 +00:00
return fmt . Sprintf ( ` SELECT %v FROM %v ORDER BY username %v LIMIT %v OFFSET %v ` , selectUserFields , sqlTableUsers ,
2019-07-20 10:26:52 +00:00
order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
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 ,
download_data_transfer , used_upload_data_transfer , used_download_data_transfer , filters FROM % v WHERE username IN % v ` ,
2022-01-20 17:19:20 +00:00
sqlTableUsers , sb . String ( ) )
}
2021-08-20 07:35:06 +00:00
func getRecentlyUpdatedUsersQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v WHERE updated_at >= %v ` , selectUserFields , sqlTableUsers , sqlPlaceholders [ 0 ] )
}
2019-12-27 22:12:44 +00:00
func getDumpUsersQuery ( ) string {
2020-06-07 21:30:18 +00:00
return fmt . Sprintf ( ` SELECT %v FROM %v ` , selectUserFields , sqlTableUsers )
}
func getDumpFoldersQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v ` , 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 {
return fmt . Sprintf ( ` UPDATE % v SET used_upload_data_transfer = % v , used_download_data_transfer = % v , last_quota_update = % v
WHERE username = % v ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
return fmt . Sprintf ( ` UPDATE % v SET used_upload_data_transfer = used_upload_data_transfer + % v ,
used_download_data_transfer = used_download_data_transfer + % v , last_quota_update = % v
WHERE username = % v ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
}
2019-07-28 11:24:46 +00:00
func getUpdateQuotaQuery ( reset bool ) string {
if reset {
2019-11-13 10:36:21 +00:00
return fmt . Sprintf ( ` UPDATE % v SET used_quota_size = % v , used_quota_files = % v , last_quota_update = % v
2020-06-07 21:30:18 +00:00
WHERE username = % v ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2019-07-28 11:24:46 +00:00
}
2019-11-13 10:36:21 +00:00
return fmt . Sprintf ( ` UPDATE % v SET used_quota_size = used_quota_size + % v , used_quota_files = used_quota_files + % v , last_quota_update = % v
2020-06-07 21:30:18 +00:00
WHERE username = % v ` , 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 {
return fmt . Sprintf ( ` UPDATE %v SET updated_at = %v WHERE username = %v ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
2019-11-13 10:36:21 +00:00
func getUpdateLastLoginQuery ( ) string {
2020-06-07 21:30:18 +00:00
return fmt . Sprintf ( ` UPDATE %v SET last_login = %v WHERE username = %v ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
2019-11-13 10:36:21 +00:00
}
2021-08-19 13:51:43 +00:00
func getUpdateAdminLastLoginQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %v SET last_login = %v WHERE username = %v ` , sqlTableAdmins , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
2021-08-17 16:08:32 +00:00
func getUpdateAPIKeyLastUseQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %v SET last_use_at = %v WHERE key_id = %v ` , sqlTableAPIKeys , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
2021-11-06 13:13:20 +00:00
func getUpdateShareLastUseQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %v SET last_use_at = %v, used_tokens = used_tokens +%v WHERE share_id = %v ` ,
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 ,
used_download_data_transfer FROM % v WHERE username = % v ` ,
sqlTableUsers , sqlPlaceholders [ 0 ] )
2019-07-20 10:26:52 +00:00
}
func getAddUserQuery ( ) string {
2019-08-07 21:41:10 +00:00
return fmt . Sprintf ( ` INSERT INTO % v ( 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 ,
used_upload_data_transfer , used_download_data_transfer )
VALUES ( % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , 0 , 0 , 0 , % v , % v , % v , 0 , % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , 0 , 0 ) ` ,
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 ] ,
sqlPlaceholders [ 20 ] , sqlPlaceholders [ 21 ] , sqlPlaceholders [ 22 ] , sqlPlaceholders [ 23 ] )
2019-07-20 10:26:52 +00:00
}
func getUpdateUserQuery ( ) string {
2019-08-07 21:41:10 +00:00
return fmt . Sprintf ( ` UPDATE % v SET password = % v , public_keys = % v , home_dir = % v , uid = % v , gid = % v , max_sessions = % v , quota_size = % v ,
2020-11-25 21:26:34 +00:00
quota_files = % v , permissions = % v , upload_bandwidth = % v , download_bandwidth = % v , status = % v , expiration_date = % v , filters = % v , filesystem = % v ,
2022-01-30 10:42:36 +00:00
additional_info = % v , description = % v , email = % v , updated_at = % v , upload_data_transfer = % v , download_data_transfer = % v ,
total_data_transfer = % v WHERE id = % v ` ,
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 ] ,
sqlPlaceholders [ 20 ] , sqlPlaceholders [ 21 ] , sqlPlaceholders [ 22 ] )
2019-07-20 10:26:52 +00:00
}
2022-04-02 20:20:21 +00:00
func getUpdateUserPasswordQuery ( ) string {
return fmt . Sprintf ( ` UPDATE %v SET password=%v WHERE username = %v ` , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
2019-07-20 10:26:52 +00:00
func getDeleteUserQuery ( ) string {
2020-06-07 21:30:18 +00:00
return fmt . Sprintf ( ` DELETE FROM %v WHERE id = %v ` , sqlTableUsers , sqlPlaceholders [ 0 ] )
}
2021-02-01 18:04:15 +00:00
func getFolderByNameQuery ( ) string {
return fmt . Sprintf ( ` SELECT %v FROM %v WHERE name = %v ` , selectFolderFields , sqlTableFolders , sqlPlaceholders [ 0 ] )
2020-06-07 21:30:18 +00:00
}
func getAddFolderQuery ( ) string {
2021-03-21 18:15:47 +00:00
return fmt . Sprintf ( ` INSERT INTO % v ( path , used_quota_size , used_quota_files , last_quota_update , name , description , filesystem )
VALUES ( % v , % v , % v , % v , % v , % v , % v ) ` , sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] ,
sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] , sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] )
2021-02-01 18:04:15 +00:00
}
func getUpdateFolderQuery ( ) string {
2021-03-21 18:15:47 +00:00
return fmt . Sprintf ( ` UPDATE %v SET path=%v,description=%v,filesystem=%v WHERE name = %v ` , sqlTableFolders , sqlPlaceholders [ 0 ] ,
sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2020-06-07 21:30:18 +00:00
}
func getDeleteFolderQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE id = %v ` , sqlTableFolders , sqlPlaceholders [ 0 ] )
}
2022-04-28 12:49:57 +00:00
func getUpsertFolderQuery ( ) string {
if config . Driver == MySQLDataProviderName {
return fmt . Sprintf ( "INSERT INTO %v (`path`,`used_quota_size`,`used_quota_files`,`last_quota_update`,`name`," +
"`description`,`filesystem`) VALUES (%v,%v,%v,%v,%v,%v,%v) ON DUPLICATE KEY UPDATE " +
"`path`=VALUES(`path`),`description`=VALUES(`description`),`filesystem`=VALUES(`filesystem`)" ,
sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] ,
sqlPlaceholders [ 5 ] , sqlPlaceholders [ 6 ] )
}
return fmt . Sprintf ( ` INSERT INTO % v ( path , used_quota_size , used_quota_files , last_quota_update , name , description , filesystem )
VALUES ( % v , % v , % v , % v , % v , % v , % v ) ON CONFLICT ( name ) DO UPDATE SET path = EXCLUDED . path , description = EXCLUDED . description ,
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 {
return fmt . Sprintf ( ` DELETE FROM %v WHERE user_id = (SELECT id FROM %v WHERE username = %v) ` , sqlTableUsersGroupsMapping ,
2020-06-07 21:30:18 +00:00
sqlTableUsers , sqlPlaceholders [ 0 ] )
}
2022-04-25 13:49:11 +00:00
func getAddUserGroupMappingQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % v ( user_id , group_id , group_type ) VALUES ( ( SELECT id FROM % v WHERE username = % v ) ,
( SELECT id FROM % v WHERE name = % v ) , % v ) ` ,
sqlTableUsersGroupsMapping , sqlTableUsers , sqlPlaceholders [ 0 ] , sqlTableGroups , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] )
}
func getClearGroupFolderMappingQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE group_id = (SELECT id FROM %v WHERE name = %v) ` , sqlTableGroupsFoldersMapping ,
sqlTableGroups , sqlPlaceholders [ 0 ] )
}
func getAddGroupFolderMappingQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % v ( virtual_path , quota_size , quota_files , folder_id , group_id )
VALUES ( % v , % v , % v , ( SELECT id FROM % v WHERE name = % v ) , ( SELECT id FROM % v WHERE name = % v ) ) ` ,
sqlTableGroupsFoldersMapping , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlTableFolders ,
sqlPlaceholders [ 3 ] , sqlTableGroups , sqlPlaceholders [ 4 ] )
}
func getClearUserFolderMappingQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE user_id = (SELECT id FROM %v WHERE username = %v) ` , sqlTableUsersFoldersMapping ,
sqlTableUsers , sqlPlaceholders [ 0 ] )
}
func getAddUserFolderMappingQuery ( ) string {
2020-06-07 21:30:18 +00:00
return fmt . Sprintf ( ` INSERT INTO % v ( virtual_path , quota_size , quota_files , folder_id , user_id )
2022-04-28 12:49:57 +00:00
VALUES ( % v , % v , % v , ( SELECT id FROM % v WHERE name = % v ) , ( SELECT id FROM % v WHERE username = % v ) ) ` ,
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 {
fieldSelection = "id,name"
} else {
fieldSelection = selectFolderFields
}
return fmt . Sprintf ( ` SELECT %v FROM %v ORDER BY name %v LIMIT %v OFFSET %v ` , fieldSelection , sqlTableFolders ,
2020-06-07 21:30:18 +00:00
order , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getUpdateFolderQuotaQuery ( reset bool ) string {
if reset {
return fmt . Sprintf ( ` UPDATE % v SET used_quota_size = % v , used_quota_files = % v , last_quota_update = % v
2021-02-01 18:04:15 +00:00
WHERE name = % v ` , sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2020-06-07 21:30:18 +00:00
}
return fmt . Sprintf ( ` UPDATE % v SET used_quota_size = used_quota_size + % v , used_quota_files = used_quota_files + % v , last_quota_update = % v
2021-02-01 18:04:15 +00:00
WHERE name = % v ` , sqlTableFolders , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] )
2020-06-07 21:30:18 +00:00
}
func getQuotaFolderQuery ( ) string {
2021-02-01 18:04:15 +00:00
return fmt . Sprintf ( ` SELECT used_quota_size,used_quota_files FROM %v WHERE name = %v ` , 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 ( ")" )
}
return fmt . Sprintf ( ` SELECT g . name , ug . group_type , ug . user_id FROM % v g INNER JOIN % v ug ON g . id = ug . group_id WHERE
ug . user_id IN % v ORDER BY ug . user_id ` , sqlTableGroups , sqlTableUsersGroupsMapping , 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 ,
fm . quota_size , fm . quota_files , fm . user_id , f . filesystem , f . description FROM % v f INNER JOIN % v fm ON f . id = fm . folder_id WHERE
2022-04-25 13:49:11 +00:00
fm . user_id IN % v 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 ( ")" )
}
return fmt . Sprintf ( ` SELECT fm . folder_id , u . username FROM % v fm INNER JOIN % v u ON fm . user_id = u . id
2022-04-25 13:49:11 +00:00
WHERE fm . folder_id IN % v ORDER BY fm . folder_id ` , sqlTableUsersFoldersMapping , sqlTableUsers , sb . String ( ) )
}
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 ( ")" )
}
return fmt . Sprintf ( ` SELECT fm . folder_id , g . name FROM % v fm INNER JOIN % v g ON fm . group_id = g . id
WHERE fm . folder_id IN % v ORDER BY fm . folder_id ` , sqlTableGroupsFoldersMapping , sqlTableGroups , sb . String ( ) )
}
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 ( ")" )
}
return fmt . Sprintf ( ` SELECT um . group_id , u . username FROM % v um INNER JOIN % v u ON um . user_id = u . id
WHERE um . group_id IN % v ORDER BY um . group_id ` , sqlTableUsersGroupsMapping , sqlTableUsers , sb . String ( ) )
}
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
fm . group_id IN % v 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 ,
current_ul_size , current_dl_size , created_at , updated_at FROM % v WHERE updated_at > % v ` ,
sqlTableActiveTransfers , sqlPlaceholders [ 0 ] )
}
func getAddActiveTransferQuery ( ) string {
return fmt . Sprintf ( ` INSERT INTO % v ( transfer_id , connection_id , transfer_type , username , folder_name , ip , truncated_size ,
current_ul_size , current_dl_size , created_at , updated_at ) VALUES ( % v , % v , % v , % v , % v , % v , % v , % v , % v , % v , % v ) ` ,
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 {
return fmt . Sprintf ( ` UPDATE %v SET current_ul_size=%v,current_dl_size=%v,updated_at=%v WHERE connection_id = %v AND transfer_id = %v ` ,
sqlTableActiveTransfers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] , sqlPlaceholders [ 2 ] , sqlPlaceholders [ 3 ] , sqlPlaceholders [ 4 ] )
}
func getRemoveActiveTransferQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE connection_id = %v AND transfer_id = %v ` ,
sqlTableActiveTransfers , sqlPlaceholders [ 0 ] , sqlPlaceholders [ 1 ] )
}
func getCleanupActiveTransfersQuery ( ) string {
return fmt . Sprintf ( ` DELETE FROM %v WHERE updated_at < %v ` , sqlTableActiveTransfers , sqlPlaceholders [ 0 ] )
}
2020-02-08 13:44:25 +00:00
func getDatabaseVersionQuery ( ) string {
2020-06-07 21:30:18 +00:00
return fmt . Sprintf ( "SELECT version from %v LIMIT 1" , sqlTableSchemaVersion )
2020-02-08 13:44:25 +00:00
}
func getUpdateDBVersionQuery ( ) string {
2020-06-07 21:30:18 +00:00
return fmt . Sprintf ( ` UPDATE %v SET version=%v ` , sqlTableSchemaVersion , sqlPlaceholders [ 0 ] )
}