duckdb.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """
  2. This module implements the handler for duckdb (in memory or not)
  3. """
  4. from transport.sql.common import Base, BaseReader, BaseWriter
  5. def template ():
  6. return {'database':'path-to-database','table':'table'}
  7. class Duck :
  8. def __init__(self,**_args):
  9. #
  10. # duckdb with none as database will operate as an in-memory database
  11. #
  12. self.database = _args['database'] if 'database' in _args else ''
  13. def get_provider(self):
  14. return "duckdb"
  15. def _get_uri(self,**_args):
  16. return f"""duckdb:///{self.database}"""
  17. class Reader(Duck,BaseReader) :
  18. def __init__(self,**_args):
  19. Duck.__init__(self,**_args)
  20. BaseReader.__init__(self,**_args)
  21. def _get_uri(self,**_args):
  22. #
  23. # if we are working with an in-memory database we can NOT set the attributes to be read-only
  24. # something to do with SQL-Alchemy
  25. p = self.database.strip().startswith(":") and self.database.strip().endswith(":")
  26. if not p :
  27. return super()._get_uri(**_args),{'connect_args':{'read_only':True}}
  28. else:
  29. #
  30. # we have an in-memory database
  31. return super()._get_uri(**_args),{}
  32. class Writer(Duck,BaseWriter):
  33. def __init__(self,**_args):
  34. Duck.__init__(self,**_args)
  35. BaseWriter.__init__(self,**_args)