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.

ledatasourcesql.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #-*- coding: utf-8 -*-
  2. from leobject.datasources.dummy import DummyDatasource
  3. from mosql.db import Database, all_to_dicts
  4. from mosql.query import select
  5. from Lodel.utils.mosql import *
  6. ## SQL DataSource for LeObject
  7. class LeDataSourceSQL(DummyDatasource):
  8. def __init__(self, module=None, *conn_args, **conn_kargs):
  9. super(LeDataSourceSQL, self).__init__()
  10. self.db = Database(self.module, self.conn_args, self.conn_kargs)
  11. ## @brief update an existing object's data
  12. # @param lodel_id int : lodel_id of the object to update
  13. # @param checked_data dict
  14. # @param filters
  15. # @param relational_filters
  16. def update (self, lodel_id, checked_data, filters, relational_filters):
  17. pass
  18. ## @brief create a new object in the datasource
  19. # @param letype LeType
  20. # @param leclass LeClass
  21. # @param data dict : dictionnary of field:value pairs to save
  22. # @return lodel_id int : lodel_id of the created object
  23. def insert(self, letype, leclass, **data):
  24. pass
  25. ## @brief delete an existing object
  26. # @param lodel_id int : lodel_id of the object to delete
  27. # @param filters list : list of typles formatted as (FIELD, OPERATOR, VALUE)
  28. # @param relational_filters list
  29. # @return bool : True on success
  30. def delete(self, lodel_id, filters, relational_filters):
  31. pass
  32. ## @brief search for a collection of objects
  33. # @param emclass
  34. # @param emtype
  35. # @param filters list : list of tuples formatted as (FIELD, OPERATOR, VALUE)
  36. # @param relational_filters
  37. def get(self, emclass, emtype, field_list, filters, relational_filters):
  38. tablename = emclass.name
  39. where_filters = self._prepare_filters(filters)
  40. rel_filters = self._prepare_filters(relational_filters)
  41. query = select(tablename, where=where_filters, select=field_list)
  42. self.db.execute(query)
  43. return all_to_dicts(self.db)
  44. # @brief prepares the filters to be used by the mosql library's functions
  45. # @params filters : (FIELD, OPERATOR, VALUE) tuples
  46. # @return dict : Dictionnary with (FIELD, OPERATOR):VALUE style elements
  47. def _prepare_filters(self, filters):
  48. prepared_filters = {}
  49. for filter in filters:
  50. prepared_filter_key = (filter[0], filter[1])
  51. prepared_filter_value = filter[2]
  52. prepared_filters[prepared_filter_key] = prepared_filter_value
  53. return prepared_filters