Skip to content

Commit

Permalink
Fix inserting default values into MySQL. (fixes webpy#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
anandology committed May 2, 2011
1 parent 42a49ea commit b1417f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions test/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def t(active):
self.assertEquals(a, active)
t(False)
t(True)

def test_insert_default_values(self):
db = webtest.setup_database(self.dbname)
db.insert("person")

class PostgresTest(DBTest):
dbname = "postgres"
Expand Down
9 changes: 7 additions & 2 deletions web/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ def __init__(self, db_module, keywords):
self.db_module = db_module
self.keywords = keywords


self._ctx = threadeddict()
# flag to enable/disable printing queries
self.printing = config.get('debug_sql', config.get('debug', False))
Expand Down Expand Up @@ -744,7 +743,7 @@ def q(x): return "(" + x + ")"
_values = SQLQuery.join([sqlparam(v) for v in values.values()], ', ')
sql_query = "INSERT INTO %s " % tablename + q(_keys) + ' VALUES ' + q(_values)
else:
sql_query = SQLQuery("INSERT INTO %s DEFAULT VALUES" % tablename)
sql_query = SQLQuery(self._get_insert_default_values_query(tablename))

if _test: return sql_query

Expand All @@ -769,6 +768,9 @@ def q(x): return "(" + x + ")"
if not self.ctx.transactions:
self.ctx.commit()
return out

def _get_insert_default_values_query(self, table):
return "INSERT INTO %s DEFAULT VALUES" % table

def multiple_insert(self, tablename, values, seqname=None, _test=False):
"""
Expand Down Expand Up @@ -971,6 +973,9 @@ def __init__(self, **keywords):

def _process_insert_query(self, query, tablename, seqname):
return query, SQLQuery('SELECT last_insert_id();')

def _get_insert_default_values_query(self, table):
return "INSERT INTO %s () VALUES()" % table

def import_driver(drivers, preferred=None):
"""Import the first available driver or preferred driver.
Expand Down

0 comments on commit b1417f5

Please sign in to comment.