khibino / haskell-relational-record

This repository includes a joined query generator based on typefull relational algebra, and mapping tools between SQL values list and Haskell record type.
233 stars 36 forks source link

use column_type instead of data_type for determining MySQL -> Haskell… #42

Closed ryantm closed 6 years ago

ryantm commented 8 years ago

… type mapping

This is not meant to be a serious pull request. It is meant to show how could implement #41. With these changes you can use a type map like:

ourTypeMap =
    [ ("CHAR",       [t| Text |])
    , ("VARCHAR",    [t| Text |])
    , ("VARCHAR(255)",    [t| Text |])
    , ("TINYTEXT",   [t| Text |])
    , ("TEXT",       [t| Text |])
    , ("MEDIUMTEXT", [t| Text |])
    , ("LONGTEXT",   [t| Text |])
    , ("TINYBLOB",   [t| ByteString |])
    , ("BLOB",       [t| ByteString |])
    , ("MEDIUMBLOB", [t| ByteString |])
    , ("LONGBLOB",   [t| ByteString |])
    , ("DATE",       [t| Day |])
    , ("DATETIME",   [t| LocalTime |])
    , ("TIME",       [t| TimeOfDay |])
    , ("TIMESTAMP",  [t| POSIXTime |])
    , ("TINYINT(1)", [t| Bool |])
    , ("TINYINT(3) UNSIGNED", [t| Int8 |])
    , ("TINYINT",    [t| Int8 |])
    , ("SMALLINT",   [t| Int16 |])
    , ("SMALLINT(5) UNSIGNED", [t| Int16 |])
    , ("SMALLINT(6)", [t| Int16 |])
    , ("MEDIUMINT",  [t| Int32 |])
    , ("INT",        [t| Int32 |])
    , ("INT(10) UNSIGNED",    [t| Int32 |])
    , ("INT(11)",    [t| Int32 |])
    , ("INT(255)",    [t| Int32 |])
    , ("INTEGER",    [t| Int32 |])
    , ("BIGINT",     [t| Int64 |])

    , ("DECIMAL(3,1)",     [t| Double |])
    , ("DECIMAL(14,6)",     [t| Double |])
    , ("DECIMAL(8,4)",     [t| Double |])
    , ("DECIMAL(9,5)",     [t| Double |])
    , ("DECIMAL(12,4)",     [t| Double |])
    ]

Which might be bad because you have to enumerate every column_type, but at least it lets you specify my use case of turning tinyint(1) into Bool. It might be okay to carefully select the type for every possible variation you use too; I'm not sure.

This is meant to be a prototype showing how we can use column_type instead of data_type to determine what type a particular column should have. This allows mappings like tinyint(1) -> Bool without mapping tinyint(3) to Bool, for example.

Probably this implementation is bad because it will break existing code, so a more clean transition path should probably be investigated.

ryantm commented 6 years ago

Closing since this was just some example prototype.