Revision 5061
Added by Aaron Marcuse-Kubitza about 12 years ago
sql.py | ||
---|---|---|
1089 | 1089 |
pkey_col = 'row_num' |
1090 | 1090 |
|
1091 | 1091 |
def pkey(db, table, recover=None): |
1092 |
'''Uses pkey_col, or if not found, the first column in the table.''' |
|
1093 |
cols = table_cols(db, table, recover) |
|
1094 |
if pkey_col in cols: return pkey_col |
|
1095 |
else: return cols[0] |
|
1092 |
'''If no pkey, returns the first column in the table.''' |
|
1093 |
table = sql_gen.as_Table(table) |
|
1094 |
|
|
1095 |
join_cols = ['table_schema', 'table_name', 'constraint_schema', |
|
1096 |
'constraint_name'] |
|
1097 |
tables = [sql_gen.Table('key_column_usage', 'information_schema'), |
|
1098 |
sql_gen.Join(sql_gen.Table('table_constraints', 'information_schema'), |
|
1099 |
dict(((c, sql_gen.join_same_not_null) for c in join_cols)))] |
|
1100 |
cols = [sql_gen.Col('column_name')] |
|
1101 |
|
|
1102 |
conds = [('constraint_type', 'PRIMARY KEY'), ('table_name', table.name)] |
|
1103 |
schema = table.schema |
|
1104 |
if schema != None: conds.append(('table_schema', schema)) |
|
1105 |
order_by = 'position_in_unique_constraint' |
|
1106 |
|
|
1107 |
try: return value(select(db, tables, cols, conds, order_by=order_by, |
|
1108 |
limit=1, log_level=4)) |
|
1109 |
except StopIteration: return table_cols(db, table, recover)[0] |
|
1096 | 1110 |
|
1097 | 1111 |
not_null_col = 'not_null_col' |
1098 | 1112 |
|
Also available in: Unified diff
sql.py: pkey(): Get the table's actual primary key column, rather than just using the first column in the table. Continue to return the first column in the table if the table has no primary key.