Skip to content

Commit

Permalink
Removed Schema.jl and added some docs (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
bosonbaas authored Jul 6, 2020
1 parent b0a4156 commit 80567f0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/AlgebraicRelations.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module AlgebraicRelations
include("Schema.jl")
include("Query.jl")
include("SQL.jl")
include("Interface.jl")
Expand Down
16 changes: 15 additions & 1 deletion src/Interface.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
module Interface
export init_tables, prepare, execute, Connection, format_form
using Catlab
using AlgebraicRelations.SchemaLib, AlgebraicRelations.QueryLib, AlgebraicRelations.SQL
using AlgebraicRelations.QueryLib, AlgebraicRelations.SQL
using LibPQ, DataFrames
import LibPQ:
Connection, Result, Statement


# init_tables:
# This function will initialize a database with the tables required for the
# given schema argument

function init_tables(conn::Connection, types, tables, schema)
st = sql(types, tables, schema)
result = LibPQ.execute(conn, st)
end

# upload_csv:
# This function uploads data from a CSV to the connected database

function upload_csv(conn::Connection, table::String, filename::String)
LibPQ.execute(conn, "COPY '$table' FROM '$filename' DELIMITER ',' CSV HEADER;")
end
Expand All @@ -27,11 +34,18 @@ module Interface
Statement(conn, uid, query, res, length(q.wd.input_ports))
end

# execute:
# This function gets the results running the provided query on the connected
# database

function execute(conn::Connection, q::Query)::DataFrame
query = sql(q)
DataFrame(LibPQ.execute(conn, query))
end

# execute(Statement, Array):
# This function runs a prepared SQL statement using the input array as
# arguments
function execute(st::Statement, input::AbstractArray)::DataFrame
DataFrame(LibPQ.execute(st, input))
end
Expand Down
5 changes: 3 additions & 2 deletions src/Query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import Catlab.Programs: @program

using AutoHashEquals

import AlgebraicRelations.SchemaLib: Schema

@auto_hash_equals struct Types
ports::Ports
end
Expand All @@ -30,6 +28,9 @@ end
This structure holds the relationship graph between fields in a query
# Fields
- `types::Dict{Symbol, Tuple{Array{String,1}, Array{T,1} where T}}`:
The mapping between a type symbols and their fundamental types along
with the field names for those types if the SQL type is composite.
- `tables::Dict{Symbol, Tuple{Array{String,1}, Array{String,1}}}`:
The mapping between a table symbols and their column names for domain
and codomain.
Expand Down
16 changes: 11 additions & 5 deletions src/SQL.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module SQL
export sql, present_sql, to_sql
using Catlab.Theories, Catlab.Present, Catlab.WiringDiagrams
using AlgebraicRelations.QueryLib, AlgebraicRelations.SchemaLib
import AlgebraicRelations.SchemaLib: Schema
using AlgebraicRelations.QueryLib

TypeToSql = Dict(String => "text",
Int64 => "int",
Float64 => "float4")

# Generates a new array where all strings are unique. The new strings are based
# off of the strings provided in the original array.
uniquify(a::Array{String,1}) = begin
a_n = Array{String,1}()
# Fill a_n with unique values
Expand All @@ -27,6 +28,7 @@ uniquify(a::Array{String,1}) = begin
return a_n
end

# Adds aliases for the SQL table names
function add_aliases(names::Array{String,1})
aliases = map(names) do name
p_ind = findfirst(".", name)[1]
Expand All @@ -36,13 +38,16 @@ function add_aliases(names::Array{String,1})
new_names = [n*" AS "*a for (n,a) in zip(names, aliases)]
return new_names
end

# Converts a Julia datatype to its SQL equivalent
function to_sql(t)
if t isa DataType
return TypeToSql[t]
end
return t
end

# Evaluates the connections between ports (primarily concerned with Junction nodes)
function evaluate_ports(q::Query)
wd = q.wd
tables = q.tables
Expand Down Expand Up @@ -122,7 +127,7 @@ function evaluate_ports(q::Query)
aliases, port_val
end


# Generates an SQL query from a provided Query object
function sql(q::Query)::String

tables = q.tables
Expand Down Expand Up @@ -166,7 +171,8 @@ function sql(q::Query)::String
return select*from*condition
end


# Generates the SQL needed to generate the tables and types necessary for a
# schema
sql(types_dict, tables, schema) = begin
primitives = map(collect(types_dict)) do (key,val)
names = val[1]
Expand Down Expand Up @@ -236,7 +242,7 @@ sql(types_dict, tables, schema) = begin
end


# Need to generate a wrapper call around this to insert parameters
# Generates a wrapper call around this to insert parameters
# If the original query accepts a person's name (p_name) and returns
# their manager's name (m_name) and the person's salary (salary), then
# the wrapped query would look like this:
Expand Down
8 changes: 0 additions & 8 deletions src/Schema.jl

This file was deleted.

2 comments on commit 80567f0

@bosonbaas
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/17532

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 80567f05452dda080a48df0b6828b2ad29ab1f8d
git push origin v0.1.0

Please sign in to comment.