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.

sqlquerybuilder.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 SqlQueryBuilder():
  7. def __init__(self, sqlwrapper, table):
  8. if not type(sqlwrapper) is SqlWrapper:
  9. logger.error("Unable to instanciate, bad argument...")
  10. raise TypeError('Excepted a SqlWrapper not a '+str(type(sqlwrapper)))
  11. self.table = table
  12. self.sqlwrapper = sqlwrapper
  13. self.proxy = None
  14. def Select(self, arg):
  15. """ Alias for select clause
  16. @param arg iterable: arg must be a Python list or other iterable and contain eather table name/type or colum/literal_column
  17. """
  18. self.proxy = sql.select(arg)
  19. return self.proxy
  20. def Where(self, arg):
  21. """ Alias for where clause
  22. @param arg SQL expression object or string
  23. """
  24. self.proxy = self.proxy.where(arg)
  25. def From(self, arg):
  26. """ Alias for select_from clause
  27. @param arg Table or table('tablename') or join clause
  28. """
  29. self.proxy = self.proxy.select_from(arg)
  30. def Update(self):
  31. self.proxy = self.table.update()
  32. def Insert(self):
  33. self.proxy = self.table.insert()
  34. def Delete(self):
  35. self.proxy = self.proxy.delete()
  36. def Value(self, arg):
  37. """
  38. Allow you to specifies the VALUES or SET clause of the statement.
  39. @param arg: VALUES or SET clause
  40. """
  41. self.proxy = self.proxy.values(arg)
  42. def Execute(self, bindedparam):
  43. """
  44. Execute the sql query constructed in the proxy and return the result.
  45. If no query then return False.
  46. @return: query result on success else False
  47. """
  48. if(self.proxy.__str__().split() == 'SELECT'):
  49. if('bindparam' in self.proxy.__str__()):
  50. #on test separement la présence de la clause bindparam et le type de l'argument correspondant
  51. #car si la clause est présente mais que l'argument est defectueux on doit renvoyer False et non pas executer la requete
  52. if(type(bindedparam) is list and type(bindedparam[0]) is dict):
  53. return self.sqlwrapper.rconn.execute(self.proxy, bindedparam)
  54. else:
  55. return False
  56. else:
  57. return self.sqlwrapper.rconn.execute(self.proxy)
  58. elif(self.proxy is not None):
  59. if('bindparam' in self.proxy.__str__()):
  60. if(type(bindedparam) is list and type(bindedparam[0]) is dict):
  61. return self.sqlwrapper.wconn.execute(self.proxy, bindedparam)
  62. else:
  63. return False
  64. else:
  65. return self.sqlwrapper.wconn.execute(self.proxy)
  66. else:
  67. return False