Source file queryArtist.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(*****************************************************************************)
(*                                                                           *)
(*  Copyright (C) 2026 Yves Ndiaye                                           *)
(*                                                                           *)
(* This Source Code Form is subject to the terms of the Mozilla Public       *)
(* License, v. 2.0. If a copy of the MPL was not distributed with this       *)
(* file, You can obtain one at https://mozilla.org/MPL/2.0/.                 *)
(*                                                                           *)
(*****************************************************************************)

include QueryUtil

(* TODO: Change the query to compute other field. *)
let matches_node _subfields artist node =
  let id = SkhcDb.Artist.id artist in
  let name = SkhcDb.Artist.name artist in
  let sort_name = SkhcDb.Artist.sort_name artist in
  let flag = SkhcDb.Artist.flag artist in
  let artwork = SkhcDb.Artist.artwork artist in
  let rating = SkhcDb.Artist.rating artist in
  match node with
  | Node.Substring s ->
      let insensible = true in
      let regex = false in
      matches ~insensible regex s name || omatches ~insensible regex s sort_name
  | Field (FString s) -> (
      let regex = Field.String.regex s in
      let insensible = Field.String.insensible s in
      let field = Field.String.key s in
      let value = Field.String.value s in
      match field with
      | SortName ->
          omatches ~insensible regex value sort_name
      | Title ->
          matches ~insensible regex value name
      | Artist ->
          matches ~insensible regex value name
      | Path ->
          matches ~insensible regex value name
      | AlbumArtist ->
          matches ~insensible regex value name
      | Composer ->
          matches ~insensible regex value name
      | Album ->
          false
      | Genre ->
          false
      | Arbitrary _ ->
          false
      | FileExtension ->
          false
    )
  | Field (FBool b) -> (
      let field = Field.Bool.key b in
      let b = Field.Bool.value b in
      match field with
      | Flag ->
          flag = b
      | Art ->
          let cover = Option.is_some artwork in
          b = cover
    )
  | Field (FInt64 i64) -> (
      let field = Field.Int64.key i64 in
      let frange = Field.Int64.value i64 in
      match field with Id -> Range.matches frange id | Size -> false
    )
  | Field (FInt i) -> (
      let field = Field.Int.key i in
      let frange = Field.Int.value i in
      match field with
      | Rating ->
          rating
          |> Option.map (Range.matches frange)
          |> Option.value ~default:false
      | Track | Year | Plays | AlbumCount | Disc | Length ->
          false
    )
  | Field (FFloat f) -> (
      let field = Field.Float.key f in
      let _frange = Field.Float.value f in
      match field with AvgDuration | AvgRating | AvgSize -> false
    )

let compare_key (r, key) lhs rhs =
  let lhs, rhs =
    if r then
      (rhs, lhs)
    else
      (lhs, rhs)
  in
  let cmp c f lhs rhs = c (f lhs) (f rhs) in
  let ocmp c f lhs rhs = Option.compare c (f lhs) (f rhs) in
  match key with
  | Field.(FString s) -> (
      let field = Field.String.key s in
      match field with
      | SortName ->
          ocmp String.compare SkhcDb.Artist.sort_name lhs rhs
      | Title | Artist | Path | AlbumArtist | Composer ->
          cmp String.compare SkhcDb.Artist.name lhs rhs
      | Album ->
          0
      | Genre ->
          0
      | Arbitrary _ ->
          0
      | FileExtension ->
          0
    )
  | FBool b -> (
      let field = Field.Bool.key b in
      match field with
      | Flag ->
          cmp Bool.compare SkhcDb.Artist.flag lhs rhs
      | Art ->
          cmp Bool.compare
            (fun s -> Option.is_some @@ SkhcDb.Artist.artwork s)
            lhs rhs
    )
  | FInt64 i64 -> (
      let field = Field.Int64.key i64 in
      match field with
      | Id ->
          cmp Int64.compare SkhcDb.Artist.id lhs rhs
      | Size ->
          0
    )
  | FInt i -> (
      let field = Field.Int.key i in
      match field with
      | Rating ->
          ocmp Int.compare SkhcDb.Artist.rating lhs rhs
      | Track | Year | Plays | AlbumCount | Disc | Length ->
          0
    )
  | FFloat f -> (
      let field = Field.Float.key f in
      match field with AvgDuration | AvgRating | AvgSize -> 0
    )