]> git.sven.stormbind.net Git - sven/scripts.git/commitdiff
Add a mysql-proxy sample which mirrors inserts to a sqlite3 db.
authorSven Hoexter <sven@timegate.de>
Sun, 5 Feb 2012 11:19:11 +0000 (12:19 +0100)
committerSven Hoexter <sven@timegate.de>
Sun, 5 Feb 2012 11:19:11 +0000 (12:19 +0100)
mysql-proxy/query-mirror-sqlite3.lua [new file with mode: 0644]
mysql-proxy/usage.txt [new file with mode: 0644]

diff --git a/mysql-proxy/query-mirror-sqlite3.lua b/mysql-proxy/query-mirror-sqlite3.lua
new file mode 100644 (file)
index 0000000..00818b1
--- /dev/null
@@ -0,0 +1,29 @@
+-- unfuck the cpath set by mysql-proxy and load sqlite3 driver
+package.cpath=package.cpath .. ";/usr/lib/i386-linux-gnu/lua/5.1/?.so"
+require "luasql.sqlite3"
+
+-- intercept mysql queries
+function read_query( packet )
+   if packet:byte() == proxy.COM_QUERY then
+      print("DEBUG: we got a normal query: " .. packet:sub(2))
+      local query = string.sub(packet, 2)
+
+      if string.match(string.upper(query), '^%s*INSERT') then
+        print("DEBUG: found a query to process")
+        -- create environment object
+        local env = assert (luasql.sqlite3())
+        -- connect to data source
+        local con = assert (env:connect("mydb.sqlite3"))
+
+        -- act on the database
+        -- local cur = assert (con:execute(query))
+        local cur = con:execute(query)
+
+        -- closing time
+        cur:close()
+        con:close()
+        env:close()
+      end
+   end
+end
+
diff --git a/mysql-proxy/usage.txt b/mysql-proxy/usage.txt
new file mode 100644 (file)
index 0000000..eeaf30b
--- /dev/null
@@ -0,0 +1,7 @@
+mysql proxy startup command:
+
+mysql-proxy --admin-username=a --admin-password=b  --admin-lua-script=/usr/share/mysql-proxy/admin.lua --proxy-backend-addresses=127.0.0.1:3306 --proxy-lua-script=/home/sven/myscript.lua
+
+
+Introduction:
+http://www.oreillynet.com/pub/a/databases/2007/07/12/getting-started-with-mysql-proxy.html