| 40 | self.fields.append((index,name,type)) |
| 41 | |
| 42 | def sql(self): |
| 43 | fields = [] |
| 44 | keys = [] |
| 45 | self.fields.sort() |
| 46 | fields = [None]*len(self.fields) |
| 47 | for index, name, type in self.fields: |
| 48 | index -= 1 |
| 49 | unk = type & ~knownbits |
| 50 | if unk: |
| 51 | print("%s.%s unknown bits %x" % (self.name, name, unk)) |
| 52 | size = type & datasizemask |
| 53 | dtype = type & typemask |
| 54 | if dtype == type_string: |
| 55 | if size: |
| 56 | tname="CHAR(%d)" % size |
| 57 | else: |
| 58 | tname="CHAR" |
| 59 | elif dtype == type_short: |
| 60 | assert size==2 |
| 61 | tname = "SHORT" |
| 62 | elif dtype == type_long: |
| 63 | assert size==4 |
| 64 | tname="LONG" |
| 65 | elif dtype == type_binary: |
| 66 | assert size==0 |
| 67 | tname="OBJECT" |
| 68 | else: |
| 69 | tname="unknown" |
| 70 | print("%s.%sunknown integer type %d" % (self.name, name, size)) |
| 71 | if type & type_nullable: |
| 72 | flags = "" |
| 73 | else: |
| 74 | flags = " NOT NULL" |
| 75 | if type & type_localizable: |
| 76 | flags += " LOCALIZABLE" |
| 77 | fields[index] = "`%s` %s%s" % (name, tname, flags) |
| 78 | if type & type_key: |
| 79 | keys.append("`%s`" % name) |
| 80 | fields = ", ".join(fields) |
| 81 | keys = ", ".join(keys) |
| 82 | return "CREATE TABLE %s (%s PRIMARY KEY %s)" % (self.name, fields, keys) |
| 83 | |
| 84 | def create(self, db): |
| 85 | v = db.OpenView(self.sql()) |