Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
bugzilla
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
etersoft
bugzilla
Commits
665c59e3
Commit
665c59e3
authored
Apr 09, 2015
by
Simon Green
Committed by
Frédéric Buclin
Apr 09, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bug 1138417: sql_group_concat() generates bad SQL code with PostgreSQL 8.x
r=LpSolit a=glob
parent
fe7e0d01
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
51 deletions
+86
-51
Pg.pm
Bugzilla/DB/Pg.pm
+86
-51
No files found.
Bugzilla/DB/Pg.pm
View file @
665c59e3
...
@@ -85,6 +85,15 @@ sub sql_group_concat {
...
@@ -85,6 +85,15 @@ sub sql_group_concat {
$sort
=
1
if
!
defined
$sort
;
$sort
=
1
if
!
defined
$sort
;
$separator
=
$self
->
quote
(
', '
)
if
!
defined
$separator
;
$separator
=
$self
->
quote
(
', '
)
if
!
defined
$separator
;
# PostgreSQL 8.x doesn't support STRING_AGG
if
(
vers_cmp
(
$self
->
bz_server_version
,
9
)
<
0
)
{
my
$sql
=
"ARRAY_ACCUM($text)"
;
if
(
$sort
)
{
$sql
=
"ARRAY_SORT($sql)"
;
}
return
"ARRAY_TO_STRING($sql, $separator)"
;
}
if
(
$order_by
&&
$text
=~
/^DISTINCT\s*(.+)$/i
)
{
if
(
$order_by
&&
$text
=~
/^DISTINCT\s*(.+)$/i
)
{
# Since Postgres (quite rightly) doesn't support "SELECT DISTINCT x
# Since Postgres (quite rightly) doesn't support "SELECT DISTINCT x
# ORDER BY y", we need to sort the list, and then get the unique
# ORDER BY y", we need to sort the list, and then get the unique
...
@@ -102,11 +111,6 @@ sub sql_group_concat {
...
@@ -102,11 +111,6 @@ sub sql_group_concat {
$order_by
=
" ORDER BY $1"
;
$order_by
=
" ORDER BY $1"
;
}
}
if
(
vers_cmp
(
$self
->
bz_server_version
,
9
)
<
0
)
{
# PostgreSQL 8.x doesn't support STRING_AGG
return
"ARRAY_TO_STRING(ARRAY_AGG($text$order_by), $separator)"
;
}
return
"STRING_AGG(${text}::text, $separator${order_by}::text)"
return
"STRING_AGG(${text}::text, $separator${order_by}::text)"
}
}
...
@@ -234,54 +238,85 @@ sub bz_setup_database {
...
@@ -234,54 +238,85 @@ sub bz_setup_database {
my
(
$has_plpgsql
)
=
$self
->
selectrow_array
(
"SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'"
);
my
(
$has_plpgsql
)
=
$self
->
selectrow_array
(
"SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'"
);
$self
->
do
(
'CREATE LANGUAGE plpgsql'
)
unless
$has_plpgsql
;
$self
->
do
(
'CREATE LANGUAGE plpgsql'
)
unless
$has_plpgsql
;
# Custom Functions
if
(
vers_cmp
(
$self
->
bz_server_version
,
9
)
<
0
)
{
# Custom Functions for Postgres 8
# -Copyright © 2013 Joshua D. Burns (JDBurnZ) and Message In Action LLC
my
$function
=
'array_accum'
;
# JDBurnZ: https://github.com/JDBurnZ
my
$array_accum
=
$self
->
selectrow_array
(
# Message In Action: https://www.messageinaction.com
'SELECT 1 FROM pg_proc WHERE proname = ?'
,
undef
,
$function
);
#
if
(
!
$array_accum
)
{
#Permission is hereby granted, free of charge, to any person obtaining a copy of
print
"Creating function $function...\n"
;
#this software and associated documentation files (the "Software"), to deal in
$self
->
do
(
"CREATE AGGREGATE array_accum (
#the Software without restriction, including without limitation the rights to
SFUNC = array_append,
#use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
BASETYPE = anyelement,
#the Software, and to permit persons to whom the Software is furnished to do so,
STYPE = anyarray,
#subject to the following conditions:
INITCOND = '{}'
#
)"
);
#The above copyright notice and this permission notice shall be included in all
}
#copies or substantial portions of the Software.
#
$self
->
do
(
<<
'END'
);
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
CREATE
OR
REPLACE
FUNCTION
array_sort
(
ANYARRAY
)
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
RETURNS
ANYARRAY
LANGUAGE
SQL
#FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
IMMUTABLE
STRICT
#COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
AS
$$
#IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
SELECT
ARRAY
(
#CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SELECT
$1
[
s
.
i
]
AS
each_item
$self
->
do
(
q|
FROM
DROP FUNCTION IF EXISTS anyarray_uniq(anyarray);
generate_series
(
array_lower
(
$1
,
1
),
array_upper
(
$1
,
1
))
AS
s(i)
CREATE OR REPLACE FUNCTION anyarray_uniq(with_array anyarray)
ORDER BY each_item
RETURNS anyarray AS $BODY$
);
DECLARE
$$
;
-- The variable used to track iteration over "with_array".
END
loop_offset integer;
}
else
{
-- The array to be returned by this function.
# Custom functions for Postgres 9.0+
return_array with_array%TYPE := '{}';
BEGIN
# -Copyright © 2013 Joshua D. Burns (JDBurnZ) and Message In Action LLC
IF with_array IS NULL THEN
# JDBurnZ: https://github.com/JDBurnZ
return NULL;
# Message In Action: https://www.messageinaction.com
END IF;
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of
-- Iterate over each element in "concat_array".
#this software and associated documentation files (the "Software"), to deal in
FOR loop_offset IN ARRAY_LOWER(with_array, 1)..ARRAY_UPPER(with_array, 1) LOOP
#the Software without restriction, including without limitation the rights to
IF NOT with_array[loop_offset] = ANY(return_array) THEN
#use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
return_array = ARRAY_APPEND(return_array, with_array[loop_offset]);
#the Software, and to permit persons to whom the Software is furnished to do so,
#subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
#FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
#COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
#IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
#CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
$self
->
do
(
q|
DROP FUNCTION IF EXISTS anyarray_uniq(anyarray);
CREATE OR REPLACE FUNCTION anyarray_uniq(with_array anyarray)
RETURNS anyarray AS $BODY$
DECLARE
-- The variable used to track iteration over "with_array".
loop_offset integer;
-- The array to be returned by this function.
return_array with_array%TYPE := '{}';
BEGIN
IF with_array IS NULL THEN
return NULL;
END IF;
END IF;
END LOOP;
RETURN return_array;
-- Iterate over each element in "concat_array".
END;
FOR loop_offset IN ARRAY_LOWER(with_array, 1)..ARRAY_UPPER(with_array, 1) LOOP
$BODY$ LANGUAGE plpgsql;
IF NOT with_array[loop_offset] = ANY(return_array) THEN
|
);
return_array = ARRAY_APPEND(return_array, with_array[loop_offset]);
END IF;
END LOOP;
RETURN return_array;
END;
$BODY$ LANGUAGE plpgsql;
|
);
}
# PostgreSQL doesn't like having *any* index on the thetext
# PostgreSQL doesn't like having *any* index on the thetext
# field, because it can't have index data longer than 2770
# field, because it can't have index data longer than 2770
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment