use crate::values::SQLiteValue;
use drizzle_core::{SQL, ToSQL};
#[derive(Debug, Clone, PartialEq)]
pub enum AutoVacuum {
None,
Full,
Incremental,
}
#[derive(Debug, Clone, PartialEq)]
pub enum JournalMode {
Delete,
Truncate,
Persist,
Memory,
Wal,
Off,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Synchronous {
Off,
Normal,
Full,
Extra,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TempStore {
Default,
File,
Memory,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LockingMode {
Normal,
Exclusive,
}
#[derive(Debug, Clone, PartialEq)]
pub enum SecureDelete {
Off,
On,
Fast,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Encoding {
Utf8,
Utf16Le,
Utf16Be,
}
#[derive(Debug, Clone, PartialEq)]
pub enum CacheSpill {
Enabled(bool),
Pages(i32),
}
#[derive(Debug, Clone, PartialEq)]
pub enum WalCheckpointMode {
Passive,
Full,
Restart,
Truncate,
Noop,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WritableSchema {
Enabled(bool),
Reset,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Pragma {
ApplicationId(i32),
AutoVacuum(AutoVacuum),
CacheSize(i32),
ForeignKeys(bool),
JournalMode(JournalMode),
WalAutocheckpoint(i32),
Synchronous(Synchronous),
TempStore(TempStore),
LockingMode(LockingMode),
SecureDelete(SecureDelete),
UserVersion(i32),
Encoding(Encoding),
PageSize(i32),
MmapSize(i64),
RecursiveTriggers(bool),
AnalysisLimit(i32),
AutomaticIndex(bool),
BusyTimeout(i32),
CacheSpill(CacheSpill),
CaseSensitiveLike(bool),
CellSizeCheck(bool),
CheckpointFullFsync(bool),
CountChanges(bool),
DataStoreDirectory(&'static str),
DefaultCacheSize(i32),
DeferForeignKeys(bool),
EmptyResultCallbacks(bool),
FullColumnNames(bool),
FullFsync(bool),
HardHeapLimit(i64),
IgnoreCheckConstraints(bool),
JournalSizeLimit(i64),
LegacyAlterTable(bool),
LegacyFileFormat,
MaxPageCount(i32),
QueryOnly(bool),
ReadUncommitted(bool),
ReverseUnorderedSelects(bool),
SchemaVersion(i32),
ShortColumnNames(bool),
SoftHeapLimit(i64),
TempStoreDirectory(&'static str),
Threads(i32),
TrustedSchema(bool),
WritableSchema(WritableSchema),
ParserTrace(bool),
VdbeAddoptrace(bool),
VdbeDebug(bool),
VdbeListing(bool),
VdbeTrace(bool),
CollationList,
CompileOptions,
DatabaseList,
FunctionList,
TableList,
TableXInfo(&'static str),
ModuleList,
DataVersion,
FreelistCount,
PageCount,
PragmaList,
Stats,
IncrementalVacuum(Option<i32>),
ShrinkMemory,
WalCheckpoint(Option<WalCheckpointMode>),
IntegrityCheck(Option<&'static str>),
QuickCheck(Option<&'static str>),
Optimize(Option<u32>),
ForeignKeyCheck(Option<&'static str>),
TableInfo(&'static str),
IndexList(&'static str),
IndexInfo(&'static str),
IndexXInfo(&'static str),
ForeignKeyList(&'static str),
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for AutoVacuum {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
AutoVacuum::None => SQL::raw("NONE"),
AutoVacuum::Full => SQL::raw("FULL"),
AutoVacuum::Incremental => SQL::raw("INCREMENTAL"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for JournalMode {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
JournalMode::Delete => SQL::raw("DELETE"),
JournalMode::Truncate => SQL::raw("TRUNCATE"),
JournalMode::Persist => SQL::raw("PERSIST"),
JournalMode::Memory => SQL::raw("MEMORY"),
JournalMode::Wal => SQL::raw("WAL"),
JournalMode::Off => SQL::raw("OFF"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for Synchronous {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
Synchronous::Off => SQL::raw("OFF"),
Synchronous::Normal => SQL::raw("NORMAL"),
Synchronous::Full => SQL::raw("FULL"),
Synchronous::Extra => SQL::raw("EXTRA"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for TempStore {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
TempStore::Default => SQL::raw("DEFAULT"),
TempStore::File => SQL::raw("FILE"),
TempStore::Memory => SQL::raw("MEMORY"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for LockingMode {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
LockingMode::Normal => SQL::raw("NORMAL"),
LockingMode::Exclusive => SQL::raw("EXCLUSIVE"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for SecureDelete {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
SecureDelete::Off => SQL::raw("OFF"),
SecureDelete::On => SQL::raw("ON"),
SecureDelete::Fast => SQL::raw("FAST"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for Encoding {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
Encoding::Utf8 => SQL::raw("UTF-8"),
Encoding::Utf16Le => SQL::raw("UTF-16LE"),
Encoding::Utf16Be => SQL::raw("UTF-16BE"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for CacheSpill {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
CacheSpill::Enabled(enabled) => SQL::raw(if *enabled { "ON" } else { "OFF" }),
CacheSpill::Pages(pages) => SQL::raw(format!("{}", pages)),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for WalCheckpointMode {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
WalCheckpointMode::Passive => SQL::raw("PASSIVE"),
WalCheckpointMode::Full => SQL::raw("FULL"),
WalCheckpointMode::Restart => SQL::raw("RESTART"),
WalCheckpointMode::Truncate => SQL::raw("TRUNCATE"),
WalCheckpointMode::Noop => SQL::raw("NOOP"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for WritableSchema {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
WritableSchema::Enabled(enabled) => SQL::raw(if *enabled { "ON" } else { "OFF" }),
WritableSchema::Reset => SQL::raw("RESET"),
}
}
}
impl<'a> ToSQL<'a, SQLiteValue<'a>> for Pragma {
fn to_sql(&self) -> SQL<'a, SQLiteValue<'a>> {
match self {
Pragma::ApplicationId(id) => SQL::raw(format!("PRAGMA application_id = {}", id)),
Pragma::AutoVacuum(mode) => SQL::raw("PRAGMA auto_vacuum = ").append(mode.to_sql()),
Pragma::CacheSize(size) => SQL::raw(format!("PRAGMA cache_size = {}", size)),
Pragma::ForeignKeys(enabled) => SQL::raw("PRAGMA foreign_keys = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::JournalMode(mode) => SQL::raw("PRAGMA journal_mode = ").append(mode.to_sql()),
Pragma::Synchronous(mode) => SQL::raw("PRAGMA synchronous = ").append(mode.to_sql()),
Pragma::WalAutocheckpoint(pages) => {
SQL::raw(format!("PRAGMA wal_autocheckpoint = {}", pages))
}
Pragma::TempStore(store) => SQL::raw("PRAGMA temp_store = ").append(store.to_sql()),
Pragma::LockingMode(mode) => SQL::raw("PRAGMA locking_mode = ").append(mode.to_sql()),
Pragma::SecureDelete(mode) => SQL::raw("PRAGMA secure_delete = ").append(mode.to_sql()),
Pragma::UserVersion(version) => SQL::raw(format!("PRAGMA user_version = {}", version)),
Pragma::Encoding(encoding) => SQL::raw("PRAGMA encoding = ").append(encoding.to_sql()),
Pragma::PageSize(size) => SQL::raw(format!("PRAGMA page_size = {}", size)),
Pragma::MmapSize(size) => SQL::raw(format!("PRAGMA mmap_size = {}", size)),
Pragma::RecursiveTriggers(enabled) => SQL::raw("PRAGMA recursive_triggers = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::AnalysisLimit(limit) => SQL::raw(format!("PRAGMA analysis_limit = {}", limit)),
Pragma::AutomaticIndex(enabled) => SQL::raw("PRAGMA automatic_index = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::BusyTimeout(timeout) => SQL::raw(format!("PRAGMA busy_timeout = {}", timeout)),
Pragma::CacheSpill(setting) => {
SQL::raw("PRAGMA cache_spill = ").append(setting.to_sql())
}
Pragma::CaseSensitiveLike(enabled) => SQL::raw("PRAGMA case_sensitive_like = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::CellSizeCheck(enabled) => SQL::raw("PRAGMA cell_size_check = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::CheckpointFullFsync(enabled) => SQL::raw("PRAGMA checkpoint_fullfsync = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::CountChanges(enabled) => SQL::raw("PRAGMA count_changes = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::DataStoreDirectory(directory) => {
SQL::raw(format!("PRAGMA data_store_directory = '{}'", directory))
}
Pragma::DefaultCacheSize(size) => {
SQL::raw(format!("PRAGMA default_cache_size = {}", size))
}
Pragma::DeferForeignKeys(enabled) => SQL::raw("PRAGMA defer_foreign_keys = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::EmptyResultCallbacks(enabled) => SQL::raw("PRAGMA empty_result_callbacks = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::FullColumnNames(enabled) => SQL::raw("PRAGMA full_column_names = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::FullFsync(enabled) => SQL::raw("PRAGMA fullfsync = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::HardHeapLimit(limit) => SQL::raw(format!("PRAGMA hard_heap_limit = {}", limit)),
Pragma::IgnoreCheckConstraints(enabled) => {
SQL::raw("PRAGMA ignore_check_constraints = ").append(SQL::raw(if *enabled {
"ON"
} else {
"OFF"
}))
}
Pragma::JournalSizeLimit(limit) => {
SQL::raw(format!("PRAGMA journal_size_limit = {}", limit))
}
Pragma::LegacyAlterTable(enabled) => SQL::raw("PRAGMA legacy_alter_table = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::LegacyFileFormat => SQL::raw("PRAGMA legacy_file_format"),
Pragma::MaxPageCount(count) => SQL::raw(format!("PRAGMA max_page_count = {}", count)),
Pragma::QueryOnly(enabled) => SQL::raw("PRAGMA query_only = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::ReadUncommitted(enabled) => SQL::raw("PRAGMA read_uncommitted = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::ReverseUnorderedSelects(enabled) => {
SQL::raw("PRAGMA reverse_unordered_selects = ").append(SQL::raw(if *enabled {
"ON"
} else {
"OFF"
}))
}
Pragma::SchemaVersion(version) => {
SQL::raw(format!("PRAGMA schema_version = {}", version))
}
Pragma::ShortColumnNames(enabled) => SQL::raw("PRAGMA short_column_names = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::SoftHeapLimit(limit) => SQL::raw(format!("PRAGMA soft_heap_limit = {}", limit)),
Pragma::TempStoreDirectory(directory) => {
SQL::raw(format!("PRAGMA temp_store_directory = '{}'", directory))
}
Pragma::Threads(threads) => SQL::raw(format!("PRAGMA threads = {}", threads)),
Pragma::TrustedSchema(enabled) => SQL::raw("PRAGMA trusted_schema = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::WritableSchema(mode) => {
SQL::raw("PRAGMA writable_schema = ").append(mode.to_sql())
}
Pragma::ParserTrace(enabled) => SQL::raw("PRAGMA parser_trace = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::VdbeAddoptrace(enabled) => SQL::raw("PRAGMA vdbe_addoptrace = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::VdbeDebug(enabled) => SQL::raw("PRAGMA vdbe_debug = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::VdbeListing(enabled) => SQL::raw("PRAGMA vdbe_listing = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::VdbeTrace(enabled) => SQL::raw("PRAGMA vdbe_trace = ")
.append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
Pragma::CollationList => SQL::raw("PRAGMA collation_list"),
Pragma::CompileOptions => SQL::raw("PRAGMA compile_options"),
Pragma::DatabaseList => SQL::raw("PRAGMA database_list"),
Pragma::FunctionList => SQL::raw("PRAGMA function_list"),
Pragma::TableList => SQL::raw("PRAGMA table_list"),
Pragma::TableXInfo(table) => SQL::raw(format!("PRAGMA table_xinfo({})", table)),
Pragma::ModuleList => SQL::raw("PRAGMA module_list"),
Pragma::DataVersion => SQL::raw("PRAGMA data_version"),
Pragma::FreelistCount => SQL::raw("PRAGMA freelist_count"),
Pragma::PageCount => SQL::raw("PRAGMA page_count"),
Pragma::PragmaList => SQL::raw("PRAGMA pragma_list"),
Pragma::Stats => SQL::raw("PRAGMA stats"),
Pragma::IncrementalVacuum(pages) => match pages {
Some(count) => SQL::raw(format!("PRAGMA incremental_vacuum({})", count)),
None => SQL::raw("PRAGMA incremental_vacuum"),
},
Pragma::ShrinkMemory => SQL::raw("PRAGMA shrink_memory"),
Pragma::WalCheckpoint(mode) => match mode {
Some(checkpoint_mode) => {
SQL::raw("PRAGMA wal_checkpoint = ").append(checkpoint_mode.to_sql())
}
None => SQL::raw("PRAGMA wal_checkpoint"),
},
Pragma::IntegrityCheck(table) => match table {
Some(t) => SQL::raw(format!("PRAGMA integrity_check({})", t)),
None => SQL::raw("PRAGMA integrity_check"),
},
Pragma::QuickCheck(table) => match table {
Some(t) => SQL::raw(format!("PRAGMA quick_check({})", t)),
None => SQL::raw("PRAGMA quick_check"),
},
Pragma::Optimize(mask) => match mask {
Some(m) => SQL::raw(format!("PRAGMA optimize({})", m)),
None => SQL::raw("PRAGMA optimize"),
},
Pragma::ForeignKeyCheck(table) => match table {
Some(t) => SQL::raw(format!("PRAGMA foreign_key_check({})", t)),
None => SQL::raw("PRAGMA foreign_key_check"),
},
Pragma::TableInfo(table) => SQL::raw(format!("PRAGMA table_info({})", table)),
Pragma::IndexList(table) => SQL::raw(format!("PRAGMA index_list({})", table)),
Pragma::IndexInfo(index) => SQL::raw(format!("PRAGMA index_info({})", index)),
Pragma::IndexXInfo(index) => SQL::raw(format!("PRAGMA index_xinfo({})", index)),
Pragma::ForeignKeyList(table) => {
SQL::raw(format!("PRAGMA foreign_key_list({})", table))
}
}
}
}
impl Pragma {
pub fn query(pragma_name: &str) -> SQL<'static, SQLiteValue<'static>> {
SQL::raw(format!("PRAGMA {}", pragma_name))
}
pub fn foreign_keys(enabled: bool) -> Self {
Self::ForeignKeys(enabled)
}
pub fn journal_mode(mode: JournalMode) -> Self {
Self::JournalMode(mode)
}
pub fn wal_autocheckpoint(pages: i32) -> Self {
Self::WalAutocheckpoint(pages)
}
pub fn table_info(table: &'static str) -> Self {
Self::TableInfo(table)
}
pub fn index_list(table: &'static str) -> Self {
Self::IndexList(table)
}
pub fn foreign_key_list(table: &'static str) -> Self {
Self::ForeignKeyList(table)
}
pub fn integrity_check(table: Option<&'static str>) -> Self {
Self::IntegrityCheck(table)
}
pub fn foreign_key_check(table: Option<&'static str>) -> Self {
Self::ForeignKeyCheck(table)
}
pub fn table_xinfo(table: &'static str) -> Self {
Self::TableXInfo(table)
}
pub fn encoding(encoding: Encoding) -> Self {
Self::Encoding(encoding)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query_pragma_helper() {
assert_eq!(Pragma::query("foreign_keys").sql(), "PRAGMA foreign_keys");
assert_eq!(Pragma::query("custom_pragma").sql(), "PRAGMA custom_pragma");
}
#[test]
fn test_convenience_constructor_integration() {
assert_eq!(
Pragma::foreign_keys(true).to_sql().sql(),
Pragma::ForeignKeys(true).to_sql().sql()
);
assert_eq!(
Pragma::table_info("users").to_sql().sql(),
Pragma::TableInfo("users").to_sql().sql()
);
assert_eq!(
Pragma::encoding(Encoding::Utf8).to_sql().sql(),
Pragma::Encoding(Encoding::Utf8).to_sql().sql()
);
}
}