53 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // ALR - Any Linux Repository
 | |
| // Copyright (C) 2025 The ALR Authors
 | |
| //
 | |
| // This program is free software: you can redistribute it and/or modify
 | |
| // it under the terms of the GNU General Public License as published by
 | |
| // the Free Software Foundation, either version 3 of the License, or
 | |
| // (at your option) any later version.
 | |
| //
 | |
| // 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 General Public License for more details.
 | |
| //
 | |
| // You should have received a copy of the GNU General Public License
 | |
| // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | |
| 
 | |
| package db
 | |
| 
 | |
| import (
 | |
| 	"database/sql/driver"
 | |
| 	"encoding/json"
 | |
| 	"errors"
 | |
| 
 | |
| 	"golang.org/x/exp/slices"
 | |
| 	"modernc.org/sqlite"
 | |
| )
 | |
| 
 | |
| func init() {
 | |
| 	sqlite.MustRegisterScalarFunction("json_array_contains", 2, jsonArrayContains)
 | |
| }
 | |
| 
 | |
| // jsonArrayContains is an SQLite function that checks if a JSON array
 | |
| // in the database contains a given value
 | |
| func jsonArrayContains(ctx *sqlite.FunctionContext, args []driver.Value) (driver.Value, error) {
 | |
| 	value, ok := args[0].(string)
 | |
| 	if !ok {
 | |
| 		return nil, errors.New("both arguments to json_array_contains must be strings")
 | |
| 	}
 | |
| 
 | |
| 	item, ok := args[1].(string)
 | |
| 	if !ok {
 | |
| 		return nil, errors.New("both arguments to json_array_contains must be strings")
 | |
| 	}
 | |
| 
 | |
| 	var array []string
 | |
| 	err := json.Unmarshal([]byte(value), &array)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return slices.Contains(array, item), nil
 | |
| }
 |