package shakuhachi

  1. Overview
  2. Docs
A music collection manager

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.1.0.tar.gz
sha256=bb7f91cc44c09f922d2a614e9e01fd0d2976840bacf839a8b99de63c6fc5b3b8
sha512=f02827db4228b017bb2ad7d64cb1c0667831ec2341e6f759266ec834e40627c2affeace143c6d5e769b3acce63f889b3cbd1a9ca4768091840e8fce1d4750f86

doc/src/shakuhachi.database/musicProperty.ml.html

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
159
(**********************************************************************************************)
(*                                                                                            *)
(* This file is part of shakuhachi: a music collection manager                                *)
(* Copyright (C) 2025 Yves Ndiaye                                                             *)
(*                                                                                            *)
(* shakuhachi 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.                    *)
(*                                                                                            *)
(* shakuhachi 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 shakuhachi.   *)
(* If not, see <http://www.gnu.org/licenses/>.                                                *)
(*                                                                                            *)
(**********************************************************************************************)

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

let init music_id key value = { music_id; key; value }
let table_name = "music_properties"
let foreign_music_id = "music_id"
let column_key = "key"
let column_value = "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