67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
# SPDX-License-Identifier: WTFPL
|
|
|
|
from .dbrecord import DbRecord
|
|
|
|
class Vessel(DbRecord):
|
|
|
|
table = "vessel"
|
|
pk_field = "vid"
|
|
pk_is_auto = True
|
|
fields = ('vid', 'vtype', 'htype', 'vesselname', 'model',
|
|
'shipyard', 'hull_number', 'loa', 'lwl', 'beam', 'draught',
|
|
'draught_min', 'displacement', 'ballast', 'beltpos_aft', 'beltpos_bow',
|
|
'shapefile', 'sid_default', 'year_built', 'remarks')
|
|
|
|
def __init__(self, db, vid=None):
|
|
super().__init__(db, vid)
|
|
self.title = _("Vessel({})")
|
|
|
|
def create(self):
|
|
self.clear()
|
|
sql = (
|
|
"SELECT IFNULL(MIN(v1.vid+1), 1) AS newid "
|
|
"FROM vessel AS v1 LEFT JOIN vessel AS v2 ON v2.vid=v1.vid+1 "
|
|
"WHERE v1.vid>=0 AND v2.vid IS NULL"
|
|
)
|
|
self.dcur.execute(sql)
|
|
row = self.dcur.fetchone()
|
|
if row is None:
|
|
# vessel table empty: create first vessel
|
|
self.pk_value = 1
|
|
else:
|
|
# create additional vessel
|
|
self.pk_value = row['newid']
|
|
self.vid = self.pk_value
|
|
self.vesselname = "Vessel #{}".format(self.pk_value)
|
|
self.htype = 'mono'
|
|
self._insert()
|
|
# set list change marker
|
|
sql = (
|
|
"INSERT INTO revision (revtype, revid, changed) "
|
|
"VALUES ('table', 1, NOW()) "
|
|
"ON DUPLICATE KEY UPDATE changed = NOW()"
|
|
)
|
|
self.db.cur.execute(sql)
|
|
self.conn.commit()
|
|
|
|
def save(self, commit=True):
|
|
if not super().save():
|
|
return False
|
|
if commit:
|
|
try:
|
|
self.conn.commit()
|
|
except mariadb.Error as e:
|
|
return False
|
|
return True
|
|
|
|
def delete(self):
|
|
# there must be no assiciated data!
|
|
pass
|
|
# set list change marker
|
|
sql = (
|
|
"INSERT INTO revision (revtype, revid, changed) "
|
|
"VALUES ('table', 1, NOW()) "
|
|
"ON DUPLICATE KEY UPDATE changed = NOW()"
|
|
)
|
|
self.db.cur.execute()
|