MCPcopy
hub / github.com/django/django / ConcatPair

Class ConcatPair

django/db/models/functions/text.py:68–122  ·  view source on GitHub ↗

Concatenate two arguments together. This is used by `Concat` because not all backend databases support more than two arguments.

Source from the content-addressed store, hash-verified

66
67
68class ConcatPair(Func):
69 """
70 Concatenate two arguments together. This is used by `Concat` because not
71 all backend databases support more than two arguments.
72 """
73
74 function = "CONCAT"
75
76 def pipes_concat_sql(self, compiler, connection, **extra_context):
77 coalesced = self.coalesce()
78 return super(ConcatPair, coalesced).as_sql(
79 compiler,
80 connection,
81 template="(%(expressions)s)",
82 arg_joiner=" || ",
83 **extra_context,
84 )
85
86 as_sqlite = pipes_concat_sql
87
88 def as_postgresql(self, compiler, connection, **extra_context):
89 c = self.copy()
90 c.set_source_expressions(
91 [
92 (
93 expression
94 if isinstance(expression.output_field, (CharField, TextField))
95 else Cast(expression, TextField())
96 )
97 for expression in c.get_source_expressions()
98 ]
99 )
100 return c.pipes_concat_sql(compiler, connection, **extra_context)
101
102 def as_mysql(self, compiler, connection, **extra_context):
103 # Use CONCAT_WS with an empty separator so that NULLs are ignored.
104 return super().as_sql(
105 compiler,
106 connection,
107 function="CONCAT_WS",
108 template="%(function)s('', %(expressions)s)",
109 **extra_context,
110 )
111
112 def coalesce(self):
113 # null on either side results in null for expression, wrap with
114 # coalesce
115 c = self.copy()
116 c.set_source_expressions(
117 [
118 Coalesce(expression, Value(""))
119 for expression in c.get_source_expressions()
120 ]
121 )
122 return c
123
124
125class Concat(Func):

Callers 2

_pairedMethod · 0.85

Calls

no outgoing calls

Tested by 1