Skip to content

Commit

Permalink
Merge 'Pragma list' from Glauber Costa
Browse files Browse the repository at this point in the history
Closes #841
  • Loading branch information
penberg committed Jan 31, 2025
2 parents e96b649 + a7cc367 commit c05b81f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
2 changes: 1 addition & 1 deletion COMPAT.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ The current status of Limbo is:
| PRAGMA page_count | No | |
| PRAGMA page_size | No | |
| PRAGMA parser_trace | No | |
| PRAGMA pragma_list | No | |
| PRAGMA pragma_list | Yes | |
| PRAGMA query_only | No | |
| PRAGMA quick_check | No | |
| PRAGMA read_uncommitted | No | |
Expand Down
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ limbo_vector = { path = "../extensions/vector", optional = true, features = ["st
limbo_regexp = { path = "../extensions/regexp", optional = true, features = ["static"] }
limbo_percentile = { path = "../extensions/percentile", optional = true, features = ["static"] }
miette = "7.4.0"
strum = "0.26"

[build-dependencies]
chrono = "0.4.38"
Expand Down
39 changes: 38 additions & 1 deletion core/translate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::storage::wal::CheckpointMode;
use crate::translate::delete::translate_delete;
use crate::util::{normalize_ident, PRIMARY_KEY_AUTOMATIC_INDEX_NAME_PREFIX};
use crate::vdbe::builder::CursorType;
use crate::vdbe::BranchOffset;
use crate::vdbe::{builder::ProgramBuilder, insn::Insn, Program};
use crate::{bail_parse_error, Connection, LimboError, Result, SymbolTable};
use insert::translate_insert;
Expand All @@ -38,6 +39,7 @@ use std::cell::RefCell;
use std::fmt::Display;
use std::rc::{Rc, Weak};
use std::str::FromStr;
use strum::IntoEnumIterator;

/// Translate SQL statement into bytecode program.
pub fn translate(
Expand Down Expand Up @@ -531,6 +533,36 @@ enum PrimaryKeyDefinitionType<'a> {
Composite,
}

fn list_pragmas(
program: &mut ProgramBuilder,
init_label: BranchOffset,
start_offset: BranchOffset,
) {
let mut pragma_strings: Vec<String> = PragmaName::iter().map(|x| x.to_string()).collect();
pragma_strings.sort();

let register = program.alloc_register();
for pragma in &pragma_strings {
program.emit_insn(Insn::String8 {
value: pragma.to_string(),
dest: register,
});
program.emit_insn(Insn::ResultRow {
start_reg: register,
count: 1,
});
}
program.emit_insn(Insn::Halt {
err_code: 0,
description: String::new(),
});
program.resolve_label(init_label, program.offset());
program.emit_constant_insns();
program.emit_insn(Insn::Goto {
target_pc: start_offset,
});
}

fn translate_pragma(
program: &mut ProgramBuilder,
schema: &Schema,
Expand All @@ -546,9 +578,14 @@ fn translate_pragma(
let start_offset = program.offset();
let mut write = false;

if name.name.0.to_lowercase() == "pragma_list" {
list_pragmas(program, init_label, start_offset);
return Ok(());
}

let pragma = match PragmaName::from_str(&name.name.0) {
Ok(pragma) => pragma,
Err(()) => bail_parse_error!("Not a valid pragma name"),
Err(_) => bail_parse_error!("Not a valid pragma name"),
};

match body {
Expand Down
2 changes: 2 additions & 0 deletions vendored/sqlite3-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ bitflags = "2.0"
uncased = "0.9.10"
indexmap = "2.0"
miette = "7.4.0"
strum = { version = "0.26", features = ["derive"] }
strum_macros = "0.26"

[dev-dependencies]
env_logger = { version = "0.11", default-features = false }
Expand Down
19 changes: 4 additions & 15 deletions vendored/sqlite3-parser/src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::num::ParseIntError;
use std::ops::Deref;
use std::str::{self, Bytes, FromStr};

use strum_macros::{EnumIter, EnumString};

use fmt::{ToTokens, TokenStream};
use indexmap::{IndexMap, IndexSet};

Expand Down Expand Up @@ -1585,7 +1587,8 @@ pub type PragmaValue = Expr; // TODO

/// `PRAGMA` value
// https://sqlite.org/pragma.html
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, EnumString, strum::Display)]
#[strum(serialize_all = "snake_case")]
pub enum PragmaName {
/// `cache_size` pragma
CacheSize,
Expand All @@ -1597,20 +1600,6 @@ pub enum PragmaName {
TableInfo,
}

impl FromStr for PragmaName {
type Err = ();

fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"cache_size" => Ok(PragmaName::CacheSize),
"wal_checkpoint" => Ok(PragmaName::WalCheckpoint),
"journal_mode" => Ok(PragmaName::JournalMode),
"table_info" => Ok(PragmaName::TableInfo),
_ => Err(()),
}
}
}

/// `CREATE TRIGGER` time
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TriggerTime {
Expand Down

0 comments on commit c05b81f

Please sign in to comment.