c\g9dZddlZddlmZejZGddZGddeZGdd eZ Gd d eZ Gd d eZ GddeZ e dZ e dZdS)zSQL composition utility module N) extensionsc<eZdZdZdZdZdZdZdZdZ dZ d S) Composablea6 Abstract base class for objects that can be used to compose an SQL string. `!Composable` objects can be passed directly to `~cursor.execute()`, `~cursor.executemany()`, `~cursor.copy_expert()` in place of the query string. `!Composable` objects can be joined using the ``+`` operator: the result will be a `Composed` instance containing the objects joined. The operator ``*`` is also supported with an integer argument: the result is a `!Composed` instance containing the left argument repeated as many times as requested. c||_dSN_wrapped)selfwrappeds X/var/lib/jenkins/jobs/Dev/workspace/my-venv/lib/python3.11/site-packages/psycopg2/sql.py__init__zComposable.__init__1s  c0|jjd|jdS)N()) __class____name__r r s r __repr__zComposable.__repr__4s .)>>DM>>>>rct)aj Return the string value of the object. :param context: the context to evaluate the string into. :type context: `connection` or `cursor` The method is automatically invoked by `~cursor.execute()`, `~cursor.executemany()`, `~cursor.copy_expert()` if a `!Composable` is passed instead of the query string. )NotImplementedErrorr contexts r as_stringzComposable.as_string7s "!rct|trt|g|zSt|tr!t|gt|gzStSr) isinstanceComposedrNotImplementedr others r __add__zComposable.__add__Ds` eX & & ,TF##e+ + eZ ( ( "TF##hw&7&77 7! !rc(t|g|zSr)r)r ns r __mul__zComposable.__mul__Ls ###rc^t|t|uo|j|jkSr)typer rs r __eq__zComposable.__eq__Os'DzzT%[[(LT]en-LLrc.|| Sr)r'rs r __ne__zComposable.__ne__Rs;;u%%%%rN) r __module__ __qualname____doc__r rrr!r$r'r)rr rr#s     ??? " " """"$$$MMM&&&&&rrcPeZdZdZfdZedZdZdZdZ dZ xZ S)ra A `Composable` object made of a sequence of `!Composable`. The object is usually created using `!Composable` operators and methods. However it is possible to create a `!Composed` directly specifying a sequence of `!Composable` as arguments. Example:: >>> comp = sql.Composed( ... [sql.SQL("insert into "), sql.Identifier("table")]) >>> print(comp.as_string(conn)) insert into "table" `!Composed` objects are iterable (so they can be used in `SQL.join` for instance). cg}|D]?}t|tstd|d||@t |dS)Nz*Composed elements must be Composable, got z instead)rr TypeErrorappendsuperr )r seqr irs r r zComposed.__init__hs  Aa,, PNNNNPPP NN1     !!!!!rc*t|jS)z+The list of the content of the `!Composed`.)listr rs r r3z Composed.seqrsDM"""rcg}|jD]*}|||+d|S)N)r r1rjoin)r rrvr4s r rzComposed.as_stringwsI  , ,A IIakk'** + + + +wwr{{rc*t|jSr)iterr rs r __iter__zComposed.__iter__}sDM"""rct|trt|j|jzSt|trt|j|gzStSr)rrr rrrs r r!zComposed.__add__sZ eX & & <DMEN:;; ; eZ ( ( "DMUG344 4! !rct|trt|}n$t|tstd||S)a| Return a new `!Composed` interposing the *joiner* with the `!Composed` items. The *joiner* must be a `SQL` or a string which will be interpreted as an `SQL`. Example:: >>> fields = sql.Identifier('foo') + sql.Identifier('bar') # a Composed >>> print(fields.join(', ').as_string(conn)) "foo", "bar" z3Composed.join() argument must be a string or an SQL)rstrSQLr0r9)r joiners r r9z Composed.joinsc fc " " G[[FFFC(( GEGG G{{4   r) rr*r+r,r propertyr3rr=r!r9 __classcell__rs@r rrVs""""""##X# ###"""!!!!!!!rrcJeZdZdZfdZedZdZdZdZ xZ S)rAaA A `Composable` representing a snippet of SQL statement. `!SQL` exposes `join()` and `format()` methods useful to create a template where to merge variable parts of a query (for instance field or table names). The *string* doesn't undergo any form of escaping, so it is not suitable to represent variable identifiers or values: you should only use it to pass constant strings representing templates or snippets of SQL statements; use other objects such as `Identifier` or `Literal` to represent variable parts. Example:: >>> query = sql.SQL("select {0} from {1}").format( ... sql.SQL(', ').join([sql.Identifier('foo'), sql.Identifier('bar')]), ... sql.Identifier('table')) >>> print(query.as_string(conn)) select "foo", "bar" from "table" ct|tstdt|dS)NzSQL values must be strings)rr@r0r2r )r stringrs r r z SQL.__init__sB&#&& :899 9      rc|jS)z(The string wrapped by the `!SQL` object.rrs r rHz SQL.string }rc|jSrrrs r rz SQL.as_strings }rcFg}d}t|jD]\}}}}|rtd|rtd|r"|t ||P|r<|rtd||t|d}|s2|td||||dz }|||t|S)a^ Merge `Composable` objects into a template. :param `Composable` args: parameters to replace to numbered (``{0}``, ``{1}``) or auto-numbered (``{}``) placeholders :param `Composable` kwargs: parameters to replace to named (``{name}``) placeholders :return: the union of the `!SQL` string with placeholders replaced :rtype: `Composed` The method is similar to the Python `str.format()` method: the string template supports auto-numbered (``{}``), numbered (``{0}``, ``{1}``...), and named placeholders (``{name}``), with positional arguments replacing the numbered placeholders and keywords replacing the named ones. However placeholder modifiers (``{0!r}``, ``{0:<10}``) are not supported. Only `!Composable` objects can be passed to the template. Example:: >>> print(sql.SQL("select * from {} where {} = %s") ... .format(sql.Identifier('people'), sql.Identifier('id')) ... .as_string(conn)) select * from "people" where "id" = %s >>> print(sql.SQL("select * from {tbl} where {pkey} = %s") ... .format(tbl=sql.Identifier('people'), pkey=sql.Identifier('id')) ... .as_string(conn)) select * from "people" where "id" = %s rz(no format specification supported by SQLz%no format conversion supported by SQLNz6cannot switch from automatic field numbering to manualz6cannot switch from manual field numbering to automatic) _formatterparser ValueErrorr1rAisdigitintr) r argskwargsr:autonumprenamespecconvs r formatz SQL.formatsP@%/%5%5dm%D%D ( ( !CtT M !KLLL J !HIII $ #c((###|||~~ (R$PRRR $s4yy/*** (?$PRRR $w-(((1  &,''''||rcg}t|} |t||D],}||||-n#t$rYnwxYwt |S)a Join a sequence of `Composable`. :param seq: the elements to join. :type seq: iterable of `!Composable` Use the `!SQL` object's *string* to separate the elements in *seq*. Note that `Composed` objects are iterable too, so they can be used as argument for this method. Example:: >>> snip = sql.SQL(', ').join( ... sql.Identifier(n) for n in ['foo', 'bar', 'baz']) >>> print(snip.as_string(conn)) "foo", "bar", "baz" )r<r1next StopIterationr)r r3r:itr4s r r9zSQL.joins$ #YY  IId2hh      $ !      D ||s"A%% A21A2) rr*r+r,r rCrHrrZr9rDrEs@r rArAs*!!!!! X>>>@rrAcZeZdZdZfdZedZedZdZdZ xZ S) Identifiera* A `Composable` representing an SQL identifier or a dot-separated sequence. Identifiers usually represent names of database objects, such as tables or fields. PostgreSQL identifiers follow `different rules`__ than SQL string literals for escaping (e.g. they use double quotes instead of single). .. __: https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html# SQL-SYNTAX-IDENTIFIERS Example:: >>> t1 = sql.Identifier("foo") >>> t2 = sql.Identifier("ba'r") >>> t3 = sql.Identifier('ba"z') >>> print(sql.SQL(', ').join([t1, t2, t3]).as_string(conn)) "foo", "ba'r", "ba""z" Multiple strings can be passed to the object to represent a qualified name, i.e. a dot-separated sequence of identifiers. Example:: >>> query = sql.SQL("select {} from {}").format( ... sql.Identifier("table", "field"), ... sql.Identifier("schema", "table")) >>> print(query.as_string(conn)) select "table"."field" from "schema"."table" c|std|D]&}t|tstd't|dS)NzIdentifier cannot be emptyz$SQL identifier parts must be strings)r0rr@r2r )r stringssrs r r zIdentifier.__init__Ass :899 9 H HAa%% H FGGG H !!!!!rc|jS)z5A tuple with the strings wrapped by the `Identifier`.rrs r rbzIdentifier.stringsKrJrcjt|jdkr |jdStd)z0The string wrapped by the `Identifier`. rMrz2the Identifier wraps more than one than one string)lenr AttributeErrorrs r rHzIdentifier.stringPs> t}   " "=# # DFF Frc||jjddtt|jdS)Nrz, r)rrr9mapreprr rs r rzIdentifier.__repr__Zs6.)RRDIIc$ 6N6N,O,ORRRRrcPdfd|jDS)N.c3BK|]}tj|VdSr)ext quote_ident).0rcrs r z'Identifier.as_string..^s/KK733KKKKKKr)r9r rs `r rzIdentifier.as_string]s,xxKKKKT]KKKKKKr) rr*r+r,r rCrbrHrrrDrEs@r r`r`"s<"""""XFFXFSSSLLLLLLLrr`c.eZdZdZedZdZdS)Literala A `Composable` representing an SQL value to include in a query. Usually you will want to include placeholders in the query and pass values as `~cursor.execute()` arguments. If however you really really need to include a literal value in the query you can use this object. The string returned by `!as_string()` follows the normal :ref:`adaptation rules ` for Python objects. Example:: >>> s1 = sql.Literal("foo") >>> s2 = sql.Literal("ba'r") >>> s3 = sql.Literal(42) >>> print(sql.SQL(', ').join([s1, s2, s3]).as_string(conn)) 'foo', 'ba''r', 42 c|jS)z%The object wrapped by the `!Literal`.rrs r r zLiteral.wrappedurJrct|tjr|}n1t|tjr|j}nt dtj|j}t|dr||| }t|tr*| tj |j }|S)Nz(context must be a connection or a cursorprepare)rrn connectioncursorr0adaptr hasattrrv getquotedbytesdecode encodingsencoding)r rconnar:s r rzLiteral.as_stringzs gs~ . . HDD  , , H%DDFGG G Idm $ $ 1i   IIdOOO [[]] b%  93=788B rN)rr*r+r,rCr rr-rr rsrsasH&XrrscFeZdZdZdfd ZedZdZdZxZ S) PlaceholderaA `Composable` representing a placeholder for query parameters. If the name is specified, generate a named placeholder (e.g. ``%(name)s``), otherwise generate a positional placeholder (e.g. ``%s``). The object is useful to generate SQL queries with a variable number of arguments. Examples:: >>> names = ['foo', 'bar', 'baz'] >>> q1 = sql.SQL("insert into table ({}) values ({})").format( ... sql.SQL(', ').join(map(sql.Identifier, names)), ... sql.SQL(', ').join(sql.Placeholder() * len(names))) >>> print(q1.as_string(conn)) insert into table ("foo", "bar", "baz") values (%s, %s, %s) >>> q2 = sql.SQL("insert into table ({}) values ({})").format( ... sql.SQL(', ').join(map(sql.Identifier, names)), ... sql.SQL(', ').join(map(sql.Placeholder, names))) >>> print(q2.as_string(conn)) insert into table ("foo", "bar", "baz") values (%(foo)s, %(bar)s, %(baz)s) Nct|trd|vrtd|n|td|t |dS)Nrzinvalid name: z%expected string or None as name, got )rr@rPr0r2r )r rWrs r r zPlaceholder.__init__sx dC  Nd{{ !:$!:!:;;; LDLLMM M rc|jS)zThe name of the `!Placeholder`.rrs r rWzPlaceholder.namerJrc\|j|jjdS|jjd|jdS)Nz()rr)r rrrs r rzPlaceholder.__repr__s< = n-111 1n-BB BBB Brc*|j d|jdSdS)Nz%(z)sz%srrs r rzPlaceholder.as_strings" = $) ))) )4rr) rr*r+r,r rCrWrrrDrEs@r rrs4XCCC rrNULLDEFAULT)r,rHpsycopg2rrn FormatterrNrrrAr`rsrrrr-rr rsv4 &&&&&&V    0&0&0&0&0&0&0&0&fF!F!F!F!F!zF!F!F!R@@@@@*@@@F<L<L<L<L<L<L<L<L~*****j***Z44444*444p s6{{ #i..r