Project

General

Profile

« Previous | Next » 

Revision 399

sql.py: Added functions to truncate tables, list all tables, and empty a database

View differences:

sql.py
52 52

  
53 53
def value(cur): return row(cur)[0]
54 54

  
55
def values(cur): return iter(lambda: value(cur), None)
56

  
55 57
def value_or_none(cur):
56 58
    try: return value(cur)
57 59
    except StopIteration: return None
......
131 133
        except DuplicateKeyException, e:
132 134
            return value(select(db, table, [pkey], {e.col: row[e.col]}))
133 135

  
136
def truncate(db, table):
137
    check_name(table)
138
    return run_query(db, 'TRUNCATE '+table+' CASCADE')
139

  
140
def tables(db):
141
    module = util.root_module(db)
142
    if module == 'psycopg2':
143
        return values(run_query(db, "SELECT tablename from pg_tables "
144
            "WHERE schemaname = 'public' ORDER BY tablename"))
145
    elif module == 'MySQLdb': return values(run_query(db, 'SHOW TABLES'))
146
    else: raise NotImplementedError("Can't list tables for "+module+' database')
147

  
148
def empty_db(db):
149
    for table in tables(db): truncate(db, table)
150

  
134 151
db_engines = {
135 152
    'MySQL': ('MySQLdb', {'password': 'passwd', 'database': 'db'}),
136 153
    'PostgreSQL': ('psycopg2', {}),

Also available in: Unified diff