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
// Copyright (C) 2026 Dylan Jones
// SPDX-License-Identifier: GPL-3.0-only
use diesel::prelude::*;
#[cfg(feature = "napi")]
use napi_derive::napi;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[cfg(feature = "pyo3")]
use rbox_derives::PyMutableMapping;
use super::schema::{agentRegistry, cloudAgentRegistry};
use super::{Date, DateString};
use crate::model_traits::Model;
#[cfg(feature = "pyo3")]
use crate::util::{PyItemsIter, PyObjectIter, PyStrIter};
/// Represents the `agentRegistry` table in the Rekordbox database.
///
/// This struct maps to the `agentRegistry` table in the SQLite database used by Rekordbox.
/// It contains various fields representing metadata and attributes of the Rekordbox collection.
/// Each entry has a unique name as `registry_id`. The local update sequence number (USN), for
/// example, is used to track changes made to the entry locally and if stored in the entry
/// with `registry_id="localUpdateCount"`.
/// Some important fields include:
/// * localUpdateCount
/// * SyncAnalysisDataRootPath
/// * SyncSettingsRootPath
/// * LangPath
#[derive(Debug, Clone, PartialEq, HasQuery, Identifiable, Insertable, AsChangeset)]
#[diesel(table_name = agentRegistry)]
#[diesel(primary_key(registry_id))]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
#[cfg_attr(feature = "pyo3", pyclass(get_all, set_all, mapping))]
#[cfg_attr(feature = "pyo3", derive(PyMutableMapping))]
#[cfg_attr(feature = "napi", napi(object))]
pub struct AgentRegistry {
/// A unique identifier for the registry entry.
#[cfg_attr(feature = "pyo3", repr_field)]
pub registry_id: String,
/// The timestamp when the entry was created, serialized/deserialized as `DateString`.
#[diesel(serialize_as = DateString)]
#[diesel(deserialize_as = DateString)]
pub created_at: Date,
/// The timestamp when the entry was last updated, serialized/deserialized as `DateString`.
#[diesel(serialize_as = DateString)]
#[diesel(deserialize_as = DateString)]
pub updated_at: Date,
/// An optional string field for additional identifier data.
pub id_1: Option<String>,
/// An optional string field for additional identifier data.
pub id_2: Option<String>,
/// An optional integer field for numerical data.
pub int_1: Option<i32>,
/// An optional integer field for numerical data.
pub int_2: Option<i32>,
/// An optional string field for textual data.
pub str_1: Option<String>,
/// An optional string field for textual data.
pub str_2: Option<String>,
/// An optional string field for date-related data.
pub date_1: Option<String>,
/// An optional string field for date-related data.
pub date_2: Option<String>,
/// An optional string field for extended text data.
pub text_1: Option<String>,
/// An optional string field for extended text data.
pub text_2: Option<String>,
}
impl Model for AgentRegistry {
type Id = str;
fn all(conn: &mut SqliteConnection) -> QueryResult<Vec<Self>> {
Self::query().load(conn)
}
fn find(conn: &mut SqliteConnection, id: &Self::Id) -> QueryResult<Option<Self>> {
Self::query().find(id).first(conn).optional()
}
fn id_exists(conn: &mut SqliteConnection, id: &Self::Id) -> QueryResult<bool> {
diesel::dsl::select(diesel::dsl::exists(Self::query().find(id))).get_result(conn)
}
}
impl AgentRegistry {
/// Queries the local update sequence number (USN) from the `agentRegistry` table.
///
/// Returns a [`diesel::result::Error::NotFound`] error if the `localUpdateCount` entry is not
/// found or the `int_1` column is `NULL`.
pub fn local_usn(conn: &mut SqliteConnection) -> QueryResult<i32> {
let result = agentRegistry::table
.find("localUpdateCount")
.select(agentRegistry::int_1)
.first(conn)?;
if let Some(value) = result {
Ok(value)
} else {
Err(diesel::result::Error::NotFound)
}
}
/// Updates the local update sequence number (USN) by a value and returns the new value.
///
/// # Arguments
/// * `n` - An integer value representing the change to apply to the local USN.
///
/// Returns a [`diesel::result::Error::NotFound`] error if the `localUpdateCount` entry is not
/// found or the `int_1` column is `NULL`.
pub fn update_local_usn(conn: &mut SqliteConnection, delta: i32) -> QueryResult<i32> {
let result = diesel::update(agentRegistry::table.find("localUpdateCount"))
.set(agentRegistry::int_1.eq(agentRegistry::int_1 + delta))
.returning(agentRegistry::int_1)
.get_result(conn)?;
if let Some(value) = result {
Ok(value)
} else {
Err(diesel::result::Error::NotFound)
}
}
/// Increments the local update sequence number (USN) and returns the new value.
///
/// # Arguments
/// * `n` - An integer value representing the amount to increment the local USN.
///
/// Returns a [`diesel::result::Error::NotFound`] error if the `localUpdateCount` entry is not
/// found or the `int_1` column is `NULL`.
#[inline]
pub fn increment_local_usn_by(conn: &mut SqliteConnection, n: usize) -> QueryResult<i32> {
Self::update_local_usn(conn, n as i32)
}
/// Increments the local update sequence number (USN) by one and returns the new value.
///
/// See [`AgentRegistry::increment_local_usn_by`] for more details.
#[inline]
pub fn increment_local_usn(conn: &mut SqliteConnection) -> QueryResult<i32> {
Self::update_local_usn(conn, 1)
}
}
/// Represents the `cloudAgentRegistry` table in the Rekordbox database.
///
/// This struct maps to the `cloudAgentRegistry` table in the SQLite database used by Rekordbox.
/// It contains similar fields as the [`AgentRegistry`], but is specifically used for cloud-related
/// registry entries.
#[derive(Debug, Clone, PartialEq, HasQuery, Identifiable, Insertable, AsChangeset)]
#[diesel(table_name = cloudAgentRegistry)]
#[diesel(primary_key(id))]
#[cfg_attr(feature = "pyo3", pyclass(unsendable, get_all, set_all, mapping))]
#[cfg_attr(feature = "pyo3", derive(PyMutableMapping))]
#[cfg_attr(feature = "napi", napi(object))]
pub struct CloudAgentRegistry {
/// A unique identifier for the entry.
#[cfg_attr(feature = "pyo3", repr_field)]
pub id: String,
/// A unique universal identifier for the entry.
pub uuid: String,
/// An integer representing the data status in Rekordbox.
pub rb_data_status: i32,
/// An integer representing the local data status in Rekordbox.
pub rb_local_data_status: i32,
/// An integer indicating whether the entry is locally deleted.
pub rb_local_deleted: i32,
/// An integer indicating whether the entry is locally synced.
pub rb_local_synced: i32,
/// An optional integer representing the update sequence number.
pub usn: Option<i32>,
/// An optional integer representing the local update sequence number.
pub rb_local_usn: Option<i32>,
/// The timestamp when the entry was created, serialized/deserialized as `DateString`.
#[diesel(serialize_as = DateString)]
#[diesel(deserialize_as = DateString)]
pub created_at: Date,
/// The timestamp when the entry was last updated, serialized/deserialized as `DateString`.
#[diesel(serialize_as = DateString)]
#[diesel(deserialize_as = DateString)]
pub updated_at: Date,
/// An optional integer field for numerical data.
pub int_1: Option<i32>,
/// An optional integer field for numerical data.
pub int_2: Option<i32>,
/// An optional string field for textual data.
pub str_1: Option<String>,
/// An optional string field for textual data.
pub str_2: Option<String>,
/// An optional string field for date-related data.
pub date_1: Option<String>,
/// An optional string field for date-related data.
pub date_2: Option<String>,
/// An optional string field for extended text data.
pub text_1: Option<String>,
/// An optional string field for extended text data.
pub text_2: Option<String>,
}
impl Model for CloudAgentRegistry {
type Id = str;
fn all(conn: &mut SqliteConnection) -> QueryResult<Vec<Self>> {
Self::query().load(conn)
}
fn find(conn: &mut SqliteConnection, id: &Self::Id) -> QueryResult<Option<Self>> {
Self::query().find(id).first(conn).optional()
}
fn id_exists(conn: &mut SqliteConnection, id: &Self::Id) -> QueryResult<bool> {
diesel::dsl::select(diesel::dsl::exists(Self::query().find(id))).get_result(conn)
}
}