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
#[macro_use]
mod macros;
mod delete;
mod insert;
mod select;
mod update;
use sqlx::database::HasArguments;
use sqlx::query::{Query, QueryAs};
use sqlx::{Database, FromRow};
use std::any::TypeId;
use std::fmt::Display;
static mut TABLE_PREFIX: String = String::new();
pub struct TableName {
name: String,
}
impl Display for TableName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
unsafe { write!(f, "{}{}", TABLE_PREFIX, self.name) }
}
}
impl SqlQuote<String> for TableName {
fn sql_quote(&self) -> String {
unsafe { format!("{}{}", TABLE_PREFIX, self.name) }
}
}
impl TableName {
pub fn set_prefix(str: String) {
unsafe {
TABLE_PREFIX = str;
}
}
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
}
}
pub fn full_name(&self) -> String {
unsafe { format!("{}{}", TABLE_PREFIX, self.name) }
}
}
#[derive(PartialEq, Eq)]
pub enum DbType {
Mysql,
Sqlite,
Postgres,
MsSql,
}
impl DbType {
pub fn type_new<DB: sqlx::Database>() -> Self {
#[cfg(feature = "sqlx-mysql")]
if TypeId::of::<DB>() == TypeId::of::<sqlx::MySql>() {
return DbType::Mysql;
}
#[cfg(feature = "sqlx-sqlite")]
if TypeId::of::<DB>() == TypeId::of::<sqlx::Sqlite>() {
return DbType::Mysql;
}
#[cfg(feature = "sqlx-postgres")]
if TypeId::of::<DB>() == TypeId::of::<sqlx::Postgres>() {
return DbType::Postgres;
}
#[cfg(feature = "sqlx-mssql")]
if TypeId::of::<DB>() == TypeId::of::<sqlx::Mssql>() {
return DbType::MsSql;
}
unimplemented!()
}
pub fn mark(&self, pos: usize) -> String {
match self {
DbType::Mysql => "?".to_string(),
DbType::Sqlite => {
format!("${}", pos)
}
DbType::Postgres => {
format!("${}", pos)
}
DbType::MsSql => "?".to_string(),
}
}
}
pub trait ModelTableName {
fn table_name() -> TableName;
}
pub trait ModelTableField<DB>
where
DB: Database,
{
fn table_pk() -> TableFields;
fn table_column() -> TableFields;
fn query_sqlx_bind<'t>(
&'t self,
table_field_val: &FieldItem,
res: Query<'t, DB, <DB as HasArguments<'t>>::Arguments>,
) -> Query<'t, DB, <DB as HasArguments<'t>>::Arguments>;
fn query_as_sqlx_bind<'t, M>(
&'t self,
table_field_val: &FieldItem,
res: QueryAs<'t, DB, M, <DB as HasArguments<'t>>::Arguments>,
) -> QueryAs<'t, DB, M, <DB as HasArguments<'t>>::Arguments>
where
for<'r> M: FromRow<'r, DB::Row> + Send + Unpin;
}
#[derive(Clone, PartialEq, Eq)]
pub struct FieldItem {
pub name: String,
pub column_name: String,
}
impl Display for FieldItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
impl FieldItem {
pub fn new(name: &str, column_name: &str) -> Self {
FieldItem {
name: name.to_string(),
column_name: column_name.to_string(),
}
}
}
pub struct TableFields(Vec<FieldItem>);
impl Display for TableFields {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let fileds = self
.0
.iter()
.map(|e| format!("{}", e))
.collect::<Vec<String>>()
.join(",");
write!(f, "{}", fileds)
}
}
impl TableFields {
pub fn new(fields: Vec<FieldItem>) -> Self {
TableFields(fields)
}
pub fn marge(&mut self, field: Vec<FieldItem>) {
for val in field.iter() {
if !self.0.iter().any(|e| e.name == val.name) {
self.0.push(val.to_owned())
}
}
}
pub fn intersect(&mut self, field: Vec<FieldItem>) {
self.0 = self
.0
.iter()
.filter_map(|e| {
if field.contains(e) {
Some(e.to_owned())
} else {
None
}
})
.collect();
}
pub fn del(&mut self, name: &str) {
self.0 = self
.0
.iter()
.filter_map(|e| {
if name == e.name {
None
} else {
Some(e.to_owned())
}
})
.collect();
}
pub fn to_vec(&self) -> Vec<String> {
let field = self.0.iter();
field
.map(|e| e.column_name.clone())
.collect::<Vec<String>>()
}
}
pub use delete::*;
pub use insert::*;
pub use select::*;
pub use update::*;
use crate::SqlQuote;