--
--  $Id$
--
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--
--  Copyright (C) 1998-2018 OpenLink Software
--
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--
--

--!AWK LONG
create procedure DB.DBA.__MAKE_UNICODE3_COLLATIONS_1 ()
{
  declare ccname varchar;
  declare ses, tbl any;
  ses := string_output ();
  gz_uncompress (uudecode ('eJyku9duHOnX7oftnHOO24DtO7B9ZvseDBiwAQf4zGe+hM4555yqq6qrK1fn
[encoded data, 173300 characters]
mnljOfH5/P9/Yhki
', 2), ses);
  tbl := charset_recode (string_output_string (ses), 'UTF-8', '_WIDE_');
  ccname := complete_collation_name ('LEXICAL', 1);
  __collation_define_memonly (ccname, tbl);
  insert replacing SYS_COLLATIONS (COLL_NAME, COLL_TABLE, COLL_WIDE) values (ccname, cast (tbl as varbinary), 1);
  commit work;
  log_text ('__collation_define_memonly (?,?)', ccname, tbl);
}
;


--!AWK LONG
create procedure DB.DBA.__MAKE_UNICODE3_COLLATIONS_2 ()
{
  declare ccname varchar;
  declare ses, tbl any;
-- LEXICAL_ACUTE collation
  ses := string_output ();
  gz_uncompress (uudecode ('eJyku+duXFuzrofjeJx9nLOPAfsObP+zfQ8GDNiAA/zP/3wLnXPOOc2ePXvm
[encoded data, 173361 characters]
52uzVUF+QO4g95DNupFc+Hr9fxeW6kE=
', 2), ses);
  tbl := charset_recode (string_output_string (ses), 'UTF-8', '_WIDE_');
  ccname := complete_collation_name ('LEXICAL_ACUTE', 1);
  __collation_define_memonly (ccname, tbl);
  insert replacing SYS_COLLATIONS (COLL_NAME, COLL_TABLE, COLL_WIDE) values (ccname, cast (tbl as varbinary), 1);
  commit work;
  log_text ('__collation_define_memonly (?,?)', ccname, tbl);
}
;

create procedure DB.DBA.__MAKE_UNICODE3_COLLATIONS (in force integer := 0)
{
  declare ctr integer;
  declare ccname varchar;
  if (exists (select 1 from DB.DBA.SYS_COLLATIONS where COLL_NAME = complete_collation_name ('LEXICAL_ACUTE', 1)) and not force)
    return;
  declare ses, tbl any;
  DB.DBA.__MAKE_UNICODE3_COLLATIONS_1();
  DB.DBA.__MAKE_UNICODE3_COLLATIONS_2();
-- Lowercase
  tbl := make_wstring (65536, wchr1(1));
  for (ctr := 1; ctr < 65536; ctr := ctr+1)
    tbl[ctr] := lcase (wchr1(ctr))[0];
  ccname := complete_collation_name ('LCASE', 1);
  __collation_define_memonly (ccname, tbl);
  insert replacing DB.DBA.SYS_COLLATIONS (COLL_NAME, COLL_TABLE, COLL_WIDE) values (ccname, cast (tbl as varbinary), 1);
  commit work;
  log_text ('__collation_define_memonly (?,?)', ccname, tbl);
-- Uppercase
  tbl := make_wstring (65536, wchr1(1));
  for (ctr := 1; ctr < 65536; ctr := ctr+1)
    tbl[ctr] := ucase (wchr1(ctr))[0];
  ccname := complete_collation_name ('UCASE', 1);
  __collation_define_memonly (ccname, tbl);
  insert replacing DB.DBA.SYS_COLLATIONS (COLL_NAME, COLL_TABLE, COLL_WIDE) values (ccname, cast (tbl as varbinary), 1);
  commit work;
  log_text ('__collation_define_memonly (?,?)', ccname, tbl);
-- Remove accents
  tbl := make_wstring (65536, wchr1(1));
  for (ctr := 1; ctr < 65536; ctr := ctr+1)
    tbl[ctr] := remove_unicode3_accents (wchr1(ctr))[0];
  ccname := complete_collation_name ('BASECHAR', 1);
  __collation_define_memonly (ccname, tbl);
  insert replacing DB.DBA.SYS_COLLATIONS (COLL_NAME, COLL_TABLE, COLL_WIDE) values (ccname, cast (tbl as varbinary), 1);
  commit work;
  log_text ('__collation_define_memonly (?,?)', ccname, tbl);
-- Remove accents, then lcase
  tbl := make_wstring (65536, wchr1(1));
  for (ctr := 1; ctr < 65536; ctr := ctr+1)
    tbl[ctr] := lcase(remove_unicode3_accents (wchr1(ctr)))[0];
  ccname := complete_collation_name ('BASECHAR_LCASE', 1);
  __collation_define_memonly (ccname, tbl);
  insert replacing DB.DBA.SYS_COLLATIONS (COLL_NAME, COLL_TABLE, COLL_WIDE) values (ccname, cast (tbl as varbinary), 1);
  commit work;
  log_text ('__collation_define_memonly (?,?)', ccname, tbl);
-- Remove accents, then ucase
  tbl := make_wstring (65536, wchr1(1));
  for (ctr := 1; ctr < 65536; ctr := ctr+1)
    tbl[ctr] := ucase(remove_unicode3_accents (wchr1(ctr)))[0];
  ccname := complete_collation_name ('BASECHAR_UCASE', 1);
  __collation_define_memonly (ccname, tbl);
  insert replacing DB.DBA.SYS_COLLATIONS (COLL_NAME, COLL_TABLE, COLL_WIDE) values (ccname, cast (tbl as varbinary), 1);
  commit work;
  log_text ('__collation_define_memonly (?,?)', ccname, tbl);
}
;
