No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlobject.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import logging as logger
  4. import sqlalchemy as sql
  5. from Database.sqlwrapper import SqlWrapper
  6. class SqlObject(object):
  7. """ Object that make aliases with sqlalchemy
  8. Example usage of object that inherite from SqlObject :
  9. class foo(SlqObject,...):
  10. def __init__(self, ...):
  11. self.__class__.tname = 'foo_table'
  12. f = foo(...)
  13. req = f.where(f.col.id == 42)
  14. res = f.rexec(req)
  15. e = bar(...)
  16. req = f.join(e.col.id == f.col.id)
  17. res = f.rexec(req)
  18. """
  19. def __init__(self, tname):
  20. if not type(tname) is str:
  21. logger.error("Unable to instanciate, bad argument...")
  22. raise TypeError('Excepted a string not a '+str(type(tname)))
  23. self.tname = tname
  24. self.table = self.Table()
  25. pass
  26. def Table(self):
  27. self.table = sql.Table(self.tname, sql.MetaData(), autoload_with=SqlWrapper.rengine, autoload=True)
  28. return self.table
  29. @property
  30. def col(self):
  31. return self.table.c
  32. @property
  33. def sel(self):
  34. return sql.select([self.table])
  35. @property
  36. def where(self):
  37. return self.sel.where
  38. @property
  39. def join(self):
  40. return self.sel.join
  41. @property
  42. def rconn(self):
  43. return SqlWrapper.rc()
  44. @property
  45. def wconn(self):
  46. return SqlWrapper.wc()
  47. def sFetchAll(self, sel):
  48. return self.rexec(sel).fetchall()
  49. def rexec(self, o):
  50. return self.rconn.execute(o)
  51. def wexec(self, o):
  52. return self.wconn.execute(o)