Revision 471
Added by Aaron Marcuse-Kubitza about 13 years ago
lib/sql.py | ||
---|---|---|
144 | 144 |
cache[table] = col(run_query(db, 'SELECT * FROM '+table+' LIMIT 0'), 0) |
145 | 145 |
return cache[table] |
146 | 146 |
|
147 |
def put(db, table, row, pkey, row_ct_ref=None): |
|
148 |
try: |
|
149 |
row_ct = try_insert(db, table, row).rowcount |
|
150 |
if row_ct_ref != None and row_ct >= 0: row_ct_ref[0] += row_ct |
|
151 |
return last_insert_id(db) |
|
152 |
except DuplicateKeyException, e: |
|
153 |
return value(select(db, table, [pkey], util.dict_subset(row, e.cols))) |
|
154 |
|
|
147 | 155 |
def get(db, table, row, pkey, create=False, row_ct_ref=None): |
148 | 156 |
try: return value(select(db, table, [pkey], row, 1)) |
149 | 157 |
except StopIteration: |
150 | 158 |
if not create: raise |
151 |
# Insert new row |
|
152 |
try: |
|
153 |
row_ct = try_insert(db, table, row).rowcount |
|
154 |
if row_ct_ref != None and row_ct >= 0: row_ct_ref[0] += row_ct |
|
155 |
return last_insert_id(db) |
|
156 |
except DuplicateKeyException, e: |
|
157 |
return value(select(db, table, [pkey], |
|
158 |
util.dict_subset(row, e.cols))) |
|
159 |
return put(db, table, row, pkey, row_ct_ref) # insert new row |
|
159 | 160 |
|
161 |
|
|
160 | 162 |
def truncate(db, table): |
161 | 163 |
check_name(table) |
162 | 164 |
return run_query(db, 'TRUNCATE '+table+' CASCADE') |
Also available in: Unified diff
sql.py: Added new function put() and changed get() to use it