extern crate core;
use std::path::Path;
use json::{array, JsonValue, Null, object};
use lazy_static::lazy_static;
use std::sync::Mutex;
use std::collections::HashMap;
#[cfg(any(feature = "default", feature = "db-mysql"))]
use ::mysql::Pool;
#[cfg(any(feature = "default", feature = "db-mssql"))]
use crate::mssql::Mssql;
#[cfg(any(feature = "default", feature = "db-mysql"))]
use crate::mysql::Mysql;
#[cfg(any(feature = "default", feature = "db-sqlite"))]
use crate::sqlite::Sqlite;
pub mod pools;
lazy_static! {
pub static ref DB: Mutex<HashMap<String,Db>> =Mutex::new(HashMap::new());
static ref CONNECTION: Mutex<HashMap<String,Connection>> =Mutex::new(HashMap::new());
static ref DEFAULT: Mutex<HashMap<usize,String>> =Mutex::new(HashMap::new());
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
mod sqlite;
#[cfg(any(feature = "default", feature = "db-mysql"))]
mod mysql;
#[cfg(any(feature = "default", feature = "db-mssql"))]
mod mssql;
pub trait Mode {
fn table_create(&mut self, data: Table) -> bool;
fn table_update(&mut self, data: Table) -> bool;
fn table_info(&mut self, table: &str) -> JsonValue;
fn table_is_exist(&mut self, name: &str) -> bool;
fn table(&mut self, name: &str) -> &mut Self;
fn autoinc(&mut self) -> &mut Self;
fn fetch_sql(&mut self) -> &mut Self;
fn order(&mut self, field: &str, by: bool) -> &mut Self;
fn group(&mut self, field: &str) -> &mut Self;
fn distinct(&mut self) -> &mut Self;
fn json(&mut self, field: &str) -> &mut Self;
fn column(&mut self, field: &str) -> JsonValue;
fn where_and(&mut self, field: &str, compare: &str, value: JsonValue) -> &mut Self;
fn where_or(&mut self, field: &str, compare: &str, value: JsonValue) -> &mut Self;
fn where_column(&mut self, field_a: &str, compare: &str, field_b: &str) -> &mut Self;
fn count(&mut self) -> JsonValue;
fn max(&mut self, field: &str) -> JsonValue;
fn min(&mut self, field: &str) -> JsonValue;
fn sum(&mut self, field: &str) -> JsonValue;
fn avg(&mut self, field: &str) -> JsonValue;
fn select(&mut self) -> JsonValue;
fn find(&mut self) -> JsonValue;
fn value(&mut self, field: &str) -> JsonValue;
fn insert(&mut self, data: JsonValue) -> JsonValue;
fn insert_all(&mut self, data: JsonValue) -> JsonValue;
fn page(&mut self, page: i32, limit: i32) -> &mut Self;
fn update(&mut self, data: JsonValue) -> JsonValue;
fn delete(&mut self) -> JsonValue;
fn field(&mut self, name: &str) -> &mut Self;
fn hidden(&mut self, name: &str) -> &mut Self;
#[cfg(any(feature = "default", feature = "db-mysql"))]
fn pool(&mut self) -> Pool;
fn transaction(&mut self) -> bool;
fn commit(&mut self) -> bool;
fn rollback(&mut self) -> bool;
fn sql(&mut self, sql: String) -> JsonValue;
fn inc(&mut self, field: &str, num: f64) -> &mut Self;
fn dec(&mut self, field: &str, num: f64) -> &mut Self;
}
#[derive(Clone, Debug)]
pub enum Db {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Mysql(Mysql),
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Sqlite(Sqlite),
#[cfg(any(feature = "default", feature = "db-mssql"))]
Mssql(Mssql),
None,
}
impl Db {
pub fn new(conf: JsonValue) -> Db {
if conf.is_empty() {
return Db::None;
}
let default = conf["default"].as_str().unwrap_or("").to_string();
let mut connections = conf["connections"].clone();
if connections.is_empty() {
connections = object! {};
}
let connection = Connection::from(connections[default.clone()].clone().into());
for (name, conn) in connections.entries() {
CONNECTION.lock().unwrap().insert(name.to_string(), Connection::from(conn.clone().into()));
}
let connections = CONNECTION.lock().unwrap().clone();
DEFAULT.lock().unwrap().insert(0, default.clone());
let db = Db::setlist(connections.clone(), connection.clone(), default.clone());
DB.lock().unwrap().insert(default.clone(), db.clone());
DB.lock().unwrap().get(&*default.clone()).unwrap().clone()
}
fn setlist(connections: HashMap<String, Connection>, connection: Connection, default: String) -> Db {
match connection.mode.as_str() {
"mysql" => {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Mysql::connect(connection.clone(), default.clone());
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(Mysql {
connections: connections.clone(),
connection: connection.clone(),
default: default.clone(),
params: Params::default("mysql"),
transaction: 0,
})
}
"sqlite" => {
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Sqlite::connect(connection.clone(), default.clone());
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(Sqlite {
connections: connections.clone(),
connection: connection.clone(),
default: default.clone(),
params: Params::default("sqlite"),
transaction: 0,
})
}
"mssql" => {
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(Mssql {
connections: connections.clone(),
connection: connection.clone(),
default: default.clone(),
params: Params::default("mssql"),
})
}
_ => Db::None
}
}
pub fn connection(&mut self, name: &str) -> Db {
let default = DEFAULT.lock().unwrap();
let default = default.get(&0).unwrap();
let connections = CONNECTION.lock().unwrap().clone();
let name = {
if name == "" {
default
} else {
name
}
}.clone();
let dbmode = DB.lock().unwrap().get(&*name.clone()).is_none();
if !dbmode {
let db = DB.lock().unwrap().get(&*name.clone()).unwrap().clone();
return db;
}
let connection = connections.get(&*name.clone()).unwrap().clone();
let db = Db::setlist(connections.clone(), connection.clone(), name.to_string().clone());
DB.lock().unwrap().insert((&*name.clone()).parse().unwrap(), db.clone());
db
}
}
impl Mode for Db {
fn table_create(&mut self, data: Table) -> bool {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.table_create(data)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.table_create(data)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.table_create(data)
}
_ => false
}
}
fn table_update(&mut self, data: Table) -> bool {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.table_update(data)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.table_update(data)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.table_update(data)
}
_ => false
}
}
fn table_info(&mut self, table: &str) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.table_info(table)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.table_info(table)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.table_info(table)
}
_ => object! {}
}
}
fn table_is_exist(&mut self, name: &str) -> bool {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.table_is_exist(name)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.table_is_exist(name)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.table_is_exist(name)
}
_ => false
}
}
fn table(&mut self, name: &str) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.table(name);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.table(name);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.table(name);
}
_ => {}
};
self
}
fn autoinc(&mut self) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.autoinc();
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.autoinc();
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.autoinc();
}
_ => {}
};
self
}
fn fetch_sql(&mut self) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.fetch_sql();
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.fetch_sql();
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.fetch_sql();
}
_ => {}
};
self
}
fn order(&mut self, field: &str, by: bool) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.order(field, by);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.order(field, by);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.order(field, by);
}
_ => {}
}
self
}
fn group(&mut self, field: &str) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.group(field);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.group(field);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.group(field);
}
_ => {}
}
self
}
fn distinct(&mut self) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.distinct();
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.distinct();
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.distinct();
}
_ => {}
}
self
}
fn json(&mut self, field: &str) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.json(field);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.json(field);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.json(field);
}
_ => {}
}
self
}
fn column(&mut self, field: &str) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.column(field)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.column(field)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.column(field)
}
_ => object! {}
}
}
fn where_and(&mut self, field: &str, compare: &str, value: JsonValue) -> &mut Self {
if field.contains("|") {
let fields: Vec<&str> = field.split("|").collect();
for field in fields.iter() {
self.where_and(field, compare, value.clone());
}
return self;
}
let compare = compare.to_lowercase();
let compare = compare.replace(" ", "");
let compare = compare.as_str();
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.where_and(field.clone(), compare.clone(), value);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.where_and(field.clone(), compare.clone(), value);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.where_and(field.clone(), compare.clone(), value);
}
_ => {}
};
self
}
fn where_or(&mut self, field: &str, compare: &str, value: JsonValue) -> &mut Self {
if field.contains("|") {
let fields: Vec<&str> = field.split("|").collect();
for field in fields.iter() {
self.where_or(field, compare, value.clone());
}
return self;
}
let compare = compare.to_lowercase();
let compare = compare.replace(" ", "");
let compare = compare.as_str();
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.where_or(field.clone(), compare.clone(), value);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.where_or(field.clone(), compare.clone(), value);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.where_or(field.clone(), compare.clone(), value);
}
_ => {}
};
self
}
fn where_column(&mut self, field_a: &str, compare: &str, field_b: &str) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.where_column(field_a, compare, field_b);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.where_column(field_a, compare, field_b);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.where_column(field_a, compare, field_b);
}
_ => {}
};
self
}
fn count(&mut self) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.count()
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.count()
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.count()
}
_ => JsonValue::from(0)
}
}
fn max(&mut self, field: &str) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.max(field)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.max(field)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.max(field)
}
_ => object! {}
}
}
fn min(&mut self, field: &str) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.min(field)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.min(field)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.min(field)
}
_ => object! {}
}
}
fn sum(&mut self, field: &str) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.sum(field)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.sum(field)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.sum(field)
}
_ => object! {}
}
}
fn avg(&mut self, field: &str) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.avg(field)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.avg(field)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.avg(field)
}
_ => object! {}
}
}
fn select(&mut self) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.select()
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.select()
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.select()
}
_ => object! {}
}
}
fn find(&mut self) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.find()
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.find()
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.find()
}
_ => object! {}
}
}
fn value(&mut self, field: &str) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.value(field)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.value(field)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.value(field)
}
_ => JsonValue::from(Null)
}
}
fn insert(&mut self, data: JsonValue) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.insert(data.clone())
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.insert(data.clone())
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.insert(data.clone())
}
_ => JsonValue::from("")
}
}
fn insert_all(&mut self, mut data: JsonValue) -> JsonValue {
if data[0].is_empty() {
data = array![data].into();
}
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.insert_all(data.clone())
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.insert_all(data.clone())
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.insert_all(data.clone())
}
_ => object! {}
}
}
fn page(&mut self, mut page: i32, limit: i32) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
if page <= 0 {
page = 1;
}
db.page(page.clone(), limit.clone());
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.page(page.clone(), limit.clone());
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
if page <= 0 {
page = 1;
}
db.page(page.clone(), limit.clone());
}
_ => {}
};
self
}
fn update(&mut self, data: JsonValue) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.update(data.clone())
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.update(data.clone())
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.update(data.clone())
}
_ => JsonValue::from(0)
}
}
fn delete(&mut self) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.delete()
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.delete()
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.delete()
}
_ => JsonValue::from(0)
}
}
fn field(&mut self, name: &str) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.field(name.clone());
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.field(name.clone());
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.field(name.clone());
}
_ => {}
};
self
}
fn hidden(&mut self, name: &str) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.hidden(name.clone());
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.hidden(name.clone());
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.hidden(name.clone());
}
_ => {}
};
self
}
#[cfg(any(feature = "default", feature = "db-mysql"))]
fn pool(&mut self) -> Pool {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.pool()
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.pool()
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.pool()
}
#[cfg(any(feature = "default", feature = "db-mysql"))]
_ => Pool::new("").unwrap()
}
}
fn transaction(&mut self) -> bool {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.transaction()
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.transaction()
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.transaction()
}
_ => false
}
}
fn commit(&mut self) -> bool {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.commit()
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.commit()
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.commit()
}
_ => false
}
}
fn rollback(&mut self) -> bool {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.rollback()
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.rollback()
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.rollback()
}
_ => false
}
}
fn sql(&mut self, sql: String) -> JsonValue {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.sql(sql)
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.sql(sql)
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.sql(sql)
}
_ => object! {}
}
}
fn inc(&mut self, field: &str, num: f64) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.inc(field, num);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.inc(field, num);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.inc(field, num);
}
_ => {}
}
self
}
fn dec(&mut self, field: &str, num: f64) -> &mut Self {
match self {
#[cfg(any(feature = "default", feature = "db-mysql"))]
Db::Mysql(db) => {
db.dec(field, num);
}
#[cfg(any(feature = "default", feature = "db-sqlite"))]
Db::Sqlite(db) => {
db.dec(field, num);
}
#[cfg(any(feature = "default", feature = "db-mssql"))]
Db::Mssql(db) => {
db.dec(field, num);
}
_ => {}
}
self
}
}
#[derive(Clone, Debug)]
pub struct Connection {
pub mode: String,
pub hostname: String,
pub hostport: String,
pub database: String,
pub username: String,
pub userpass: String,
pub charset: String,
pub prefix: String,
pub params: Vec<String>,
pub debug: bool,
}
impl Connection {
pub fn default() -> Self {
Self {
mode: "".to_string(),
hostname: "".to_string(),
hostport: "".to_string(),
database: "".to_string(),
username: "".to_string(),
userpass: "".to_string(),
charset: "".to_string(),
prefix: "".to_string(),
params: vec![],
debug: false,
}
}
pub fn from(connection: JsonValue) -> Self {
Self {
mode: connection["mode"].to_string(),
hostname: connection["hostname"].to_string(),
hostport: connection["hostport"].to_string(),
database: connection["database"].to_string(),
username: connection["username"].to_string(),
userpass: connection["userpass"].to_string(),
charset: connection["charset"].to_string(),
prefix: connection["prefix"].to_string(),
params: connection["params"].members().map(|x| x.to_string()).collect(),
debug: connection["debug"].as_bool().unwrap_or(false),
}
}
pub fn get_dsn(self) -> String {
match self.mode.as_str() {
"mysql" => {
format!("mysql://{}:{}@{}:{}/{}", self.username, self.userpass, self.hostname, self.hostport, self.database)
}
"sqlite" => {
let data = Path::new(self.database.as_str());
format!("{}", data.to_str().unwrap())
}
"mssql" => format!("sqlsrv://{}:{}@{}:{}/{}", self.username, self.userpass, self.hostname, self.hostport, self.database),
_ => "".to_string()
}
}
}
#[derive(Clone, Debug)]
pub struct Table {
pub version: String,
pub table: String,
pub title: String,
pub primary_key: String,
pub auto: bool,
pub unique: Vec<String>,
pub index: Vec<Vec<String>>,
pub fields: JsonValue,
}
impl Table {
pub fn to_string(self) -> String {
let data = object! {
version:self.version,
table:self.table,
title:self.title,
primary_key:self.primary_key,
auto:self.auto,
unique:self.unique,
index:self.index,
fields:self.fields
};
data.to_string()
}
pub fn parse(mut data: JsonValue) -> Table {
let mut unique = vec![];
for item in 0..data["unique"].len() {
let str = data["unique"][item].clone().to_string();
unique.push(str);
}
let mut index = vec![];
for item in data["index"].members_mut() {
let mut row = vec![];
for col in item.members_mut() {
row.push(col.to_string());
}
if row.len() > 0 {
index.push(row);
}
}
Self {
version: data["version"].to_string(),
table: data["table"].to_string(),
title: data["title"].to_string(),
primary_key: data["primary_key"].to_string(),
auto: data["auto"].as_bool().unwrap(),
unique,
index,
fields: data["fields"].clone(),
}
}
}
#[derive(Clone, Debug)]
pub struct Params {
pub mode: String,
pub autoinc: bool,
pub table: String,
pub where_and: Vec<String>,
pub where_or: Vec<String>,
pub where_column: String,
pub inc_dec: JsonValue,
pub page: i32,
pub limit: i32,
pub fields: JsonValue,
pub top: String,
pub top2: String,
pub order: JsonValue,
pub group: JsonValue,
pub distinct: bool,
pub json: JsonValue,
pub sql: bool,
}
impl Params {
pub fn default(mode: &str) -> Self {
Self {
mode: mode.to_string(),
autoinc: false,
table: "".to_string(),
where_and: vec![],
where_or: vec![],
where_column: "".to_string(),
inc_dec: object! {},
page: -1,
limit: 10,
fields: object! {},
top: String::new(),
top2: String::new(),
order: object! {},
group: object! {},
distinct: false,
json: object! {},
sql: false,
}
}
pub fn where_sql(&mut self) -> String {
let mut where_and_sql = vec![];
let mut where_or_sql = vec![];
let mut sql = vec![];
for item in self.where_or.iter() {
where_or_sql.push(item.clone());
}
if where_or_sql.len() > 0 {
sql.push(where_or_sql.join(" OR "));
}
for item in self.where_and.iter() {
where_and_sql.push(item.clone());
}
if where_and_sql.len() > 0 {
sql.push(where_and_sql.join(" AND "));
}
if self.where_column != "" {
sql.push(self.where_column.clone());
}
if sql.len() > 0 {
return format!("WHERE {}", sql.join(" AND "));
}
return format!("");
}
pub fn page_limit_sql(&mut self) -> String {
if self.page == -1 {
return format!("");
}
match self.mode.as_str() {
"mysql" => {
return format!("LIMIT {},{}", self.page * self.limit - self.limit, self.limit);
}
"sqlite" => {
return format!("LIMIT {} OFFSET {}", self.limit, self.page * self.limit - self.limit);
}
"mssql" => return format!(""),
_ => return format!("")
}
}
pub fn fields(&mut self) -> String {
let mut fields = vec![];
for (_, value) in self.fields.entries() {
fields.push(value.to_string());
}
let fields = {
if fields.len() == 0 {
"*".into()
} else {
fields.join(",")
}
};
match self.mode.as_str() {
"mysql" => {
return format!("{}", fields);
}
"sqlite" => {
return format!("{}", fields);
}
"mssql" => {
return format!("{}", fields);
}
_ => return format!("{}", fields)
}
}
pub fn top(&mut self) -> String {
match self.mode.as_str() {
"mssql" => {
return format!("{}", self.top);
}
"mysql" | "sqlite" | _ => {
return format!("");
}
}
}
pub fn top2(&mut self) -> String {
match self.mode.as_str() {
"mssql" => {
if self.where_and.len() == 0 && self.where_or.len() == 0 {
return format!("where {}", self.top2);
}
return format!("{}", self.top2);
}
"mysql" | "sqlite" | _ => {
return format!("");
}
}
}
pub fn table(&mut self) -> String {
match self.mode.as_str() {
"mssql" => {
if self.top2 != "" {
return format!("t");
}
return format!("{}", self.table);
}
"mysql" | "sqlite" | _ => {
return format!("{}", self.table);
}
}
}
pub fn order(&mut self) -> String {
let mut sql = vec![];
for (field, item) in self.order.entries() {
sql.push(format!("{} {}", field, item));
}
if sql.len() > 0 {
return format!("ORDER BY {}", sql.join(","));
}
return format!("");
}
pub fn group(&mut self) -> String {
let mut sql = vec![];
for (field, _) in self.group.entries() {
sql.push(format!("{}", field));
}
if sql.len() > 0 {
return format!("GROUP BY {}", sql.join(","));
}
return format!("");
}
pub fn distinct(&self) -> String {
if self.distinct {
return "DISTINCT".to_string();
} else {
return "".to_string();
}
}
pub fn select_sql(&mut self) -> String {
return format!("SELECT {} {} FROM {} {} {} {} {} {} {}", self.distinct(), self.fields(), self.top(), self.table(), self.where_sql(), self.top2(), self.group(), self.order(), self.page_limit_sql());
}
}
#[derive(Clone, Debug)]
pub enum Charset {
Utf8mb4,
Utf8,
}
impl Charset {
pub fn from(str: &str) -> Charset {
match str {
"utf8" => Charset::Utf8,
"utf8mb4" => Charset::Utf8mb4,
_ => Charset::Utf8mb4
}
}
}