Fix issues with column placeholders

This commit is contained in:
XANTRONIX Industrial 2025-02-13 17:44:12 -05:00
parent f8b6093d4e
commit 5bab45b779

View file

@ -91,23 +91,42 @@ class Database():
return Database(db)
def column_placeholders(self, table, obj) -> list:
ctors = getattr(table, '__constructors__')
if ctors is None:
return ['?' for c in table.__columns__ if c != table.__key__]
if ctors is not None:
ret = list()
for c in table.__columns__:
if c == table.__key__:
continue
if c in ctors:
ret.append(ctors[c](getattr(obj, c)))
else:
ret.append('?')
return ret
def add(self, obj):
table = type(obj)
sql = f"insert into {table.__table__} ("
sql += ", ".join([c for c in table.__columns__ if c != table.__key__])
sql += ') values ('
sql += ", ".join(['?' for c in table.__columns__ if c != table.__key__])
sql += ", ".join(self.column_placeholders(table, obj))
sql += f") returning {table.__key__}"
fn = getattr(obj, '__values__', None)
if fn is not None:
values = fn()
values = fn(obj)
else:
values = list()
for column in table.__columns__:
if column != table.__key__:
if column not in table.__constructors__ and column != table.__key__:
values.append(getattr(obj, column, None))
cr = self.db.execute(sql, values)