Revision 5388
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
702 | 702 |
assert start == None or isinstance(start, (int, long)) |
703 | 703 |
if order_by is order_by_pkey: |
704 | 704 |
if table0 == None or distinct_on != []: order_by = None |
705 |
else: order_by = pkey(db, table0, recover=True) |
|
705 |
else: order_by = pkey_name(db, table0, recover=True)
|
|
706 | 706 |
|
707 | 707 |
query = 'SELECT' |
708 | 708 |
|
... | ... | |
1052 | 1052 |
return [sql_gen.as_Col(strings.ustr(c), table) |
1053 | 1053 |
for c in table_col_names(db, table, *args, **kw_args)] |
1054 | 1054 |
|
1055 |
def pkey(db, table, recover=None): |
|
1055 |
def pkey_name(db, table, recover=None):
|
|
1056 | 1056 |
'''If no pkey, returns the first column in the table.''' |
1057 | 1057 |
table = sql_gen.as_Table(table) |
1058 | 1058 |
|
... | ... | |
1073 | 1073 |
except StopIteration: return table_col_names(db, table, recover)[0] |
1074 | 1074 |
|
1075 | 1075 |
def pkey_col(db, table, *args, **kw_args): |
1076 |
return sql_gen.Col(pkey(db, table, *args, **kw_args), table) |
|
1076 |
return sql_gen.Col(pkey_name(db, table, *args, **kw_args), table)
|
|
1077 | 1077 |
|
1078 | 1078 |
not_null_col = 'not_null_col' |
1079 | 1079 |
|
1080 | 1080 |
def table_not_null_col(db, table, recover=None): |
1081 | 1081 |
'''Name assumed to be the value of not_null_col. If not found, uses pkey.''' |
1082 | 1082 |
if not_null_col in table_col_names(db, table, recover): return not_null_col |
1083 |
else: return pkey(db, table, recover) |
|
1083 |
else: return pkey_name(db, table, recover)
|
|
1084 | 1084 |
|
1085 | 1085 |
def constraint_cond(db, constraint): |
1086 | 1086 |
module = util.root_module(db.db) |
... | ... | |
1185 | 1185 |
@pre The table must not already have a primary key. |
1186 | 1186 |
''' |
1187 | 1187 |
table = sql_gen.as_Table(table) |
1188 |
if cols == None: cols = [pkey(db, table, recover)] |
|
1188 |
if cols == None: cols = [pkey_name(db, table, recover)]
|
|
1189 | 1189 |
col_strs = [sql_gen.to_name_only_col(v).to_str(db) for v in cols] |
1190 | 1190 |
|
1191 | 1191 |
run_query(db, 'ALTER TABLE '+table.to_str(db)+' ADD PRIMARY KEY (' |
lib/db_xml.py | ||
---|---|---|
115 | 115 |
in_tables.append(in_table) |
116 | 116 |
no_empty.add(in_table) |
117 | 117 |
|
118 |
def pkey(table): return sql.pkey(db, table, True)
|
|
118 |
def pkey_name(table): return sql.pkey_name(db, table, True)
|
|
119 | 119 |
|
120 | 120 |
# Add fkey to parent |
121 | 121 |
if parent_ids_loc != None: |
122 | 122 |
if sql_gen.is_table_col(parent_ids_loc): |
123 | 123 |
no_empty.add(parent_ids_loc.table) |
124 | 124 |
parent_ptr = node.getAttribute('fkey') |
125 |
if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode)) |
|
125 |
if parent_ptr == '': parent_ptr = pkey_name(name_of(node.parentNode))
|
|
126 | 126 |
row[parent_ptr] = parent_ids_loc |
127 | 127 |
|
128 | 128 |
# Parse input columns |
... | ... | |
171 | 171 |
return pkeys_loc |
172 | 172 |
|
173 | 173 |
def get(db, node, limit=None, start=None): |
174 |
def pkey(table): return sql.pkey(db, table)
|
|
174 |
def pkey_name(table): return sql.pkey_name(db, table)
|
|
175 | 175 |
|
176 | 176 |
node = node.firstChild |
177 | 177 |
table = name_of(node) |
178 |
pkey_ = pkey(table) |
|
178 |
pkey_ = pkey_name(table)
|
|
179 | 179 |
|
180 | 180 |
fields = [] |
181 | 181 |
conds = {} |
... | ... | |
185 | 185 |
elif xml_dom.is_text(child): conds[child_name] = xml_dom.value(child) |
186 | 186 |
else: raise Exception('Joins not supported yet') |
187 | 187 |
id_ = xml_dom.get_id(node) |
188 |
if id_ != None: conds[pkey(table)] = id_ # replace any existing pkey value
|
|
188 |
if id_ != None: conds[pkey_name(table)] = id_ # replace any existing value
|
|
189 | 189 |
if fields == []: fields.append(pkey_) |
190 | 190 |
|
191 | 191 |
return sql.select(db, table, fields, conds, limit, start) |
lib/sql_io.py | ||
---|---|---|
371 | 371 |
warnings.warn(UserWarning('Inserting empty table row(s)')) |
372 | 372 |
|
373 | 373 |
if is_function: out_pkey = 'result' |
374 |
else: out_pkey = sql.pkey(db, out_table, recover=True) |
|
374 |
else: out_pkey = sql.pkey_name(db, out_table, recover=True)
|
|
375 | 375 |
out_pkey_col = sql_gen.as_Col(out_pkey, out_table) |
376 | 376 |
|
377 | 377 |
in_tables_ = copy.copy(in_tables) # don't modify input! |
378 | 378 |
try: in_tables0 = in_tables_.pop(0) # first table is separate |
379 | 379 |
except IndexError: in_tables0 = None |
380 | 380 |
else: |
381 |
in_pkey = sql.pkey(db, in_tables0, recover=True) |
|
381 |
in_pkey = sql.pkey_name(db, in_tables0, recover=True)
|
|
382 | 382 |
in_pkey_col = sql_gen.as_Col(in_pkey, in_tables0) |
383 | 383 |
|
384 | 384 |
# Determine if can use optimization for only literal values |
Also available in: Unified diff
sql.py: Renamed pkey() to pkey_name()