Revision
15053
Author
hunmonk
Date
2009-10-02 12:09:02 -0500 (Fri, 02 Oct 2009)

Log Message

support functions for database operations in Lua.  currently includes simple escaping functions for MySQL and Postgres database queries -- good to use with LuaSQL's ODBC driver, for example...

Added Paths

Diff

Added: freeswitch/trunk/contrib/hunmonk/lua/support_functions_database.lua (0 => 15053)


--- freeswitch/trunk/contrib/hunmonk/lua/support_functions_database.lua	                        (rev 0)
+++ freeswitch/trunk/contrib/hunmonk/lua/support_functions_database.lua	2009-10-02 17:09:02 UTC (rev 15053)
@@ -0,0 +1,28 @@
+--[[
+  Helper functions for database operations.
+]]
+
+--[[
+  Escapes special characters for the MySQL database.
+]]
+function mysql_escape(string_to_escape)
+  -- Single quote, double quote, backspace.
+  escaped_string, replacements = string.gsub(string_to_escape, "(['\"\\])", function(x) return "\\" .. x end)
+  -- Null byte.
+  escaped_string, replacements = string.gsub(escaped_string, "%z", "\\0")
+
+  return escaped_string
+end
+
+--[[
+  Escapes special characters for the Postgres database.
+]]
+function pgsql_escape(string_to_escape)
+  -- Single quote..
+  escaped_string, replacements = string.gsub(string_to_escape, "'", "''")
+  -- Null byte.
+  escaped_string, replacements = string.gsub(escaped_string, "%z", "")
+
+  return escaped_string
+end
+