Skip to content

Commit 394a7e8

Browse files
committed
fix(sqlite): fix unit and doctests
1 parent 27c5730 commit 394a7e8

File tree

4 files changed

+80
-62
lines changed

4 files changed

+80
-62
lines changed

sqlx-sqlite/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ features = [
6363
workspace = true
6464

6565
[dev-dependencies]
66-
sqlx = { workspace = true, default-features = false, features = ["macros", "runtime-tokio", "tls-none"] }
66+
sqlx = { workspace = true, default-features = false, features = ["macros", "runtime-tokio", "tls-none", "sqlite"] }
6767

6868
[lints]
6969
workspace = true

sqlx-sqlite/src/connection/explain.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,147 +1633,147 @@ fn test_root_block_columns_has_types() {
16331633
{
16341634
let table_db_block = table_block_nums["t"];
16351635
assert_eq!(
1636-
ColumnType::Single {
1636+
Some(&ColumnType::Single {
16371637
datatype: DataType::Integer,
16381638
nullable: Some(true) //sqlite primary key columns are nullable unless declared not null
1639-
},
1640-
root_block_cols[&table_db_block][&0]
1639+
}),
1640+
root_block_cols[&table_db_block].get(&0)
16411641
);
16421642
assert_eq!(
1643-
ColumnType::Single {
1643+
Some(&ColumnType::Single {
16441644
datatype: DataType::Text,
16451645
nullable: Some(true)
1646-
},
1647-
root_block_cols[&table_db_block][&1]
1646+
}),
1647+
root_block_cols[&table_db_block].get(&1)
16481648
);
16491649
assert_eq!(
1650-
ColumnType::Single {
1650+
Some(&ColumnType::Single {
16511651
datatype: DataType::Text,
16521652
nullable: Some(false)
1653-
},
1654-
root_block_cols[&table_db_block][&2]
1653+
}),
1654+
root_block_cols[&table_db_block].get(&2)
16551655
);
16561656
}
16571657

16581658
{
16591659
let table_db_block = table_block_nums["i1"];
16601660
assert_eq!(
1661-
ColumnType::Single {
1661+
Some(&ColumnType::Single {
16621662
datatype: DataType::Integer,
16631663
nullable: Some(true) //sqlite primary key columns are nullable unless declared not null
1664-
},
1665-
root_block_cols[&table_db_block][&0]
1664+
}),
1665+
root_block_cols[&table_db_block].get(&0)
16661666
);
16671667
assert_eq!(
1668-
ColumnType::Single {
1668+
Some(&ColumnType::Single {
16691669
datatype: DataType::Text,
16701670
nullable: Some(true)
1671-
},
1672-
root_block_cols[&table_db_block][&1]
1671+
}),
1672+
root_block_cols[&table_db_block].get(&1)
16731673
);
16741674
}
16751675

16761676
{
16771677
let table_db_block = table_block_nums["i2"];
16781678
assert_eq!(
1679-
ColumnType::Single {
1679+
Some(&ColumnType::Single {
16801680
datatype: DataType::Integer,
16811681
nullable: Some(true) //sqlite primary key columns are nullable unless declared not null
1682-
},
1683-
root_block_cols[&table_db_block][&0]
1682+
}),
1683+
root_block_cols[&table_db_block].get(&0)
16841684
);
16851685
assert_eq!(
1686-
ColumnType::Single {
1686+
Some(&ColumnType::Single {
16871687
datatype: DataType::Text,
16881688
nullable: Some(true)
1689-
},
1690-
root_block_cols[&table_db_block][&1]
1689+
}),
1690+
root_block_cols[&table_db_block].get(&1)
16911691
);
16921692
}
16931693

16941694
{
16951695
let table_db_block = table_block_nums["t2"];
16961696
assert_eq!(
1697-
ColumnType::Single {
1697+
Some(&ColumnType::Single {
16981698
datatype: DataType::Integer,
16991699
nullable: Some(false)
1700-
},
1701-
root_block_cols[&table_db_block][&0]
1700+
}),
1701+
root_block_cols[&table_db_block].get(&0)
17021702
);
17031703
assert_eq!(
1704-
ColumnType::Single {
1704+
Some(&ColumnType::Single {
17051705
datatype: DataType::Null,
17061706
nullable: Some(true)
1707-
},
1708-
root_block_cols[&table_db_block][&1]
1707+
}),
1708+
root_block_cols[&table_db_block].get(&1)
17091709
);
17101710
assert_eq!(
1711-
ColumnType::Single {
1711+
Some(&ColumnType::Single {
17121712
datatype: DataType::Null,
17131713
nullable: Some(false)
1714-
},
1715-
root_block_cols[&table_db_block][&2]
1714+
}),
1715+
root_block_cols[&table_db_block].get(&2)
17161716
);
17171717
}
17181718

17191719
{
17201720
let table_db_block = table_block_nums["t2i1"];
17211721
assert_eq!(
1722-
ColumnType::Single {
1722+
Some(&ColumnType::Single {
17231723
datatype: DataType::Integer,
17241724
nullable: Some(false)
1725-
},
1726-
root_block_cols[&table_db_block][&0]
1725+
}),
1726+
root_block_cols[&table_db_block].get(&0)
17271727
);
17281728
assert_eq!(
1729-
ColumnType::Single {
1729+
Some(&ColumnType::Single {
17301730
datatype: DataType::Null,
17311731
nullable: Some(true)
1732-
},
1733-
root_block_cols[&table_db_block][&1]
1732+
}),
1733+
root_block_cols[&table_db_block].get(&1)
17341734
);
17351735
}
17361736

17371737
{
17381738
let table_db_block = table_block_nums["t2i2"];
17391739
assert_eq!(
1740-
ColumnType::Single {
1740+
Some(&ColumnType::Single {
17411741
datatype: DataType::Integer,
17421742
nullable: Some(false)
1743-
},
1744-
root_block_cols[&table_db_block][&0]
1743+
}),
1744+
root_block_cols[&table_db_block].get(&0)
17451745
);
17461746
assert_eq!(
1747-
ColumnType::Single {
1747+
Some(&ColumnType::Single {
17481748
datatype: DataType::Null,
17491749
nullable: Some(false)
1750-
},
1751-
root_block_cols[&table_db_block][&1]
1750+
}),
1751+
root_block_cols[&table_db_block].get(&1)
17521752
);
17531753
}
17541754

17551755
{
17561756
let table_db_block = table_block_nums["t3"];
17571757
assert_eq!(
1758-
ColumnType::Single {
1758+
Some(&ColumnType::Single {
17591759
datatype: DataType::Text,
17601760
nullable: Some(true)
1761-
},
1762-
root_block_cols[&table_db_block][&0]
1761+
}),
1762+
root_block_cols[&table_db_block].get(&0)
17631763
);
17641764
assert_eq!(
1765-
ColumnType::Single {
1765+
Some(&ColumnType::Single {
17661766
datatype: DataType::Float,
17671767
nullable: Some(false)
1768-
},
1769-
root_block_cols[&table_db_block][&1]
1768+
}),
1769+
root_block_cols[&table_db_block].get(&1)
17701770
);
17711771
assert_eq!(
1772-
ColumnType::Single {
1772+
Some(&ColumnType::Single {
17731773
datatype: DataType::Float,
17741774
nullable: Some(true)
1775-
},
1776-
root_block_cols[&table_db_block][&2]
1775+
}),
1776+
root_block_cols[&table_db_block].get(&2)
17771777
);
17781778
}
17791779
}

sqlx-sqlite/src/options/parse.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
use crate::error::Error;
2-
use crate::SqliteConnectOptions;
3-
use percent_encoding::{percent_decode_str, utf8_percent_encode, NON_ALPHANUMERIC};
41
use std::borrow::Cow;
52
use std::path::{Path, PathBuf};
63
use std::str::FromStr;
74
use std::sync::atomic::{AtomicUsize, Ordering};
5+
6+
use percent_encoding::{percent_decode_str, percent_encode, AsciiSet};
87
use url::Url;
98

9+
use crate::error::Error;
10+
use crate::SqliteConnectOptions;
11+
1012
// https://www.sqlite.org/uri.html
1113

1214
static IN_MEMORY_DB_SEQ: AtomicUsize = AtomicUsize::new(0);
@@ -114,10 +116,25 @@ impl SqliteConnectOptions {
114116
}
115117

116118
pub(crate) fn build_url(&self) -> Url {
117-
let filename =
118-
utf8_percent_encode(&self.filename.to_string_lossy(), NON_ALPHANUMERIC).to_string();
119-
let mut url =
120-
Url::parse(&format!("sqlite://{}", filename)).expect("BUG: generated un-parseable URL");
119+
// https://url.spec.whatwg.org/#path-percent-encode-set
120+
static PATH_ENCODE_SET: AsciiSet = percent_encoding::CONTROLS
121+
.add(b' ')
122+
.add(b'"')
123+
.add(b'#')
124+
.add(b'<')
125+
.add(b'>')
126+
.add(b'?')
127+
.add(b'`')
128+
.add(b'{')
129+
.add(b'}');
130+
131+
let filename_encoded = percent_encode(
132+
self.filename.as_os_str().as_encoded_bytes(),
133+
&PATH_ENCODE_SET,
134+
);
135+
136+
let mut url = Url::parse(&format!("sqlite://{filename_encoded}"))
137+
.expect("BUG: generated un-parseable URL");
121138

122139
let mode = match (self.in_memory, self.create_if_missing, self.read_only) {
123140
(true, _, _) => "memory",
@@ -133,8 +150,9 @@ impl SqliteConnectOptions {
133150
};
134151
url.query_pairs_mut().append_pair("cache", cache);
135152

136-
url.query_pairs_mut()
137-
.append_pair("immutable", &self.immutable.to_string());
153+
if self.immutable {
154+
url.query_pairs_mut().append_pair("immutable", "true");
155+
}
138156

139157
if let Some(vfs) = &self.vfs {
140158
url.query_pairs_mut().append_pair("vfs", vfs);

sqlx-sqlite/src/regexp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ unsafe extern "C" fn cleanup_arc_regex_pointer(ptr: *mut std::ffi::c_void) {
170170

171171
#[cfg(test)]
172172
mod tests {
173-
use sqlx::{ConnectOptions, Connection, Row};
173+
use sqlx::{ConnectOptions, Row};
174174
use std::str::FromStr;
175175

176176
async fn test_db() -> crate::SqliteConnection {

0 commit comments

Comments
 (0)