-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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