[Sqlalchemy-commits] [1089] sqlalchemy/trunk/lib/sqlalchemy/databases/oracle.py: initial table refle
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-03-04 17:46:15
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[1089] sqlalchemy/trunk/lib/sqlalchemy/databases/oracle.py: initial table reflection support courtesy Andrija Zaric</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1089</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-03-04 11:46:08 -0600 (Sat, 04 Mar 2006)</dd> </dl> <h3>Log Message</h3> <pre>initial table reflection support courtesy Andrija Zaric</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunklibsqlalchemydatabasesoraclepy">sqlalchemy/trunk/lib/sqlalchemy/databases/oracle.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunklibsqlalchemydatabasesoraclepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/databases/oracle.py (1088 => 1089)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/databases/oracle.py 2006-03-04 17:45:19 UTC (rev 1088) +++ sqlalchemy/trunk/lib/sqlalchemy/databases/oracle.py 2006-03-04 17:46:08 UTC (rev 1089) </span><span class="lines">@@ -65,6 +65,16 @@ </span><span class="cx"> sqltypes.CHAR: OracleChar, </span><span class="cx"> } </span><span class="cx"> </span><ins>+ +ischema_names = { + 'VARCHAR2' : OracleString, + 'DATE' : OracleDateTime, + 'DATETIME' : OracleDateTime, + 'NUMBER' : OracleNumeric, + 'BLOB' : OracleBinary, + 'CLOB' : OracleText +} + </ins><span class="cx"> def engine(*args, **params): </span><span class="cx"> return OracleSQLEngine(*args, **params) </span><span class="cx"> </span><span class="lines">@@ -113,8 +123,59 @@ </span><span class="cx"> return OracleDefaultRunner(self, proxy) </span><span class="cx"> </span><span class="cx"> def reflecttable(self, table): </span><del>- raise "not implemented" </del><ins>+ c = self.execute ("select COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, NULLABLE, DATA_DEFAULT from USER_TAB_COLUMNS where TABLE_NAME = :table_name", {'table_name':table.name}) + + while True: + row = c.fetchone() + if row is None: + break </ins><span class="cx"> </span><ins>+ (name, coltype, length, precision, scale, nullable, default) = (row[0], row[1], row[2], row[3], row[4], row[5]=='Y', row[6]) + + # INTEGER if the scale is 0 and precision is null + # NUMBER if the scale and precision are both null + # NUMBER(9,2) if the precision is 9 and the scale is 2 + # NUMBER(3) if the precision is 3 and scale is 0 + #length is ignored except for CHAR and VARCHAR2 + if coltype=='NUMBER' : + if precision is None and scale is None: + coltype = OracleNumeric + elif precision is None and scale == 0 : + coltype = OracleInteger + else : + coltype = OracleNumeric(precision, scale) + elif coltype=='CHAR' or coltype=='VARCHAR2': + coltype = ischema_names.get(coltype, OracleString)(length) + else: + coltype = ischema_names.get(coltype) + + colargs = [] + if default is not None: + colargs.append(PassiveDefault(sql.text(default, escape=False))) + + name = name.lower() + + table.append_item (schema.Column(name, coltype, nullable=nullable, *colargs)) + + + c = self.execute("""select UCC.CONSTRAINT_NAME, UCC.COLUMN_NAME, UC.CONSTRAINT_TYPE, UC.SEARCH_CONDITION, UC2.TABLE_NAME as REFERENCES_TABLE +from USER_CONS_COLUMNS UCC, USER_CONSTRAINTS UC, USER_CONSTRAINTS UC2 +where UCC.CONSTRAINT_NAME = UC.CONSTRAINT_NAME +and UC.R_CONSTRAINT_NAME = UC2.CONSTRAINT_NAME(+) +and UCC.TABLE_NAME = :table_name +order by UCC.CONSTRAINT_NAME""",{'table_name' : table.name}) + while True: + row = c.fetchone() + if row is None: + break + + (cons_name, column_name, type, search, referred_table) = row + if type=='P' : + table.c[column_name.lower()]._set_primary_key() + elif type=='R': + remotetable = Table(referred_table, referred_table.engine, autoload = True) + table.c[column_name.lower()].append_item(schema.ForeignKey(remotetable.primary_key[0])) + </ins><span class="cx"> def last_inserted_ids(self): </span><span class="cx"> return self.context.last_inserted_ids </span><span class="cx"> </span></span></pre> </div> </div> </body> </html> |