diff options
| author | Peter Eisentraut | 2022-03-17 10:11:21 +0000 |
|---|---|---|
| committer | Peter Eisentraut | 2022-03-17 10:13:16 +0000 |
| commit | f2553d43060edb210b36c63187d52a632448e1d2 (patch) | |
| tree | 7c75fca6ad45c4c25f8b2c6d3d51fa871d2c72d8 /src/bin/scripts | |
| parent | f6f0db4d62400ff88f523dcc4d7e25f9506bc0d8 (diff) | |
Add option to use ICU as global locale provider
This adds the option to use ICU as the default locale provider for
either the whole cluster or a database. New options for initdb,
createdb, and CREATE DATABASE are used to select this.
Since some (legacy) code still uses the libc locale facilities
directly, we still need to set the libc global locale settings even if
ICU is otherwise selected. So pg_database now has three
locale-related fields: the existing datcollate and datctype, which are
always set, and a new daticulocale, which is only set if ICU is
selected. A similar change is made in pg_collation for consistency,
but in that case, only the libc-related fields or the ICU-related
field is set, never both.
Reviewed-by: Julien Rouhaud <rjuju123@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/5e756dd6-0e91-d778-96fd-b1bcb06c161a%402ndquadrant.com
Diffstat (limited to 'src/bin/scripts')
| -rw-r--r-- | src/bin/scripts/Makefile | 2 | ||||
| -rw-r--r-- | src/bin/scripts/createdb.c | 20 | ||||
| -rw-r--r-- | src/bin/scripts/t/020_createdb.pl | 28 |
3 files changed, 50 insertions, 0 deletions
diff --git a/src/bin/scripts/Makefile b/src/bin/scripts/Makefile index b833109da6e..25e7da3d3f4 100644 --- a/src/bin/scripts/Makefile +++ b/src/bin/scripts/Makefile @@ -53,6 +53,8 @@ clean distclean maintainer-clean: rm -f common.o $(WIN32RES) rm -rf tmp_check +export with_icu + check: $(prove_check) diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c index b0c6805bc97..6f612abf7c6 100644 --- a/src/bin/scripts/createdb.c +++ b/src/bin/scripts/createdb.c @@ -38,6 +38,8 @@ main(int argc, char *argv[]) {"lc-ctype", required_argument, NULL, 2}, {"locale", required_argument, NULL, 'l'}, {"maintenance-db", required_argument, NULL, 3}, + {"locale-provider", required_argument, NULL, 4}, + {"icu-locale", required_argument, NULL, 5}, {NULL, 0, NULL, 0} }; @@ -61,6 +63,8 @@ main(int argc, char *argv[]) char *lc_collate = NULL; char *lc_ctype = NULL; char *locale = NULL; + char *locale_provider = NULL; + char *icu_locale = NULL; PQExpBufferData sql; @@ -119,6 +123,12 @@ main(int argc, char *argv[]) case 3: maintenance_db = pg_strdup(optarg); break; + case 4: + locale_provider = pg_strdup(optarg); + break; + case 5: + icu_locale = pg_strdup(optarg); + break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -217,6 +227,13 @@ main(int argc, char *argv[]) appendPQExpBufferStr(&sql, " LC_CTYPE "); appendStringLiteralConn(&sql, lc_ctype, conn); } + if (locale_provider) + appendPQExpBuffer(&sql, " LOCALE_PROVIDER %s", locale_provider); + if (icu_locale) + { + appendPQExpBufferStr(&sql, " ICU_LOCALE "); + appendStringLiteralConn(&sql, icu_locale, conn); + } appendPQExpBufferChar(&sql, ';'); @@ -273,6 +290,9 @@ help(const char *progname) printf(_(" -l, --locale=LOCALE locale settings for the database\n")); printf(_(" --lc-collate=LOCALE LC_COLLATE setting for the database\n")); printf(_(" --lc-ctype=LOCALE LC_CTYPE setting for the database\n")); + printf(_(" --icu-locale=LOCALE ICU locale setting for the database\n")); + printf(_(" --locale-provider={libc|icu}\n" + " locale provider for the database's default collation\n")); printf(_(" -O, --owner=OWNER database user to own the new database\n")); printf(_(" -T, --template=TEMPLATE template database to copy\n")); printf(_(" -V, --version output version information, then exit\n")); diff --git a/src/bin/scripts/t/020_createdb.pl b/src/bin/scripts/t/020_createdb.pl index 639245466e8..35deec9a929 100644 --- a/src/bin/scripts/t/020_createdb.pl +++ b/src/bin/scripts/t/020_createdb.pl @@ -25,9 +25,37 @@ $node->issues_sql_like( qr/statement: CREATE DATABASE foobar2 ENCODING 'LATIN1'/, 'create database with encoding'); +if ($ENV{with_icu} eq 'yes') +{ + # This fails because template0 uses libc provider and has no ICU + # locale set. It would succeed if template0 used the icu + # provider. XXX Maybe split into multiple tests? + $node->command_fails( + [ 'createdb', '-T', 'template0', '--locale-provider=icu', 'foobar4' ], + 'create database with ICU fails without ICU locale specified'); + + $node->issues_sql_like( + [ 'createdb', '-T', 'template0', '--locale-provider=icu', '--icu-locale=en', 'foobar5' ], + qr/statement: CREATE DATABASE foobar5 .* LOCALE_PROVIDER icu ICU_LOCALE 'en'/, + 'create database with ICU locale specified'); + + $node->command_fails( + [ 'createdb', '-T', 'template0', '--locale-provider=icu', '--icu-locale=@colNumeric=lower', 'foobarX' ], + 'fails for invalid ICU locale'); +} +else +{ + $node->command_fails( + [ 'createdb', '-T', 'template0', '--locale-provider=icu', 'foobar4' ], + 'create database with ICU fails since no ICU support'); +} + $node->command_fails([ 'createdb', 'foobar1' ], 'fails if database already exists'); +$node->command_fails([ 'createdb', '-T', 'template0', '--locale-provider=xyz', 'foobarX' ], + 'fails for invalid locale provider'); + # Check use of templates with shared dependencies copied from the template. my ($ret, $stdout, $stderr) = $node->psql( 'foobar2', |
