|
| 1 | +from easycoder import Handler, FatalError, RuntimeError, json |
| 2 | + |
| 3 | +class SQL(Handler): |
| 4 | + |
| 5 | + def __init__(self, compiler): |
| 6 | + Handler.__init__(self, compiler) |
| 7 | + |
| 8 | + def getName(self): |
| 9 | + return 'sql' |
| 10 | + |
| 11 | + ############################################################################# |
| 12 | + # Keyword handlers |
| 13 | + |
| 14 | + # create {table} {name} [with ...] |
| 15 | + # {name} {flag(s)} {type} [default {value}] [and ..] |
| 16 | + def k_create(self, command): |
| 17 | + if self.nextIsSymbol(): |
| 18 | + record = self.getSymbolRecord() |
| 19 | + if record['keyword'] == 'table': |
| 20 | + command['target'] = record['name'] |
| 21 | + command['tableName'] = self.nextValue() |
| 22 | + keys = [] |
| 23 | + while self.peek() in ['key', 'include']: |
| 24 | + item = {} |
| 25 | + token = self.nextToken() |
| 26 | + if token == 'include': |
| 27 | + item['include'] = self.nextValue() |
| 28 | + else: |
| 29 | + item['name'] = self.nextValue() |
| 30 | + token = self.peek() |
| 31 | + if token == 'primary': |
| 32 | + item['primary'] = True |
| 33 | + self.nextToken() |
| 34 | + if token == 'secondary': |
| 35 | + item['secondary'] = True |
| 36 | + self.nextToken() |
| 37 | + elif token == 'required': |
| 38 | + item['required'] = True |
| 39 | + self.nextToken() |
| 40 | + elif token == 'auto': |
| 41 | + item['required'] = True |
| 42 | + self.nextToken() |
| 43 | + item['type'] = self.nextToken() |
| 44 | + if self.peek() in ['default', '=']: |
| 45 | + self.nextToken() |
| 46 | + item['default'] = self.nextValue() |
| 47 | + elif self.peek() == 'check': |
| 48 | + self.nextToken() |
| 49 | + item['check'] = self.nextValue() |
| 50 | + keys.append(item) |
| 51 | + command['keys'] = keys |
| 52 | + self.add(command) |
| 53 | + return True |
| 54 | + return False |
| 55 | + |
| 56 | + def r_create(self, command): |
| 57 | + record = self.getVariable(command['target']) |
| 58 | + self.putSymbolValue(record, command) |
| 59 | + return self.nextPC() |
| 60 | + |
| 61 | + def k_get(self, command): |
| 62 | + if self.nextIsSymbol(): |
| 63 | + record = self.getSymbolRecord() |
| 64 | + if record['hasValue']: |
| 65 | + command['target'] = record['name'] |
| 66 | + self.skip('from') |
| 67 | + if self.nextIsSymbol(): |
| 68 | + record = self.getSymbolRecord() |
| 69 | + if record['keyword'] == 'table': |
| 70 | + command['entity'] = record['name'] |
| 71 | + if self.peek() == 'as': |
| 72 | + self.nextToken() |
| 73 | + command['form'] = self.nextToken() |
| 74 | + else: command['form'] = 'sql' |
| 75 | + self.add(command) |
| 76 | + return True |
| 77 | + return False |
| 78 | + |
| 79 | + def r_get(self, command): |
| 80 | + target = self.getVariable(command['target']) |
| 81 | + entity = self.getVariable(command['entity']) |
| 82 | + form = command['form'] |
| 83 | + keyword = entity['keyword'] |
| 84 | + if keyword == 'table': |
| 85 | + value = self.getSymbolValue(entity) |
| 86 | + tableName = self.getRuntimeValue(value['tableName']) |
| 87 | + output = [] |
| 88 | + if form == 'sql': |
| 89 | + # ------------------------------------------------------------- |
| 90 | + # Here are the rules for generating SQL |
| 91 | + output.append(f'DROP TABLE IF EXISTS {tableName} CASCADE;') |
| 92 | + output.append(f'CREATE TABLE {tableName} {{') |
| 93 | + secondary = False |
| 94 | + includes = [] |
| 95 | + keys = entity['value'][entity['index']]['keys'] |
| 96 | + for index, key in enumerate(keys): |
| 97 | + item = [] |
| 98 | + if 'include' in key: |
| 99 | + name = self.getRuntimeValue(key['include']) |
| 100 | + includes.append(f'{name}_id') |
| 101 | + item = f'{name}_id BIGINT REFERENCES {name}' |
| 102 | + else: |
| 103 | + if 'secondary' in key: |
| 104 | + secondary = True |
| 105 | + output.append(' id BIGSERIAL PRIMARY KEY,') |
| 106 | + item.append(self.getRuntimeValue(key['name'])) |
| 107 | + type = key['type'] |
| 108 | + if type == 'string': type = 'text' |
| 109 | + elif type == 'datetime': type = 'timestamptz' |
| 110 | + elif type == 'u64': type = 'bigint' |
| 111 | + item.append(type.upper()) |
| 112 | + if secondary: |
| 113 | + item.append('UNIQUE NOT NULL') |
| 114 | + secondary = False |
| 115 | + if 'primary' in key: item.append('PRIMARY KEY') |
| 116 | + if 'required' in key: item.append('NOT NULL') |
| 117 | + if 'default' in key: |
| 118 | + default = self.getRuntimeValue(key['default']) |
| 119 | + item.append(f'DEFAULT \'{default}\'') |
| 120 | + if 'check' in key: |
| 121 | + check = self.getRuntimeValue(key['check']) |
| 122 | + item.append(f'CHECK ({check})') |
| 123 | + item = ' '.join(item) |
| 124 | + if index < len(keys) - 1 or len(includes) > 0: item = f'{item},' |
| 125 | + output.append(f' {item}') |
| 126 | + if len(includes) > 0: |
| 127 | + includes = ', '.join(includes) |
| 128 | + item = f' PRIMARY KEY ({includes})' |
| 129 | + output.append(item) |
| 130 | + output.append('};') |
| 131 | + # ------------------------------------------------------------- |
| 132 | + v = {} |
| 133 | + v['type'] = 'text' |
| 134 | + v['content'] = '\n'.join(output) |
| 135 | + self.putSymbolValue(target, v) |
| 136 | + return self.nextPC() |
| 137 | + |
| 138 | + def k_table(self, command): |
| 139 | + return self.compileVariable(command, False) |
| 140 | + |
| 141 | + def r_table(self, command): |
| 142 | + return self.nextPC() |
| 143 | + |
| 144 | + ############################################################################# |
| 145 | + # Compile a value in this domain |
| 146 | + def compileValue(self): |
| 147 | + return None |
| 148 | + |
| 149 | + ############################################################################# |
| 150 | + # Modify a value or leave it unchanged. |
| 151 | + def modifyValue(self, value): |
| 152 | + return value |
| 153 | + |
| 154 | + ############################################################################# |
| 155 | + # Value handlers |
| 156 | + |
| 157 | + ############################################################################# |
| 158 | + # Compile a condition |
| 159 | + def compileCondition(self): |
| 160 | + condition = {} |
| 161 | + return condition |
| 162 | + |
| 163 | + ############################################################################# |
| 164 | + # Condition handlers |
0 commit comments