use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaConfig {
pub tables: Vec<TableConfig>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableConfig {
pub name: String,
pub columns: Vec<String>,
}
#[must_use]
pub fn load_schema_config() -> SchemaConfig {
let mut paths = vec![
String::from("schema.json"),
String::from(".sql-cli/schema.json"),
];
if let Some(config_dir) = dirs::config_dir() {
if let Some(path_str) = config_dir.join("sql-cli/schema.json").to_str() {
paths.push(String::from(path_str));
}
}
for path in paths {
if Path::new(&path).exists() {
if let Ok(contents) = fs::read_to_string(&path) {
if let Ok(config) = serde_json::from_str::<SchemaConfig>(&contents) {
eprintln!("Loaded schema from: {path}");
return config;
}
}
}
}
SchemaConfig {
tables: vec![
TableConfig {
name: String::from("trade_deal"),
columns: get_full_trade_deal_columns(),
},
TableConfig {
name: String::from("instrument"),
columns: vec![
String::from("instrumentId"),
String::from("name"),
String::from("type"),
],
},
],
}
}
pub fn get_full_trade_deal_columns() -> Vec<String> {
vec![
"dealId",
"platformOrderId",
"externalOrderId",
"parentOrderId",
"tradeDate",
"settlementDate",
"valueDate",
"maturityDate",
"lastModifiedDate",
"createdDate",
"confirmationDate",
"executionDate",
"instrumentId",
"instrumentName",
"instrumentType",
"isin",
"cusip",
"sedol",
"ticker",
"exchange",
"quantity",
"price",
"notional",
"settlementAmount",
"grossAmount",
"netAmount",
"accruedInterest",
"accrual",
"commission",
"fees",
"tax",
"spread",
"currency",
"baseCurrency",
"quoteCurrency",
"settlementCurrency",
"counterparty",
"counterpartyId",
"counterpartyType",
"counterpartyCountry",
"counterpartyLei",
"trader",
"traderId",
"book",
"bookId",
"portfolio",
"portfolioId",
"strategy",
"desk",
"legalEntity",
"branch",
"region",
"side",
"productType",
"instrumentClass",
"assetClass",
"venue",
"executionVenue",
"clearingHouse",
"clearingBroker",
"prime",
"custodian",
"subCustodian",
"status",
"confirmationStatus",
"settlementStatus",
"allocationStatus",
"clearingStatus",
"bookingStatus",
"pv01",
"dv01",
"delta",
"gamma",
"vega",
"theta",
"duration",
"convexity",
"yield",
"spread",
"regulatoryReporting",
"mifidClassification",
"bestExecution",
"preTradeTransparency",
"postTradeTransparency",
"comments",
"notes",
"auditTrail",
"version",
"source",
"sourceSystem",
"lastUpdatedBy",
"createdBy",
"clientOrderId",
"brokerOrderId",
"exchangeOrderId",
"blockTradeId",
"allocationId",
"confirmationId",
]
.into_iter()
.map(String::from)
.collect()
}
pub fn save_schema_example(path: &str) -> Result<(), Box<dyn std::error::Error>> {
let schema = load_schema_config();
let json = serde_json::to_string_pretty(&schema)?;
fs::write(path, json)?;
Ok(())
}
pub enum LinqOperator {
Contains(String), StartsWith(String), EndsWith(String), GreaterThan(String), LessThan(String), Between(String, String), In(Vec<String>), IsNull, IsNotNull, }
pub enum DateConstructor {
Today, Now, Date(i32, u32, u32), DateOffset(i32), }