98 """Yield (attrs, index name) where attrs is a list of entity type's attribute names that should |
98 """Yield (attrs, index name) where attrs is a list of entity type's attribute names that should |
99 be unique together, and index name the unique index name. |
99 be unique together, and index name the unique index name. |
100 """ |
100 """ |
101 for attrs in eschema._unique_together or (): |
101 for attrs in eschema._unique_together or (): |
102 yield attrs, unique_index_name(eschema, attrs) |
102 yield attrs, unique_index_name(eschema, attrs) |
|
103 |
|
104 |
|
105 def eschema_sql_def(dbhelper, eschema, skip_relations=(), prefix=''): |
|
106 """Return a list of (column names, sql type def) for the given entity schema. |
|
107 |
|
108 No constraint nor index are considered - this function is usually for massive import purpose. |
|
109 """ |
|
110 attrs = [attrdef for attrdef in eschema.attribute_definitions() |
|
111 if not attrdef[0].type in skip_relations] |
|
112 attrs += [(rschema, None) |
|
113 for rschema in eschema.subject_relations() |
|
114 if not rschema.final and rschema.inlined] |
|
115 result = [] |
|
116 for i in range(len(attrs)): |
|
117 rschema, attrschema = attrs[i] |
|
118 if attrschema is not None: |
|
119 # creating = False will avoid NOT NULL / REFERENCES constraints |
|
120 sqltype = aschema2sql(dbhelper, eschema, rschema, attrschema, creating=False) |
|
121 else: # inline relation |
|
122 sqltype = 'integer' |
|
123 result.append(('%s%s' % (prefix, rschema.type), sqltype)) |
|
124 return result |
103 |
125 |
104 |
126 |
105 def eschema2sql(dbhelper, eschema, skip_relations=(), prefix=''): |
127 def eschema2sql(dbhelper, eschema, skip_relations=(), prefix=''): |
106 """Yield SQL statements to initialize database from an entity schema.""" |
128 """Yield SQL statements to initialize database from an entity schema.""" |
107 table = prefix + eschema.type |
129 table = prefix + eschema.type |