Skip to content

Instantly share code, notes, and snippets.

@milesfrain
Forked from MrFincher/db.purs
Last active October 17, 2020 23:05
Show Gist options
  • Save milesfrain/88f83d649e1cc25f1620d8b5a722867d to your computer and use it in GitHub Desktop.
Save milesfrain/88f83d649e1cc25f1620d8b5a722867d to your computer and use it in GitHub Desktop.
Now compiles (although some additional debug still required), see https://try.ps.ai/?gist=540b36954ee840f8c32a95bfa4324d1f
data Column v = Column String
data Table columnRow = Table {name :: String, columns :: Record columnRow}
data Select tableColumns selectedColumns = Select
(Record tableColumns -> Record selectedColumns)
(Table tableColumns)
data ToStr = ToStr
instance toStrColum :: Folding ToStr String (Column t) String where
folding ToStr acc (Column n) = acc <> ", " <> n
-- instance toStrConst :: Folding ToStr String t String where
-- folding ToStr acc _ = acc <> ", " <> "const"
-- A more general signature is also allowed
toStr :: forall t o. HFoldl ToStr String t o => t -> o
--toStr :: forall t . HFoldl ToStr String t String => t -> String
toStr = hfoldl ToStr ""
toSQL :: forall tableColumns selectedColumns .
HFoldl ToStr String (Record selectedColumns) String =>
Select tableColumns selectedColumns -> String
toSQL (Select selector (Table {name, columns})) =
let selectedColumnRecord = selector columns :: Record selectedColumns
in "SELECT " <> toStr selectedColumnRecord <> " FROM " <> name
-- Testing
type TestTableColums = (
id :: Column Int,
name :: Column String
)
testTableColumns :: Record TestTableColums
testTableColumns = {
id : Column "id" :: Column Int,
name : Column "name" :: Column String
}
testTable :: Table TestTableColums
testTable = Table {
name : "TEST",
columns : testTableColumns
}
selectId :: Record TestTableColums -> Record (id :: Column Int)
selectId = \t -> {id : t.id}
query :: Select TestTableColums (id :: Column Int)
query = Select selectId testTable
test = toSQL query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment