-
Notifications
You must be signed in to change notification settings - Fork 210
/
select.rs
174 lines (162 loc) · 5.12 KB
/
select.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use {
crate::*,
gluesql_core::{ast_builder::*, executor::Payload, prelude::Value::*},
};
test_case!(select, {
let glue = get_glue!();
// create table - Category
let actual = table("Category")
.create_table()
.add_column("id INTEGER PRIMARY KEY")
.add_column("name TEXT")
.execute(glue)
.await;
let expected = Ok(Payload::Create);
assert_eq!(actual, expected, "create table - Category");
// create table - Item
let actual = table("Item")
.create_table()
.add_column("id INTEGER")
.add_column("category_id INTEGER")
.add_column("name TEXT")
.add_column("price INTEGER")
.execute(glue)
.await;
let expected = Ok(Payload::Create);
assert_eq!(actual, expected, "create table - Item");
// insert into Category
let actual = table("Category")
.insert()
.values(vec!["1, 'Fruit'", "2, 'Meat'", "3, 'Drink'"])
.execute(glue)
.await;
let expected = Ok(Payload::Insert(3));
assert_eq!(actual, expected, "insert into Category");
// insert into Item
let actual = table("Item")
.insert()
.values(vec![
"100, 1, 'Pineapple', 40",
"200, 2, 'Pork belly', 90",
"300, 1, 'Strawberry', 30",
"400, 3, 'Coffee', 25",
"500, 3, 'Orange juice', 60",
])
.execute(glue)
.await;
let expected = Ok(Payload::Insert(5));
assert_eq!(actual, expected, "insert into Item");
// basic select
let actual = table("Category").select().execute(glue).await;
let expected = Ok(select!(
id | name
I64 | Str;
1 "Fruit".to_owned();
2 "Meat".to_owned();
3 "Drink".to_owned()
));
assert_eq!(actual, expected, "basic select");
// filter (WHERE name = "Meat")
let actual = table("Category")
.select()
.filter("name = 'Meat'")
.execute(glue)
.await;
let expected = Ok(select!(
id | name
I64 | Str;
2 "Meat".to_owned()
));
assert_eq!(actual, expected, "filter (WHERE name = 'Meat')");
// inner join
let actual = table("Item")
.alias_as("i")
.select()
.join_as("Category", "c")
.on("c.id = i.category_id")
.filter("c.name = 'Fruit' OR c.name = 'Meat'")
.project("i.name AS item")
.project("c.name AS category")
.execute(glue)
.await;
let expected = Ok(select!(
item | category
Str | Str;
"Pineapple".to_owned() "Fruit".to_owned();
"Pork belly".to_owned() "Meat".to_owned();
"Strawberry".to_owned() "Fruit".to_owned()
));
assert_eq!(actual, expected, "inner join");
// left outer join
let actual = table("Category")
.select()
.left_join("Item")
.on(col("Category.id")
.eq(col("Item.category_id"))
.and(col("price").gt(50)))
.project(vec![
"Category.name AS category",
"Item.name AS item",
"price",
])
.execute(glue)
.await;
let expected = Ok(select_with_null!(
category | item | price;
Str("Fruit".to_owned()) Null Null;
Str("Meat".to_owned()) Str("Pork belly".to_owned()) I64(90);
Str("Drink".to_owned()) Str("Orange juice".to_owned()) I64(60)
));
assert_eq!(actual, expected, "left outer join");
// group by - having
let actual = table("Item")
.select()
.join("Category")
.on(col("Category.id").eq("Item.category_id"))
.group_by("Item.category_id")
.having("SUM(Item.price) > 80")
.project("Category.name AS category")
.project("SUM(Item.price) AS sum_price")
.execute(glue)
.await;
let expected = Ok(select!(
category | sum_price
Str | I64;
"Meat".to_owned() 90;
"Drink".to_owned() 85
));
assert_eq!(actual, expected, "group by - having");
// order by
let actual = table("Item")
.select()
.project("name, price")
.order_by("price DESC")
.execute(glue)
.await;
let expected = Ok(select!(
name | price
Str | I64;
"Pork belly".to_owned() 90;
"Orange juice".to_owned() 60;
"Pineapple".to_owned() 40;
"Strawberry".to_owned() 30;
"Coffee".to_owned() 25
));
assert_eq!(actual, expected, "order by");
// offset, limit
let actual = table("Item")
.select()
.project("name, price")
.order_by("price DESC")
.offset(1)
.limit(2)
.execute(glue)
.await;
let expected = Ok(select!(
name | price
Str | I64;
"Orange juice".to_owned() 60;
"Pineapple".to_owned() 40
));
assert_eq!(actual, expected, "offset, limit");
});