{"action":"created","issue":{"url":"https://api.github.com/repos/openresty/lua-resty-mysql/issues/29","labels_url":"https://api.github.com/repos/openresty/lua-resty-mysql/issues/29/labels{/name}","comments_url":"https://api.github.com/repos/openresty/lua-resty-mysql/issues/29/comments","events_url":"https://api.github.com/repos/openresty/lua-resty-mysql/issues/29/events","html_url":"https://github.com/openresty/lua-resty-mysql/issues/29","id":95361224,"number":29,"title":"event hooks for `lua-resty-mysql`","user":{"login":"sinogermany","id":4897141,"avatar_url":"https://avatars.githubusercontent.com/u/4897141?v=3","gravatar_id":"","url":"https://api.github.com/users/sinogermany","html_url":"https://github.com/sinogermany","followers_url":"https://api.github.com/users/sinogermany/followers","following_url":"https://api.github.com/users/sinogermany/following{/other_user}","gists_url":"https://api.github.com/users/sinogermany/gists{/gist_id}","starred_url":"https://api.github.com/users/sinogermany/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sinogermany/subscriptions","organizations_url":"https://api.github.com/users/sinogermany/orgs","repos_url":"https://api.github.com/users/sinogermany/repos","events_url":"https://api.github.com/users/sinogermany/events{/privacy}","received_events_url":"https://api.github.com/users/sinogermany/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-07-16T06:27:32Z","updated_at":"2015-07-17T03:39:07Z","closed_at":null,"body":"First of all, `cosocket` is cool :) Thanks for the excellent work @agentzh and other contributors. \r\nI could never imagine writing an async mysql / postgres driver in lua within 1k lines of codes.\r\n\r\nCurrently when we fetch a result set, we call `db:query`, which returns all the `rows`:\r\n```lua\r\nlocal row = _parse_row_data_packet(packet, cols, compact)\r\ni = i + 1\r\nrows[i] = row\r\n-- ......\r\nreturn rows\r\n```\r\n\r\nIn many (or at lease some) cases however we don't need to keep all records in memory.\r\nIt would be nice if we could do something like this (take json output as an example):\r\n```lua\r\nfunction row_event_hook(row)\r\n  -- 'row' is returned by '_parse_row_data_packet' function, a lua table\r\n  -- you can do something like ngx.say(cjson.encode(row)) or do whatever you want\r\n  ngx.say(cjson.encode(row))\r\n  -- the above is just an arbitrary example without details like commas etc.\r\nend\r\n\r\nngx.say('[')\r\n-- if row_event_hook is provided, records are not saved in the memory by default\r\n-- if row_event_hook is not provided, return the array of records as usual\r\ndb:query('SELECT foo FROM bar', row_event_hook)\r\nngx.say(']')\r\n```\r\n\r\nI do understand it's probably not worth it in the above example because it was mentioned in another issue that `ngx.say` doesn't buffer (it would be nice to have `ngx.write_to_buffer` function :) ). But there are other scenarios where a `row_event_hook` does make sense to prevent the request cycle from unnecessary memory allocations. Like writing BLOB data (blob_data_event_hook) through some IO stream (or HTTP response) etc. \r\n\r\nSo to wrap up, it would be nice to have some event hook support since we can potentially save some memory per request cycle, which is very important for a high concurrency web api / app.\r\n\r\nAny suggestions / comments welcome."},"comment":{"url":"https://api.github.com/repos/openresty/lua-resty-mysql/issues/comments/122160435","html_url":"https://github.com/openresty/lua-resty-mysql/issues/29#issuecomment-122160435","issue_url":"https://api.github.com/repos/openresty/lua-resty-mysql/issues/29","id":122160435,"user":{"login":"sinogermany","id":4897141,"avatar_url":"https://avatars.githubusercontent.com/u/4897141?v=3","gravatar_id":"","url":"https://api.github.com/users/sinogermany","html_url":"https://github.com/sinogermany","followers_url":"https://api.github.com/users/sinogermany/followers","following_url":"https://api.github.com/users/sinogermany/following{/other_user}","gists_url":"https://api.github.com/users/sinogermany/gists{/gist_id}","starred_url":"https://api.github.com/users/sinogermany/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sinogermany/subscriptions","organizations_url":"https://api.github.com/users/sinogermany/orgs","repos_url":"https://api.github.com/users/sinogermany/repos","events_url":"https://api.github.com/users/sinogermany/events{/privacy}","received_events_url":"https://api.github.com/users/sinogermany/received_events","type":"User","site_admin":false},"created_at":"2015-07-17T03:39:07Z","updated_at":"2015-07-17T03:39:07Z","body":"> Will you contribute a patch for that? Thanks!\r\n\r\nYeah sure. I'll try to do that in the weekend :) \r\nI'm just not familiar with `Test::Nginx::Socket::Lua` though.\r\nLet's see what I can contribute.\r\nBTW thanks for your fast response @agentzh . You're awesome."}}