-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmod.rs
379 lines (368 loc) · 10.2 KB
/
mod.rs
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
use crate::repository_error::RepositoryError;
pub mod abbreviation;
pub mod abbreviation_row;
pub mod activity_log;
mod activity_log_row;
pub mod adjustment;
pub mod assets;
pub mod backend_plugin_row;
pub mod barcode;
mod barcode_row;
pub mod category_row;
pub mod changelog;
pub mod clinician;
mod clinician_link_row;
mod clinician_row;
mod clinician_store_join_row;
pub mod cold_storage_type;
mod cold_storage_type_row;
pub mod consumption;
pub mod contact_form;
pub mod contact_form_row;
pub mod contact_trace;
pub mod contact_trace_row;
mod context_row;
pub mod currency;
mod currency_row;
pub mod demographic;
pub mod demographic_indicator;
pub mod demographic_indicator_row;
pub mod demographic_projection;
pub mod demographic_projection_row;
pub mod demographic_row;
pub mod diagnosis;
pub mod diagnosis_row;
pub mod diesel_schema;
pub mod document;
pub mod document_registry;
mod document_registry_config;
mod document_registry_row;
pub mod email_queue_row;
pub mod encounter;
pub mod encounter_row;
mod filter_restriction;
mod filter_sort_pagination;
pub mod form_schema;
mod form_schema_row;
mod frontend_plugin_row;
pub mod indicator_column;
mod indicator_column_row;
pub mod indicator_line;
mod indicator_line_row;
pub mod indicator_value;
mod indicator_value_row;
pub mod insurance_provider_row;
pub mod inventory_adjustment_reason;
mod inventory_adjustment_reason_row;
pub mod invoice;
pub mod invoice_line;
mod invoice_line_row;
mod invoice_row;
pub mod item;
pub mod item_category;
pub mod item_category_row;
pub mod item_direction;
pub mod item_direction_row;
mod item_link_row;
mod item_row;
pub mod item_variant;
pub mod key_value_store;
pub mod ledger;
pub mod location;
pub mod location_movement;
mod location_movement_row;
mod location_row;
pub mod master_list;
pub mod master_list_line;
mod master_list_line_row;
pub mod master_list_name_join;
mod master_list_row;
mod migration_fragment_log;
pub mod name;
pub mod name_insurance_join_row;
mod name_link_row;
pub mod name_property;
pub mod name_property_row;
mod name_row;
pub mod name_store_join;
pub mod name_tag;
pub mod name_tag_join;
mod name_tag_row;
mod number_row;
mod patient;
pub mod period;
pub mod plugin_data;
mod plugin_data_row;
pub mod preference;
mod preference_row;
pub mod printer;
pub mod printer_row;
pub mod program_enrolment;
mod program_enrolment_row;
pub mod program_event;
mod program_event_row;
pub mod program_indicator;
mod program_indicator_row;
mod program_requisition;
pub mod property;
pub mod property_row;
pub mod reason_option;
pub mod reason_option_row;
pub mod replenishment;
pub mod report;
mod report_query;
mod report_row;
pub mod requisition;
pub mod requisition_line;
pub mod return_reason;
mod return_reason_row;
pub mod rnr_form;
pub mod rnr_form_line;
pub mod rnr_form_line_row;
pub mod rnr_form_row;
pub mod sensor;
mod sensor_row;
pub mod stock_line;
mod stock_line_row;
pub mod stock_movement;
pub mod stock_on_hand;
pub mod stocktake;
pub mod stocktake_line;
mod stocktake_line_row;
mod stocktake_row;
mod storage_connection;
pub mod store;
mod store_preference_row;
mod store_row;
pub mod sync_buffer;
pub mod sync_file_reference;
pub mod sync_file_reference_row;
pub mod sync_log;
mod sync_log_row;
pub mod system_log_row;
pub mod temperature_breach;
pub mod temperature_breach_config;
mod temperature_breach_config_row;
mod temperature_breach_row;
mod temperature_excursion;
pub mod temperature_log;
mod temperature_log_row;
mod unit_row;
mod user;
pub mod user_permission;
mod user_permission_row;
mod user_row;
mod user_store_join_row;
pub mod vaccination;
pub mod vaccination_card;
pub mod vaccination_row;
pub mod vaccine_course;
pub use abbreviation_row::*;
pub use activity_log_row::*;
pub use adjustment::*;
pub use assets::*;
pub use backend_plugin_row::*;
pub use barcode_row::*;
pub use changelog::*;
pub use clinician::*;
pub use clinician_link_row::*;
pub use clinician_row::*;
pub use clinician_store_join_row::*;
pub use cold_storage_type::*;
pub use cold_storage_type_row::*;
pub use consumption::*;
pub use context_row::*;
pub use currency::*;
pub use currency_row::*;
pub use demographic_indicator::*;
pub use demographic_indicator_row::*;
pub use demographic_projection_row::*;
pub use demographic_row::*;
pub use diagnosis_row::*;
pub use document::*;
pub use document_registry::*;
pub use document_registry_config::*;
pub use document_registry_row::*;
pub use encounter::*;
pub use encounter_row::*;
pub use filter_sort_pagination::*;
pub use form_schema::*;
pub use form_schema_row::*;
pub use frontend_plugin_row::*;
pub use indicator_column_row::*;
pub use indicator_line_row::*;
pub use indicator_value_row::*;
pub use insurance_provider_row::*;
pub use inventory_adjustment_reason_row::*;
pub use invoice::*;
pub use invoice_line::*;
pub use invoice_line_row::*;
pub use invoice_row::*;
pub use item::*;
pub use item_direction_row::*;
pub use item_link_row::*;
pub use item_row::*;
pub use key_value_store::*;
pub use location_movement_row::*;
pub use location_row::*;
pub use master_list::*;
pub use master_list_line::*;
pub use master_list_line_row::*;
pub use master_list_name_join::*;
pub use master_list_row::*;
pub(crate) use migration_fragment_log::*;
pub use name::*;
pub use name_link_row::*;
pub use name_property::*;
pub use name_property_row::*;
pub use name_row::*;
pub use name_store_join::*;
pub use name_tag::*;
pub use name_tag_join::*;
pub use name_tag_row::*;
pub use number_row::*;
pub use patient::*;
pub use period::*;
pub use plugin_data::*;
pub use plugin_data_row::*;
pub use preference::*;
pub use preference_row::*;
pub use printer_row::*;
pub use program_enrolment::*;
pub use program_enrolment_row::*;
pub use program_event::*;
pub use program_event_row::*;
pub use program_indicator::*;
pub use program_indicator_row::*;
pub use program_requisition::*;
pub use property_row::*;
pub use reason_option::*;
pub use replenishment::*;
pub use report::*;
pub use report_query::*;
pub use report_row::*;
pub use requisition::*;
pub use requisition_line::*;
pub use return_reason_row::*;
pub use rnr_form::*;
pub use rnr_form_line::*;
pub use rnr_form_line_row::*;
pub use rnr_form_row::*;
pub use sensor::*;
pub use sensor_row::*;
pub use stock_line::*;
pub use stock_line_row::*;
pub use stock_movement::*;
pub use stock_on_hand::*;
pub use stocktake::*;
pub use stocktake_line::*;
pub use stocktake_line_row::*;
pub use stocktake_row::*;
pub use storage_connection::*;
pub use store::*;
pub use store_preference_row::*;
pub use store_row::*;
pub use sync_buffer::*;
pub use sync_file_reference::*;
pub use sync_file_reference_row::*;
pub use sync_log::*;
pub use sync_log_row::*;
pub use temperature_breach::*;
pub use temperature_breach_config::*;
pub use temperature_breach_config_row::*;
pub use temperature_breach_row::*;
pub use temperature_excursion::*;
pub use temperature_log::*;
pub use temperature_log_row::*;
pub use unit_row::*;
pub use user::*;
pub use user_permission::*;
pub use user_permission_row::*;
pub use user_row::*;
pub use user_store_join_row::*;
pub use vaccination::*;
pub use vaccination_card::*;
pub use vaccination_row::*;
use diesel::{
prelude::*,
r2d2::{ConnectionManager, Pool, PooledConnection},
result::{DatabaseErrorKind as DieselDatabaseErrorKind, Error as DieselError},
sql_query,
sql_types::Text,
};
#[cfg(not(feature = "postgres"))]
pub type DBBackendConnection = SqliteConnection;
#[cfg(feature = "postgres")]
pub type DBBackendConnection = PgConnection;
#[cfg(not(feature = "postgres"))]
pub type DBType = diesel::sqlite::Sqlite;
#[cfg(feature = "postgres")]
pub type DBType = diesel::pg::Pg;
pub type DBConnection = PooledConnection<ConnectionManager<DBBackendConnection>>;
impl From<DieselError> for RepositoryError {
fn from(err: DieselError) -> Self {
use RepositoryError as Error;
match err {
DieselError::InvalidCString(extra) => {
Error::as_db_error("DIESEL_INVALID_C_STRING", extra)
}
DieselError::DatabaseError(err, extra) => {
let extra = format!("{:?}", extra);
match err {
DieselDatabaseErrorKind::UniqueViolation => Error::UniqueViolation(extra),
DieselDatabaseErrorKind::ForeignKeyViolation => {
Error::ForeignKeyViolation(extra)
}
DieselDatabaseErrorKind::UnableToSendCommand => {
Error::as_db_error("UNABLE_TO_SEND_COMMAND", extra)
}
DieselDatabaseErrorKind::SerializationFailure => {
Error::as_db_error("SERIALIZATION_FAILURE", extra)
}
_ => Error::as_db_error("UNKNOWN", extra),
}
}
DieselError::NotFound => RepositoryError::NotFound,
DieselError::QueryBuilderError(extra) => {
Error::as_db_error("DIESEL_QUERY_BUILDER_ERROR", extra)
}
DieselError::DeserializationError(extra) => {
Error::as_db_error("DIESEL_DESERIALIZATION_ERROR", extra)
}
DieselError::SerializationError(extra) => {
Error::as_db_error("DIESEL_SERIALIZATION_ERROR", extra)
}
DieselError::RollbackTransaction => {
Error::as_db_error("DIESEL_ROLLBACK_TRANSACTION", "")
}
DieselError::AlreadyInTransaction => {
Error::as_db_error("DIESEL_ALREADY_IN_TRANSACTION", "")
}
_ => {
// try to get a more detailed diesel msg:
let diesel_msg = format!("{}", err);
Error::as_db_error("DIESEL_UNKNOWN", diesel_msg)
}
}
}
}
fn get_connection(
pool: &Pool<ConnectionManager<DBBackendConnection>>,
) -> Result<DBConnection, RepositoryError> {
pool.get().map_err(|error| RepositoryError::DBError {
msg: "Failed to open Connection".to_string(),
extra: format!("{:?}", error),
})
}
#[derive(QueryableByName, Debug, PartialEq)]
pub struct JsonRawRow {
#[diesel(sql_type = Text)]
pub json_row: String,
}
// TODO should accept parameters
pub fn raw_query(
connection: &StorageConnection,
query: String,
) -> Result<Vec<JsonRawRow>, RepositoryError> {
Ok(sql_query(&query).get_results::<JsonRawRow>(connection.lock().connection())?)
}