Нема описа
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, alchemy_logs=True)
  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":"INTEGER", "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":"INTEGER", "extra":{"nullable":False, "primarykey":True, 'autoincrement':True}},
  25. {"name":"table", "type":"VARCHAR(50)"}
  26. ]
  27. }
  28. tables.append(uids)
  29. # Table listing the classes
  30. em_class = {"name":"em_class"}
  31. em_class['columns'] = default_columns + [
  32. {"name":"classtype", "type":"VARCHAR(50)"},
  33. {"name":"sortcolumn", "type":"VARCHAR(50)", "extra":{"default":"rank"}},
  34. {"name":"icon", "type":"INTEGER"},
  35. ]
  36. tables.append(em_class)
  37. # Table listing the types
  38. em_type = {"name":"em_type"}
  39. em_type['columns'] = default_columns + [
  40. {"name":"class_id", "type":"INTEGER", "extra":{"foreignkey":"em_class.uid", "nullable":False}},
  41. {"name":"sortcolumn", "type":"VARCHAR(50)", "extra":{"default":"rank"}},
  42. {"name":"icon", "type":"INTEGER"},
  43. ]
  44. tables.append(em_type)
  45. # relation between types: which type can be a child of another
  46. em_type_hierarchy = {"name":"em_type_hierarchy"}
  47. em_type_hierarchy['columns'] = [
  48. {"name":"superior_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
  49. {"name":"subordinate_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
  50. {"name":"nature", "type":"VARCHAR(50)"},
  51. ]
  52. tables.append(em_type_hierarchy)
  53. # Table listing the fieldgroups of a class
  54. em_fieldgroup = {"name":"em_fieldgroup"}
  55. em_fieldgroup['columns'] = default_columns + [
  56. {"name":"class_id", "type":"INTEGER", "extra":{"foreignkey":"em_class.uid", "nullable":False}},
  57. ]
  58. tables.append(em_fieldgroup)
  59. # Table listing the fields of a fieldgroup
  60. em_field = {"name":"em_field"}
  61. em_field['columns'] = default_columns + [
  62. {"name":"fieldtype", "type":"VARCHAR(50)", "extra":{"nullable":False}},
  63. {"name":"fieldgroup_id", "type":"INTEGER", "extra":{"foreignkey":"em_fieldgroup.uid", "nullable":False}},
  64. {"name":"rel_to_type_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False}}, # if relational: type this field refer to
  65. {"name":"rel_field_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False}}, # if relational: field that specify the rel_to_type_id
  66. {"name":"optional", "type":"BOOLEAN"},
  67. {"name":"internal", "type":"BOOLEAN"},
  68. {"name":"icon", "type":"INTEGER"},
  69. ]
  70. tables.append(em_field)
  71. # selected field for each type
  72. em_field_type = {"name":"em_field_type"}
  73. em_field_type['columns'] = [
  74. {"name":"type_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
  75. {"name":"field_id", "type":"INTEGER", "extra":{"foreignkey":"em_field.uid", "nullable":False, "primarykey":True}},
  76. ]
  77. tables.append(em_field_type)
  78. # Table of the objects created by the user (instance of the types)
  79. objects = {
  80. "name":"objects",
  81. "columns":[
  82. {"name":"uid", "type":"INTEGER", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
  83. {"name":"string", "type":"VARCHAR(50)"},
  84. {"name":"class_id", "type":"INTEGER", "extra":{"foreignkey":"em_class.uid"}},
  85. {"name":"type_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid"}},
  86. {"name":"date_update", "type":"DATE"},
  87. {"name":"date_create", "type":"DATE"},
  88. {"name":"history", "type":"TEXT"}
  89. ]
  90. }
  91. tables.append(objects)
  92. # Table listing all files
  93. # TODO Préciser les colonnes à ajouter
  94. files = {
  95. "name":"files",
  96. "columns":[
  97. {"name":"uid", "type":"INTEGER", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
  98. {"name":"field1", "type":"VARCHAR(50)"}
  99. ]
  100. }
  101. tables.append(files)
  102. return tables