Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/annenkov/sql-dsl
https://github.com/annenkov/sql-dsl
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/annenkov/sql-dsl
- Owner: annenkov
- Created: 2020-01-25T18:26:34.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-25T18:26:50.000Z (almost 5 years ago)
- Last Synced: 2024-11-06T08:03:07.306Z (about 2 months ago)
- Language: Racket
- Size: 16.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.rst
Awesome Lists containing this project
README
Overview
========
Simple embedded DSL for making SQL-like queries.
Now supports SELECT, INSERT, UPDATE and DELETE commands.Working with DSL
================Defining entities
-----------------
Define "user" entity with "id" and "name"
::
(define-entity user
user_table ; DB table name
(id name)) ; fields listDefining connection
-------------------
Connection used implicitly to execute all commands
::
(define-current-connection
#:database "test"
#:password "test"
#:user "test")Making queries
--------------
Select queries has following format
::
(select ENTITY [where CONDITIONS] [order-by FIELD asc/desc])For example
::
(select user) ; SELECT id,name FROM user_table
(select user [where (and (> id 1) (< id 3))]) ; SELECT id,name FROM user_table WHERE ((id > '1') and (id < '3'))
(select user [order-by name asc]) ; SELECT id,name FROM user_table ORDER BY name ASCSelect returns list of user structs. User field can be accessed with standard struct selectors (user-id, user-name)
::
(define (print-users user-list)
(for ([user user-list])
(printf "Id: ~a, name: ~a\n" (user-id user) (user-name user))))Insert/Update
-------------
Creating user
::
(insert user (user 1 "John Doe"))Rename user
::
(define (rename-user target-user new-name)
(update user (struct-copy user target-user [name new-name])))Delete
------
You can delete entity instance after selecting it.
::
(define (delete-user-by-id id)
(let ([selected-user (first (select user [where (= id id)]))]) ; assuming, that user with given id exists
(delete user selected-user)))