• Stars
    star
    30
  • Rank 812,116 (Top 17 %)
  • Language
    Julia
  • License
    MIT License
  • Created over 3 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

Safe interpolations for SQL query strings

SQLStrings

SQLStrings.jl provides the @sql_cmd macro to allow SQL query strings to be constructed by normal-looking string interpolation but without risking SQL formatting errors or SQL injection attacks on your application. For example, the code

query = "INSERT INTO Students VALUES ('$name', $age, '$class')"
runquery(connection, query);

is vulerable to the canonical SQL injection attack:

Little Bobby Tables

Here's how to make this safe using SQLStrings.jl:

query = sql`INSERT INTO Students VALUES ($name, $age, $class)`
runquery(connection, query);

In addition to making the above code safe, it allows the Julia types of interpolated parameters to be preserved and passed to the database driver library which can then marshal them correctly into types it understands. This provides more control than using string interpolation which is for human readability rather than data transfer.

Simple usage

To use with a given database backend, you'll need a small amount of integration code. In the examples below we'll use with LibPQ.jl and a runquery() function (hopefully integration will be automatic in future).

using SQLStrings
import LibPQ

function runquery(conn, sql::SQLStrings.Sql)
    query, args = SQLStrings.prepare(sql)
    LibPQ.execute(conn, query, args)
end

Creating a table and inserting some values

conn = LibPQ.connection(your_connection_string)

runquery(conn, sql`CREATE TABLE foo (email text, userid integer)`)

for (email,id) in [ ("[email protected]", 1)
                    ("[email protected]",   2)]
    runquery(conn, sql`INSERT INTO foo VALUES ($email, $id)`)
end

Thence:

julia> runquery(conn, sql`SELECT * FROM foo`) |> DataFrame
2×2 DataFrame
 Row │ email              userid
     │ String?            Int32?
─────┼───────────────────────────
   1 │ admin@example.com       1
   2 │ foo@example.com         2

Howtos

Inserting values from a Julia collection into a row

In some circumstances it can be useful to use splatting syntax to interpolate a Julia collection into a comma-separated list of values. Generally simple scalar parameters should be preferred for simplicity, but splatting can be useful on occasion:

email_and_id = ("[email protected]", 3)
runquery(conn, sql`INSERT INTO foo VALUES ($(email_and_id...))`)

Using the in operator with a Julia collection

There's two ways to do this. First, using in and splatting syntax

julia> ids = (1,2)
       runquery(conn, sql`SELECT * FROM foo WHERE userid IN ($(ids...))`) |> DataFrame
       2×2 DataFrame
        Row │ email              userid
            │ String?            Int32?
       ─────┼───────────────────────────
          1 │ admin@example.com       1
          2 │ foo@example.com         2

Second, using the SQL any operator and simply passing a single SQL array parameter:

julia> ids = [1,2]
       runquery(conn, sql`SELECT * FROM foo WHERE userid = any($ids)`) |> DataFrame
       2×2 DataFrame
        Row │ email              userid
            │ String?            Int32?
       ─────┼───────────────────────────
          1 │ admin@example.com       1
          2 │ foo@example.com         2

Building up a query from fragments

On occasion you might want to dynamically build up a complicated query from fragments of SQL source text. To do this, the result of @sql_cmd can be interpolated into a larger query as follows.

conn = LibPQ.connection(your_connection_string)

some_condition = true

x = 100
x = 20
# Example of an optional clauses - use empty sql` to disable it.
and_clause = some_condition ? sql`AND y=$y` : sql``

# Interpolation of values produces SQL parameters; interpolating sql`
# fragments adds them to the query.
q = sql`SELECT * FROM table WHERE x=$x $and_clause`
runquery(conn, q)

A word of warning that constructing SQL logic with Julia-level logic can make the code quite hard to understand. It can be worth considering writing one larger SQL query which does more of the logic on the SQL side.

Design

SQLStrings is a minimal approach to integrating SQL with Julia code in a safe way — it understands only the basic rules of SQL quoting and Julia string interpolation, but does no other parsing of the source text. This allows tight integration with your database of choice by being unopinionated about its source language and any SQL language extensions it may have.

I've chosen backticks for @sql_cmd rather than a normal string macro because

  • It's important to have syntax highlighting for interpolations, but editors typically disable this within normal string macros.
  • @sql_cmd is very conceptually similar to the builtin backticks and Base.Cmd: it's a lightweight layer which deals only with preserving the structure of tokens in the source text.

More Repositories

1

static-julia

Static AOT Julia compilation, demos and instructions
Julia
146
star
2

DataSets.jl

Julia
104
star
3

TableView.jl

A Tables.jl compatible table viewer based on ag-grid
Julia
99
star
4

pluto-vscode

Pluto inside a VS Code Webview, WIP
TypeScript
97
star
5

PlotlyLight.jl

Plotly.js via Julia made easy.
Julia
82
star
6

Miletus.jl

Writing financial contracts in Julia
Julia
80
star
7

XML.jl

Read and write XML in pure Julia
Julia
75
star
8

FemtoCleaner.jl

The code behind femtocleaner
Julia
63
star
9

Kuber.jl

Julia Kubernetes Client
Julia
58
star
10

MultiDocumenter.jl

Aggregating all the docs
Julia
57
star
11

gRPCClient.jl

A Julia gRPC Client
Julia
46
star
12

Training

Jupyter Notebook
45
star
13

IRViz.jl

Tools for visualizing Julia IR
Julia
43
star
14

Cobweb.jl

🕸️ Cobble together web content in Julia
Julia
42
star
15

Unums.jl

Unum arithmetic in Julia
Julia
41
star
16

OReilly

Jupyter Notebook
39
star
17

ModelingToolkitWorkshop

Training materials for ModelingToolkit and JuliaSim
HTML
38
star
18

Swagger.jl

Swagger (OpenAPI) helper and code generator for Julia
Julia
37
star
19

EasyConfig.jl

Easy-to-write JSON-like data structure for Julia
Julia
37
star
20

OpenAPI.jl

OpenAPI helper and code generator for Julia
Julia
36
star
21

AMQPClient.jl

A Julia AMQP (Advanced Message Queuing Protocol) / RabbitMQ Client.
Julia
35
star
22

HTTPDownloads.jl

Use Downloads.jl as a backend for HTTP.jl
Julia
29
star
23

SQLiteGraph.jl

A lightweight SQLite-based Graph Database for Julia.
Julia
28
star
24

Azure.jl

Julia interface to Azure APIs
Julia
26
star
25

JuliaInXL

JuliaInXL C# code to create JuliaInXL plugin for Microsoft Excel
C#
25
star
26

ODSC2019

Material for "Differentiable programming with Flux" at ODSC East & Europe, 2019
Jupyter Notebook
24
star
27

ScoringEngineDemo.jl

Demo of a scoring engine. From data wrangling to model serving on docker
HTML
22
star
28

Deprecations.jl

A database of julia deprecations and how to fix them
Julia
17
star
29

MosaicPlutoPlugin.jl

A Pluto.jl plugin for embedding Mosaic inside a notebook for interactive schematic analysis
Julia
16
star
30

gdb-solib-wine

GDB enhanced to debug wine processes
C
15
star
31

JuliaHub.jl

A Julia client for the JuliaHub platform
Julia
15
star
32

KML.jl

Write in Google Earth's KML format in pure Julia
Julia
15
star
33

Badges.jl

Badges in Julia
Julia
14
star
34

OmniPackage.jl

Big package depending on a lot of open source repos.
Julia
14
star
35

Xyce.jl

A Julia wrapper for Xyce.
Julia
12
star
36

JuliaInXL.jl

Julia interface to JuliaInXL addin
Julia
12
star
37

SemiringAlgebra.jl

Semiring Algebra
Julia
12
star
38

nvidia-driver-pcie-rebar

Patches to enable PCIe resizable BARs in the Linux NVIDIA kernel driver
Makefile
10
star
39

Humpty.jl

All the kings horses and all the king's men can put our Jacobians together again
Julia
9
star
40

TrueFX

JuliaDB demonstration using TrueFX data
Jupyter Notebook
8
star
41

JuliaSimIssues

Public issue tracker for JuliaSim by JuliaHub
8
star
42

GitLab.jl

Julia
8
star
43

GenieAppTutorial.jl

Tutorial for hosting a Genie app on JuliaHub
JavaScript
8
star
44

ScaledNumbersInput.jl

Provides an `@SI` macro for converting numbers with SI prefixes
Julia
8
star
45

ORNLTraining

Jupyter Notebook
8
star
46

JuliaHubVSCodeExtensionTutorial.jl

Tutorial on usage of JuliaHub VSCode extension
Julia
8
star
47

Training.jl

Collection of Training Materials for Julia
Julia
8
star
48

JuliaPharmaDemo

HTML
7
star
49

HPCDC

Notebooks for Intel's HPC DevCon '17
Jupyter Notebook
7
star
50

semgrep-rules-julia

Julia rules for semgrep
Julia
7
star
51

Daal.jl

Julia wrapper for Intel Data Analytics Acceleration Library (DAAL)
Julia
7
star
52

FixedSizeStrings.jl

a type for efficiently storing short strings of known size
Julia
7
star
53

Gunrock.jl

Julia Wrapper for Gunrock library
Julia
6
star
54

JuliaHub-Feedback

Public repo for filing JuliaHub issues
6
star
55

DepotDelivery.jl

Julia Project → Standalone Julia Depot
Julia
6
star
56

QuanserInterface.jl

An Julia interface to the Quanser hardware devices
Julia
5
star
57

SJCE-workshop

Jupyter Notebook
5
star
58

IntroductionTraining

Julia
4
star
59

CxxrtlCosim

Cosimulation of ModelingToolkit and Cxxrtl
Julia
4
star
60

NgSpice.jl

This package wraps NgSpice for Julia users
Julia
4
star
61

JSC-Training

Julia
4
star
62

JuliaRunClient.jl

A client library to invoke JuliaRun HTTP APIs
Julia
4
star
63

HTMLSanitizer.jl

HTML sanitization in Julia
Julia
3
star
64

DynamicScope.jl

Dynamically scoped functions in Julia
Julia
3
star
65

VectorPrisms.jl

Making structs viewable as AbstractVectors
Julia
3
star
66

rootfs-images

Julia
2
star
67

Heise_October

Supplementary Materials for the Julia article in Heise October 2020 issue
HTML
2
star
68

SecretAnalysis.jl

Julia
2
star
69

PkgAuthentication.jl

Julia
2
star
70

DashTutorials

Tutorials for running Dash.jl apps on JuliaHub
Julia
2
star
71

PyData2017

PyData NYC 2017 Tutorial
Jupyter Notebook
2
star
72

JuliaSimTraining

Julia
2
star
73

IngestTest.jl

project to test ingesting
Julia
2
star
74

ScaledNumbersOutput.jl

Pretty printing of numbers that are rounded and in SI notation (eg 3.25n)
Julia
2
star
75

Webinar_PumasQSP_20230321

Julia
2
star
76

InteractiveGeospatial.jl

Julia
2
star
77

PublicNotebooks

Pluto notebooks for demonstrations and public consumption
Julia
1
star
78

OpenPolicyAgent.jl

Julia OpenPolicyAgent Client, Server and CLI.
Julia
1
star
79

JuliaHub101App

Simple Stipple application using the JuliaHub101.jl package
JavaScript
1
star
80

JuliaProIssues

1
star
81

JuliaHub101.jl

Welcome to JuliaHub 101 - explore features and capabilities of the platform with this package.
Julia
1
star
82

JuliaTeam-GCP-k8s

This repo contains the CLI instructions that could be used to deploy JuliaTeam on GCP
Shell
1
star
83

QBO.jl

Work with QuickBooksOnline from Julia
Julia
1
star
84

Finance_demos

Jupyter Notebook
1
star