• Stars
    star
    136
  • Rank 258,578 (Top 6 %)
  • Language
    Julia
  • License
    Other
  • Created over 6 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Octo.jl πŸ™ is an SQL Query DSL in Julia

Octo.jl

Documentation Build Status

Octo.jl is an SQL Query DSL in Julia. It also comes with a very useful tool called Repo. You could Repo.get, Repo.insert! Repo.update! Repo.delete! for many database drivers without hand-written SQL.

It's influenced by Ecto.

SQL Query DSL

julia> using Octo.Adapters.SQL

julia> struct User
       end

julia> Schema.model(User, table_name="users")
| primary_key   | table_name   |
| ------------- | ------------ |
| id            | users        |

julia> u = from(User)
FromItem users

julia> [SELECT * FROM u]
SELECT * FROM users

julia> [SELECT (u.name, u.salary) FROM u]
SELECT name, salary FROM users

julia> [SELECT * FROM u WHERE u.id == 2]
SELECT * FROM users WHERE id = 2

julia> to_sql([SELECT * FROM u WHERE u.id == 2])
"SELECT * FROM users WHERE id = 2"

structured.svg

Repo

Current supported database drivers:

julia> using Octo.Adapters.PostgreSQL

julia> Repo.debug_sql()
LogLevelDebugSQL::RepoLogLevel = -1

julia> Repo.connect(
           adapter = Octo.Adapters.PostgreSQL,
           dbname = "postgresqltest",
           user = "postgres",
       )
Octo.Repo.Connection(false, "postgresqltest", Main.PostgreSQLLoader, PostgreSQL connection (CONNECTION_OK) with parameters:
  user = postgres
  passfile = /Users/wookyoung/.pgpass
  dbname = postgresqltest
  port = 5432
  client_encoding = UTF8
  options = -c DateStyle=ISO,YMD -c IntervalStyle=iso_8601 -c TimeZone=UTC
  application_name = LibPQ.jl
  sslmode = prefer
  sslcompression = 0
  gssencmode = disable
  target_session_attrs = any)

julia> struct Employee
       end

julia> Schema.model(Employee, table_name="Employee", primary_key="ID")
| primary_key   | table_name   |
| ------------- | ------------ |
| ID            | Employee     |

julia> Repo.execute([DROP TABLE IF EXISTS Employee])
[ Info: DROP TABLE IF EXISTS Employee

julia> Repo.execute(Raw("""
           CREATE TABLE Employee (
               ID SERIAL,
               Name VARCHAR(255),
               Salary FLOAT(8),
               PRIMARY KEY (ID)
           )"""))
β”Œ Info: CREATE TABLE Employee (
β”‚     ID SERIAL,
β”‚     Name VARCHAR(255),
β”‚     Salary FLOAT(8),
β”‚     PRIMARY KEY (ID)
β”” )

julia> Repo.insert!(Employee, [
           (Name="Jeremy",  Salary=10000.50),
           (Name="Cloris",  Salary=20000.50),
           (Name="John",    Salary=30000.50),
           (Name="Hyunden", Salary=40000.50),
           (Name="Justin",  Salary=50000.50),
           (Name="Tom",     Salary=60000.50),
       ])
[ Info: INSERT INTO Employee (Name, Salary) VALUES ($1, $2) RETURNING ID    (Name = "Jeremy", Salary = 10000.5), (Name = "Cloris", Salary = 20000.5), (Name = "John", Salary = 30000.5), (Name = "Hyunden", Salary = 40000.5), (Name = "Justin", Salary = 50000.5), (Name = "Tom", Salary = 60000.5)
|   id |   num_affected_rows |
| ---- | ------------------- |
|    6 |                   6 |

julia> Repo.get(Employee, 2)
[ Info: SELECT * FROM Employee WHERE ID = 2
|   id | name     |    salary |
| ---- | -------- | --------- |
|    2 | Cloris   |   20000.5 |
1 row.

julia> Repo.get(Employee, 2:5)
[ Info: SELECT * FROM Employee WHERE ID BETWEEN 2 AND 5
|   id | name      |    salary |
| ---- | --------- | --------- |
|    2 | Cloris    |   20000.5 |
|    3 | John      |   30000.5 |
|    4 | Hyunden   |   40000.5 |
|    5 | Justin    |   50000.5 |
4 rows.

julia> Repo.get(Employee, (Name="Jeremy",))
[ Info: SELECT * FROM Employee WHERE Name = 'Jeremy'
|   id | name     |    salary |
| ---- | -------- | --------- |
|    1 | Jeremy   |   10000.5 |
1 row.

julia> Repo.query(Employee)
[ Info: SELECT * FROM Employee
|   id | name      |    salary |
| ---- | --------- | --------- |
|    1 | Jeremy    |   10000.5 |
|    2 | Cloris    |   20000.5 |
|    3 | John      |   30000.5 |
|    4 | Hyunden   |   40000.5 |
|    5 | Justin    |   50000.5 |
|    6 | Tom       |   60000.5 |
6 rows.

julia> Repo.insert!(Employee, (Name="Jessica", Salary=70000.50))
[ Info: INSERT INTO Employee (Name, Salary) VALUES ($1, $2) RETURNING ID    (Name = "Jessica", Salary = 70000.5)
|   id |   num_affected_rows |
| ---- | ------------------- |
|    7 |                   1 |

julia> Repo.update!(Employee, (ID=2, Salary=85000))
[ Info: UPDATE Employee SET Salary = $1 WHERE ID = 2    85000
|   num_affected_rows |
| ------------------- |
|                   1 |

julia> Repo.delete!(Employee, (ID=3,))
[ Info: DELETE FROM Employee WHERE ID = 3
|   num_affected_rows |
| ------------------- |
|                   1 |

julia> Repo.delete!(Employee, 3:5)
[ Info: DELETE FROM Employee WHERE ID BETWEEN 3 AND 5
|   num_affected_rows |
| ------------------- |
|                   2 |

julia> em = from(Employee)
FromItem Employee

julia> Repo.query(em)
[ Info: SELECT * FROM Employee
|   id | name      |    salary |
| ---- | --------- | --------- |
|    1 | Jeremy    |   10000.5 |
|    6 | Tom       |   60000.5 |
|    7 | Jessica   |   70000.5 |
|    2 | Cloris    |   85000.0 |
4 rows.

julia> Repo.query([SELECT * FROM em WHERE em.Name == "Cloris"])
[ Info: SELECT * FROM Employee WHERE Name = 'Cloris'
|   id | name     |    salary |
| ---- | -------- | --------- |
|    2 | Cloris   |   85000.0 |
1 row.

julia> Repo.query(em, (Name="Cloris",))
[ Info: SELECT * FROM Employee WHERE Name = 'Cloris'
|   id | name     |    salary |
| ---- | -------- | --------- |
|    2 | Cloris   |   85000.0 |
1 row.

julia> ❓ = Octo.PlaceHolder
PlaceHolder

julia> Repo.query([SELECT * FROM em WHERE em.Name == ❓], ["Cloris"])
[ Info: SELECT * FROM Employee WHERE Name = $1    "Cloris"
|   id | name     |    salary |
| ---- | -------- | --------- |
|    2 | Cloris   |   85000.0 |
1 row.

Subqueries

julia> sub = from([SELECT * FROM em WHERE em.Salary > 30000], :sub)
SubQuery (SELECT * FROM Employee WHERE Salary > 30000) AS sub

julia> Repo.query(sub)
[ Info: SELECT * FROM Employee WHERE Salary > 30000
|   id | name      |    salary |
| ---- | --------- | --------- |
|    6 | Tom       |   60000.5 |
|    7 | Jessica   |   70000.5 |
|    2 | Cloris    |   85000.0 |
3 rows.

julia> Repo.query([SELECT sub.Name FROM sub])
[ Info: SELECT sub.Name FROM (SELECT * FROM Employee WHERE Salary > 30000) AS sub
| name      |
| --------- |
| Tom       |
| Jessica   |
| Cloris    |
3 rows.

Colored SQL statements

colored_sql_statements.png

Requirements

You need Julia.

julia> type ] key

(v1.8) pkg> add Octo
(v1.8) pkg> add LibPQ   # for PostgreSQL (depends on LibPQ.jl 1.6, 1.7)
(v1.8) pkg> add SQLite  # for SQLite     (depends on SQLite.jl 1.6)
(v1.8) pkg> add MySQL   # for MySQL      (depends on MySQL.jl 1.1, 1.4)
(v1.8) pkg> add DuckDB  # for DuckDB     (depends on DuckDB.jl 0.4, 0.8)

See also DBInterface.jl.

More Repositories

1

Bukdu.jl

Bukdu 🌌 is a web development framework for Julia
Julia
138
star
2

libcat

interactive iOS application development
Objective-C
53
star
3

Poptart.jl

πŸ‚ GUI programming in Julia based on CImGui.jl
Julia
45
star
4

Jive.jl

some useful steps in tests πŸ‘£
Julia
42
star
5

H3.jl

H3.jl ⬑ provides a Julia version of H3, Hexagonal hierarchical geospatial indexing system. https://github.com/uber/h3
Julia
21
star
6

luacat

unit testing, logger, OOP, string, table extensions for lua.
Lua
19
star
7

pycat

Jupyter Notebook
18
star
8

touching

iphone apps
Objective-C
17
star
9

BlenderPlot.jl

a prototype version of the πŸ“Š plot on Blender using PyCall
Julia
13
star
10

EmojiSymbols.jl

additional Emoji symbols for Julia REPL πŸ€”
Julia
13
star
11

Millboard.jl

Displaying data in tables for Julia
Julia
11
star
12

HangulSwift

Hangul Automata for Swift
Swift
10
star
13

da

test codes for many programming languages
Objective-C
9
star
14

LOLTools.jl

Julia package to the Riot Games API for League of Legends.
Julia
6
star
15

PushInterface.jl

Julia library for Ableton Push 2 Interface πŸŽ›
Julia
6
star
16

Calendars.jl

VerticalCalendar, HorizontalCalendar, Times, LunarCalendars πŸ—“
Julia
6
star
17

factor-bert

Factor BERT
4
star
18

juliacat

Julia practices
Julia
4
star
19

starter-snake-julia

A simple Battlesnake AI written in Julia http://battlesnake.io
Julia
4
star
20

Swifter.jl

iOS REPL with Swifter.jl + AppConsole
Julia
4
star
21

book

book sample code
Objective-C
3
star
22

Sexagesimal.jl

Sexagesimal ♒️ 六十甲子
Julia
3
star
23

journal

πŸ“Ÿ κ°λ‚œλ‹«λž„λ§˜λ°₯μ‚Ώμ•™μž¦μ°ΏμΊŒνƒ™νŒ¦ν•³
HTML
3
star
24

PoptartExamples.jl

πŸ–Ό Poptart examples
Julia
3
star
25

AppConsole

iOS REPL with Swifter.jl + AppConsole
Swift
3
star
26

knol

Vim Script
2
star
27

ted_sub_to_smi

TED subtitle to SMI
Ruby
2
star
28

anti-vaxxers

🍍 γ…‚γ…… λΆ€μž‘μš©, 치료 자료 λͺ¨μŒ
2
star
29

Tutte.jl

Tutte.Graphs ፨
Julia
2
star
30

TestJulia11.jl

Practices with Julia II - things that merged recently
Julia
2
star
31

swiftcat

Objective-C
2
star
32

TestFlux.jl

Julia
1
star
33

unicat

Unity3D sample
C#
1
star
34

AheuiSwift

Swift
1
star
35

HED.jl

Python
1
star
36

motioncat

mix arccat in RubyMotion
Ruby
1
star
37

DARPA

1
star
38

Sam1.jl

삼ꡭ지1 무μž₯일람
Julia
1
star
39

Aheui.jl

Julia
1
star
40

Sunyata.jl

Speech to Text using DeepSpeech πŸ’‹
Julia
1
star
41

wookay.github.com

homepage
HTML
1
star
42

cacao

cocoa apps
Objective-C
1
star
43

jscat

javascript playground
JavaScript
1
star
44

Shaders.jl

Julia
1
star
45

TestEmojiSymbols.jl

Julia
1
star
46

HumanMotionPrediction.jl

Python
1
star
47

ccat

Unit testing for C
C
1
star
48

Logic.jl

Identity and Inverse Properties in Julia
Julia
1
star
49

heroku-sevenstars

Bukdu sevenstars 🌌 on Heroku
Julia
1
star
50

dataplay

Jupyter Notebook
1
star
51

heroku-buildpack-julia-13

Julia v1.3.0 buildpack for Heroku
Shell
1
star
52

UIDesign

a simple UI DSL for swift
Swift
1
star
53

GithubTools.jl

Julia package for the Github GraphQL API v4
Julia
1
star
54

DataLogger.jl

read_stdout
Julia
1
star
55

bjp

BJP 1μž₯μ—μ„œ 6μž₯ μž…λ‹ˆλ‹€. 7μž₯ μ΄ν›„λŠ” κ΅¬μž…ν•˜μ‹œλ©΄ λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.
1
star
56

Sam14.jl

삼ꡭ지 14
Julia
1
star
57

objcat

Unit testing for Objective-C ARC.
Objective-C
1
star
58

bloque

objective-c blocks
Objective-C
1
star