Project

General

Profile

« Previous | Next » 

Revision 5521

sql.py: Added table_pkey_index(), index_order_by(), table_cluster_on(), table_order_by()

View differences:

lib/sql.py
1060 1060
    return [sql_gen.as_Col(strings.ustr(c), table)
1061 1061
        for c in table_col_names(db, table, *args, **kw_args)]
1062 1062

  
1063
def table_pkey_index(db, table, recover=None):
1064
    table_str = sql_gen.Literal(table.to_str(db))
1065
    try:
1066
        return sql_gen.Table(value(run_query(db, '''\
1067
SELECT relname
1068
FROM pg_index
1069
JOIN pg_class index ON index.oid = indexrelid
1070
WHERE
1071
indrelid = '''+table_str.to_str(db)+'''::regclass
1072
AND indisprimary
1073
'''
1074
            , recover, cacheable=True, log_level=4)), table.schema)
1075
    except StopIteration: raise DoesNotExistException('primary key', '')
1076

  
1063 1077
def table_pkey_col(db, table, recover=None):
1064 1078
    table = sql_gen.as_Table(table)
1065 1079
    
......
1143 1157
            , cacheable=True, log_level=4))
1144 1158
    else: raise NotImplementedError()
1145 1159

  
1160
def index_order_by(db, index):
1161
    return sql_gen.CustomCode(', '.join(index_exprs(db, index)))
1162

  
1163
def table_cluster_on(db, table, recover=None):
1164
    '''
1165
    @return The table's cluster index, or its pkey if none is set
1166
    '''
1167
    table_str = sql_gen.Literal(table.to_str(db))
1168
    try:
1169
        return sql_gen.Table(value(run_query(db, '''\
1170
SELECT relname
1171
FROM pg_index
1172
JOIN pg_class index ON index.oid = indexrelid
1173
WHERE
1174
indrelid = '''+table_str.to_str(db)+'''::regclass
1175
AND indisclustered
1176
'''
1177
            , recover, cacheable=True, log_level=4)), table.schema)
1178
    except StopIteration: return table_pkey_index(db, table, recover)
1179

  
1180
def table_order_by(db, table, recover=None):
1181
    try: return index_order_by(db, table_cluster_on(db, table, recover))
1182
    except DoesNotExistException: return None
1183

  
1146 1184
#### Functions
1147 1185

  
1148 1186
def function_exists(db, function):

Also available in: Unified diff