Skip to content

Commit

Permalink
Merge 16ce92c into 9953283
Browse files Browse the repository at this point in the history
  • Loading branch information
boblefrag committed May 11, 2015
2 parents 9953283 + 16ce92c commit 3e84d81
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
35 changes: 27 additions & 8 deletions drunken_boat/db/postgresql/projections.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def make_returning(self, sql_template, returning=None):
",".join(returning_fields))
return sql_template

def insert(self, values, returning=None, **kwargs):
def insert(self, values, returning=None):
"""
Insert a new row into the table checking for constraints. If
returning is set, return the corresponding column(s). If the
Expand Down Expand Up @@ -231,15 +231,11 @@ def insert(self, values, returning=None, **kwargs):
if returning:
res = cur.fetchone()
self.db.conn.commit()
if returning == "self":
results = [self.hydrate(res)[0]]
for field in self.fields:
if hasattr(field, "extra"):
results = field.extra(self, results)
results = self.return_results([res], returning)
if len(results) > 0:
return results[0]
return res

def update(self, where, values, where_params, returning=None, **kwargs):
def update(self, where, values, where_params, returning=None):
args = []
params = []
for k, v in values.items():
Expand Down Expand Up @@ -267,6 +263,29 @@ def update(self, where, values, where_params, returning=None, **kwargs):
if returning:
res = cur.fetchall()
self.db.conn.commit()
return self.return_results(res, returning)

def delete(self, where, params, returning=None):
if isinstance(where, Where):
where = where()
sql_template = """DELETE FROM {table} WHERE {where}""".format(
table=self.Meta.table,
where=where
)
sql_template = self.make_returning(sql_template, returning)
res = None
with self.db.cursor() as cur:
try:
cur.execute(sql_template, params)
except Exception as e:
self.db.conn.rollback()
raise e
if returning:
res = cur.fetchall()
self.db.conn.commit()
return self.return_results(res, returning)

def return_results(self, res, returning):
results = []
if res:
if returning == "self":
Expand Down
22 changes: 22 additions & 0 deletions drunken_boat/db/postgresql/tests/test_db_projection_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,28 @@ def test_projection_update(prepare_test):
assert p == [(4,)]


def test_projection_delete(prepare_test):
projection = projections_fixtures.TestProjectionWithVirtual(get_test_db())
projection.insert(
{"title": "dummy",
"introduction": "a meaningfull introduction"})
assert projection.delete("title=%s", ("dummy",)) == []
projection.insert(
{"title": "dummy",
"introduction": "a meaningfull introduction"})
assert projection.delete(Where("title", "=", "%s"), ("dummy",)) == []
pytest.raises(ProgrammingError,
projection.delete,
Where("name", "=", "%s"), ("Pouet",))
projection.insert(
{"title": "dummy",
"introduction": "a meaningfull introduction"})
p = projection.delete(Where("title", "=", "%s"),
("dummy",),
returning="self")
assert isinstance(p[0], DataBaseObject)


def test_projection_foreign(prepare_test):
projection_author = projections_fixtures.AuthorProjection(get_test_db())
projection_author.insert(
Expand Down

0 comments on commit 3e84d81

Please sign in to comment.