github.com/status-im/status-go@v1.1.0/protocol/persistence_metrics.go (about) 1 package protocol 2 3 import ( 4 "database/sql" 5 "fmt" 6 "strings" 7 ) 8 9 const selectTimestampsQuery = "SELECT whisper_timestamp FROM user_messages WHERE %s whisper_timestamp >= ? AND whisper_timestamp <= ?" 10 const selectCountQuery = "SELECT COUNT(*) FROM user_messages WHERE %s whisper_timestamp >= ? AND whisper_timestamp <= ?" 11 12 func querySeveralChats(chatIDs []string) string { 13 if len(chatIDs) == 0 { 14 return "" 15 } 16 17 var conditions []string 18 for _, chatID := range chatIDs { 19 conditions = append(conditions, fmt.Sprintf("local_chat_id = '%s'", chatID)) 20 } 21 return fmt.Sprintf("(%s) AND", strings.Join(conditions, " OR ")) 22 } 23 24 func (db sqlitePersistence) SelectMessagesTimestampsForChatsByPeriod(chatIDs []string, startTimestamp uint64, endTimestamp uint64) ([]uint64, error) { 25 query := fmt.Sprintf(selectTimestampsQuery, querySeveralChats(chatIDs)) 26 27 rows, err := db.db.Query(query, startTimestamp, endTimestamp) 28 if err != nil { 29 return []uint64{}, err 30 } 31 defer rows.Close() 32 33 var timestamps []uint64 34 for rows.Next() { 35 var timestamp uint64 36 err := rows.Scan(×tamp) 37 if err != nil { 38 return nil, err 39 } 40 timestamps = append(timestamps, timestamp) 41 } 42 43 err = rows.Err() 44 if err != nil { 45 return []uint64{}, err 46 } 47 48 return timestamps, nil 49 } 50 51 func (db sqlitePersistence) SelectMessagesCountForChatsByPeriod(chatIDs []string, startTimestamp uint64, endTimestamp uint64) (int, error) { 52 query := fmt.Sprintf(selectCountQuery, querySeveralChats(chatIDs)) 53 54 var count int 55 if err := db.db.QueryRow(query, startTimestamp, endTimestamp).Scan(&count); err != nil { 56 if err == sql.ErrNoRows { 57 return 0, nil 58 } 59 return 0, err 60 } 61 62 return count, nil 63 }