Skip to main content

Module pragma

Module pragma 

Source
Available on crate feature __sqlite-shared only.
Expand description

Typed access to SQLite pragmas.

A pragma is declared as a type implementing Pragma, with ReadPragma when it returns rows and WritePragma when it can be assigned an argument. Its Scope decides whether it is used per database or per connection, and the caller picks the Rust type each row decodes into.

§Per-schema pragmas

A PerSchema pragma applies to one database and is read and written through SchemaPragma, where a None schema selects main.

use diesel::sqlite::pragma::{PerSchema, Pragma, ReadPragma, SchemaPragma, WritePragma};

struct UserVersion;

impl Pragma for UserVersion {
    const NAME: &'static str = "user_version";
    type Scope = PerSchema;
}

impl ReadPragma for UserVersion {
    type SqlType = Integer;
}

impl WritePragma for UserVersion {
    type Argument<'a> = i32;
}

let conn = &mut SqliteConnection::establish(":memory:").unwrap();
UserVersion::set(7, None, conn)?;
assert_eq!(7, UserVersion::get::<i32>(None, conn)?);

§Per-connection pragmas

A PerConnection pragma applies to the whole connection and is read and written through ConnectionPragma, which takes no schema.

use diesel::sqlite::pragma::{
    ConnectionPragma, PerConnection, Pragma, ReadPragma, WritePragma,
};

struct ForeignKeys;

impl Pragma for ForeignKeys {
    const NAME: &'static str = "foreign_keys";
    type Scope = PerConnection;
}

impl ReadPragma for ForeignKeys {
    type SqlType = Bool;
}

impl WritePragma for ForeignKeys {
    type Argument<'a> = bool;
}

let conn = &mut SqliteConnection::establish(":memory:").unwrap();
ForeignKeys::set(true, conn)?;
assert!(ForeignKeys::get::<bool>(conn)?);

Structs§

AutoVacuum
PRAGMA auto_vacuum, read and set as an AutoVacuumMode.
FreelistCount
PRAGMA freelist_count, the unused pages on a database’s freelist, read as an i64.
IncrementalVacuum
PRAGMA incremental_vacuum, shrinking a database by releasing freelist pages without the full rewrite VACUUM performs.
PageCount
PRAGMA page_count, the total number of pages in a database, read as an i64.
PragmaKeyword
A keyword written unquoted, such as WAL or NORMAL.
PragmaText
Text written as a single-quoted SQL string with embedded quotes doubled, such as 'UTF-16le'.

Enums§

PerConnection
A connection-wide pragma, whose schema qualifier SQLite ignores.
PerSchema
A pragma that applies to one database, main or an attached one.

Traits§

ConnectionPragma
Reads and writes a PerConnection pragma.
Pragma
A pragma, named by NAME and scoped by Scope.
PragmaArgument
A value written into a pragma statement, which takes no bind parameters.
PragmaScope
Whether a pragma targets one database or the whole connection.
ReadPragma
A pragma that returns rows when queried.
SchemaPragma
Reads and writes a PerSchema pragma, where a None schema selects main.
WritePragma
A pragma that can be assigned an Argument.