Git Product home page Git Product logo

Comments (5)

flash-gordon avatar flash-gordon commented on August 29, 2024

"bool.cast(TRUE).as(:visible)" is a SQL statement, merely CAST(TRUE AS boolean) AS 'visible'. Setting the bool type does nothing on the casting side, it's on you. Sequel returns a string so it'll be a string. Same for all default types in rom-sql, they are nominal, with no typecasting for performance reasons.

from rom-sql.

cllns avatar cllns commented on August 29, 2024

Thanks @flash-gordon, that makes sense. That was a silly example since 'boolean' doesn't exist in SQLITE, but it's so permissive that it happens to work anyway. (and TRUE is an alias for 1).

My workaround for now is to override the initializer for my Struct with

def initialize(attributes)
  # Workaround for SQLite not having boolean type
  attributes[:visible] = attributes[:visible] == 1
  super
end

I guess the right way to do this is with a mapper?

from rom-sql.

flash-gordon avatar flash-gordon commented on August 29, 2024

I guess the right way to do this is with a mapper?

Yes, but you can also replace the read type, it worked for me:

    def with_visible
      select_append {
        `TRUE`.cast(:boolean).as(:visible).meta(
          read: Types::Bool.constructor { _1.eql?(1) }
        )
      }
    end

from rom-sql.

cllns avatar cllns commented on August 29, 2024

Oh nice, thanks. That works for me too.

Do you think there could be room for a ROM plugin that does this SQLite boolean coercion? It'd be nice to abstract away this quirk so it doesn't have to be done every time.

from rom-sql.

flash-gordon avatar flash-gordon commented on August 29, 2024

To do this, you need to swap the default :bool type with something. I don't know if this can be nicely done by a plugin. We have a dedicated namespace for Posgres' JSON(b) types: Types::PG::JSONB. We could have Types::SQLite::Bool but's not enough, we need a way to associate :bool with this particular type. I'll take a look into rom-sql's internals to see if it's possible to modify the type container for a relation.

from rom-sql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.