From 4164eda299a44dcd0c1d13e1a6d458be26b1fe31 Mon Sep 17 00:00:00 2001
From: Bulat Niatshin <theairvideo@gmail.com>
Date: Wed, 5 Jul 2017 14:57:02 +0300
Subject: [PATCH] Remove UTF-16 encoding option

* UTF-16 support removed
* Tests with UTF-16* cases removed

Closes #2295
---
 src/box/sql/pragma.c          |  30 ++++----
 test/sql-tap/e_expr.test.lua  | 109 +---------------------------
 test/sql-tap/func.test.lua    |  86 +++++++----------------
 test/sql-tap/func3.test.lua   | 129 ----------------------------------
 test/sql-tap/misc3.test.lua   |  20 ------
 test/sql-tap/numcast.test.lua |   2 +-
 test/sql-tap/types.test.lua   |  13 ----
 7 files changed, 44 insertions(+), 345 deletions(-)

diff --git a/src/box/sql/pragma.c b/src/box/sql/pragma.c
index ed7dd3d30b..35cc785705 100644
--- a/src/box/sql/pragma.c
+++ b/src/box/sql/pragma.c
@@ -21,6 +21,14 @@
 #  endif
 #endif
 
+
+/*
+*************************************************************************
+** pragma.h contains several pragmas, including utf's pragmas.
+** All that is not utf-8 should be omitted
+*************************************************************************
+*/
+
 /***************************************************************************
 ** The "pragma.h" include file is an automatically generated file that
 ** that includes the PragType_XXXX macro definitions and the aPragmaName[]
@@ -1605,10 +1613,9 @@ void sqlite3Pragma(
   break;
 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 
-#ifndef SQLITE_OMIT_UTF16
   /*
   **   PRAGMA encoding
-  **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
+  **   PRAGMA encoding = "utf-8"
   **
   ** In its first form, this pragma returns the encoding of the main
   ** database. If the database is not initialized, it is initialized now.
@@ -1618,7 +1625,7 @@ void sqlite3Pragma(
   ** encoding that will be used for the main database file if a new file
   ** is created. If an existing main database file is opened, then the
   ** default text encoding for the existing database is used.
-  ** 
+  **
   ** In all cases new databases created using the ATTACH command are
   ** created to use the same default text encoding as the main database. If
   ** the main database has not been initialized and/or created when ATTACH
@@ -1635,20 +1642,12 @@ void sqlite3Pragma(
     } encnames[] = {
       { "UTF8",     SQLITE_UTF8        },
       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
-      { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
-      { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
-      { "UTF16le",  SQLITE_UTF16LE     },
-      { "UTF16be",  SQLITE_UTF16BE     },
-      { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
-      { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
       { 0, 0 }
     };
     const struct EncName *pEnc;
     if( !zRight ){    /* "PRAGMA encoding" */
       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
-      assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
-      assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
       returnSingleText(v, encnames[ENC(pParse->db)].zName);
     }else{                        /* "PRAGMA encoding = XXX" */
       /* Only change the value of sqlite.enc if the database handle is not
@@ -1656,14 +1655,14 @@ void sqlite3Pragma(
       ** will be overwritten when the schema is next loaded. If it does not
       ** already exists, it will be created to use the new encoding value.
       */
-      if( 
-        !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
-        DbHasProperty(db, 0, DB_Empty) 
+      if(
+        !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
+        DbHasProperty(db, 0, DB_Empty)
       ){
         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
             SCHEMA_ENC(db) = ENC(db) =
-                pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
+                pEnc->enc;
             break;
           }
         }
@@ -1674,7 +1673,6 @@ void sqlite3Pragma(
     }
   }
   break;
-#endif /* SQLITE_OMIT_UTF16 */
 
 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
   /*
diff --git a/test/sql-tap/e_expr.test.lua b/test/sql-tap/e_expr.test.lua
index 5987f507f4..f15deaef96 100755
--- a/test/sql-tap/e_expr.test.lua
+++ b/test/sql-tap/e_expr.test.lua
@@ -3467,42 +3467,6 @@ do_qexpr_test("e_expr-27.4.1", " CAST('ghi' AS blob) ", "X'676869'")
 do_qexpr_test("e_expr-27.4.2", " CAST(456 AS blob) ", "X'343536'")
 do_qexpr_test("e_expr-27.4.3", " CAST(1.78 AS blob) ", "X'312E3738'")
 
--- MUST_WORK_TEST bd should be restarted? is it necessary?
-if 0>0 then
-    X(1454, "X!cmd", [=[["rename","db","db2"]]=])
-    sqlite3("db", ":memory:")
-    test:execsql " PRAGMA encoding = 'utf-16le' "
-    do_qexpr_test("e_expr-27.4.4", " CAST('ghi' AS blob) ", "X'670068006900'")
-    do_qexpr_test("e_expr-27.4.5", " CAST(456 AS blob) ", "X'340035003600'")
-    do_qexpr_test("e_expr-27.4.6", " CAST(1.78 AS blob) ", "X'31002E0037003800'")
-
-
-    db("close")
-    sqlite3("db", ":memory:")
-    test:execsql " PRAGMA encoding = 'utf-16be' "
-    do_qexpr_test("e_expr-27.4.7", " CAST('ghi' AS blob) ", "X'006700680069'")
-    do_qexpr_test("e_expr-27.4.8", " CAST(456 AS blob) ", "X'003400350036'")
-    do_qexpr_test("e_expr-27.4.9", " CAST(1.78 AS blob) ", "X'0031002E00370038'")
-
-
-    db("close")
-    X(1471, "X!cmd", [=[["rename","db2","db"]]=])
-    -- EVIDENCE-OF: R-04207-37981 To cast a BLOB value to TEXT, the sequence
-    -- of bytes that make up the BLOB is interpreted as text encoded using
-    -- the database encoding.
-    --
-    do_expr_test("e_expr-28.1.1", " CAST (X'676869' AS text) ", "text", "ghi")
-    do_expr_test("e_expr-28.1.2", " CAST (X'670068006900' AS text) ", "text", "g")
-    X(1479, "X!cmd", [=[["rename","db","db2"]]=])
-    sqlite3("db", ":memory:")
-    test:execsql " PRAGMA encoding = 'utf-16le' "
-    do_expr_test("e_expr-28.1.3", " CAST (X'676869' AS text) == 'ghi' ", "integer", 0)
-    do_expr_test("e_expr-28.1.4", " CAST (X'670068006900' AS text) ", "text", "ghi")
-
-
-    db("close")
-    X(1487, "X!cmd", [=[["rename","db2","db"]]=])
-end
 -- EVIDENCE-OF: R-22235-47006 Casting an INTEGER or REAL value into TEXT
 -- renders the value as if via sqlite3_snprintf() except that the
 -- resulting TEXT uses the encoding of the database connection.
@@ -3523,24 +3487,6 @@ do_expr_test("e_expr-29.1.2", " CAST (X'3233302E30' AS REAL) ", "real", 230.0)
 do_expr_test("e_expr-29.1.3", " CAST (X'2D392E3837' AS REAL) ", "real", -9.87)
 do_expr_test("e_expr-29.1.4", " CAST (X'302E30303031' AS REAL) ", "real", 0.0001)
 
--- MUST_WORK_TEST bd should be restarted? is it necessary?
-if 0>0 then
-    X(1509, "X!cmd", [=[["rename","db","db2"]]=])
-    sqlite3("db", ":memory:")
-    test:execsql " PRAGMA encoding = 'utf-16le' "
-    do_expr_test("e_expr-29.1.5", [[
-        CAST (X'31002E0032003300' AS REAL) ]], "real", 1.23)
-    do_expr_test("e_expr-29.1.6", [[
-        CAST (X'3200330030002E003000' AS REAL) ]], "real", 230.0)
-    do_expr_test("e_expr-29.1.7", [[
-        CAST (X'2D0039002E0038003700' AS REAL) ]], "real", -9.87)
-    do_expr_test("e_expr-29.1.8", [[
-        CAST (X'30002E003000300030003100' AS REAL) ]], "real", 0.0001)
-
-
-    db("close")
-    X(1523, "X!cmd", [=[["rename","db2","db"]]=])
-end
 -- EVIDENCE-OF: R-54898-34554 When casting a TEXT value to REAL, the
 -- longest possible prefix of the value that can be interpreted as a real
 -- number is extracted from the TEXT value and the remainder ignored.
@@ -3573,24 +3519,7 @@ do_expr_test("e_expr-30.1.3", [[
 do_expr_test("e_expr-30.1.4", [[
   CAST(X'2D31313235383939393036383432363234' AS INTEGER)
 ]], "integer", -1125899906842624LL)
--- MUST_WORK_TEST bd should be restarted? is it necessary?
-if 0>0 then
-    X(1561, "X!cmd", [=[["rename","db","db2"]]=])
-    sqlite3("db", ":memory:")
-    test:execsql " PRAGMA encoding = 'utf-16be' "
-    do_expr_test("e_expr-30.1.5", " CAST(X'003100320033' AS INTEGER) ", "integer", 123)
-    do_expr_test("e_expr-30.1.6", " CAST(X'002D003600370038' AS INTEGER) ", "integer", -678)
-    do_expr_test("e_expr-30.1.7", [[
-      CAST(X'0031003000300030003000300030' AS INTEGER)
-    ]], "integer", 1000000)
-    do_expr_test("e_expr-30.1.8", [[
-      CAST(X'002D0031003100320035003800390039003900300036003800340032003600320034' AS INTEGER)
-    ]], "integer", -1125899906842624)
-
-
-    db("close")
-    X(1575, "X!cmd", [=[["rename","db2","db"]]=])
-end
+
 -- EVIDENCE-OF: R-47612-45842 When casting a TEXT value to INTEGER, the
 -- longest possible prefix of the value that can be interpreted as an
 -- integer number is extracted from the TEXT value and the remainder
@@ -3666,42 +3595,8 @@ do_expr_test("e_expr-32.2.4", [[
 -- EVIDENCE-OF: R-64550-29191 Note that the result from casting any
 -- non-BLOB value into a BLOB and the result from casting any BLOB value
 -- into a non-BLOB value may be different depending on whether the
--- database encoding is UTF-8, UTF-16be, or UTF-16le.
+-- database encoding is UTF-8
 --
--- MUST_WORK_TEST bd should be restarted? is it necessary?
-if 0>0 then
-    sqlite3("db1", ":memory:")
-    X(1666, "X!cmd", [=[["db1","eval"," PRAGMA encoding = 'utf-8' "]]=])
-    sqlite3("db2", ":memory:")
-    test:execsql " PRAGMA encoding = 'utf-16le' "
-    sqlite3("db3", ":memory:")
-    test:execsql " PRAGMA encoding = 'utf-16be' "
-    for _ in X(0, "X!foreach", [=[["tn castexpr differs","\n  1 { CAST(123 AS BLOB)    } 1\n  2 { CAST('' AS BLOB)     } 0\n  3 { CAST('abcd' AS BLOB) } 1\n\n  4 { CAST(X'abcd' AS TEXT) } 1\n  5 { CAST(X'' AS TEXT)     } 0\n"]]=]) do
-        r1 = X(1672, "X!cmd", [=[["db1","eval",["SELECT typeof(",["castexpr"],"), quote(",["castexpr"],")"]]]=])
-        r2 = test:execsql(string.format("SELECT typeof(%s), quote(%s)", castexpr, castexpr))
-        r3 = test:execsql(string.format("SELECT typeof(%s), quote(%s)", castexpr, castexpr))
-        if differs
-     then
-            res = ((r1 ~= r2) and (r2 ~= r3))
-        else
-            res = ((r1 == r2) and (r2 == r3))
-        end
-        test:do_test(
-            "e_expr-33.1."..tn,
-            function()
-                return res
-            end, {
-                1
-            })
-
-    end
-    X(1689, "X!cmd", [=[["db1","close"]]=])
-    db2("close")
-    db3("close")
-    X(1696, "X!cmd", [=[["catch"," db close "]]=])
-    forcedelete("test.db")
-end
-
 ---------------------------------------------------------------------------
 -- Test statements related to the EXISTS and NOT EXISTS operators.
 --
diff --git a/test/sql-tap/func.test.lua b/test/sql-tap/func.test.lua
index 5e969b7731..13d606069a 100755
--- a/test/sql-tap/func.test.lua
+++ b/test/sql-tap/func.test.lua
@@ -1010,69 +1010,37 @@ test:do_execsql_test(
 
 
 --encoding = db("one", "PRAGMA encoding")
-if 0>0 then--(encoding == "UTF-16le") then
-    test:do_execsql_test(
-        "func-9.11-utf16le",
-        [[
-            SELECT hex(replace('abcdefg','ef','12'))
-        ]], {
-            -- <func-9.11-utf16le>
-            6100620063006400310032006700
-            -- </func-9.11-utf16le>
-        })
-
-    test:do_execsql_test(
-        "func-9.12-utf16le",
-        [[
-            SELECT hex(replace('abcdefg','','12'))
-        ]], {
-            -- <func-9.12-utf16le>
-            6100620063006400650066006700
-            -- </func-9.12-utf16le>
-        })
-
-    test:do_execsql_test(
-        "func-9.13-utf16le",
-        [[
-            SELECT hex(replace('aabcdefg','a','aaa'))
-        ]], {
-            -- <func-9.13-utf16le>
-            610061006100610061006100620063006400650066006700
-            -- </func-9.13-utf16le>
-        })
 
-else --elseif (encoding == "UTF-8") then
-    test:do_execsql_test(
-        "func-9.11-utf8",
-        [[
-            SELECT hex(replace('abcdefg','ef','12'))
-        ]], {
-            -- <func-9.11-utf8>
-            "61626364313267"
-            -- </func-9.11-utf8>
-        })
+test:do_execsql_test(
+    "func-9.11-utf8",
+    [[
+        SELECT hex(replace('abcdefg','ef','12'))
+    ]], {
+    -- <func-9.11-utf8>
+    "61626364313267"
+    -- </func-9.11-utf8>
+})
 
-    test:do_execsql_test(
-        "func-9.12-utf8",
-        [[
-            SELECT hex(replace('abcdefg','','12'))
-        ]], {
-            -- <func-9.12-utf8>
-            "61626364656667"
-            -- </func-9.12-utf8>
-        })
+test:do_execsql_test(
+    "func-9.12-utf8",
+    [[
+        SELECT hex(replace('abcdefg','','12'))
+    ]], {
+    -- <func-9.12-utf8>
+    "61626364656667"
+    -- </func-9.12-utf8>
+})
 
-    test:do_execsql_test(
-        "func-9.13-utf8",
-        [[
-            SELECT hex(replace('aabcdefg','a','aaa'))
-        ]], {
-            -- <func-9.13-utf8>
-            "616161616161626364656667"
-            -- </func-9.13-utf8>
-        })
+test:do_execsql_test(
+    "func-9.13-utf8",
+    [[
+        SELECT hex(replace('aabcdefg','a','aaa'))
+    ]], {
+    -- <func-9.13-utf8>
+    "616161616161626364656667"
+    -- </func-9.13-utf8>
+})
 
-end
 -- Use the "sqlite_register_test_function" TCL command which is part of
 -- the text fixture in order to verify correct operation of some of
 -- the user-defined SQL function APIs that are not used by the built-in
diff --git a/test/sql-tap/func3.test.lua b/test/sql-tap/func3.test.lua
index 626dfe610e..96ebd824a9 100755
--- a/test/sql-tap/func3.test.lua
+++ b/test/sql-tap/func3.test.lua
@@ -21,136 +21,7 @@ test:plan(25)
 -- ["set","testdir",[["file","dirname",["argv0"]]]]
 -- ["source",[["testdir"],"\/tester.tcl"]]
 -- MUST_WORK_TEST built-in functions check desrtoy api of sqlite3_create_function_v2
-if 0>0 then
---ifcapable utf16 {
-test:do_test(
-    "func3-1.1",
-    function()
-        destroyed = 0
-        local function destroy()
-            destroyed = 1
-        end
-
-        sqlite3_create_function_v2("db", "f2", -1, "any", "-func", "f2", "-destroy", "destroy")
-        return destroyed
-    end, {
-        -- <func3-1.1>
-        0
-        -- </func3-1.1>
-    })
-
-test:do_test(
-    "func3-1.2",
-    function()
-        sqlite3_create_function_v2("db", "f2", -1, "utf8", "-func", "f2")
-        return destroyed
-    end, {
-        -- <func3-1.2>
-        0
-        -- </func3-1.2>
-    })
-
-test:do_test(
-    "func3-1.3",
-    function()
-        sqlite3_create_function_v2("db", "f2", -1, "utf16le", "-func", "f2")
-        return destroyed
-    end, {
-        -- <func3-1.3>
-        0
-        -- </func3-1.3>
-    })
-
-test:do_test(
-    "func3-1.4",
-    function()
-        sqlite3_create_function_v2("db", "f2", -1, "utf16be", "-func", "f2")
-        return destroyed
-    end, {
-        -- <func3-1.4>
-        1
-        -- </func3-1.4>
-    })
---end
-
-
-test:do_test(
-    "func3-2.1",
-    function()
-        destroyed = 0
-        local function destroy()
-            destroyed = 1
-        end
-
-        sqlite3_create_function_v2("db", "f3", -1, "utf8", "-func", "f3", "-destroy", "destroy")
-        return destroyed
-    end, {
-        -- <func3-2.1>
-        0
-        -- </func3-2.1>
-    })
-
-test:do_test(
-    "func3-2.2",
-    function()
-        sqlite3_create_function_v2("db", "f3", -1, "utf8", "-func", "f3")
-        return destroyed
-    end, {
-        -- <func3-2.2>
-        1
-        -- </func3-2.2>
-    })
 
-test:do_test(
-    "func3-3.1",
-    function()
-        destroyed = 0
-        local function destroy()
-            destroyed = 1
-        end
-
-        sqlite3_create_function_v2("db", "f3", -1, "any", "-func", "f3", "-destroy", "destroy")
-        return destroyed
-    end, {
-        -- <func3-3.1>
-        0
-        -- </func3-3.1>
-    })
-
-test:do_test(
-    "func3-3.2",
-    function()
-        db("close")
-        return destroyed
-    end, {
-        -- <func3-3.2>
-        1
-        -- </func3-3.2>
-    })
-
-sqlite3("db", "test.db")
-test:do_test(
-    "func3-4.1",
-    function()
-        destroyed = 0
-        rc = X(70, "X!cmd", [=[["catch"," \n    sqlite3_create_function_v2 db f3 -1 any -func f3 -step f3 -destroy destroy\n  ","msg"]]=])
-        return { rc, msg }
-    end, {
-        -- <func3-4.1>
-        1, "SQLITE_MISUSE"
-        -- </func3-4.1>
-    })
-
-test:do_test(
-    "func3-4.2",
-    function()
-        return destroyed
-    end, {
-        -- <func3-4.2>
-        1
-        -- </func3-4.2>
-    })
-end
 -- EVIDENCE-OF: R-41921-05214 The likelihood(X,Y) function returns
 -- argument X unchanged.
 --
diff --git a/test/sql-tap/misc3.test.lua b/test/sql-tap/misc3.test.lua
index 1db076057a..59fac92f49 100755
--- a/test/sql-tap/misc3.test.lua
+++ b/test/sql-tap/misc3.test.lua
@@ -462,26 +462,6 @@ test:do_test(
         return string.find(x, "\"SorterCompare\",%d+,%d+,%d+") > 0
     end, true)
 
---if X(284, "X!cmd", [=[["regexp","16",[["db","one","PRAGMA encoding"]]]]=])
--- then
---    test:do_test(
---        "misc3-6.11-utf16",
---        function()
---            x = test:execsql([[
---                EXPLAIN SELECT a+123456789012, b*4.5678, c FROM ex1 ORDER BY +a, b DESC
---            ]])
---            y = X(291, "X!cmd", [=[["regexp"," 123456789012 ",["x"]]]=])
---            table.insert(y,X(292, "X!cmd", [=[["regexp"," 4.5678 ",["x"]]]=]))
---            return table.insert(y,X(293, "X!cmd", [=[["regexp",",-B",["x"]]]=])) or y
---        end, {
---            -- <misc3-6.11-utf16>
---            1, 1, 1
---            -- </misc3-6.11-utf16>
---        })
---
---else
--- MUST_WORK_TEST
-
 test:do_test(
     "misc3-6.11-utf8",
     function()
diff --git a/test/sql-tap/numcast.test.lua b/test/sql-tap/numcast.test.lua
index 02b6c03e5d..04241e3e73 100755
--- a/test/sql-tap/numcast.test.lua
+++ b/test/sql-tap/numcast.test.lua
@@ -21,7 +21,7 @@ test:plan(17)
 -- ["source",[["testdir"],"\/tester.tcl"]]
 
 -- MUST_WORK_TEST should we use
--- for _, enc in ipairs({"utf8", "utf16le", "utf16be"}) do
+-- for _, enc in ipairs({"utf8", }) do
 for _, enc in ipairs({"utf8"}) do
     test:do_test(
         "numcast-"..enc..".0",
diff --git a/test/sql-tap/types.test.lua b/test/sql-tap/types.test.lua
index ab33f418d0..c102efdd28 100755
--- a/test/sql-tap/types.test.lua
+++ b/test/sql-tap/types.test.lua
@@ -354,19 +354,6 @@ test:do_execsql_test(
         -- </types-2.4.2>
     })
 
--- # Check that all the record sizes are as we expected. This is dependant on
--- # the database encoding.
--- if { $sqlite_options(utf16)==0 || [execsql {pragma encoding}] == "UTF-8" } {
---   do_test types-2.4.3 {
---     set root [db eval {select rootpage from sqlite_master where name = 't4'}]
---     record_sizes $root
---   } {12 503 500004}
--- } else {
---   do_test types-2.4.3 {
---     set root [db eval {select rootpage from sqlite_master where name = 't4'}]
---     record_sizes $root
---   } {22 1003 1000004}
--- }
 test:do_execsql_test(
     "types-2.5.1",
     [[
-- 
GitLab