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.

sqlsetup.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. from Database.sqlwrapper import SqlWrapper
  3. class SQLSetup(object):
  4. def initDb(self, dbconfname = 'default'):
  5. db = SqlWrapper(read_db = dbconfname, write_db = dbconfname)
  6. tables = self.get_schema()
  7. db.dropAll()
  8. db.createAllFromConf(tables)
  9. def get_schema(self):
  10. tables = []
  11. default_columns = [
  12. {"name":"uid", "type":"VARCHAR(50)", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
  13. {"name":"name", "type":"VARCHAR(50)", "extra":{"nullable":False, "unique":True}},
  14. {"name":"string", "type":"TEXT"},
  15. {"name":"help", "type":"TEXT"},
  16. {"name":"rank", "type":"INTEGER"},
  17. {"name":"date_update", "type":"DATE"},
  18. {"name":"date_create", "type":"DATE"}
  19. ]
  20. # Table listing all objects created by lodel, giving them an unique id
  21. uids = {
  22. "name":"uids",
  23. "columns":[
  24. {"name":"uid", "type":"VARCHAR(50)", "extra":{"nullable":False, "primarykey":True}},
  25. {"name":"class", "type":"VARCHAR(50)"},
  26. {"name":"type", "type":"VARCHAR(50)"}
  27. ]
  28. }
  29. tables.append(uids)
  30. # Table listing the classes
  31. em_class = {"name":"em_class"}
  32. em_class['columns'] = default_columns + [
  33. {"name":"classtype", "type":"INTEGER"},
  34. {"name":"sortcolumn", "type":"VARCHAR(50)", "extra":{"default":"rank"}},
  35. {"name":"icon", "type":"INTEGER"},
  36. ]
  37. tables.append(em_class)
  38. # Table listing the types
  39. em_type = {"name":"em_type"}
  40. em_type['columns'] = default_columns + [
  41. {"name":"class_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_class.uid", "nullable":False}},
  42. {"name":"sortcolumn", "type":"VARCHAR(50)", "extra":{"default":"rank"}},
  43. {"name":"icon", "type":"INTEGER"},
  44. ]
  45. tables.append(em_type)
  46. # relation between types: which type can be a child of another
  47. em_type_hierarchy = {"name":"em_type_hierarchy"}
  48. em_type_hierarchy['columns'] = [
  49. {"name":"superior_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
  50. {"name":"subordinate_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
  51. {"name":"nature", "type":"VARCHAR(50)"},
  52. ]
  53. tables.append(em_type_hierarchy)
  54. # Table listing the fieldgroups of a class
  55. em_fieldgroup = {"name":"em_fieldgroup"}
  56. em_fieldgroup['columns'] = default_columns + [
  57. {"name":"class_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_class.uid", "nullable":False}},
  58. ]
  59. tables.append(em_fieldgroup)
  60. # Table listing the fields of a fieldgroup
  61. em_field = {"name":"em_field"}
  62. em_field['columns'] = default_columns + [
  63. {"name":"fieldtype_id", "type":"VARCHAR(50)", "extra":{"nullable":False}},
  64. {"name":"fieldgroup_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_fieldgroup.uid", "nullable":False}},
  65. {"name":"rel_to_type_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False}}, # if relational: type this field refer to
  66. {"name":"rel_field_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False}}, # if relational: field that specify the rel_to_type_id
  67. {"name":"optional", "type":"BOOLEAN"},
  68. {"name":"internal", "type":"BOOLEAN"},
  69. {"name":"icon", "type":"INTEGER"},
  70. ]
  71. tables.append(em_field)
  72. # selected field for each type
  73. em_field_type = {"name":"em_field_type"}
  74. em_field_type['columns'] = [
  75. {"name":"type_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
  76. {"name":"field_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_field.uid", "nullable":False, "primarykey":True}},
  77. ]
  78. tables.append(em_field_type)
  79. # Table of the objects created by the user (instance of the types)
  80. objects = {
  81. "name":"objects",
  82. "columns":[
  83. {"name":"uid", "type":"VARCHAR(50)", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
  84. {"name":"string", "type":"VARCHAR(50)"},
  85. {"name":"class_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_class.uid"}},
  86. {"name":"type_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid"}},
  87. {"name":"date_update", "type":"DATE"},
  88. {"name":"date_create", "type":"DATE"},
  89. {"name":"history", "type":"TEXT"}
  90. ]
  91. }
  92. tables.append(objects)
  93. # Table listing all files
  94. # TODO Préciser les colonnes à ajouter
  95. files = {
  96. "name":"files",
  97. "columns":[
  98. {"name":"uid", "type":"VARCHAR(50)", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
  99. {"name":"field1", "type":"VARCHAR(50)"}
  100. ]
  101. }
  102. tables.append(files)
  103. return tables