Skip to content

Latest commit

 

History

History
213 lines (191 loc) · 4.27 KB

README.tmpl.md

File metadata and controls

213 lines (191 loc) · 4.27 KB

gosqle

gosqle is a golang package that can generate sql queries.

Table of Contents:

Examples

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

Select

Create a select statement with the following syntax:

gosqle.NewSelect(...columns)

Generate a select query:

SELECT id, name, email FROM users LIMIT 10;
{{insertGoFile "examples/select.go" }}

Generate select query using group by and aggregate functions:

SELECT country, COUNT(id) AS address_count
FROM addresses
GROUP BY country
ORDER BY address_count DESC;
{{insertGoFile "examples/select-aggregate.go" }}

Subquery

SELECT name
FROM users
WHERE id IN (
  SELECT user_id
  FROM addresses
  WHERE city = 'Amsterdam'
);
{{insertGoFile "examples/subquery.go" }}

Insert

gosqle.NewInsert(table, ...columns)

Generate an insert query:

INSERT INTO users (name, email) VALUES ($1, $2)
{{insertGoFile "examples/insert.go" }}

Delete

Generate a delete query:

DELETE FROM users WHERE id = $1
{{insertGoFile "examples/delete.go" }}

Update

Generate an update query:

UPDATE users SET name = $1 WHERE id = $2
{{insertGoFile "examples/update.go" }}

Where conditions

equal

SELECT id FROM users WHERE name = $1;
{{insertGoFile "examples/where-eq.go" }}

Not equal

SELECT id FROM users WHERE name != $1;
{{insertGoFile "examples/where-ne.go" }}

Greater than

SELECT id FROM users WHERE id > $1;
{{insertGoFile "examples/where-gt.go" }}

Greater than or equal

SELECT id FROM users WHERE id >= $1;
{{insertGoFile "examples/where-gte.go" }}

Less than

SELECT id FROM users WHERE id < $1;
{{insertGoFile "examples/where-lt.go" }}

Less than or equal

SELECT id FROM users WHERE id <= $1;
{{insertGoFile "examples/where-lte.go" }}

Like

SELECT id FROM users WHERE name LIKE $1;
{{insertGoFile "examples/where-like.go" }}

In

SELECT id FROM users WHERE name IN ($1, $2, $3);
{{insertGoFile "examples/where-in.go" }}

Between

SELECT id FROM users WHERE id BETWEEN $1 AND $2;
{{insertGoFile "examples/where-between.go" }}

Is null

SELECT id FROM addresses WHERE phone IS NULL;
{{insertGoFile "examples/where-is-null.go" }}

Grouping

SELECT id FROM users WHERE (id BETWEEN $1 AND $2 OR id BETWEEN $3 AND $4) OR name = $5;
{{insertGoFile "examples/where-wrap.go" }}

Not

SELECT id FROM users WHERE NOT name = $1;
{{insertGoFile "examples/where-not.go" }}

Syntax used

image