Blady-Com / gnoga

gnoga - The GNU Omnificent GUI for Ada
GNU General Public License v3.0
12 stars 4 forks source link

Compatibility between MySQL and SQLite (escape chars) #4

Open sowebio opened 1 year ago

sowebio commented 1 year ago

In gnoga-database-server-sqlite.adb, the Escape_String function was outputting nonsense.

Below is the corrected version, which is consistent with the version of gnoga-database-server-mysql.adb gnoga-database-server-mysql.adb using an internal function of libmysql/libmariadb.I confess I didn't look to see if it handles more cases than I did and which correspond to the Ada example...

Example before/after correction with the db_sqlite.adb test

V.Put_Line (Connection.Escape_String ("I've been thinking.. ""escaped"" \ is it?"));I''ve been thinking.. "escaped" \ is it? < NoI\'ve been thinking.. \"escaped\" \ is it? < Yes

-- Escape_String --
-- overriding function Escape_String
-- (C : Connection;
-- S : String)
-- return String
-- is
-- pragma Unreferenced (C);
--
-- New_String : String;
-- begin
-- for J in S loop
-- if S (J) = ''' then
-- New_String := New_String & "''";
-- else
-- New_String := New_String & S (J);
-- end if;
-- end loop;
--
-- return New_String;
-- end Escape_String;

overriding function Escape_String
(C : Connection;
S : String)
return String
is
pragma Unreferenced (C);

  New_String : String;
begin
for J in S loop
if S (J) = ''' then
New_String := New_String & "\" & S (J);
elsif S (J) = '"' then
New_String := New_String & "\" & S (J);
elsif S (J) = '\' then
New_String := New_String & "\" & S (J);
else
New_String := New_String & S (J);
end if;
end loop;

  return New_String;
end Escape_String;
Blady-Com commented 11 months ago

The abstract function Escape_String has 2 implementations:

The mysql_real_escape_string documentation has a note which points out the NO_BACKSLASH_ESCAPES SQL mode. In this mode is enabled the function cannot escape quote characters except by doubling them.

Before making changes in Gnoga, how to deal with this mode?