81
81
])
82
82
83
83
class SQLAnyNoPrimaryKeyError (Exception ):
84
- ''' exception that is raised when trying to load the primary keys for a
84
+ """ exception that is raised when trying to load the primary keys for a
85
85
table that does not have any columns marked as being a primary key.
86
86
As noted in this documentation:
87
87
http://docs.sqlalchemy.org/en/latest/faq.html#how-do-i-map-a-table-that-has-no-primary-key
88
88
if a table has fully duplicate rows, and has no primary key, it cannot be mapped.
89
89
Since we can't tell if a table has rows that are 'supposed' to act like a primary key,
90
90
we just throw an exception and hopes the user adds primary keys to the table instead.
91
- '''
91
+ """
92
92
93
93
def __init__ (self , message , table_name ):
94
94
@@ -310,8 +310,8 @@ class SQLAnySQLCompiler(compiler.SQLCompiler):
310
310
'milliseconds' : 'millisecond'
311
311
})
312
312
313
- def get_select_precolumns (self , select ):
314
- s = "DISTINCT" if select ._distinct else ""
313
+ def get_select_precolumns (self , select , ** kw ):
314
+ s = "DISTINCT " if select ._distinct else ""
315
315
if select ._limit :
316
316
if select ._limit == 1 :
317
317
s += "FIRST "
@@ -322,12 +322,16 @@ def get_select_precolumns(self, select):
322
322
# SQL Anywhere doesn't allow "start at" without "top n"
323
323
s += "TOP ALL "
324
324
s += "START AT %s " % (select ._offset + 1 ,)
325
- return s
325
+ if s != '' :
326
+ return s
327
+ return compiler .SQLCompiler .get_select_precolumns (
328
+ self , select , ** kw )
329
+
326
330
327
331
def get_from_hint_text (self , table , text ):
328
332
return text
329
333
330
- def limit_clause (self , select ):
334
+ def limit_clause (self , select , ** kw ):
331
335
# Limit in sybase is after the select keyword
332
336
return ""
333
337
@@ -466,6 +470,11 @@ def initialize(self, connection):
466
470
super (SQLAnyDialect , self ).initialize (connection )
467
471
self .max_identifier_length = 128
468
472
473
+ VERSION_SQL = text ('select @@version' )
474
+ result = connection .execute (VERSION_SQL )
475
+ vers = result .scalar ()
476
+ self .server_version_info = tuple ( vers .split (' ' )[0 ].split ( '.' ) )
477
+
469
478
def get_table_id (self , connection , table_name , schema = None , ** kw ):
470
479
"""Fetch the id for schema.table_name.
471
480
@@ -592,10 +601,12 @@ def get_foreign_keys(self, connection, table_name, schema=None, **kw):
592
601
column_cache [table_id ] = columns
593
602
594
603
REFCONSTRAINT_SQL = text ("""
595
- SELECT fk.foreign_index_id, pt.table_name AS name, pt.table_id AS reftable_id
604
+ SELECT fk.foreign_index_id, i.index_name AS name, pt.table_id AS reftable_id
596
605
FROM sys.sysfkey fk
597
606
join sys.systab pt on fk.primary_table_id = pt.table_id
607
+ join sys.sysidx i on i.table_id=fk.primary_table_id
598
608
WHERE fk.foreign_table_id = :table_id
609
+ and i.index_category=2
599
610
""" )
600
611
referential_constraints = connection .execute (REFCONSTRAINT_SQL ,
601
612
table_id = table_id )
@@ -714,11 +725,8 @@ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
714
725
results .close ()
715
726
716
727
if not pks :
717
- # if we don't have any primary keys, then we will get a
718
- # "TypeError: 'NoneType' object is not subscriptable" below.
719
- raise SQLAnyNoPrimaryKeyError (
720
- "The table %s has no primary key and therefore can't be mapped using SQLAlchemy!" % table_name ,
721
- table_name )
728
+ return {"constrained_columns" : [],
729
+ "name" : None }
722
730
723
731
PKCOL_SQL = text ("""
724
732
select tc.column_name as col
0 commit comments