Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit944b729

Browse files
Add ltree, lquery and ltxtquery support
1 parenta638ada commit944b729

File tree

6 files changed

+131
-16
lines changed

6 files changed

+131
-16
lines changed

‎postgres-protocol/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name ="postgres-protocol"
3-
version ="0.6.3"
3+
version ="0.6.4"
44
authors = ["Steven Fackler <sfackler@gmail.com>"]
55
edition ="2018"
66
description ="Low level Postgres protocol APIs"

‎postgres-protocol/src/types/mod.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,3 +1059,19 @@ impl Inet {
10591059
self.netmask
10601060
}
10611061
}
1062+
1063+
/// Serializes a Postgres l{tree,query,txtquery} string
1064+
#[inline]
1065+
pubfnltree_to_sql(v:&str,buf:&mutBytesMut){
1066+
// A version number is prepended to an Ltree string per spec
1067+
buf.put_u8(1);
1068+
// Append the rest of the query
1069+
buf.put_slice(v.as_bytes());
1070+
}
1071+
1072+
/// Deserialize a Postgres l{tree,query,txtquery} string
1073+
#[inline]
1074+
pubfnltree_from_sql(buf:&[u8]) ->Result<&str,StdBox<dynError +Sync +Send>>{
1075+
// Remove the version number from the front of the string per spec
1076+
Ok(str::from_utf8(&buf[1..])?)
1077+
}

‎postgres-types/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name ="postgres-types"
3-
version ="0.2.2"
3+
version ="0.2.3"
44
authors = ["Steven Fackler <sfackler@gmail.com>"]
55
edition ="2018"
66
license ="MIT/Apache-2.0"
@@ -28,7 +28,7 @@ with-time-0_3 = ["time-03"]
2828
[dependencies]
2929
bytes ="1.0"
3030
fallible-iterator ="0.2"
31-
postgres-protocol = {version ="0.6.1",path ="../postgres-protocol" }
31+
postgres-protocol = {version ="0.6.4",path ="../postgres-protocol" }
3232
postgres-derive = {version ="0.4.0",optional =true,path ="../postgres-derive" }
3333

3434
array-init = {version ="2",optional =true }

‎postgres-types/src/lib.rs‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,8 @@ impl<'a> FromSql<'a> for &'a [u8] {
594594
}
595595

596596
impl<'a>FromSql<'a>forString{
597-
fnfrom_sql(_:&Type,raw:&'a[u8]) ->Result<String,Box<dynError +Sync +Send>>{
598-
types::text_from_sql(raw).map(ToString::to_string)
597+
fnfrom_sql(ty:&Type,raw:&'a[u8]) ->Result<String,Box<dynError +Sync +Send>>{
598+
<&strasFromSql>::from_sql(ty,raw).map(ToString::to_string)
599599
}
600600

601601
fnaccepts(ty:&Type) ->bool{
@@ -604,8 +604,8 @@ impl<'a> FromSql<'a> for String {
604604
}
605605

606606
impl<'a>FromSql<'a>forBox<str>{
607-
fnfrom_sql(_:&Type,raw:&'a[u8]) ->Result<Box<str>,Box<dynError +Sync +Send>>{
608-
types::text_from_sql(raw)
607+
fnfrom_sql(ty:&Type,raw:&'a[u8]) ->Result<Box<str>,Box<dynError +Sync +Send>>{
608+
<&strasFromSql>::from_sql(ty,raw)
609609
.map(ToString::to_string)
610610
.map(String::into_boxed_str)
611611
}
@@ -616,14 +616,26 @@ impl<'a> FromSql<'a> for Box<str> {
616616
}
617617

618618
impl<'a>FromSql<'a>for&'astr{
619-
fnfrom_sql(_:&Type,raw:&'a[u8]) ->Result<&'astr,Box<dynError +Sync +Send>>{
620-
types::text_from_sql(raw)
619+
fnfrom_sql(ty:&Type,raw:&'a[u8]) ->Result<&'astr,Box<dynError +Sync +Send>>{
620+
match*ty{
621+
ref tyif(
622+
ty.name() =="ltree" ||
623+
ty.name() =="lquery" ||
624+
ty.name() =="ltxtquery"
625+
) => types::ltree_from_sql(raw),
626+
_ => types::text_from_sql(raw)
627+
}
621628
}
622629

623630
fnaccepts(ty:&Type) ->bool{
624631
match*ty{
625632
Type::VARCHAR |Type::TEXT |Type::BPCHAR |Type::NAME |Type::UNKNOWN =>true,
626-
ref tyif ty.name() =="citext" =>true,
633+
ref tyif(
634+
ty.name() =="citext" ||
635+
ty.name() =="ltree" ||
636+
ty.name() =="lquery" ||
637+
ty.name() =="ltxtquery"
638+
) =>true,
627639
_ =>false,
628640
}
629641
}
@@ -924,15 +936,27 @@ impl ToSql for Vec<u8> {
924936
}
925937

926938
impl<'a>ToSqlfor&'astr{
927-
fnto_sql(&self, _:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +Sync +Send>>{
928-
types::text_to_sql(*self, w);
939+
fnto_sql(&self,ty:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +Sync +Send>>{
940+
match ty{
941+
ref tyif(
942+
ty.name() =="ltree" ||
943+
ty.name() =="lquery" ||
944+
ty.name() =="ltxtquery"
945+
) => types::ltree_to_sql(*self, w),
946+
_ => types::text_to_sql(*self, w)
947+
}
929948
Ok(IsNull::No)
930949
}
931950

932951
fnaccepts(ty:&Type) ->bool{
933952
match*ty{
934953
Type::VARCHAR |Type::TEXT |Type::BPCHAR |Type::NAME |Type::UNKNOWN =>true,
935-
ref tyif ty.name() =="citext" =>true,
954+
ref tyif(
955+
ty.name() =="citext" ||
956+
ty.name() =="ltree" ||
957+
ty.name() =="lquery" ||
958+
ty.name() =="ltxtquery"
959+
) =>true,
936960
_ =>false,
937961
}
938962
}

‎tokio-postgres/Cargo.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name ="tokio-postgres"
3-
version ="0.7.5"
3+
version ="0.7.6"
44
authors = ["Steven Fackler <sfackler@gmail.com>"]
55
edition ="2018"
66
license ="MIT/Apache-2.0"
@@ -50,8 +50,8 @@ parking_lot = "0.12"
5050
percent-encoding ="2.0"
5151
pin-project-lite ="0.2"
5252
phf ="0.10"
53-
postgres-protocol = {version ="0.6.1",path ="../postgres-protocol" }
54-
postgres-types = {version ="0.2.2",path ="../postgres-types" }
53+
postgres-protocol = {version ="0.6.4",path ="../postgres-protocol" }
54+
postgres-types = {version ="0.2.3",path ="../postgres-types" }
5555
socket2 ="0.4"
5656
tokio = {version ="1.0",features = ["io-util"] }
5757
tokio-util = {version ="0.7",features = ["codec"] }

‎tokio-postgres/tests/test/types/mod.rs‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,78 @@ async fn inet() {
648648
)
649649
.await;
650650
}
651+
652+
#[tokio::test]
653+
asyncfnltree(){
654+
let client =connect("user=postgres").await;
655+
client.execute("CREATE EXTENSION IF NOT EXISTS ltree;",&[]).await.unwrap();
656+
657+
test_type("ltree",&[
658+
(Some("b.c.d".to_owned()),"'b.c.d'"),
659+
(None,"NULL"),
660+
]).await;
661+
}
662+
663+
#[tokio::test]
664+
asyncfnltree_any(){
665+
let client =connect("user=postgres").await;
666+
client.execute("CREATE EXTENSION IF NOT EXISTS ltree;",&[]).await.unwrap();
667+
668+
test_type("ltree[]",&[
669+
(Some(vec![]),"ARRAY[]"),
670+
(Some(vec!["a.b.c".to_string()]),"ARRAY['a.b.c']"),
671+
(Some(vec!["a.b.c".to_string(),"e.f.g".to_string()]),"ARRAY['a.b.c','e.f.g']"),
672+
(None,"NULL"),
673+
]).await;
674+
}
675+
676+
#[tokio::test]
677+
asyncfnlquery(){
678+
let client =connect("user=postgres").await;
679+
client.execute("CREATE EXTENSION IF NOT EXISTS ltree;",&[]).await.unwrap();
680+
681+
test_type("lquery",&[
682+
(Some("b.c.d".to_owned()),"'b.c.d'"),
683+
(Some("b.c.*".to_owned()),"'b.c.*'"),
684+
(Some("b.*{1,2}.d|e".to_owned()),"'b.*{1,2}.d|e'"),
685+
(None,"NULL"),
686+
]).await;
687+
}
688+
689+
#[tokio::test]
690+
asyncfnlquery_any(){
691+
let client =connect("user=postgres").await;
692+
client.execute("CREATE EXTENSION IF NOT EXISTS ltree;",&[]).await.unwrap();
693+
694+
test_type("lquery[]",&[
695+
(Some(vec![]),"ARRAY[]"),
696+
(Some(vec!["b.c.*".to_string()]),"ARRAY['b.c.*']"),
697+
(Some(vec!["b.c.*".to_string(),"b.*{1,2}.d|e".to_string()]),"ARRAY['b.c.*','b.*{1,2}.d|e']"),
698+
(None,"NULL"),
699+
]).await;
700+
}
701+
702+
#[tokio::test]
703+
asyncfnltxtquery(){
704+
let client =connect("user=postgres").await;
705+
client.execute("CREATE EXTENSION IF NOT EXISTS ltree;",&[]).await.unwrap();
706+
707+
test_type("ltxtquery",&[
708+
(Some("b & c & d".to_owned()),"'b & c & d'"),
709+
(Some("b@* & !c".to_owned()),"'b@* & !c'"),
710+
(None,"NULL"),
711+
]).await;
712+
}
713+
714+
#[tokio::test]
715+
asyncfnltxtquery_any(){
716+
let client =connect("user=postgres").await;
717+
client.execute("CREATE EXTENSION IF NOT EXISTS ltree;",&[]).await.unwrap();
718+
719+
test_type("ltxtquery[]",&[
720+
(Some(vec![]),"ARRAY[]"),
721+
(Some(vec!["b & c & d".to_string()]),"ARRAY['b & c & d']"),
722+
(Some(vec!["b & c & d".to_string(),"b@* & !c".to_string()]),"ARRAY['b & c & d','b@* & !c']"),
723+
(None,"NULL"),
724+
]).await;
725+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp