Skip to content

Instantly share code, notes, and snippets.

@MrFincher
Created October 17, 2020 20:31
Show Gist options
  • Save MrFincher/7b8a5dc3d65e27de04290448b47de467 to your computer and use it in GitHub Desktop.
Save MrFincher/7b8a5dc3d65e27de04290448b47de467 to your computer and use it in GitHub Desktop.
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"
toStr :: forall t . HFoldl ToStr String t String => t -> String
toStr = hfoldl ToStr ""
toSQL :: forall tableColumns selectedColumns . 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