Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Commit 8b563fc

Browse files
committed
Added DROP INDEX and fixed CREATE INDEX
1 parent cc29910 commit 8b563fc

4 files changed

Lines changed: 251 additions & 8 deletions

File tree

‎Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rorm-sql"
3-
version = "0.11.0"
3+
version = "0.11.1"
44
edition = "2021"
55
keywords = ["database", "abstraction-layer", "sqlite", "postgres", "mysql"]
66
categories = ["database"]
@@ -32,7 +32,7 @@ ipnetwork = { version = "~0.21", optional = true }
3232
# Mac Address support
3333
mac_address = { version = "~1", optional = true }
3434

35-
rorm-declaration = { version = "~0.5", path = "../rorm-declaration" }
35+
rorm-declaration = { version = "~0.5.1", path = "../rorm-declaration" }
3636

3737
[package.metadata.docs.rs]
3838
all-features = true

‎src/create_index.rs‎

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,20 @@ impl<'until_build> CreateIndex<'until_build> for CreateIndexImpl<'until_build> {
121121
}
122122

123123
Ok(format!(
124-
"CREATE {} INDEX{} {} ON {} ({}) {};",
125-
if d.unique { "UNIQUE" } else { "" },
124+
"CREATE{} INDEX{} \"{}\" ON \"{}\" ({}){};",
125+
if d.unique { " UNIQUE" } else { "" },
126126
if d.if_not_exists {
127127
" IF NOT EXISTS"
128128
} else {
129129
""
130130
},
131131
d.name,
132132
d.table_name,
133-
d.columns.join(", "),
134-
d.condition.as_ref().map_or("", |x| x.as_str()),
133+
quote_columns(&d.columns),
134+
match d.condition {
135+
None => String::from(""),
136+
Some(cond) => format!(" WHERE {}", cond.as_str()),
137+
}
135138
))
136139
}
137140
#[cfg(feature = "postgres")]
@@ -144,7 +147,7 @@ impl<'until_build> CreateIndex<'until_build> for CreateIndexImpl<'until_build> {
144147
}
145148

146149
Ok(format!(
147-
"CREATE{} INDEX{} {} ON {} ({}){};",
150+
"CREATE{} INDEX{} \"{}\" ON \"{}\" ({}){};",
148151
if d.unique { " UNIQUE" } else { "" },
149152
if d.if_not_exists {
150153
" IF NOT EXISTS"
@@ -153,7 +156,7 @@ impl<'until_build> CreateIndex<'until_build> for CreateIndexImpl<'until_build> {
153156
},
154157
d.name,
155158
d.table_name,
156-
d.columns.join(", "),
159+
quote_columns(&d.columns),
157160
match d.condition {
158161
None => String::from(""),
159162
Some(cond) => format!(" WHERE {}", cond.as_str()),
@@ -163,3 +166,110 @@ impl<'until_build> CreateIndex<'until_build> for CreateIndexImpl<'until_build> {
163166
}
164167
}
165168
}
169+
170+
/// Joins the `columns` into the comma separated list of a CREATE INDEX statement
171+
fn quote_columns(columns: &[&str]) -> String {
172+
columns
173+
.iter()
174+
.map(|column| format!("\"{column}\""))
175+
.collect::<Vec<_>>()
176+
.join(", ")
177+
}
178+
179+
#[cfg(test)]
180+
mod test {
181+
use crate::create_index::CreateIndex;
182+
use crate::DBImpl;
183+
184+
#[cfg(feature = "sqlite")]
185+
#[test]
186+
fn single_column_sqlite() {
187+
assert_eq!(
188+
DBImpl::SQLite
189+
.create_index("user_login_idx", "user")
190+
.add_column("login")
191+
.build()
192+
.unwrap(),
193+
r#"CREATE INDEX "user_login_idx" ON "user" ("login");"#
194+
);
195+
}
196+
197+
#[cfg(feature = "postgres")]
198+
#[test]
199+
fn single_column_postgres() {
200+
assert_eq!(
201+
DBImpl::Postgres
202+
.create_index("user_login_idx", "user")
203+
.add_column("login")
204+
.build()
205+
.unwrap(),
206+
r#"CREATE INDEX "user_login_idx" ON "user" ("login");"#
207+
);
208+
}
209+
210+
#[cfg(feature = "sqlite")]
211+
#[test]
212+
fn multiple_columns_sqlite() {
213+
assert_eq!(
214+
DBImpl::SQLite
215+
.create_index("user_full_name_idx", "user")
216+
.add_column("last_name")
217+
.add_column("first_name")
218+
.build()
219+
.unwrap(),
220+
r#"CREATE INDEX "user_full_name_idx" ON "user" ("last_name", "first_name");"#
221+
);
222+
}
223+
224+
#[cfg(feature = "postgres")]
225+
#[test]
226+
fn multiple_columns_postgres() {
227+
assert_eq!(
228+
DBImpl::Postgres
229+
.create_index("user_full_name_idx", "user")
230+
.add_column("last_name")
231+
.add_column("first_name")
232+
.build()
233+
.unwrap(),
234+
r#"CREATE INDEX "user_full_name_idx" ON "user" ("last_name", "first_name");"#
235+
);
236+
}
237+
238+
#[cfg(feature = "sqlite")]
239+
#[test]
240+
fn unique_and_if_not_exists_sqlite() {
241+
assert_eq!(
242+
DBImpl::SQLite
243+
.create_index("user_login_idx", "user")
244+
.add_column("login")
245+
.unique()
246+
.if_not_exists()
247+
.build()
248+
.unwrap(),
249+
r#"CREATE UNIQUE INDEX IF NOT EXISTS "user_login_idx" ON "user" ("login");"#
250+
);
251+
}
252+
253+
#[cfg(feature = "sqlite")]
254+
#[test]
255+
fn condition_sqlite() {
256+
assert_eq!(
257+
DBImpl::SQLite
258+
.create_index("user_login_idx", "user")
259+
.add_column("login")
260+
.set_condition("\"login\" IS NOT NULL".to_string())
261+
.build()
262+
.unwrap(),
263+
r#"CREATE INDEX "user_login_idx" ON "user" ("login") WHERE "login" IS NOT NULL;"#
264+
);
265+
}
266+
267+
#[cfg(feature = "sqlite")]
268+
#[test]
269+
fn missing_columns_is_an_error() {
270+
assert!(DBImpl::SQLite
271+
.create_index("user_login_idx", "user")
272+
.build()
273+
.is_err());
274+
}
275+
}

‎src/drop_index.rs‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
Trait representing a drop index builder.
3+
*/
4+
pub trait DropIndex {
5+
/**
6+
Drops the index only, if it exists.
7+
*/
8+
fn if_exists(self) -> Self;
9+
10+
/**
11+
This method is used to build the drop index statement.
12+
*/
13+
fn build(self) -> String;
14+
}
15+
16+
/**
17+
The representation of data of the drop index statement.
18+
*/
19+
#[derive(Debug, Copy, Clone)]
20+
pub struct DropIndexData<'until_build> {
21+
pub(crate) name: &'until_build str,
22+
pub(crate) if_exists: bool,
23+
}
24+
25+
/**
26+
Implementation of the [DropIndex] trait for the different implementations.
27+
28+
Should only be constructed via [crate::DBImpl::drop_index].
29+
*/
30+
#[derive(Debug)]
31+
pub enum DropIndexImpl<'until_build> {
32+
/**
33+
SQLite representation of the DROP INDEX operation.
34+
*/
35+
#[cfg(feature = "sqlite")]
36+
SQLite(DropIndexData<'until_build>),
37+
/**
38+
Postgres representation of the DROP INDEX operation.
39+
*/
40+
#[cfg(feature = "postgres")]
41+
Postgres(DropIndexData<'until_build>),
42+
}
43+
44+
impl DropIndex for DropIndexImpl<'_> {
45+
fn if_exists(mut self) -> Self {
46+
match self {
47+
#[cfg(feature = "sqlite")]
48+
DropIndexImpl::SQLite(ref mut d) => d.if_exists = true,
49+
#[cfg(feature = "postgres")]
50+
DropIndexImpl::Postgres(ref mut d) => d.if_exists = true,
51+
};
52+
self
53+
}
54+
55+
fn build(self) -> String {
56+
match self {
57+
#[cfg(feature = "sqlite")]
58+
DropIndexImpl::SQLite(d) => format!(
59+
"DROP INDEX{} \"{}\";",
60+
if d.if_exists { " IF EXISTS" } else { "" },
61+
d.name
62+
),
63+
64+
#[cfg(feature = "postgres")]
65+
DropIndexImpl::Postgres(d) => format!(
66+
"DROP INDEX{} \"{}\";",
67+
if d.if_exists { " IF EXISTS" } else { "" },
68+
d.name
69+
),
70+
}
71+
}
72+
}
73+
74+
#[cfg(test)]
75+
mod test {
76+
use crate::drop_index::DropIndex;
77+
use crate::DBImpl;
78+
79+
#[cfg(feature = "sqlite")]
80+
#[test]
81+
fn drop_index_sqlite() {
82+
assert_eq!(
83+
DBImpl::SQLite.drop_index("user_login_idx").build(),
84+
r#"DROP INDEX "user_login_idx";"#
85+
);
86+
}
87+
88+
#[cfg(feature = "postgres")]
89+
#[test]
90+
fn drop_index_postgres() {
91+
assert_eq!(
92+
DBImpl::Postgres.drop_index("user_login_idx").build(),
93+
r#"DROP INDEX "user_login_idx";"#
94+
);
95+
}
96+
97+
#[cfg(feature = "sqlite")]
98+
#[test]
99+
fn drop_index_if_exists() {
100+
assert_eq!(
101+
DBImpl::SQLite
102+
.drop_index("user_login_idx")
103+
.if_exists()
104+
.build(),
105+
r#"DROP INDEX IF EXISTS "user_login_idx";"#
106+
);
107+
}
108+
}

‎src/lib.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub mod create_table;
2121
pub mod create_trigger;
2222
/// Implementation of SQL DELETE operation
2323
pub mod delete;
24+
/// Implementation of SQL DROP INDEX statements
25+
pub mod drop_index;
2426
/// Implementation of SQL DROP TABLE statements
2527
pub mod drop_table;
2628
/// Definition of error types that can occur.
@@ -64,6 +66,7 @@ use crate::create_trigger::{
6466
SQLCreateTrigger, SQLCreateTriggerOperation, SQLCreateTriggerPointInTime,
6567
};
6668
use crate::delete::{Delete, DeleteData, DeleteImpl};
69+
use crate::drop_index::{DropIndex, DropIndexData, DropIndexImpl};
6770
use crate::drop_table::{DropTable, DropTableData, DropTableImpl};
6871
use crate::insert::{Insert, InsertData, InsertImpl};
6972
use crate::join_table::{JoinTableData, JoinTableImpl, JoinType};
@@ -174,6 +177,28 @@ impl DBImpl {
174177
}
175178
}
176179

180+
/**
181+
The entry point to drop an index.
182+
183+
**Parameter**:
184+
- `name`: Name of the index to drop.
185+
*/
186+
pub fn drop_index<'until_build>(
187+
&self,
188+
name: &'until_build str,
189+
) -> impl DropIndex + 'until_build {
190+
let d = DropIndexData {
191+
name,
192+
if_exists: false,
193+
};
194+
match self {
195+
#[cfg(feature = "sqlite")]
196+
DBImpl::SQLite => DropIndexImpl::SQLite(d),
197+
#[cfg(feature = "postgres")]
198+
DBImpl::Postgres => DropIndexImpl::Postgres(d),
199+
}
200+
}
201+
177202
/**
178203
The entry point to drop a table.
179204

0 commit comments

Comments
 (0)