gosqle is a golang package that can generate sql queries.
Table of Contents:
Examples shown here are generated into this README.md file from the examples folder. See README.tmpl.md for more information. To generate the examples into this README.md file, run the following command:
./run_readme.sh
To run examples from the examples folder, run the following command:
docker-compose up -d
# if needed you can seed the database with some data
./run_seed.sh
./run_examples.sh
Create a select statement with the following syntax:
gosqle.NewSelect(...columns)
SELECT id, name, email FROM users LIMIT 10;
{{insertGoFile "examples/select.go" }}
SELECT country, COUNT(id) AS address_count
FROM addresses
GROUP BY country
ORDER BY address_count DESC;
{{insertGoFile "examples/select-aggregate.go" }}
SELECT name
FROM users
WHERE id IN (
SELECT user_id
FROM addresses
WHERE city = 'Amsterdam'
);
{{insertGoFile "examples/subquery.go" }}
gosqle.NewInsert(table, ...columns)
INSERT INTO users (name, email) VALUES ($1, $2)
{{insertGoFile "examples/insert.go" }}
DELETE FROM users WHERE id = $1
{{insertGoFile "examples/delete.go" }}
UPDATE users SET name = $1 WHERE id = $2
{{insertGoFile "examples/update.go" }}
SELECT id FROM users WHERE name = $1;
{{insertGoFile "examples/where-eq.go" }}
SELECT id FROM users WHERE name != $1;
{{insertGoFile "examples/where-ne.go" }}
SELECT id FROM users WHERE id > $1;
{{insertGoFile "examples/where-gt.go" }}
SELECT id FROM users WHERE id >= $1;
{{insertGoFile "examples/where-gte.go" }}
SELECT id FROM users WHERE id < $1;
{{insertGoFile "examples/where-lt.go" }}
SELECT id FROM users WHERE id <= $1;
{{insertGoFile "examples/where-lte.go" }}
SELECT id FROM users WHERE name LIKE $1;
{{insertGoFile "examples/where-like.go" }}
SELECT id FROM users WHERE name IN ($1, $2, $3);
{{insertGoFile "examples/where-in.go" }}
SELECT id FROM users WHERE id BETWEEN $1 AND $2;
{{insertGoFile "examples/where-between.go" }}
SELECT id FROM addresses WHERE phone IS NULL;
{{insertGoFile "examples/where-is-null.go" }}
SELECT id FROM users WHERE (id BETWEEN $1 AND $2 OR id BETWEEN $3 AND $4) OR name = $5;
{{insertGoFile "examples/where-wrap.go" }}
SELECT id FROM users WHERE NOT name = $1;
{{insertGoFile "examples/where-not.go" }}