leafo / pgmoon

A pure Lua Postgres driver for use in OpenResty & more
MIT License
391 stars 93 forks source link

convert_null works wrong #94

Closed alexandrim0 closed 4 years ago

alexandrim0 commented 4 years ago

Hi!

I set convert_null = true but it returns ["NULL"] but not ngx.null It is completely unusable! I belive it should not works like that. May be it broken with pgsql 11?

leafo commented 4 years ago

By default convert_null will convert null to the value stored in Postgres.NULL, which is a unique table {"NULL"}. If you want to have it convert to something else then you can override the Postgres.NULL value.

https://github.com/leafo/pgmoon#converting-nulls

pgmoon is runtime agnostic, so it does not default to ngx.null

alexandrim0 commented 4 years ago

Ok, you are right, pgmoon is runtime agnostic and it is good! But you also use cjson which also have cjson.null user type represents SQL NULL same way as ngx.null does. I do not like use nullable types in SQL schemas but in some case it is correct design. pgmoon is the best lib for lua to deal with pgsql, I think. So, I need simple way to handle NULL with pgsql as I can do with mysql and etc... And now I should patch this lib to make my code works after migration from mysql to pgsql. It is no good. I guess, I am not the only one who faces this.

So, is it possible to choose which way convert_null will works? Thank you!

leafo commented 4 years ago

I'm not sure I follow, you can override which value gets set to null with something like this:

local pg = Postgres:new(...)
pg.convert_null = true
pg.NULL = ngx.null

local res = pg:query("select NULL")
alexandrim0 commented 4 years ago

Great!

This works fine! It should be in readme, I think.

Thanks, you are the best!