rodloboz
Inferring & casting param types for raw SQL
Is there any way to do SQL type inference/value casting when I have a raw SQL statement without having access to the underlying Ecto schemas?
In the Ruby/ActiveRecord world, I’ve used arel_table.type_cast_for_database(attribute_name, value)
So given a column name and a value from the SQL statement, is it possible to do something similar in Elixir (that would work for SQLite, MySQL, Postgres)?
Most Liked
LostKobrakai
At least in postgres you can query information_schema for the column type and Postgrex.Types.encode can encode data to that. There’s afaik no shared functionality. Ecto simply makes it the users responsibility that the ecto types they use work with the database they use.
LostKobrakai
I have no idea how ActiveRecord does it, but postgrex uses the binary protocol of postgres, which is very much a postgres specific format. Nothing in that process ever converts the value to a string/text format.
dimitarvp
Not really, because they are way too different (as @LostKobrakai pointed out, postgrex uses Postgres’ binary protocol and many other drivers in other programming languages don’t). It works in ActiveRecord because likely because somebody put on the elbow grease to have a branching code… the same one you would invent yourself if you chase after this further.
To shill a little bit for myself again: when I finally overcome my being extremely busy and tired to deal with the final issue of CI workers running out of memory on machines with 92% free memory, I’ll release my SQLite3 thing that has a ton of introspection and makes writing the wiring you need trivial. But that’s an announcement for a weeks from now (hopefully not months but oh well, I’ve been saying this for like two months now so…).







