forked from datacharmer/test_db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects
More file actions
executable file
·201 lines (187 loc) · 4.84 KB
/
Copy pathobjects
File metadata and controls
executable file
·201 lines (187 loc) · 4.84 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
[[ -z "$1" ]] && echo "usage: objects <dbname>" && exit 1
cdb2sql -s $1 -<<'EOF'
create procedure emp_dept_id version '1.0' {
--
-- returns the department id of a given employee
--
local function emp_dept_id(employee_id)
local stmt
stmt = db:prepare([[
select
max(from_date) as max_date
from
dept_emp
where emp_no = @employee_id
]])
stmt:bind("employee_id", employee_id)
local max_date = stmt:fetch().max_date
stmt = db:prepare([[
select
dept_no as dept_no
from
dept_emp
where
emp_no = @employee_id
and
from_date = @max_date
limit 1
]])
stmt:bind("employee_id", employee_id)
stmt:bind("max_date", max_date)
return stmt:fetch().dept_no
end
local function main(employee_id)
return emp_dept_id(employee_id)
end
}$$
put default procedure emp_dept_id '1.0'
create lua scalar function emp_dept_id
create procedure emp_dept_name version '1.0' {
--
-- returns the department name of a given employee
--
local function emp_dept_name(employee_id)
local emp_dept_id = db:sp("emp_dept_id")
local stmt = db:prepare([[
select
dept_name as dept_name
from
departments
where
dept_no = @employee_id
]])
stmt:bind("employee_id", emp_dept_id(employee_id))
return stmt:fetch().dept_name
end
}$$
put default procedure emp_dept_name '1.0'
create lua scalar function emp_dept_name
create procedure emp_name version '1.0' {
--
-- returns the employee name of a given employee id
--
local function emp_name(employee_id)
local stmt = db:prepare([[
select
first_name || ' ' || last_name as name
from
employees
where
emp_no = @employee_id
]])
stmt:bind("employee_id", employee_id)
return stmt:fetch().name
end
local function main(employee_id)
return emp_name(employee_id)
end
}$$
put default procedure emp_name '1.0'
create lua scalar function emp_name
create procedure current_manager version '1.0' {
--
-- returns the manager of a department
-- choosing the most recent one
-- from the manager list
--
local function current_manager(dept_id)
local stmt = db:prepare([[
select
max(from_date) as max_date
from
dept_manager
where
dept_no = @dept_id
]])
stmt:bind("dept_id", dept_id)
local max_date = stmt:fetch().max_date
stmt = db:prepare([[
select
emp_no
from
dept_manager
where
dept_no = @dept_id
and
from_date = @max_date
limit 1
]])
stmt:bind("dept_id", dept_id)
stmt:bind("max_date", max_date)
local emp_no = stmt:fetch().emp_no
local emp_name = db:sp("emp_name")
return emp_name(emp_no)
end
local function main(dept_id)
return current_manager(dept_id)
end
}$$
put default procedure current_manager '1.0'
create lua scalar function current_manager
create procedure show_departments version '1.0' {
--
-- shows the departments with the number of employees
-- per department
--
local function main()
db:table("department_max_date", {
{"emp_no", "int"},
{"dept_from_date", "datetime"},
{"dept_to_date", "datetime"}})
db:exec([[
INSERT INTO department_max_date
SELECT
emp_no, max(from_date), max(to_date)
FROM
dept_emp
GROUP BY
emp_no;
]])
db:table("department_people", {
{"emp_no","int"},
{"dept_no","text"}})
db:exec([[
insert into department_people
select dmd.emp_no, dept_no
from
department_max_date dmd
inner join dept_emp de
on dmd.dept_from_date=de.from_date
and dmd.dept_to_date=de.to_date
and dmd.emp_no=de.emp_no;
]])
--
-- selects the department list with manager names
--
local current_manager = db:sp("current_manager");
local full_departments = db:table("full_departments", {
{"dept_no","text"},
{"dept_name","text"},
{"manager", "text"}})
local stmt = db:exec([[
SELECT
dept_no, dept_name
FROM
departments;
]])
local row = stmt:fetch()
while row do
full_departments:insert({
dept_no = row.dept_no,
dept_name = row.dept_name,
manager = current_manager(row.dept_no)})
row = stmt:fetch()
end
stmt = db:exec([[
SELECT
dept_no,dept_name,manager, count(*)
from full_departments
inner join department_people using (dept_no)
group by dept_no;
]])
stmt:emit()
end
}$$
put default procedure show_departments '1.0'
EOF