1#![allow(dead_code)]
2use sqlx::any::AnyArguments;
3use sqlx::any::AnyRow;
4use sqlx::Any;
5use sqlx::Encode;
6use sqlx::Row;
7use sqlx::Type;
8
9use serde::{Deserialize, Serialize};
10
11mod field;
12mod location;
13mod parser;
14mod request;
15mod timer;
16mod utils;
17pub use location::*;
18pub use parser::ParsedTemplateSql;
19pub use request::WriteCommand;
20use std::fmt::Debug;
21pub use timer::Timer;
22pub use utils::array_str_equal;
23
24pub type SqlxError = sqlx::Error;
25
26pub trait Schema {}
27
28pub trait Primary: Sync + Debug {
29 fn get_table_name(&self) -> &'static str;
30
31 fn get_primary_field_names(&self) -> &'static [&'static str];
32
33 fn any_arguments(&self) -> AnyArguments<'_>;
34}
35
36pub trait Mutation: Sync + Debug {
37 fn any_arguments(&self) -> AnyArguments<'_>;
38
39 fn get_fields_name(&self) -> Vec<String>;
40}
41
42pub trait Location: Sync + Debug {
43 fn get_table_name(&self) -> &'static str;
44
45 fn any_arguments(&self) -> AnyArguments<'_>;
46
47 fn get_fields_name(&self) -> Vec<String>;
48
49 fn get_where_clause(&self, wrap_char: char, place_holder: char) -> String;
50
51 fn check_valid_order_by(&self, fields: &[&str]) -> bool;
52}
53
54pub trait Entity: Sync + Debug {
55 fn get_table_name(&self) -> &'static str;
56
57 fn get_insert_fields(&self) -> Vec<String>;
58
59 fn get_upsert_set_fields(&self) -> Vec<String>;
60
61 fn get_auto_increment_field(&self) -> Option<&'static str>;
62
63 fn set_auto_increment_field(&mut self, value: Option<i64>) -> bool;
64
65 fn any_arguments_of_insert(&self) -> AnyArguments<'_>;
66
67 fn any_arguments_of_upsert(&self) -> AnyArguments<'_>;
68}
69
70pub trait Selection: Sync + Debug {
71 fn get_table_name(&self) -> &'static str;
72
73 fn get_selected_fields(&self) -> Vec<String>;
74}
75
76pub trait OrderBy: Sync + Debug {
77 fn get_order_by_fields(&self) -> &'static [&'static str];
78}
79
80pub trait SelectedEntity: Debug {
81 fn from_any_row(row: AnyRow) -> Result<Self, SqlxError>
82 where
83 Self: Sized;
84}
85
86pub enum CountSql {
87 Empty,
88 PlainSql(String),
89 VariabledSql(String),
90}
91
92pub trait TemplateRecord: Sync + Debug {
93 fn get_sql(&self, page: Option<&Pagination>) -> String;
94
95 fn get_count_sql(&self) -> CountSql;
96
97 fn get_variables(&self) -> Vec<String>;
98
99 fn any_arguments(&self) -> AnyArguments<'_>;
100}
101
102#[derive(Clone, Debug)]
103pub struct RecordCount {
104 pub count: i64,
105}
106
107impl SelectedEntity for RecordCount {
108 fn from_any_row(row: AnyRow) -> Result<Self, SqlxError>
109 where
110 Self: Sized,
111 {
112 let count: i64 = row.try_get("count")?;
113 Ok(Self { count })
114 }
115}
116
117#[derive(Clone, Debug)]
118pub struct LastRowId {
119 pub id: i64,
120}
121
122impl SelectedEntity for LastRowId {
123 fn from_any_row(row: AnyRow) -> Result<Self, SqlxError>
124 where
125 Self: Sized,
126 {
127 let last_row_id: i64 = row.try_get("last_row_id")?;
128 Ok(Self { id: last_row_id })
129 }
130}
131
132#[derive(Clone, Debug)]
133pub struct Pagination {
134 pub page_size: usize,
135 pub page_num: usize,
136}
137
138#[derive(Clone, Debug)]
139pub struct PageInfo {
140 pub page_size: usize,
141 pub page_num: usize,
142 pub page_total: usize,
143 pub total: usize,
144}
145
146impl PageInfo {
147 pub fn empty(page_size: usize, page_num: usize) -> Self {
148 Self {
149 page_size,
150 page_num,
151 page_total: 0,
152 total: 0,
153 }
154 }
155}
156
157#[derive(Clone, Debug)]
158pub struct PagedList<T>
159where
160 T: SelectedEntity,
161{
162 pub data: Vec<T>,
163 pub page: PageInfo,
164}
165
166impl<T> PagedList<T>
167where
168 T: SelectedEntity,
169{
170 pub fn empty(page_size: usize, page_num: usize) -> Self {
171 Self {
172 page: PageInfo::empty(page_size, page_num),
173 data: Vec::new(),
174 }
175 }
176}
177
178pub fn luna_merge_args<'p>(
180 mut args_a: AnyArguments<'p>,
181 args_b: AnyArguments<'p>,
182) -> AnyArguments<'p> {
183 args_a.values.0.extend(args_b.values.0);
184 args_a
185}
186
187pub fn luna_add_arg<'q, T>(args: &mut AnyArguments<'q>, value: &T)
189where
190 T: 'q + Send + Encode<'q, Any> + Type<Any>,
191{
192 let _ = value.encode_by_ref(&mut args.values);
193}