Search code examples
lasso-lang

MySQL Connector field() Auto-Conversion to Lasso Types?


In Lasso 8 with MySQL connector, the field() method seemed to always return a string type, regardless of what data was in the column, or the column's data type. The exception might've been BLOB columns, which might've returned a bytes type. (I don't recall at the moment.)

In Lasso 9 I see that the field() method returns an integer type for integer columns. This is causing some issue with conditionals where I tested for '1' instead of 1.

Is Lasso really using the MySQL data type, or is Lasso just interpreting the results?

Is there any documentation as to what column types are cast to what Lasso data types?


Solution

  • It's easy to find out what column types Lasso reports.
    With a table looking like this:

    CREATE TABLE `addressb` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `type` enum('commercial','residential') COLLATE utf8_swedish_ci DEFAULT NULL,
     `street` varchar(50) COLLATE utf8_swedish_ci NOT NULL DEFAULT NULL,
     `city` varchar(25) COLLATE utf8_swedish_ci NOT NULL DEFAULT NULL,
     `state` char(2) COLLATE utf8_swedish_ci NOT NULL DEFAULT NULL,
     `zip` char(10) COLLATE utf8_swedish_ci NOT NULL DEFAULT NULL,
     `image` blob,
     `created` datetime DEFAULT NULL,
     PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
    

    You can get the reported column types like this:

    inline(-database = 'testdb', -sql = "SELECT * FROM address") => {^
        with col in column_names do {^
            column(#col) -> type
            '<br />'
        ^}
    ^}
    

    Result:
    id integer
    type string
    street string
    city string
    state string
    zip null
    image bytes
    created string

    As you can see, everything is reported as string except the integer and blob field. Plus the zip field that in this record happen to contain NULL and is reported as such.

    When doing comparisons it is always a good idea to make sure you're comparing apples with apples. That is, making sure you're comparing values of the same type. Also, to check if there's content I always go for size.
    string(column('zip')) -> size > 0 ?