Skip to content

Commit

Permalink
feat: introduce LTree column type
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai committed Sep 9, 2023
1 parent b7e5413 commit 3569d54
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl TableBuilder for MysqlQueryBuilder {
ColumnType::Cidr => unimplemented!("Cidr is not available in MySQL."),
ColumnType::Inet => unimplemented!("Inet is not available in MySQL."),
ColumnType::MacAddr => unimplemented!("MacAddr is not available in MySQL."),
ColumnType::LTree => unimplemented!("LTree is not available in MySQL."),
}
)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl TableBuilder for PostgresQueryBuilder {
ColumnType::Inet => "inet".into(),
ColumnType::MacAddr => "macaddr".into(),
ColumnType::Year(_) => unimplemented!("Year is not available in Postgres."),
ColumnType::LTree => "ltree".into(),
}
)
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions src/backend/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl TableBuilder for SqliteQueryBuilder {
ColumnType::Year(_) => unimplemented!("Year is not available in Sqlite."),
ColumnType::Bit(_) => unimplemented!("Bit is not available in Sqlite."),
ColumnType::VarBit(_) => unimplemented!("VarBit is not available in Sqlite."),
ColumnType::LTree => unimplemented!("LTree is not available in Sqlite."),
}
)
.unwrap()
Expand Down
18 changes: 18 additions & 0 deletions src/extension/postgres/ltree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::fmt;

use crate::PgLTree;

impl fmt::Display for PgLTree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl<T> From<T> for PgLTree
where
T: Into<String>,
{
fn from(field: T) -> Self {
PgLTree(field.into())
}
}
2 changes: 2 additions & 0 deletions src/extension/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use expr::*;
pub use extension::*;
pub use func::*;
pub use interval::*;
pub use ltree::*;
pub use types::*;

use crate::types::BinOper;
Expand All @@ -10,6 +11,7 @@ pub(crate) mod expr;
pub(crate) mod extension;
pub(crate) mod func;
pub(crate) mod interval;
pub(crate) mod ltree;
pub(crate) mod types;

/// Binary operator
Expand Down
8 changes: 8 additions & 0 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub enum ColumnType {
Cidr,
Inet,
MacAddr,
LTree,
}

impl PartialEq for ColumnType {
Expand Down Expand Up @@ -580,6 +581,13 @@ impl ColumnDef {
self
}

/// Set column type as `ltree`
/// This is only supported on Postgres.
pub fn ltree(&mut self) -> &mut Self {
self.types = Some(ColumnType::LTree);
self
}

/// Set constraints as SimpleExpr
pub fn check<T>(&mut self, value: T) -> &mut Self
where
Expand Down
24 changes: 24 additions & 0 deletions tests/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,27 @@ fn alter_with_check_constraint() {
r#"ALTER TABLE "glyph" ADD COLUMN "aspect" integer NOT NULL DEFAULT 101 CHECK ("aspect" > 100)"#,
);
}

#[test]
fn create_16() {
assert_eq!(
Table::create()
.table(Glyph::Table)
.col(
ColumnDef::new(Glyph::Id)
.integer()
.not_null()
.auto_increment()
.primary_key()
)
.col(ColumnDef::new(Glyph::Tokens).ltree())
.to_string(PostgresQueryBuilder),
[
r#"CREATE TABLE "glyph" ("#,
r#""id" serial NOT NULL PRIMARY KEY,"#,
r#""tokens" ltree"#,
r#")"#,
]
.join(" ")
);
}

0 comments on commit 3569d54

Please sign in to comment.