package shakuhachi
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
A music collection manager
Install
dune-project
Dependency
Authors
Maintainers
Sources
0.3.2.tar.gz
sha256=c72807d303de137441b643fec1c7630c930ebc792b576783c7961b237c2114db
sha512=770f878bbdac8033d90e904ce8e9c84fc75331c7ff9b35e2204fafe3746f432f2664296dac91942a788b82a2da07478d91b76ed85cd880758d2def8a55144840
doc/src/shakuhachi.database/album.ml.html
Source file album.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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510(*****************************************************************************) (* *) (* 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 = { id : Int64.t; name : string; sort_name : string option; year : int option; compilation : bool; flag : bool; rating : int option; comment : string option; genre : string option; cover_path : string option; created_at : float; updated_at : float; } [@@deriving yojson] type (_, _) field = | Id : ('a, 'b) field -> (Int64.t -> 'a, 'b) field | Name : ('a, 'b) field -> (string -> 'a, 'b) field | SortName : ('a, 'b) field -> (string option -> 'a, 'b) field | Year : ('a, 'b) field -> (int option -> 'a, 'b) field | Compilation : ('a, 'b) field -> (bool -> 'a, 'b) field | Flag : ('a, 'b) field -> (bool -> 'a, 'b) field | Rating : ('a, 'b) field -> (int option -> 'a, 'b) field | Comment : ('a, 'b) field -> (string option -> 'a, 'b) field | Genre : ('a, 'b) field -> (string option -> 'a, 'b) field | CoverPath : ('a, 'b) field -> (string option -> 'a, 'b) field | CreatedAt : ('a, 'b) field -> (float -> 'a, 'b) field | UpdatedAt : ('a, 'b) field -> (float -> 'a, 'b) field | End : ('b, 'b) field module Column = struct let id = "id" let name = "name" let sort_name = "sort_name" let year = "year" let compilation = "compilation" let flag = "flag" let rating = "rating" let comment = "comment" let genre = "genre" let cover_path = "cover_path" let created_at = "created_at" let updated_at = "updated_at" end let table_name = "albums" let primary_key = Column.id let p_id = Printf.sprintf "%s.%s" table_name primary_key module Stats = struct type t = { music_count : int; duration : int; play_count : int; size : int64; avg_duration : float; avg_rating : float; avg_size : float; } [@@deriving yojson] let init' ~music_count ~duration ~play_count ~size ~avg_duration ~avg_rating ~avg_size = { music_count; duration; play_count; size; avg_duration; avg_rating; avg_size; } let init ~music_count ~duration ~play_count ~size ~avg_rating = Util.Stats.init ~init' ~music_count ~duration ~play_count ~size ~avg_rating let column_query ~music_album_key ~id ~play_count ~duration ~size ~rating = Util.Stats.column_query ~music_album_key ~album_key:(table_name, Column.id) ~id ~play_count ~duration ~size ~rating let columns statement offset = let music_count = Sqlite3.column_int statement offset in let play_count = Sqlite3.column_int statement (offset + 1) in let duration = Sqlite3.column_int statement (offset + 2) in let size = Sqlite3.column_int64 statement (offset + 3) in let avg_duration = Sqlite3.column_double statement (offset + 4) in let avg_rating = Sqlite3.column_double statement (offset + 5) in let avg_size = Sqlite3.column_double statement (offset + 6) in ( init' ~music_count ~duration ~play_count ~size ~avg_duration ~avg_rating ~avg_size, offset + 7 ) let music_count t = t.music_count let duration t = t.duration let play_count t = t.play_count let size t = t.size let avg_duration t = t.avg_duration let avg_rating t = t.avg_rating let avg_size t = t.avg_size end let init ~sort_name ~year ~compilation ~flag ~rating ~comment ~genre ~cover_path name = let created_at = Unix.time () in let updated_at = created_at in { id = 0L; name; sort_name; year; compilation; flag; rating; comment; genre; cover_path; created_at; updated_at; } let rec fields : type a b. string list -> (a, b) field -> string list = fun fls field -> match field with | Id next -> fields (Column.id :: fls) next | Name next -> fields (Column.name :: fls) next | SortName next -> fields (Column.sort_name :: fls) next | Year next -> fields (Column.year :: fls) next | Compilation next -> fields (Column.compilation :: fls) next | Flag next -> fields (Column.flag :: fls) next | Rating next -> fields (Column.rating :: fls) next | Comment next -> fields (Column.comment :: fls) next | Genre next -> fields (Column.genre :: fls) next | CoverPath next -> fields (Column.cover_path :: fls) next | CreatedAt next -> fields (Column.created_at :: fls) next | UpdatedAt next -> fields (Column.updated_at :: fls) next | End -> List.rev fls let rec select : type a b. Sqlite3.stmt -> int -> (a, b) field -> a -> b = fun stmt i field f -> match field with | Id next -> let id = Sqlite3.column_int64 stmt i in select stmt (i + 1) next (f id) | Name next -> let name = Sqlite3.column_text stmt i in select stmt (i + 1) next (f name) | SortName next -> let data = Sqlite3.column stmt i in let sort_name = Sqlite3.Data.to_string data in select stmt (i + 1) next (f sort_name) | Year next -> let data = Sqlite3.column stmt i in let year = Sqlite3.Data.to_int data in select stmt (i + 1) next (f year) | Rating next -> let data = Sqlite3.column stmt i in let rating = Sqlite3.Data.to_int data in select stmt (i + 1) next (f rating) | Compilation next -> let compilation = Sqlite3.column_bool stmt i in select stmt (i + 1) next (f compilation) | Flag next -> let flag = Sqlite3.column_bool stmt i in select stmt (i + 1) next (f flag) | Comment next -> let data = Sqlite3.column stmt i in let comment = Sqlite3.Data.to_string data in select stmt (i + 1) next (f comment) | Genre next -> let data = Sqlite3.column stmt i in let genre = Sqlite3.Data.to_string data in select stmt (i + 1) next (f genre) | CoverPath next -> let data = Sqlite3.column stmt i in let cover = Sqlite3.Data.to_string data in select stmt (i + 1) next (f cover) | CreatedAt next -> let create_at = Sqlite3.column_double stmt i in select stmt (i + 1) next (f create_at) | UpdatedAt next -> let updated_at = Sqlite3.column_double stmt i in select stmt (i + 1) next (f updated_at) | End -> f let select stmt = select stmt 0 let sql_table = let open Column in Printf.sprintf {|CREATE TABLE IF NOT EXISTS %s ( %s INTEGER PRIMARY KEY, %s TEXT NOT NULL, %s TEXT, %s INTEGER, %s BOOLEAN NOT NULL, %s BOOLEAN NOT NULL, %s INTEGER, %s TEXT, %s TEXT, %s TEXT, %s REAL NOT NULL, %s REAL NOT NULL );|} table_name primary_key name sort_name year compilation flag rating comment genre cover_path created_at updated_at let field_all = Id (Name (SortName (Year (Compilation (Flag (Rating (Comment (Genre (CoverPath (CreatedAt (UpdatedAt End))))) ) ) ) ) ) ) let columns statement offset = let module D = Sqlite3.Data in let column_option statement conv offset = let data = Sqlite3.column statement offset in conv data in let id = Sqlite3.column_int64 statement offset in let name = Sqlite3.column_text statement (offset + 1) in let sort_name = column_option statement D.to_string (offset + 2) in let year = column_option statement D.to_int (offset + 3) in let compilation = Sqlite3.column_bool statement (offset + 4) in let flag = Sqlite3.column_bool statement (offset + 5) in let rating = column_option statement D.to_int (offset + 6) in let comment = column_option statement D.to_string (offset + 7) in let genre = column_option statement D.to_string (offset + 8) in let cover_path = column_option statement D.to_string (offset + 9) in let created_at = Sqlite3.column_double statement (offset + 10) in let updated_at = Sqlite3.column_double statement (offset + 11) in ( { id; name; sort_name; year; compilation; flag; rating; comment; genre; cover_path; created_at; updated_at; }, offset + 12 ) let filter_map database func = let request = Printf.sprintf "SELECT * from %s;" table_name in let statement = Sqlite3.prepare database request in let queue = Queue.create () in let rc = Sqlite3.iter statement ~f:(fun _ -> (*HACK: use statement instead of data *) let album, _ = columns statement 0 in match func album with None -> () | Some album -> Queue.add album queue ) in let albums = queue |> Queue.to_seq |> List.of_seq in Util.bind_ok database rc @@ fun () -> Ok albums let all database = filter_map database Option.some let find_from_id db id = let ( let* ) = Util.bind_ok db in let request = Printf.sprintf "SELECT * from %s where %s = ?;" table_name Column.id in let stmt = Sqlite3.prepare db request in let* () = Sqlite3.bind_int64 stmt 1 id in let queue = Queue.create () in let rc = Sqlite3.iter stmt ~f:(fun _ -> (*HACK: use statement instead of data *) let album, _ = columns stmt 0 in Queue.add album queue ) in Util.bind_ok db rc @@ fun () -> Ok (Queue.take_opt queue) let filter_map' database combine field f = let rec sql_loop acc stmt = match Sqlite3.step stmt with | Sqlite3.Rc.ROW -> ( match combine @@ select stmt field f with | None -> sql_loop acc stmt | Some t -> sql_loop (t :: acc) stmt ) | Sqlite3.Rc.DONE as rc -> (rc, List.rev acc) | rc -> (rc, List.rev acc) in let fields' = fields [] field in let fields = match fields' with [] -> fields [] field_all | _ :: _ as f -> f in let fields = String.concat ", " fields in let sql = Printf.sprintf "SELECT (%s) from %s;" fields table_name in let stmt = Sqlite3.prepare database sql in sql_loop [] stmt let count database = let statement = Printf.sprintf "SELECT COUNT(*) from %s" table_name in let statement = Sqlite3.prepare database statement in match Sqlite3.step statement with | Sqlite3.Rc.OK | Sqlite3.Rc.ROW -> let count = Sqlite3.column_int64 statement 0 in let _ = Sqlite3.finalize statement in Ok count | rc -> let message = Sqlite3.errmsg database in Error (rc, message) let update database t = let ( let* ) = Util.bind_ok database in let bind_option = Util.bind_option in let updated_at = Unix.time () in let t = { t with updated_at } in let statement = Printf.sprintf "UPDATE %s SET %s = ?2, %s = ?3, %s = ?4, %s = ?5, %s = ?11, %s = ?6, %s \ = ?7, %s = ?8, %s = ?9, %s = ?10 WHERE %s = ?1;" table_name Column.name Column.sort_name Column.year Column.compilation Column.flag Column.rating Column.comment Column.genre Column.cover_path Column.updated_at Column.id in let statement = Sqlite3.prepare database statement in let* () = Sqlite3.bind_int64 statement 1 t.id in let* () = Sqlite3.bind_text statement 2 t.name in let* () = bind_option Sqlite3.bind_text statement 3 t.sort_name in let* () = bind_option Sqlite3.bind_int statement 4 t.year in let* () = Sqlite3.bind_bool statement 5 t.compilation in let* () = bind_option Sqlite3.bind_int statement 6 t.rating in let* () = bind_option Sqlite3.bind_text statement 7 t.comment in let* () = bind_option Sqlite3.bind_text statement 8 t.genre in let* () = bind_option Sqlite3.bind_text statement 9 t.cover_path in let* () = Sqlite3.bind_double statement 10 t.updated_at in let* () = Sqlite3.bind_bool statement 11 t.flag in let* () = Sqlite3.step statement in let* () = Sqlite3.finalize statement in Ok t let remove_id database id = 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 "DELETE FROM %s WHERE %s = ?;" table_name primary_key in let statement = Sqlite3.prepare database statement in let* () = Sqlite3.bind_int64 statement 1 id in let* () = Sqlite3.step statement in let* () = Sqlite3.finalize statement in Ok () let remove database t = remove_id database t.id let insert database t = let ( let* ) = Util.bind_ok database in let bind_option = Util.bind_option in let statement' = let open Column in Printf.sprintf "INSERT INTO %s (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) VALUES (?, \ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" table_name name sort_name year compilation flag rating comment genre cover_path created_at updated_at in let statement = Sqlite3.prepare database statement' in (* Sqlite bind index are 1-based *) let* () = Sqlite3.bind_text statement 1 t.name in let* () = bind_option Sqlite3.bind_text statement 2 t.sort_name in let* () = bind_option Sqlite3.bind_int statement 3 t.year in let* () = Sqlite3.bind_bool statement 4 t.compilation in let* () = Sqlite3.bind_bool statement 5 t.flag in let* () = bind_option Sqlite3.bind_int statement 6 t.rating in let* () = bind_option Sqlite3.bind_text statement 7 t.comment in let* () = bind_option Sqlite3.bind_text statement 8 t.genre in let* () = bind_option Sqlite3.bind_text statement 9 t.cover_path in let* () = Sqlite3.bind_double statement 10 t.created_at in let* () = Sqlite3.bind_double statement 11 t.updated_at in let* () = Sqlite3.step statement in let* () = Sqlite3.finalize statement in let id = Sqlite3.last_insert_rowid database in Ok { t with id } let find_or_insert database t = let { id = _; name; _ } = t in let _, albums = filter_map' database (fun album -> if album.name = name then Some album else None ) field_all (fun id name sort_name year compilation flag rating comment genre cover_path created_at updated_at -> { id; name; sort_name; compilation; flag; year; rating; genre; comment; cover_path; created_at; updated_at; } ) in match albums with t :: _ -> Ok t | [] -> insert database t (* Getter *) let id t = t.id let name t = t.name let sort_name t = t.sort_name let year t = t.year let compilation t = t.compilation let flag t = t.flag let rating t = t.rating let comment t = t.comment let genre t = t.genre let cover_path t = t.cover_path let created_at t = t.created_at let updated_at t = t.updated_at (* Setter *) let set_id id t = { t with id } let set_name name t = { t with name } let set_sort_name sort_name t = { t with sort_name } let set_year year t = { t with year } let set_compilation compilation t = { t with compilation } let set_flag flag t = { t with flag } let set_rating rating t = { t with rating } let set_comment comment t = { t with comment } let set_genre genre t = { t with genre } let set_cover_path cover_path t = { t with cover_path } let set_created_at created_at t = { t with created_at } let set_updated_at updated_at t = { t with updated_at } (* Test *) (* test : Issue when float is NaN. *) let equal : t -> t -> bool = ( = ) let compare : t -> t -> int = Stdlib.compare (* Pp *) let to_string t = Yojson.Safe.to_string (to_yojson t) let pp format t = Format.fprintf format "%a" (Yojson.Safe.pretty_print ?std:None) (to_yojson t)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>