Skip to content

Commit 20a95eb

Browse files
authored
Merge pull request #1455 from erikoqvist/fix_id_returned_after_sql_inserts
nipapd: fix id returned after sql inserts
2 parents f17b642 + 40254f5 commit 20a95eb

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

nipap/nipap/backend.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -902,15 +902,6 @@ def _execute(self, sql, opt=None, callno=0):
902902
except psycopg2.Warning as warn:
903903
self._logger.warning(warn)
904904

905-
def _lastrowid(self):
906-
""" Get ID of last inserted column.
907-
"""
908-
909-
# TODO: hmm, we can do this by doing fetchone() on our cursor
910-
self._execute("SELECT lastval() AS last")
911-
for row in self._curs_pg:
912-
return row['last']
913-
914905
def _sql_expand_insert(self, spec, key_prefix='', col_prefix=''):
915906
""" Expand a dict so it fits in a INSERT clause
916907
"""
@@ -1196,10 +1187,10 @@ def add_vrf(self, auth, attr):
11961187
self._check_attr(attr, req_attr, _vrf_attrs)
11971188

11981189
insert, params = self._sql_expand_insert(attr)
1199-
sql = "INSERT INTO ip_net_vrf " + insert
1190+
sql = "INSERT INTO ip_net_vrf " + insert + " RETURNING id"
12001191

12011192
self._execute(sql, params)
1202-
vrf_id = self._lastrowid()
1193+
vrf_id = self._curs_pg.fetchone()[0]
12031194
vrf = self.list_vrf(auth, {'id': vrf_id})[0]
12041195

12051196
# write to audit table
@@ -1722,10 +1713,10 @@ def add_pool(self, auth, attr):
17221713
self._check_pool_attr(attr, req_attr)
17231714

17241715
insert, params = self._sql_expand_insert(attr)
1725-
sql = "INSERT INTO ip_net_pool " + insert
1716+
sql = "INSERT INTO ip_net_pool " + insert + " RETURNING id"
17261717

17271718
self._execute(sql, params)
1728-
pool_id = self._lastrowid()
1719+
pool_id = self._curs_pg.fetchone()[0]
17291720
pool = self.list_pool(auth, {'id': pool_id})[0]
17301721

17311722
# write to audit table
@@ -2502,10 +2493,10 @@ def add_prefix(self, auth, attr, args=None):
25022493
attr['expires'] = _parse_expires(attr['expires'])
25032494

25042495
insert, params = self._sql_expand_insert(attr)
2505-
sql = "INSERT INTO ip_net_plan " + insert
2496+
sql = "INSERT INTO ip_net_plan " + insert + " RETURNING id"
25062497

25072498
self._execute(sql, params)
2508-
prefix_id = self._lastrowid()
2499+
prefix_id = self._curs_pg.fetchone()[0]
25092500
prefix = self.list_prefix(auth, {'id': prefix_id})[0]
25102501

25112502
# write to audit table

0 commit comments

Comments
 (0)