Source file musicProperty.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
(*****************************************************************************)
(*                                                                           *)
(*  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/.                 *)
(*                                                                           *)
(*****************************************************************************)

type t = { music_id : Int64.t; key : string; value : string }
[@@deriving yojson]

module Column = struct
  let music_id = "music_id"
  let key = "key"
  let value = "value"
end

let init music_id key value = { music_id; key; value }
let table_name = "music_properties"
let foreign_music_id = Column.music_id
let column_key = Column.key
let column_value = Column.value
let p_music_id = Printf.sprintf "%s.%s" table_name foreign_music_id
let p_key = Printf.sprintf "%s.%s" table_name column_key
let p_value = Printf.sprintf "%s.%s" table_name column_value

let sql_table =
  Printf.sprintf
    {| 
CREATE TABLE IF NOT EXISTS %s (
    %s INTEGER NOT NULL,
    %s TEXT NOT NULL,
    %s TEXT NOT NULL,
    FOREIGN KEY (%s) REFERENCES %s (%s)
      ON UPDATE CASCADE
      ON DELETE CASCADE
);|}
    table_name foreign_music_id column_key column_value foreign_music_id
    MusicFile.table_name MusicFile.primary_key

let columns statement offset =
  let module D = Sqlite3.Data in
  let music_id = Sqlite3.column_int64 statement offset in
  let key = Sqlite3.column_text statement (offset + 1) in
  let value = Sqlite3.column_text statement (offset + 2) in
  ({ music_id; key; value }, offset + 3)

let columns_data data offset =
  let music_id = Sqlite3.Data.to_int64_exn data.(offset) in
  let key = Sqlite3.Data.to_string_exn data.(offset + 1) in
  let value = Sqlite3.Data.to_string_exn data.(offset + 2) in
  ({ music_id; key; value }, offset + 3)

let update_key database music_id key old_value new_value =
  let ( let* ) rc f =
    match rc with
    | Sqlite3.Rc.OK | Sqlite3.Rc.DONE ->
        f ()
    | rc ->
        let message = Sqlite3.errmsg database in
        Error (rc, message)
  in
  let statement =
    Printf.sprintf
      "UPDATE %s SET %s = ?4 WHERE %s = ?1 AND %s = ?2 AND %s = ?3;" table_name
      column_value foreign_music_id column_key column_value
  in
  let statement = Sqlite3.prepare database statement in
  let* () = Sqlite3.bind_int64 statement 1 music_id in
  let* () = Sqlite3.bind_text statement 2 key in
  let* () = Sqlite3.bind_text statement 3 old_value in
  let* () = Sqlite3.bind_text statement 4 new_value in
  let* () = Sqlite3.step statement in
  let* () = Sqlite3.finalize statement in
  Ok ()

let remove_key database t =
  let ( let* ) rc f =
    match rc with
    | Sqlite3.Rc.OK | Sqlite3.Rc.DONE ->
        f ()
    | rc ->
        let message = Sqlite3.errmsg database in
        Error (rc, message)
  in
  let { music_id; key; value = _ } = t in
  let statement =
    Printf.sprintf "DELETE FROM %s WHERE %s = ? AND key = ?;" table_name
      foreign_music_id
  in
  let statement = Sqlite3.prepare database statement in
  let* () = Sqlite3.bind_int64 statement 1 music_id in
  let* () = Sqlite3.bind_text statement 2 key in
  let* () = Sqlite3.step statement in
  let* () = Sqlite3.finalize statement in
  Ok ()

let insert database t =
  let ( let* ) rc f =
    match rc with
    | Sqlite3.Rc.OK | Sqlite3.Rc.DONE | Sqlite3.Rc.CONSTRAINT ->
        f ()
    | rc ->
        let message = Sqlite3.errmsg database in
        Error (rc, message)
  in
  let statement =
    Printf.sprintf "INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?);" table_name
      foreign_music_id column_key column_value
  in
  let statement = Sqlite3.prepare database statement in

  let* () = Sqlite3.bind_int64 statement 1 t.music_id in
  let* () = Sqlite3.bind_text statement 2 t.key in
  let* () = Sqlite3.bind_text statement 3 t.value in
  let* () = Sqlite3.step statement in
  let* () = Sqlite3.finalize statement in
  Ok ()

let properties database music_id =
  let module P = SkhcMetadata.Metadata.Properties in
  let module V = SkhcMetadata.Metadata.Values in
  let ( let* ) rc f =
    match rc with
    | Sqlite3.Rc.OK | Sqlite3.Rc.DONE ->
        f ()
    | rc ->
        let message = Sqlite3.errmsg database in
        Error (rc, message)
  in
  let statement =
    Printf.sprintf "SELECT * from %s WHERE %s = ?;" table_name foreign_music_id
  in
  let statement = Sqlite3.prepare database statement in
  let* () = Sqlite3.bind_int64 statement 1 music_id in
  let _, properties =
    Sqlite3.fold ~init:P.empty
      ~f:(fun properties data ->
        let { key; value; music_id = _ }, _ = columns_data data 0 in
        P.update key
          (function
            | None -> Some (V.singleton value) | Some v -> Some (V.add value v)
            )
          properties
      )
      statement
  in
  Ok properties

let music_id s = s.music_id
let key s = s.key
let value s = s.value

(* Test *)
let equal : t -> t -> bool = ( = )
let compare : t -> t -> int = Stdlib.compare