Ingen beskrivning
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.

MySQL.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf8 -*-
  2. from Lodel.settings import Settings
  3. ## @brief Manages the accesses to a MySQL datasource
  4. class MySQL(object):
  5. relations_table_name = 'relation'
  6. relations_pkname = 'id_relation'
  7. relations_field_nature = 'nature'
  8. field_lodel_id = 'lodel_id'
  9. class_table_prefix = 'class_'
  10. objects_table_name = 'object'
  11. connections = Settings.get("datasource")
  12. ## @brief indicates if we want ON DELETE CASCADE on foreign keys
  13. # @todo implementation in migration handler
  14. fk_on_delete_cascade = False
  15. ## @brief Lodel_id for the hierachy root
  16. leroot_lodel_id = 0
  17. @staticmethod
  18. ## @brief Exec a query
  19. # @param query str : SQL query
  20. def query(connection, query):
  21. with connection as cur:
  22. try:
  23. cur.execute(query)
  24. except Exception as err:
  25. raise err
  26. return cur
  27. @classmethod
  28. ## @brief gets the table name from class name
  29. # @param class_name str
  30. # @return str
  31. def get_table_name_from_class(cls, class_name):
  32. return (class_name if cls.class_table_prefix in class_name else "%s%s" % (cls.class_table_prefix, class_name)).lower()
  33. @classmethod
  34. ## @brief gets the table name given a class, a type and a field names
  35. # @param class_name str
  36. # @param type_name str
  37. # @param field_name str
  38. # @return str
  39. def get_r2t2table_name(cls, class_name, type_name):
  40. return "r2t_%s_%s" % (class_name, type_name)
  41. @classmethod
  42. ## @brief gets the fk name between two tables
  43. # @param src_table_name str
  44. # @param dst_table_name str
  45. # @return str
  46. def get_fk_name(cls, src_table_name, dst_table_name):
  47. return "fk_%s_%s" % (src_table_name, dst_table_name)
  48. @classmethod
  49. ## @brief Identifier escaping
  50. # @param idname str : An SQL identifier
  51. def escape_idname(cls, idname):
  52. if '`' in idname:
  53. raise ValueError("Invalid name : '%s'" % idname)
  54. return '`%s`' % idname
  55. @classmethod
  56. ## @brief Given a fieldtype, returns a MySQL type specifier
  57. # @param emfieldType EmFieldType : A fieldtype
  58. # @return str
  59. def get_type_spec_from_fieldtype(cls, emfieldtype):
  60. ftype = emfieldtype.ftype
  61. if ftype == 'char' or ftype == 'str':
  62. res = "VARCHAR(%d)" % emfieldtype.max_length
  63. elif ftype == 'text':
  64. res = 'TEXT'
  65. elif ftype == 'datetime':
  66. res = "DATETIME"
  67. # client side workaround for only one column with CURRENT_TIMESTAMP : giving NULL to timestamp that don't allows NULL
  68. # cf. https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html#idm139961275230400
  69. # The solution for the migration handler is to create triggers :
  70. # CREATE TRIGGER trigger_name BEFORE INSERT ON `my_super_table`
  71. # FOR EACH ROW SET NEW.my_date_column = NOW();
  72. # and
  73. # CREATE TRIGGER trigger_name BEFORE UPDATE ON
  74. elif ftype == 'bool':
  75. res = "BOOL"
  76. elif ftype == 'int':
  77. res = "INT"
  78. elif ftype == 'rel2type':
  79. res = "INT"
  80. else:
  81. raise ValueError("Unsupported fieldtype ftype : %s" % ftype)
  82. return res