From 0b73d91261d98cddddac1e335e63b2747b1105e1 Mon Sep 17 00:00:00 2001
From: Vitaly <vitaly.kosenko@gmail.com>
Date: Mon, 21 Jul 2014 09:36:07 +0600
Subject: [PATCH 1/3] thread_id api call added

to get mysql connection thread id
---
 lib/resty/mysql.lua | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lib/resty/mysql.lua b/lib/resty/mysql.lua
index a629daa..e888131 100644
--- a/lib/resty/mysql.lua
+++ b/lib/resty/mysql.lua
@@ -551,6 +551,8 @@ function _M.connect(self, opts)
     self._server_ver = server_ver
 
     local thread_id, pos = _get_byte4(packet, pos)
+    
+    self._thread_id = thread_id
 
     --print("thread id: ", thread_id)
 
@@ -694,6 +696,9 @@ function _M.server_ver(self)
     return self._server_ver
 end
 
+function _M.thread_id(self)
+    return self._thread_id
+end
 
 local function send_query(self, query)
     if self.state ~= STATE_CONNECTED then

From a8a89f338c537078626aab8a01c9edd6bdfa0f5b Mon Sep 17 00:00:00 2001
From: Vitaly <vitaly.kosenko@gmail.com>
Date: Mon, 21 Jul 2014 09:40:37 +0600
Subject: [PATCH 2/3] test for thread_id added

in TEST 3: connected
---
 t/sanity.t | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/t/sanity.t b/t/sanity.t
index 7a7dd5c..2b6310f 100644
--- a/t/sanity.t
+++ b/t/sanity.t
@@ -121,7 +121,7 @@ GET /t
                 return
             end
 
-            ngx.say("connected to mysql ", db:server_ver())
+            ngx.say("connected to mysql ", db:server_ver(), " thread ", db:thread_id())
 
             db:close()
         ';
@@ -129,7 +129,7 @@ GET /t
 --- request
 GET /t
 --- response_body_like
-connected to mysql \d\.\S+
+connected to mysql \d\.\S+\sthread\s\d+
 --- no_error_log
 [error]
 

From a79b533563bbce38222fdb0ea82b2f9b13fcf2aa Mon Sep 17 00:00:00 2001
From: Vitaly <vitaly.kosenko@gmail.com>
Date: Mon, 21 Jul 2014 09:44:20 +0600
Subject: [PATCH 3/3] thread_id api call added to README

---
 README.markdown | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/README.markdown b/README.markdown
index 3d8f091..85c1ef9 100644
--- a/README.markdown
+++ b/README.markdown
@@ -21,6 +21,7 @@ Table of Contents
     * [read_result](#read_result)
     * [query](#query)
     * [server_ver](#server_ver)
+    * [thread_id](#thread_id)
     * [set_compact_arrays](#set_compact_arrays)
 * [SQL Literal Quoting](#sql-literal-quoting)
 * [Multi-Resultset Support](#multi-resultset-support)
@@ -332,6 +333,16 @@ You should only call this method after successfully connecting to a MySQL server
 
 [Back to TOC](#table-of-contents)
 
+thread_id
+----------
+`syntax: tid = db:thread_id()`
+
+Returns the MySQL connection thread id, like `"758"`.
+
+You should only call this method after successfully connecting to a MySQL server, otherwise `nil` will be returned.
+
+[Back to TOC](#table-of-contents)
+
 set_compact_arrays
 ------------------
 `syntax: db:set_compact_arrays(boolean)`