L i][dZddlmZddlmZddlZddlZddlmZddlm Z ddl m Z dd l m Z dd l mZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddl mZddl mZddlmZddlmZddl m!Z!ddl m"Z"e r ddlm#Z#ddl$m%Z%ejLdZ'GddejPZ)Gdd eZ*Gd!d"eZ+Gd#d$eZ,Gd%d&ej*jZZ.Gd'd(ej*j^Z0Gd)d*eZ1Gd+d,eZ2Gd-d.ejfZ4Gd/d0ejjZ6Gd1d2ejnZ8Gd3d4ejrZ:Gd5d6ejvZ<Gd7d8ejzZ>Gd9d:ej~Z@Gd;de jZDGd?d@e jZFGdAdBeZGGdCdDeZHGdEdFeZIdGZJGdHdIe ZKGdJdKZLGdLdMeLZMGdNdOeZNGdPdQeNZOGdRdSZPGdTdUeKZQeKZReQZSy)Vau .. dialect:: postgresql+psycopg :name: psycopg (a.k.a. psycopg 3) :dbapi: psycopg :connectstring: postgresql+psycopg://user:password@host:port/dbname[?key=value&key=value...] :url: https://pypi.org/project/psycopg/ ``psycopg`` is the package and module name for version 3 of the ``psycopg`` database driver, formerly known as ``psycopg2``. This driver is different enough from its ``psycopg2`` predecessor that SQLAlchemy supports it via a totally separate dialect; support for ``psycopg2`` is expected to remain for as long as that package continues to function for modern Python versions, and also remains the default dialect for the ``postgresql://`` dialect series. The SQLAlchemy ``psycopg`` dialect provides both a sync and an async implementation under the same dialect name. The proper version is selected depending on how the engine is created: * calling :func:`_sa.create_engine` with ``postgresql+psycopg://...`` will automatically select the sync version, e.g.:: from sqlalchemy import create_engine sync_engine = create_engine( "postgresql+psycopg://scott:tiger@localhost/test" ) * calling :func:`_asyncio.create_async_engine` with ``postgresql+psycopg://...`` will automatically select the async version, e.g.:: from sqlalchemy.ext.asyncio import create_async_engine asyncio_engine = create_async_engine( "postgresql+psycopg://scott:tiger@localhost/test" ) The asyncio version of the dialect may also be specified explicitly using the ``psycopg_async`` suffix, as:: from sqlalchemy.ext.asyncio import create_async_engine asyncio_engine = create_async_engine( "postgresql+psycopg_async://scott:tiger@localhost/test" ) .. seealso:: :ref:`postgresql_psycopg2` - The SQLAlchemy ``psycopg`` dialect shares most of its behavior with the ``psycopg2`` dialect. Further documentation is available there. Using a different Cursor class ------------------------------ One of the differences between ``psycopg`` and the older ``psycopg2`` is how bound parameters are handled: ``psycopg2`` would bind them client side, while ``psycopg`` by default will bind them server side. It's possible to configure ``psycopg`` to do client side binding by specifying the ``cursor_factory`` to be ``ClientCursor`` when creating the engine:: from psycopg import ClientCursor client_side_engine = create_engine( "postgresql+psycopg://...", connect_args={"cursor_factory": ClientCursor}, ) Similarly when using an async engine the ``AsyncClientCursor`` can be specified:: from psycopg import AsyncClientCursor client_side_engine = create_async_engine( "postgresql+psycopg://...", connect_args={"cursor_factory": AsyncClientCursor}, ) .. seealso:: `Client-side-binding cursors `_ ) annotations)dequeN)cast) TYPE_CHECKING)ranges)_PGDialect_common_psycopg)"_PGExecutionContext_common_psycopg)INTERVAL) PGCompiler)PGIdentifierPreparer) REGCONFIG)JSON)JSONB) JSONPathType)CITEXT)pool)util)AdaptedConnection)sqltypes)await_fallback) await_only)Iterable)AsyncConnectionzsqlalchemy.dialects.postgresqlceZdZdZy) _PGStringTN__name__ __module__ __qualname__render_bind_castl/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/psycopg.pyrrr$rceZdZdZy) _PGREGCONFIGTNrr#r$r%r(r(r&r$r(ceZdZdZdZy)_PGJSONc:|jd|jSN)_make_bind_processor _psycopg_Jsonselfdialects r%bind_processorz_PGJSON.bind_processors((w/D/DEEr$cyr,r#r0r1coltypes r%result_processorz_PGJSON.result_processorr$Nrr r!r2r6r#r$r%r*r*s Fr$r*ceZdZdZdZy)_PGJSONBc:|jd|jSr,)r-_psycopg_Jsonbr/s r%r2z_PGJSONB.bind_processors((w/E/EFFr$cyr,r#r4s r%r6z_PGJSONB.result_processorr7r$Nr8r#r$r%r:r:s Gr$r:ceZdZdZdZy)_PGJSONIntIndexTypejson_int_indexTNrr r!__visit_name__r"r#r$r%r?r? %Nr$r?ceZdZdZdZy)_PGJSONStrIndexTypejson_str_indexTNrAr#r$r%rErErCr$rEc eZdZy)_PGJSONPathTypeNrr r!r#r$r%rHrHr$rHceZdZdZy) _PGIntervalTNrr#r$r%rLrLr&r$rLceZdZdZy) _PGTimeStampTNrr#r$r%rNrNr&r$rNceZdZdZy)_PGDateTNrr#r$r%rPrPr&r$rPceZdZdZy)_PGTimeTNrr#r$r%rRrRr&r$rRceZdZdZy) _PGIntegerTNrr#r$r%rTrTr&r$rTceZdZdZy)_PGSmallIntegerTNrr#r$r%rVrVr&r$rVceZdZdZy) _PGNullTypeTNrr#r$r%rXrXr&r$rXceZdZdZy) _PGBigIntegerTNrr#r$r%rZrZr&r$rZceZdZdZy) _PGBooleanTNrr#r$r%r\r\r&r$r\ceZdZdZdZy) _PsycopgRangecFtt|jfd}|S)Nct|tjr3|j|j|j |j }|Sr,) isinstancerRangelowerupperboundsempty)value psycopg_Ranges r%to_rangez._PsycopgRange.bind_processor..to_ranges;%.%KKellEKKLr$)rPGDialect_psycopg_psycopg_Range)r0r1rirhs @r%r2z_PsycopgRange.bind_processors".8GG  r$c d}|S)Nc|Ptj|j|j|jr |jnd|j }|S)N[)rerfrrb_lower_upper_boundsrgs r%riz0_PsycopgRange.result_processor..to_rangesD  LLLL,1MM5==t#mm+  Lr$r#r0r1r5ris r%r6z_PsycopgRange.result_processors r$Nr8r#r$r%r^r^s   r$r^ceZdZdZdZy)_PsycopgMultiRangectt|jtt|jt dfd}|S)Nc t|tfr|Std|Dcgc]5}|j|j|j |j 7c}Scc}w)NzIterable[ranges.Range])rastrrrcrdrerf)rgelementNoneTypepsycopg_Multirangerhs r%riz3_PsycopgMultiRange.bind_processor..to_rangeso%#x1C!DE %$((@%#H "     s:A()rrjrk_psycopg_Multirangetype)r0r1rir|r}rhs @@@r%r2z!_PsycopgMultiRange.bind_processorsE.8GG ! w   :  r$c d}|S)Nc@|ytjd|DS)Nc3K|]T}tj|j|j|jr |jnd|j Vyw)rnroNrp).0elems r% zH_PsycopgMultiRange.result_processor..to_range..sO) LL  /3||t||"&,,. )sAA)r MultiRangerts r%riz5_PsycopgMultiRange.result_processor..to_ranges,}(()!&)r$r#rus r%r6z#_PsycopgMultiRange.result_processors r$Nr8r#r$r%rwrws 4r$rwc eZdZy)PGExecutionContext_psycopgNrIr#r$r%rrrJr$rc eZdZy)PGCompiler_psycopgNrIr#r$r%rrrJr$rc eZdZy)PGIdentifierPreparer_psycopgNrIr#r$r%rrrJr$rcZtjd|j|jy)Nz%s: %s)loggerinfoseveritymessage_primary) diagnostics r% _log_noticesr!s KK*--z/I/IJr$ceZdZdZdZdZdZdZeZ e Z e Z dZdZdZej$ej(iej,eeeeeeeej4eeeej4j>e ej4jBe"ej4jFe$ejJe&e'e&ejPe)ejTe+ejXe-ej\e/ej`e1ejde3e4jje6e4jne8iZfdZ9fdZ:dZ;fd Ze=d Z?ejd ZAejd ZBejdZCejdZDejdZEejdZFdZGfdZHdZIdZJdZKdZLdZMddZN ddZO ddZPejdZQxZRS)rjpsycopgTpyformat)rrNc t|di||jrUtjd|jj }|r(t d|jdddD|_|jdkr tddd l m }||jjx|_ }|jd urdddl}|j!d |j"j$j&|j!d |j"j$j&|j(rdd lm}||j(||j.rddlm}||j.|yyy)Nz(\d+)\.(\d+)(?:\.(\d+))?c38K|]}|t|ywr,)int)rxs r%rz-PGDialect_psycopg.__init__..Ts-  CF-srr)rrrz,psycopg version 3.0.2 or higher is required.r) AdaptersMapFinetcidr)set_json_loads)set_json_dumpsr#)super__init__dbapirematch __version__tuplegrouppsycopg_version ImportError psycopg.adaptradapters_psycopg_adapters_map_native_inet_typespsycopg.types.stringregister_loadertypesstring TextLoader_json_deserializerpsycopg.types.jsonr_json_serializerr) r0kwargsmr adapters_maprrr __class__s r%rzPGDialect_psycopg.__init__NsE "6" ::4djj6L6LMA',-$%GGAq!$4-($##i/!B 28C ##9 D &&&%/+,,GMM00;;,,GMM00;;&&=t66 E$$=t44lC%C r$ct||\}}|jr|j|d<|j|j|d<||fS)Ncontextclient_encoding)rcreate_connect_argsrr)r0urlcargscparamsrs r%rz%PGDialect_psycopg.create_connect_argswsV4S9w  % %!%!;!;GI     +)-)=)=G% &g~r$cZddlm}|j|jj|SNr)TypeInfo) psycopg.typesrfetch connectiondriver_connection)r0rnamers r%_type_info_fetchz"PGDialect_psycopg._type_info_fetchs"*~~j33EEtLLr$cXt|||jsd|_|jr{|j |d}|du|_|j rSddlm}|jsJ|||j|jsJ|||jjyyy)NFhstorer)register_hstore) r initializeinsert_returninginsert_executemany_returninguse_native_hstorer_has_native_hstorepsycopg.types.hstorerrrr)r0rrrrs r%rzPGDialect_psycopg.initializes :&$$05D -  ! !((X>D&*$&6D #&&@1111d&@&@A",,,,j&;&;&M&MN' "r$cddl}|S)Nr)r)clsrs r% import_dbapizPGDialect_psycopg.import_dbapis r$ctSr,)PGDialectAsync_psycopg)rrs r%get_async_dialect_clsz'PGDialect_psycopg.get_async_dialect_clss%%r$c|jjj|jjj|jjj|jjj dS)N)zREAD COMMITTEDzREAD UNCOMMITTEDzREPEATABLE READ SERIALIZABLE)rIsolationLevelREAD_COMMITTEDREAD_UNCOMMITTEDREPEATABLE_READrr0s r%_isolation_lookupz#PGDialect_psycopg._isolation_lookupsZ#jj77FF $ 9 9 J J#zz88HH JJ55BB   r$c&ddlm}|jSNr)json)rrJsonr0rs r%r.zPGDialect_psycopg._psycopg_Jsons&yyr$c&ddlm}|jSr)rrJsonbrs r%r<z PGDialect_psycopg._psycopg_Jsonbs&zzr$cddlm}|S)Nr)TransactionStatus) psycopg.pqr)r0rs r%_psycopg_TransactionStatusz,PGDialect_psycopg._psycopg_TransactionStatuss 0  r$cddlm}|S)Nr)rb)psycopg.types.rangerb)r0rbs r%rkz PGDialect_psycopg._psycopg_Ranges - r$cddlm}|S)Nr) Multirange)psycopg.types.multiranger)r0rs r%r~z%PGDialect_psycopg._psycopg_Multiranges 7r$c ||_||_yr, autocommitisolation_levelr0rrrs r%_do_isolation_levelz%PGDialect_psycopg._do_isolation_levels * %4 "r$c|jj}t| |}||jj k(r|j |Sr,)rtransaction_statusrget_isolation_levelrIDLErollback)r0dbapi_connection status_beforergrs r%rz%PGDialect_psycopg.get_isolation_levelsK(--@@ +,<= D;;@@ @  % % ' r$cz|dk(r|j|ddy|j|d|j|y)N AUTOCOMMITTrF)rr)r0rlevels r%set_isolation_levelz%PGDialect_psycopg.set_isolation_levelsM L  $ $ T4 %   $ $  $ 6 6u = % r$c||_yr, read_onlyr0rrgs r% set_readonlyzPGDialect_psycopg.set_readonlys $ r$c|jSr,rr0rs r% get_readonlyzPGDialect_psycopg.get_readonlys###r$cdd}|gjfd}j|fd}|S)Nc.|jtyr,)add_notice_handlerr)conns r%noticesz-PGDialect_psycopg.on_connect..noticess  # #L 1r$c>j|jyr,)rr)rr0s r% on_connectz0PGDialect_psycopg.on_connect..on_connects((t/C/CDr$c$D] }|| yr,r#)rfnfnss r%rz0PGDialect_psycopg.on_connect..on_connects 4 r$)rappend)r0rrrs` @r%rzPGDialect_psycopg.on_connects> 2i    + E JJz " r$czt||jjr||js |jryy)NTF)rarErrorclosedbroken)r0ercursors r% is_disconnectzPGDialect_psycopg.is_disconnects2 a)) *z/E  J$5$5r$cp|jj}|s-|jj|jj k7r|j |j} |s|j|d|j||s|j||yy#|s|j||wwxYw)NT) rrrrrrrr_do_autocommitexecute)r0rcommandrecover dbapi_connbefore_autocommits r%_do_prepared_twophasez'PGDialect_psycopg._do_prepared_twophase s**;; 11..334    !&11 C$##J5   w '$##J0AB%$##J0AB%s #%BB5cp|r|j|d|d|y|j|jy)NzROLLBACK PREPARED ''r!)r$ do_rollbackrr0rxid is_preparedr!s r%do_rollback_twophasez&PGDialect_psycopg.do_rollback_twophasesA   & &1#a8' '    Z22 3r$cp|r|j|d|d|y|j|jy)NzCOMMIT PREPARED 'r&r')r$ do_commitrr)s r%do_commit_twophasez$PGDialect_psycopg.do_commit_twophase(s?   & &/uA6 '  NN:00 1r$cy)N;r#rs r%_dialect_specific_select_onez.PGDialect_psycopg._dialect_specific_select_one2sr$)F)TF)Srr r!driversupports_statement_cachesupports_server_side_cursorsdefault_paramstylesupports_sane_multi_rowcountrexecution_ctx_clsrstatement_compilerrpreparerrrrr update_copyr colspecsrStringrrr(rr*rrr:rrHJSONIntIndexTyper?JSONStrIndexTyperEIntervalrLr DaterPDateTimerNTimerRIntegerrT SmallIntegerrV BigIntegerrZrAbstractSingleRanger^AbstractMultiRangerwrrrr classmethodrrmemoized_propertyrr.r<rrkr~rrrrr rrr$r,r/r2 __classcell__)rs@r%rjrj%s F##' ##' 2++HO t!** OOY |  '  F  MM7  8   MM & &  MM * *,?  MM * *,?    {  k  MM7    |  MM7    j  ! !?! "   # $  & &  % %'9' H2'DRM O4 &&        !!     5 %$( C(:?4:?2 r$rjceZdZdZdZddZdZedZejdZddZ dZ dd Z d Z d Zd Zdd ZdZy)AsyncAdapt_psycopg_cursor)_cursorawait__rowsNc>||_||_t|_yr,)rNrOrrP)r0rrOs r%rz"AsyncAdapt_psycopg_cursor.__init__<s  W r$c.t|j|Sr,)getattrrNr0rs r% __getattr__z%AsyncAdapt_psycopg_cursor.__getattr__Ast||T**r$c.|jjSr,rN arraysizers r%rXz#AsyncAdapt_psycopg_cursor.arraysizeDs||%%%r$c&||j_yr,rWr0rgs r%rXz#AsyncAdapt_psycopg_cursor.arraysizeHs!& r$c Kywr,r#rs r%_async_soft_closez+AsyncAdapt_psycopg_cursor._async_soft_closeLs scl|jj|jjyr,)rPclearrN_closers r%closezAsyncAdapt_psycopg_cursor.closeOs"  r$c H|j|jj||fi|}|jj}|r\|j|j j k(r9|j|jj}t||_ |Sr,) rOrNrpgresultstatus_psycopg_ExecStatus TUPLES_OKfetchallrrP)r0queryparamskwresultresrowss r%rz!AsyncAdapt_psycopg_cursor.executeTs1T\\11%F2FGll## 3::!9!9!C!CC;;t||4467DtDJ r$cX|j|jj||Sr,)rOrN executemany)r0rg params_seqs r%rnz%AsyncAdapt_psycopg_cursor.executemany`s"{{4<<33E:FGGr$c#xK|jr*|jj|jr)yywr,rPpopleftrs r%__iter__z"AsyncAdapt_psycopg_cursor.__iter__cs)jj**$$& &jjs5::cP|jr|jjSyr,rqrs r%fetchonez"AsyncAdapt_psycopg_cursor.fetchonegs ::::%%' 'r$c||jj}|j}tt |t |Dcgc]}|j c}Scc}wr,)rNrXrPrangeminlenrr)r0sizerr_s r% fetchmanyz#AsyncAdapt_psycopg_cursor.fetchmanymsI <<<))D ZZ&+Cc"g,>&?@ @@@sAcdt|j}|jj|Sr,)listrPr^)r0retvals r%rfz"AsyncAdapt_psycopg_cursor.fetchallts%djj!  r$returnNoner,)rr r! __slots__rdrrUpropertyrXsetterr\r`rrnrsrur}rfr#r$r%rMrM7so.I +&&'' H' Ar$rMc4eZdZddZdZdZd dZdZdZy) AsyncAdapt_psycopg_ss_cursorNc `|j|jj||fi||Sr,)rOrNr)r0rgrhris r%rz$AsyncAdapt_psycopg_ss_cursor.execute{s, (DLL((="=> r$cV|j|jjyr,)rOrNr`rs r%r`z"AsyncAdapt_psycopg_ss_cursor.closes DLL&&()r$cT|j|jjSr,)rOrNrurs r%ruz%AsyncAdapt_psycopg_ss_cursor.fetchone{{4<<00233r$cV|j|jj|Sr,)rOrNr})r0rzs r%r}z&AsyncAdapt_psycopg_ss_cursor.fetchmanys {{4<<11$788r$cT|j|jjSr,)rOrNrfrs r%rfz%AsyncAdapt_psycopg_ss_cursor.fetchallrr$c#K|jj} |j|j##t$rYywxYwwr,)rN __aiter__rO __anext__StopAsyncIteration)r0iterators r%rsz%AsyncAdapt_psycopg_ss_cursor.__iter__sP<<))+ kk("4"4"677&  s(A!AA A  A A  Ar,)r) rr r!rr`rur}rfrsr#r$r%rrzs *494r$rceZdZUded<dZeeZddZdZ ddZ dZ d Z d Z d Zed Zej"d ZdZdZdZdZy)AsyncAdapt_psycopg_connectionr _connectionr#c||_yr,rr s r%rz&AsyncAdapt_psycopg_connection.__init__s %r$c.t|j|Sr,)rSrrTs r%rUz)AsyncAdapt_psycopg_connection.__getattr__st''..r$Nc |j|jj||fi|}t||jSr,)rOrrrM)r0rgrhrirs r%rz%AsyncAdapt_psycopg_connection.executes<5T--55eVJrJK(==r$c|jj|i|}t|drt||jSt ||jS)Nr)rrhasattrrrOrM)r0argsrirs r%rz$AsyncAdapt_psycopg_connection.cursorsK(!!(($5"5 66 "/ D D,VT[[A Ar$cV|j|jjyr,)rOrcommitrs r%rz$AsyncAdapt_psycopg_connection.commits D$$++-.r$cV|j|jjyr,)rOrrrs r%rz&AsyncAdapt_psycopg_connection.rollbacks D$$--/0r$cV|j|jjyr,)rOrr`rs r%r`z#AsyncAdapt_psycopg_connection.closes D$$**,-r$c.|jjSr,)rrrs r%rz(AsyncAdapt_psycopg_connection.autocommits***r$c&|j|yr,set_autocommitrZs r%rz(AsyncAdapt_psycopg_connection.autocommits E"r$cX|j|jj|yr,)rOrrrZs r%rz,AsyncAdapt_psycopg_connection.set_autocommit D$$33E:;r$cX|j|jj|yr,)rOrrrZs r%rz1AsyncAdapt_psycopg_connection.set_isolation_levels D$$88?@r$cX|j|jj|yr,)rOr set_read_onlyrZs r%rz+AsyncAdapt_psycopg_connection.set_read_onlys D$$2259:r$cX|j|jj|yr,)rOrset_deferrablerZs r%rz,AsyncAdapt_psycopg_connection.set_deferrablerr$rr,)rr r!__annotations__r staticmethodrrOrrUrrrrr`rrrrrrrr#r$r%rrs~  I * %F&/>B/1.++##<A;r?r?rErHrLrBrNrArPrCrRrDrTrErVNullTyperXrFrZBooleanr\AbstractSingleRangeImplr^AbstractMultiRangeImplrwrrrrrjrMrrrrrr1 dialect_asyncr#r$r%rs-Ul#  6?&'.*'   ; <9du(--88 (--88  l (8$$hmmhmm!!h++(##H''!!F226*66*Z !C    #7 KO1Od@@F#<41<$51