-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgen.sh
More file actions
executable file
·89 lines (66 loc) · 1.94 KB
/
gen.sh
File metadata and controls
executable file
·89 lines (66 loc) · 1.94 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
#!/bin/bash
DBUSER=booktest
DBPASS=booktest
DBHOST=localhost
DBNAME=booktest
DB=postgres://$DBUSER:$DBPASS@$DBHOST/$DBNAME
EXTRA=$1
SRC=$(realpath $(cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))
XOBIN=$(which gendal)
if [ -e $SRC/../../../gendal ]; then
XOBIN=$SRC/../../../gendal
fi
DEST=$SRC/models
set -x
mkdir -p $DEST
rm -f $DEST/*.go
rm -f $SRC/postgres
psql -U postgres -c "create user booktest password 'booktest';"
psql -U postgres -c 'drop database booktest;'
psql -U postgres -c 'create database booktest owner booktest;'
psql -U $DBUSER << 'ENDSQL'
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
name text NOT NULL DEFAULT ''
);
CREATE INDEX authors_name_idx ON authors(name);
CREATE TYPE book_type AS ENUM (
'FICTION',
'NONFICTION'
);
CREATE TABLE books (
book_id SERIAL PRIMARY KEY,
author_id integer NOT NULL REFERENCES authors(author_id),
isbn text NOT NULL DEFAULT '' UNIQUE,
booktype book_type NOT NULL DEFAULT 'FICTION',
title text NOT NULL DEFAULT '',
year integer NOT NULL DEFAULT 2000,
available timestamp with time zone NOT NULL DEFAULT 'NOW()',
tags varchar[] NOT NULL DEFAULT '{}'
);
CREATE INDEX books_title_idx ON books(title, year);
CREATE FUNCTION say_hello(text) RETURNS text AS $$
BEGIN
RETURN CONCAT('hello ', $1);
END;
$$ LANGUAGE plpgsql;
CREATE INDEX books_title_lower_idx ON books(title);
ENDSQL
$XOBIN $DB -o $SRC/models -j $EXTRA
$XOBIN $DB -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models $EXTRA << ENDSQL
SELECT
a.author_id::integer AS author_id,
a.name::text AS author_name,
b.book_id::integer AS book_id,
b.isbn::text AS book_isbn,
b.title::text AS book_title,
b.tags::text[] AS book_tags
FROM books b
JOIN authors a ON a.author_id = b.author_id
WHERE b.tags && %%tags StringSlice%%::varchar[]
ENDSQL
pushd $SRC &> /dev/null
go build
./postgres $EXTRA
popd &> /dev/null
psql -U $DBUSER <<< 'select * from books;'