sailorproject / sailor

A Lua MVC Web Framework.
MIT License
921 stars 125 forks source link

DB usage (mysql, posgresql). #143

Closed aleksmelnikov closed 6 years ago

aleksmelnikov commented 7 years ago

Hello

It is seems to me the code is not good:

sailor/src/sailor/db/resty_mysql.lua line 31: port = 3306,

Is it better to make a new parameter 'conf.port' for databases in the config file?

And this code:

sailor/src/sailor/db/luasql_common.lua line 43: db.con = assert (db.env:connect(conf.dbname,conf.user,conf.pass,conf.host))

Is it better to make like: db.con = assert (db.env:connect(conf.dbname,conf.user,conf.pass,conf.host,conf.port)) Because it doesn't work for me with postresql without port parameter.

Thank you.

d9k commented 6 years ago

so sad it's not fixed yet :crying_cat_face:

luasql postgres env:connect params: https://keplerproject.github.io/luasql/manual.html#postgres_extensions

luasql mysql env:connect params: https://keplerproject.github.io/luasql/manual.html#mysql_extensions

the port param is not used

d9k commented 6 years ago

Solved with monkey patching (how can this even work?! :scream_cat:)

mysite/index.lua:

require 'monkey_patching'

local sailor = require "sailor"
sailor.launch()

mysite/monkey_patching.lua:

luasql_common = require 'sailor.db.luasql_common'
local conf = require 'conf.conf'

-- adding port param to luasql_common.connect
local db_conf = conf.db[conf.sailor.environment]
local luasql = require("luasql."..db_conf.driver)

luasql_common.connect = function ()
    if luasql_common.transaction then return end
    luasql_common.env = assert (luasql[db_conf.driver]())
    luasql_common.con = assert (luasql_common.env:connect(db_conf.dbname, db_conf.user, db_conf.pass, db_conf.host, db_conf.port))
end

Monkey patching is weird, but it works :sweat_smile: I :heart: lua

max-bertinetti commented 6 years ago

Fixed in next release.