pragma.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #![allow(clippy::upper_case_acronyms)]
  2. use crate::errors::{Error, Result};
  3. use diesel::{
  4. expression::SqlLiteral,
  5. query_dsl::load_dsl::LoadQuery,
  6. sql_types::{Integer, Text},
  7. SqliteConnection,
  8. };
  9. use crate::conn_ext::ConnectionExtension;
  10. use std::{
  11. convert::{TryFrom, TryInto},
  12. fmt,
  13. str::FromStr,
  14. };
  15. pub trait PragmaExtension: ConnectionExtension {
  16. fn pragma<D: std::fmt::Display>(&self, key: &str, val: D, schema: Option<&str>) -> Result<()> {
  17. let query = match schema {
  18. Some(schema) => format!("PRAGMA {}.{} = '{}'", schema, key, val),
  19. None => format!("PRAGMA {} = '{}'", key, val),
  20. };
  21. log::trace!("SQLITE {}", query);
  22. self.exec(&query)?;
  23. Ok(())
  24. }
  25. fn pragma_ret<ST, T, D: std::fmt::Display>(&self, key: &str, val: D, schema: Option<&str>) -> Result<T>
  26. where
  27. SqlLiteral<ST>: LoadQuery<SqliteConnection, T>,
  28. {
  29. let query = match schema {
  30. Some(schema) => format!("PRAGMA {}.{} = '{}'", schema, key, val),
  31. None => format!("PRAGMA {} = '{}'", key, val),
  32. };
  33. log::trace!("SQLITE {}", query);
  34. self.query::<ST, T>(&query)
  35. }
  36. fn pragma_get<ST, T>(&self, key: &str, schema: Option<&str>) -> Result<T>
  37. where
  38. SqlLiteral<ST>: LoadQuery<SqliteConnection, T>,
  39. {
  40. let query = match schema {
  41. Some(schema) => format!("PRAGMA {}.{}", schema, key),
  42. None => format!("PRAGMA {}", key),
  43. };
  44. log::trace!("SQLITE {}", query);
  45. self.query::<ST, T>(&query)
  46. }
  47. fn pragma_set_busy_timeout(&self, timeout_ms: i32) -> Result<i32> {
  48. self.pragma_ret::<Integer, i32, i32>("busy_timeout", timeout_ms, None)
  49. }
  50. fn pragma_get_busy_timeout(&self) -> Result<i32> {
  51. self.pragma_get::<Integer, i32>("busy_timeout", None)
  52. }
  53. fn pragma_set_journal_mode(&self, mode: SQLiteJournalMode, schema: Option<&str>) -> Result<i32> {
  54. self.pragma_ret::<Integer, i32, SQLiteJournalMode>("journal_mode", mode, schema)
  55. }
  56. fn pragma_get_journal_mode(&self, schema: Option<&str>) -> Result<SQLiteJournalMode> {
  57. self.pragma_get::<Text, String>("journal_mode", schema)?.parse()
  58. }
  59. fn pragma_set_synchronous(&self, synchronous: SQLiteSynchronous, schema: Option<&str>) -> Result<()> {
  60. self.pragma("synchronous", synchronous as u8, schema)
  61. }
  62. fn pragma_get_synchronous(&self, schema: Option<&str>) -> Result<SQLiteSynchronous> {
  63. self.pragma_get::<Integer, i32>("synchronous", schema)?.try_into()
  64. }
  65. }
  66. impl PragmaExtension for SqliteConnection {}
  67. #[derive(Debug, PartialEq, Eq, Clone, Copy)]
  68. pub enum SQLiteJournalMode {
  69. DELETE,
  70. TRUNCATE,
  71. PERSIST,
  72. MEMORY,
  73. WAL,
  74. OFF,
  75. }
  76. impl fmt::Display for SQLiteJournalMode {
  77. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  78. write!(
  79. f,
  80. "{}",
  81. match self {
  82. Self::DELETE => "DELETE",
  83. Self::TRUNCATE => "TRUNCATE",
  84. Self::PERSIST => "PERSIST",
  85. Self::MEMORY => "MEMORY",
  86. Self::WAL => "WAL",
  87. Self::OFF => "OFF",
  88. }
  89. )
  90. }
  91. }
  92. impl FromStr for SQLiteJournalMode {
  93. type Err = Error;
  94. fn from_str(s: &str) -> Result<Self> {
  95. match s.to_uppercase().as_ref() {
  96. "DELETE" => Ok(Self::DELETE),
  97. "TRUNCATE" => Ok(Self::TRUNCATE),
  98. "PERSIST" => Ok(Self::PERSIST),
  99. "MEMORY" => Ok(Self::MEMORY),
  100. "WAL" => Ok(Self::WAL),
  101. "OFF" => Ok(Self::OFF),
  102. _ => Err(format!("Unknown value {} for JournalMode", s).into()),
  103. }
  104. }
  105. }
  106. #[derive(Debug, PartialEq, Eq, Clone, Copy)]
  107. pub enum SQLiteSynchronous {
  108. EXTRA = 3,
  109. FULL = 2,
  110. NORMAL = 1,
  111. OFF = 0,
  112. }
  113. impl fmt::Display for SQLiteSynchronous {
  114. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  115. write!(
  116. f,
  117. "{}",
  118. match self {
  119. Self::OFF => "OFF",
  120. Self::NORMAL => "NORMAL",
  121. Self::FULL => "FULL",
  122. Self::EXTRA => "EXTRA",
  123. }
  124. )
  125. }
  126. }
  127. impl TryFrom<i32> for SQLiteSynchronous {
  128. type Error = Error;
  129. fn try_from(v: i32) -> Result<Self> {
  130. match v {
  131. 0 => Ok(Self::OFF),
  132. 1 => Ok(Self::NORMAL),
  133. 2 => Ok(Self::FULL),
  134. 3 => Ok(Self::EXTRA),
  135. _ => Err(format!("Unknown value {} for Synchronous", v).into()),
  136. }
  137. }
  138. }
  139. impl FromStr for SQLiteSynchronous {
  140. type Err = Error;
  141. fn from_str(s: &str) -> Result<Self> {
  142. match s.to_uppercase().as_ref() {
  143. "0" | "OFF" => Ok(Self::OFF),
  144. "1" | "NORMAL" => Ok(Self::NORMAL),
  145. "2" | "FULL" => Ok(Self::FULL),
  146. "3" | "EXTRA" => Ok(Self::EXTRA),
  147. _ => Err(format!("Unknown value {} for Synchronous", s).into()),
  148. }
  149. }
  150. }