Xׯd_ ddlmZddlmZddlmZmZmZddlm Z ddl m Z ddl Z eddd Z d ZGd d eZe e jjdZe e je jfdZe e je jdZe e je jjdZe e je jje jdZe e je jjdZe e je jjdZe e jdZe e jdZ e e jj!e jjdZ"e e je jjdZ#e e je jje jdZ$e e je jje je jdZ%e e je jje je jdZ&e e je jje je jdZ'e e je jje je jdZ(e e je jje je jdZ)e e jj*e jje je je jfe j+dZ,e e je jje jj*d Z-e e jj*e jje jd!Z.e e je jje jj/d"Z0e e je jje jj/d#Z1e e je jje jj/d$Z2e e je jje jj/d%Z3e e je jje jj/d&Z4e e je jjd'Z5dS)()GenericReverseEngineering) DefineModule)find_object_with_nameserver_version_str2tuplereplace_string_parameters) db_driver)NotConnectedErrorNDbSQLAnywhereREz Oracle Corp.z1.0)nameauthorversioncfd}|S)aDeletes the available cursors in the connection once the wrapped method finishes. This is needed because in sqlanydb the cursors are not deleted once they go out of scope, because the connection keeps a set with each instantiated cursor. To make things worse, the number of available cursors is limited so the rev eng process may fail at some point when this limit is reached. Any method of the Rev Eng class that executes a query should be decorated with this function. c||g|R}||}|jjdkr=|jjD]}|dt |j_|S)NsqlanydbF)remove)get_connectiondriverdriverLibraryNameconncursorscloseset)cls connectionargsresccursormethods #..\modules\db_sqlanywhere_re_grt.pywrapped_methodz'release_cursors..wrapped_method+s|fS*,t,,,   z * *   .* < <&. + + E **** UUAFN )rr!s` r release_cursorsr$!s$ r"c"eZdZedZedZeedZeedZeedZ eedZ eedZ eedZ efd Z eed Zeed Zeed Zeed ZeedZeedZxZS)SQLAnywhereReverseEngineeringcdS)N SQLAnywherer#)rs r getTargetDBMSNamez/SQLAnywhereReverseEngineering.getTargetDBMSName7s}r"c2|j|jdS)Nr ) _connections__id__rrs r serverVersionz+SQLAnywhereReverseEngineering.serverVersion;s 129==r"cd} ||} |dstdn=#t$r0}t jd|jztdd}~wwxYwn#t$r}t jd|jz|jj dkrddl }t|jj t|j}ddl} ||}n*#t$r}t jd |zd}~wwxYwtd t%|D} || d <t| } d | d <t)| |jd <|jdi| }nt-j||}|s#t jdt/|t jdd|i|j|j<Yd}~nd}~wwxYw|r||dd} t jdd|jd| dt=| dz} tj } | dd\| _!| _"| _#| _$| |j|jd<dS)aEstablishes a connection to the server and stores the connection object in the connections pool. It first looks for a connection with the given connection parameters in the connections pool to reuse existent connections. If such connection is found it queries the server to ensure that the connection is alive and reestablishes it if is dead. If no suitable connection is found in the connections pool, a new one is created and stored in the pool. Parameters: =========== connection: an object of the class db_mgmt_Connection storing the parameters for the connection. password: a string with the password to use for the connection (ignored for SQLite). NzSELECT 1zconnection errorz1Connection to %s apparently lost, reconnecting...zConnection errorzConnecting to %s...rrz:The given connection string is not a valid python dict: %sc3xK|]5\}}|dr|d/||fV6dS)%N) startswithendswith).0keyvalues r z8SQLAnywhereReverseEngineering.connect..ese U494D4DS4I4INSnn]`NaNaU|r"passwordz %password%wbcopytables_connection_stringzConnection failed ConnectedrzSELECT @@versionzSQLAnywhere REz Connected to z,  )rrrrr r#)%rrexecute Exceptiongrt send_infohostIdentifierr rrsqlanydbwrapperrconnectionStringTemplatedictparameterValuesast literal_eval send_errorlistitemsreprconnectrstrr+r, execute_queryfetchonelog_infor rclasses GrtVersion majorNumber minorNumber releaseNumber buildNumber)rrr8conexcrconnstrrGall_params_dictparams conn_paramsver ver_partsr s r rMz%SQLAnywhereReverseEngineering.connect?s? $ F$$Z00C <zz||++J778#$67778 < < < QT^Tmmnnn'(:;;; <8 ! F F F M/*2KK L L L 2j@@22223J4E4^489S4T4TVV &)&6&6w&?&?OO N#_bi#ijjjd?CXCXCZCZ>[>[&.z""6ll *6 J'OST_O`O` *+KL&h&0000' H== 2CHH=== M+ & & &3?2EC Z. / / / / / /9 F:  E##J0BCCLLNNqQC L))joooWZWZWZ+[ \ \ \055 DIk,,..Gclmonomocp `G !4g6KWM`=DC Z. / :qs`B6AB B +BB  B HA H<DH D9D44D99CHHc`||ddgS)zReturns a list of the available catalogs. [NOTE] This will in fact return the name of the database we are connected to. zSELECT DB_PROPERTY('Name')r)rOrPr-s r getCatalogNamesz-SQLAnywhereReverseEngineering.getCatalogNamess0!!*.JKKTTVVWXYZZr"c vttd||dDS)z;Returns a list of schemata for the given connection object.c3&K|] }|dV dS)r=Nr#r4rows r r7z?SQLAnywhereReverseEngineering.getSchemaNames..s&WWS#a&WWWWWWr" sp_tables)sortedrrO)rr catalog_names r getSchemaNamesz,SQLAnywhereReverseEngineering.getSchemaNamess9 cWWC,=,=j+,V,VWWWWWXXXr"cLd|z}d|||DS)NzSELECT st.table_name FROM SYSTAB st LEFT JOIN SYSUSER su ON st.creator=su.user_id WHERE su.user_name = '%s' AND st.table_type = 1cg|] }|d Srr#rds r z?SQLAnywhereReverseEngineering.getTableNames..GGG3AGGGr"rOrrrh schema_namequerys r getTableNamesz+SQLAnywhereReverseEngineering.getTableNamess935@AHG#"3"3J"F"FGGGGr"cLd|z}d|||DS)NzSELECT st.table_name FROM SYSTAB st LEFT JOIN SYSUSER su ON st.creator=su.user_id WHERE su.user_name = '%s' AND st.table_type IN (2, 21)cg|] }|d Srlr#rds r rmz>SQLAnywhereReverseEngineering.getViewNames..rnr"rorps r getViewNamesz*SQLAnywhereReverseEngineering.getViewNamess9:.rnr"rorps r getProcedureNamesz/SQLAnywhereReverseEngineering.getProcedureNamess8*+HG#"3"3J"F"FGGGGr"cltt||d||}||_|S)N)superr&reverseEngineerr )rrrh schemata_listcontextcatalog __class__s r r}z-SQLAnywhereReverseEngineering.reverseEngineers65s;;KKJXZ\ikrss# r"cd|jd|jjd}dd|||DS)NzSELECT sr.remarks FROM SYSTAB st JOIN SYSUSER su ON st.creator=su.user_id JOIN SYSREMARK sr ON st.object_id=sr.object_id WHERE st.table_name='' AND su.user_name=''r{cg|] }|d Srlr#rds r rmzDSQLAnywhereReverseEngineering.getCommentForTable..sOOO3AOOOr")r ownerjoinrO)rrtablerrs r getCommentForTablez0SQLAnywhereReverseEngineering.getCommentForTablesW 6;ZZZAQAQAQSwwOO#*;*;J*N*NOOOPPPr"cL|jd}||jdkrd}t }|jjD]2}||jgt|j z3| ||D]\}}}||vr tj }||_||_|jjD]} | j|ks || j vr | |_n ||_|j|dS)NzSELECT UPPER(st.type_name), UPPER(base_type_str), UPPER(sd.domain_name ) FROM SYSUSERTYPE st LEFT JOIN SYSDOMAIN sd ON st.domain_id=sd.domain_id aSELECT UPPER(st.type_name), CASE WHEN st.type_name = 'money' THEN UPPER(sd.domain_name+'(19,4)') WHEN st.type_name = 'smallmoney' THEN UPPER(sd.domain_name+'(10,4)') WHEN st.type_name = 'sysname' THEN UPPER(sd.domain_name+'(30)') WHEN st.type_name = 'uniqueidentifierstr' THEN 'CHAR(36)' WHEN st.type_name = 'uniqueidentifier' THEN 'BINARY(16)' ELSE UPPER(sd.domain_name) END AS base_type_str, UPPER(sd.domain_name ) FROM SYSUSERTYPE st LEFT JOIN SYSDOMAIN sd ON st.domain_id=sd.domain_id) userDatatypes remove_allr.rTr_rdbmssimpleDatatypesupdater rJsynonymsrOr@rRdb_UserDatatype sqlDefinition actualTyperappend) rrrrrsimple_datatypesdatatype type_namesql_definition parent_typestypes r reverseEngineerUserDatatypesz:SQLAnywhereReverseEngineering.reverseEngineerUserDatatypessX ((***S   Z ( ( 4r 9 9=E"55 2 O OH  # #X]Od8;L6M6M$M N N N N696G6G TY6Z6Z 3 3 2I~{,,,{2244H%HM%3H "3  :,, u~0M0M*/H'E1N%HN  ! ( ( 2 2 2 2 3 3r"c d|jd|jjd}|||D]\}}}}}} tj} |pd| _|dv| _d| _|dr$|dd}| j d| |jj|\} } | s]| |jjd \} } d }d |d | jd |jjd|jd } tj d| | r| | _ n| | _| t| nd| _| r | jjn| jjj}|t'|nd}|dkrd| _|| _|| _n|| _dx| _| _|| dS)NaSELECT UPPER(sd.domain_name), sc.column_name, sc.nulls, sc.width, sc.scale, sc."default" FROM SYSTABCOL sc JOIN SYSDOMAIN sd ON sc.domain_id=sd.domain_id JOIN SYSTAB st ON sc.table_id=st.table_id JOIN SYSUSER su ON st.creator=su.user_id WHERE st.table_name='rz' ORDER BY sc.column_idr{)Nnz UNSIGNED UNSIGNEDVARCHARzColumn datatype "z" for column "z " in table ".z$" reverse engineered as VARCHAR(255)z(SQL Anywhere reverseEngineerTableColumnsNUMERIC)r rrOr@rR db_Column isNotNull collationNamer2flagsrfind_datatype_object send_warning simpleTypeuserTyperN defaultValuegrouprintupperlength precisionscale addColumn)rrrrrrcol_namenullablewidthr default_valuecolumnis_simple_datatypedatatype_objectmsgrs r reverseEngineerTableColumnsz9SQLAnywhereReverseEngineering.reverseEngineerTableColumnss["JJJ (8(8(8 : JMIZIZ[eglImIm$ $$ $ EHh% [**,,F".bFK':5F #%F "";// 0#ABB< ##J///252J2J5;K\^f2g2g / " R696N6Nu{O`bk6l6l3"Ot|t|t|EJJJLQLWL\L\L\^c^h^h^hi !KSQQQ! 2$3!!"18E8Q#m"4"4"4WYF 2DoO)../JdJjJoE"'"3CJJJE{{}} )) " #( $ % 244 6< OOF # # # #I$ $$ $r"c|j}t|jdkrtjd|jzdSd|jd|jd}||}||D]v\}}}} } tj } || _| dkrdnd| _ | dvrdnd| _ | dkrd| _ n*| d kr_| d kr| d krd | _ nd | _ nd | _ d|d|d} ||} | | D]\}}t|j|}|rotj }|dz|z|_||_|r|dkrdnd|_| j||| | j r| |_xdS)z9Reverse engineers the primary key(s) for the given table.rzwMigration: reverseEngineerTablePK: Reverse engineering of table %s was attempted but the table has no columns attributer=zSELECT st.table_id, si.index_id, si.index_name, si.index_category, si."unique" FROM SYSIDX si JOIN SYSTAB st ON si.table_id=st.table_id JOIN SYSUSER su ON st.creator=su.user_id WHERE st.table_name='rz' ORDER BY si.index_id)r=PRIMARYrUNIQUEINDEXFULLTEXTzSELECT sc.column_name, sic."order" FROM SYSIDXCOL sic JOIN SYSTAB st ON sic.table_id=st.table_id JOIN SYSTABCOL sc ON (sc.column_id = sic.column_id AND sc.table_id = sic.table_id) WHERE st.table_id=z AND sic.index_id=z ORDER BY sic.sequencerD)rlencolumnsr@rIr rrr>rRdb_Index isPrimaryunique indexTyperdb_IndexColumnreferencedColumnrdescendraddIndex primaryKey)rrrschemarr idx_cursortable_idindex_id index_nameindex_category index_uniqueindexidx_cols_queryidx_cols_cursor column_nameorderr index_columns r reverseEngineerTablePKz4SQLAnywhereReverseEngineering.reverseEngineerTablePKs[  u}   " " NUX]Xbb c c c1 #!:::v{{{ 4 '' 33::<< LVL^L^_dLeLe& )& ) HHh NLK((**E#EJ#1Q#6#6aaAEO , 6 611AEL"""+1$$1$$1$$&.EOO&-EOO",%HHhhh 0N "00<<CCEEO&5&=&=n&M&M 7 7" U.u}kJJ7#&;#=#=#?#?L(2S(8;(FL%4:L105+U%++--3:N:N11TUL(M((666 NN5 ! ! ! )#( qr"c |j}|j}t|jdkrtjd|jzdSd|jd|jd}||}||D]\}}} } } d|| fz} d| | fz} | ||  }| ||  }tj }||_||_|ddr!t|j|ddn|}|ddr!t|j|ddn||_t#||D]\\}\}}}t|j|}|s!tjd |d |jd Ct|jj|}|s&tjd |d |jjd |j||j||j|dS) z7Reverse engineers the foreign keys for the given table.rzxMigration: reverseEngineerTableFKs: Reverse engineering of table %s was attempted but the table has no columns attributer=aISELECT si.index_name, sfk.foreign_table_id, sfk.foreign_index_id, sfk.primary_table_id, sfk.primary_index_id FROM SYSFKEY sfk JOIN SYSIDX si ON (sfk.foreign_index_id=si.index_id AND sfk.foreign_table_id=si.table_id) JOIN SYSTAB st ON sfk.foreign_table_id=st.table_id JOIN SYSUSER su ON st.creator=su.user_id WHERE st.table_name='rz' ORDER BY sfk.primary_index_idzSELECT stc.column_name FROM SYSIDXCOL sic JOIN SYSTABCOL stc ON (sic.table_id=stc.table_id AND sic.column_id=stc.column_id) WHERE sic.table_id=%d AND sic.index_id=%d ORDER BY sic.sequencea,SELECT su.user_name, st.table_name, stc.column_name FROM SYSIDXCOL sic JOIN SYSTABCOL stc ON (sic.table_id=stc.table_id AND sic.column_id=stc.column_id) JOIN SYSTAB st ON stc.table_id=st.table_id JOIN SYSUSER su ON st.creator=su.user_id WHERE sic.table_id=%d AND sic.index_id=%d ORDER BY sic.sequencez,Migration: reverseEngineerTableFKs: Column "z" not found in table "")rrrr@rIr rrr>rOfetchallrR db_ForeignKeyrschematatablesreferencedTableziprreferencedColumns foreignKeys)rrrrrrr fk_cursorfk_name this_table_id this_index_idother_table_idother_index_idthis_column_queryother_column_query these_columns other_columns foreign_keyreferenced_schemathis_column_name_other_column_namer ref_columns r reverseEngineerTableFKsz5SQLAnywhereReverseEngineering.reverseEngineerTableFKsVsL , u}   " " NVY^Ycc d d d1 #%*JJJ =&&z2299;; U^UfUfglUmUm' 2' 2 QG]M>>!*=9 !:  " +N; "< !..z;LMMVVXXM ..z;MNNWWYYM+3355K %K &K anno`pqr`s  5g6F VWHXYZH[ \ \ \y rABsCDEsF+Q*?@Q@XZghiZjklZm*n*n*nLQK 'BEmUbBcBc A A>#!%>a,=.u}>NOONNqAqAqACHCMCMCM$NPPP2;3N3VXijj !NNqBqBqBDOD_DdDdDd$eggg#**6222-44Z@@@@   $ $[ 1 1 1 1qr"c||dd}d|vrdSd|vrdSdS)NzSELECT PROPERTY ('Platform')runixlinuxwindowsdarwin)rOrPlower)rr_oss r getOSz#SQLAnywhereReverseEngineering.getOSs[ ,JKKTTVVWXY__aa S==7 #  98r")__name__ __module__ __qualname__ classmethodr)r.r$rMrarirsrvryr}rrrrrr __classcell__)rs@r r&r&6sK[>>[><<_[<|[[_[[YY_[Y HH_[H HH_[H HH_[H[ QQ_[Q'3'3_['3T+$+$_[+$\99_[9x;;_[;z  _[     r"r&c6tdS)Nzsqlanywhere_rdbms_info.xml)r&initializeDBMSInfor#r"r r r s ( ; ; >>r"c6t|Sr )r&fullyQualifiedObjectName)objs r rrs ( A A# F FFr"c8t||Sr )r&rM)rr8s r rMrMs ( 0 0X F FFr"c6t|Sr )r& disconnectrs r rrs ( 3 3J ? ??r"c6t|Sr )r& isConnectedrs r rrs ( 4 4Z @ @@r"c4tSr )r&r)r#r"r r)r)s ( : : < < >z6 R RRr"c8t||Sr )r&reverseEngineerViewsr/s r r1r1s ( = =j& Q QQr"c8t||Sr )r&reverseEngineerProceduresr/s r r3r3s ( B B:v V VVr"c8t||Sr )r&reverseEngineerFunctionsr/s r r5r5s ( A A*f U UUr"c8t||Sr )r&reverseEngineerTriggersr/s r r7r7s ( @ @V T TTr"c6t|Sr )r&rrs r rr s ( . .z : ::r")6db_generic_re_grtrwbrworkbench.utilsrrr workbenchrworkbench.exceptionsr r@ ModuleInfor$r&exportrR db_mgmt_Rdbmsr LISTSTRINGr rGrtNamedObjectrINTdb_mgmt_ConnectionrMrrr)rrSrrarirsrvr%ryr( db_CatalogDICTr}rr, db_Schemar.r1r3r5r7rr#r"r rIs,877777ffffffffff222222 \ 1>SX Y Y Y *jjjjj$=jjjZ  3;,--ZZ.-Z CHcj)**>>+*> 3:sz**??+*? 3:s{9::GG;:G 37CK:CJGGGGHGG 37CK:;;@@<;@ 37CK:;;AA<;A 3:=== 38CCC 3;)3;+IJJFFKJF 38S[;<<EE=<E 38S[;SZHHRRIHR 38S[;SZTT^^UT^ 38S[;SZTT]]UT] 38S[;SZTT``UT` 38S[;SZTTbbUTb 38S[;SZTTaaUTa 3;)3;+I3:X[X`beblWmorowxxkkyxk 37CK:CK>;;?>;;;r"