Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 142x 142x 142x 142x 142x 1x 1x 1x 1x 141x 141x 141x 142x 142x 1x 1x 1x 1x 140x 140x 142x 142x 142x 142x 142x 142x 142x 142x 9x 9x 9x 140x 142x 138x 138x 140x 140x 140x 142x 142x 142x 9x 9x 9x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 4x 4x 9x 138x 138x 142x 1x 1x 142x 13x 13x 13x 13x 142x 9x 9x 9x 9x 138x 142x 98x 98x 138x 142x 7x 7x 138x 142x 43x 43x 138x 142x 9x 9x 142x 1x 1x 1x 138x 138x 138x 138x 138x 10x 10x 128x 128x 138x 5x 5x 123x 123x 138x 83x 13x 13x 83x 110x 110x 110x 138x 15x 15x 95x 95x 95x 1x 1x 43x 43x 43x 54x 54x 11x 54x 29x 43x 12x 12x 2x 2x 2x 43x 1x 1x 140x 140x 140x 140x 140x 48x 48x 22x 22x 48x 140x 140x 140x 74x 74x 34x 74x 40x 40x 74x 140x 140x 140x 8x 8x 8x 8x 8x 16x 16x 16x 16x 16x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 16x 8x 140x 1x 1x 140x 6x 6x 134x 134x 140x 113x 113x 113x 21x 21x 140x 140x 21x 21x 21x 14x 14x 7x 21x 21x 21x 1x 1x 34x 32x 32x 20x 20x 20x 20x 20x 20x 32x 9x 32x 3x 3x 3x 3x 3x 3x 3x 34x 2x 2x 34x 1x 1x 22x 2x 2x 20x 22x 19x 19x 19x 1x 22x 1x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 141x 141x 141x 141x 22x 141x 119x 119x 141x 141x 2x 2x 141x 141x 83x 83x 83x 12x 12x 83x 83x 141x 141x 99x 99x 141x 141x 141x 1x | import * as Expressions from "../../2_statements/expressions"; import {ExpressionNode} from "../../nodes"; import {CurrentScope} from "../_current_scope"; import {IStructureComponent, StructureType, TableKeyType, TableType, UnknownType, VoidType} from "../../types/basic"; import {InlineData} from "./inline_data"; import {Target} from "./target"; import {SQLFrom} from "./sql_from"; import {SQLForAllEntries} from "./sql_for_all_entries"; import {ScopeType} from "../_scope_type"; import {SQLSource} from "./sql_source"; import {SQLCompare} from "./sql_compare"; import {DatabaseTableSource} from "./database_table"; import {AbstractType} from "../../types/basic/_abstract_type"; import {SQLOrderBy} from "./sql_order_by"; import {Dynamic} from "./dynamic"; import {ReferenceType} from "../_reference"; import {SyntaxInput, syntaxIssue} from "../_syntax_input"; type FieldList = {code: string, as: string, expression: ExpressionNode}[]; const isSimple = /^\w+$/; export class Select { public static runSyntax(node: ExpressionNode, input: SyntaxInput, skipImplicitInto = false): void { const token = node.getFirstToken(); const from = node.findDirectExpression(Expressions.SQLFrom); const dbSources = from ? SQLFrom.runSyntax(from, input) : []; if (from === undefined) { const message = `Missing FROM`; input.issues.push(syntaxIssue(input, node.getFirstToken(), message)); return; } const fields = this.findFields(node, input); if (fields.length === 0 && node.findDirectExpression(Expressions.SQLFieldListLoop) === undefined && node.findDirectExpression(Expressions.SQLAggregation) === undefined) { const message = `SELECT: fields missing`; input.issues.push(syntaxIssue(input, node.getFirstToken(), message)); return; } const isSingle = node.getChildren()[1].concatTokens().toUpperCase() === "SINGLE" || node.get() instanceof Expressions.SelectLoop; this.checkFields(fields, dbSources, input, node); this.handleInto(node, input, fields, dbSources, isSingle); const fae = node.findDirectExpression(Expressions.SQLForAllEntries); if (fae) { input.scope.push(ScopeType.OpenSQL, "SELECT", token.getStart(), input.filename); SQLForAllEntries.runSyntax(fae, input); } for (const t of node.findAllExpressions(Expressions.Target)) { Target.runSyntax(t, input); } // check implicit into, the target field is implict equal to the table name if (skipImplicitInto === false && node.findDirectExpression(Expressions.SQLIntoTable) === undefined && node.findDirectExpression(Expressions.SQLIntoList) === undefined && node.findDirectExpression(Expressions.SQLIntoStructure) === undefined) { const fields = node.findFirstExpression(Expressions.SQLAggregation)?.concatTokens(); const c = new RegExp(/^count\(\s*\*\s*\)$/, "i"); if (fields === undefined || c.test(fields) === false) { const nameToken = from?.findDirectExpression(Expressions.SQLFromSource); if (nameToken) { const found = input.scope.findVariable(nameToken.concatTokens()); if (found) { input.scope.addReference(nameToken.getFirstToken(), found, ReferenceType.DataWriteReference, input.filename); } else { const message = `Target variable ${nameToken.concatTokens()} not found in scope`; input.issues.push(syntaxIssue(input, node.getFirstToken(), message)); return; } } } } // OFFSET for (const s of node.findDirectExpressions(Expressions.SQLSource)) { SQLSource.runSyntax(s, input); } for (const up of node.findDirectExpressions(Expressions.SQLUpTo)) { for (const s of up.findDirectExpressions(Expressions.SQLSource)) { SQLSource.runSyntax(s, input); } } for (const fae of node.findDirectExpressions(Expressions.SQLForAllEntries)) { for (const s of fae.findDirectExpressions(Expressions.SQLSource)) { SQLSource.runSyntax(s, input); } } for (const s of node.findAllExpressions(Expressions.SQLCompare)) { SQLCompare.runSyntax(s, input, dbSources); } for (const s of node.findDirectExpressions(Expressions.SQLOrderBy)) { SQLOrderBy.runSyntax(s, input); } if (this.isStrictMode(node)) { this.strictModeChecks(node, input); } if (input.scope.getType() === ScopeType.OpenSQL) { input.scope.pop(node.getLastToken().getEnd()); } } // there are multiple rules, but gotta start somewhere private static isStrictMode(node: ExpressionNode) { const into = node.findDirectExpressionsMulti([Expressions.SQLIntoList, Expressions.SQLIntoStructure, Expressions.SQLIntoTable])[0]; const where = node.findDirectExpression(Expressions.SQLCond); // INTO is after WHERE if (into && where && into.getFirstToken().getStart().isAfter(where.getFirstToken().getStart())) { return true; } // FIELDS is used if (node.findFirstExpression(Expressions.SQLFields)) { return true; } // any field is escaped with @ for (const source of node.findAllExpressions(Expressions.SQLSource)) { if (source.getFirstToken().getStr() === "@") { return true; } } // comma used in FROM const fieldList = node.findFirstExpression(Expressions.SQLFieldList); if (fieldList && fieldList.findDirectTokenByText(",")) { return true; } return false; } private static strictModeChecks(node: ExpressionNode, input: SyntaxInput) { const sources = node.findAllExpressions(Expressions.SQLSource); for (const source of sources) { const first = source.getFirstChild(); if (first?.get() instanceof Expressions.SQLAliasField) { continue; } else if (first?.getFirstToken().getStr() === "@") { continue; } else if (first?.getChildren()[0].get() instanceof Expressions.Constant) { continue; } const message = `SELECT: "${source.concatTokens()}" must be escaped with @ in strict mode`; input.issues.push(syntaxIssue(input, node.getFirstToken(), message)); } } private static handleInto(node: ExpressionNode, input: SyntaxInput, fields: FieldList, dbSources: DatabaseTableSource[], _isSingle: boolean) { const intoTable = node.findDirectExpression(Expressions.SQLIntoTable); if (intoTable) { const inline = intoTable.findFirstExpression(Expressions.InlineData); if (inline) { InlineData.runSyntax(inline, input, this.buildTableType(fields, dbSources, input.scope)); } } const intoStructure = node.findDirectExpression(Expressions.SQLIntoStructure); if (intoStructure) { const inlineList = intoStructure.findAllExpressions(Expressions.InlineData); if (inlineList.length === 1) { InlineData.runSyntax(inlineList[0], input, this.buildStructureType(fields, dbSources, input.scope)); } else { for (const inline of inlineList) { // todo, for now these are voided InlineData.runSyntax(inline, input, VoidType.get("SELECT_todo1")); } } } const intoList = node.findDirectExpression(Expressions.SQLIntoList); if (intoList) { const isDynamic = fields.length === 1 && fields[0].expression.findDirectExpression(Expressions.Dynamic) !== undefined; const targets = intoList.findDirectExpressions(Expressions.SQLTarget); if (targets.length !== fields.length && isDynamic !== true) { const message = `number of fields selected vs list does not match`; input.issues.push(syntaxIssue(input, node.getFirstToken(), message)); return; } for (let i = 0; i < targets.length; i++) { const target = targets[i]; const field = fields[i]; const inline = target.findFirstExpression(Expressions.InlineData); if (inline) { if (isDynamic) { const message = `dynamic field list, inlining not possible`; input.issues.push(syntaxIssue(input, node.getFirstToken(), message)); return; } let type: AbstractType = VoidType.get("SELECT_todo2"); if (isSimple.test(field.code)) { for (const dbSource of dbSources) { if (dbSource === undefined) { continue; } const dbType = dbSource.parseType(input.scope.getRegistry()); if (dbType instanceof StructureType) { const found = dbType.getComponentByName(field.code); if (found) { type = found; break; } } } } InlineData.runSyntax(inline, input, type); } } } } private static checkFields(fields: FieldList, dbSources: DatabaseTableSource[], input: SyntaxInput, node: ExpressionNode) { if (dbSources.length > 1) { return; } const first = dbSources[0]; if (first === undefined) { // then its voided return; } const type = first.parseType(input.scope.getRegistry()); if (type instanceof VoidType || type instanceof UnknownType) { return; } if (!(type instanceof StructureType)) { const message = "checkFields, expected structure, " + type.constructor.name; input.issues.push(syntaxIssue(input, node.getFirstToken(), message)); return; } for (const field of fields) { if (field.code === "*") { continue; } if (isSimple.test(field.code) && type.getComponentByName(field.code) === undefined) { const message = `checkFields, field ${field.code} not found`; input.issues.push(syntaxIssue(input, node.getFirstToken(), message)); return; } } } private static buildStructureType(fields: FieldList, dbSources: DatabaseTableSource[], scope: CurrentScope): AbstractType | undefined { if (fields.length === 1 && dbSources.length === 1) { const dbType = dbSources[0]?.parseType(scope.getRegistry()); if (dbType === undefined) { const name = dbSources[0]?.getName() || "buildStructureTypeError"; if (scope.getRegistry().inErrorNamespace(name) === true) { return new UnknownType("Select, " + name + " not found"); } else { return VoidType.get(dbSources[0]?.getName()); } } if (fields[0].code === "*") { return dbType; } else { if (dbType instanceof StructureType) { const field = dbType.getComponentByName(fields[0].code); if (field) { return field; } else { // todo: aggregated/calculated values return VoidType.get("SELECT_todo11"); } } else { return VoidType.get("SELECT_todo10"); } } } else { return VoidType.get("SELECT_todo9"); } } private static buildTableType(fields: FieldList, dbSources: DatabaseTableSource[], scope: CurrentScope) { if (dbSources.length !== 1) { return VoidType.get("SELECT_todo3"); } if (dbSources[0] === undefined) { // then its a voided table return VoidType.get("SELECT_todo4"); } const dbType = dbSources[0].parseType(scope.getRegistry()); if (!(dbType instanceof StructureType)) { return VoidType.get("SELECT_todo5"); } if (fields.length === 1 && fields[0].code === "*") { return new TableType(dbType, {withHeader: false, keyType: TableKeyType.default}, undefined); } const allFieldsSimple = fields.every(f => isSimple.test(f.code)); if (allFieldsSimple === true) { const components: IStructureComponent[] = []; for (const field of fields) { const type = dbType.getComponentByName(field.code); if (type === undefined) { return VoidType.get("SELECT_todo6"); } components.push({name: field.code, type}); } return new TableType(new StructureType(components), {withHeader: false, keyType: TableKeyType.default}, undefined); } return VoidType.get("SELECT_todo7"); } private static findFields(node: ExpressionNode, input: SyntaxInput): FieldList { let expr: ExpressionNode | undefined = undefined; const ret = []; if (node.get() instanceof Expressions.SelectLoop) { expr = node.findFirstExpression(Expressions.SQLFieldListLoop); } else { expr = node.findFirstExpression(Expressions.SQLFieldList); } if (expr?.getFirstChild()?.get() instanceof Expressions.Dynamic) { Dynamic.runSyntax(expr.getFirstChild() as ExpressionNode, input); } for (const field of expr?.findDirectExpressionsMulti([Expressions.SQLField, Expressions.SQLFieldName]) || []) { let code = field.concatTokens().toUpperCase(); const as = field.findDirectExpression(Expressions.SQLAsName)?.concatTokens() || ""; if (as !== "") { code = code.replace(" AS " + as, ""); } ret.push({code, as, expression: field}); } if (ret.length === 0 && expr) { ret.push({code: expr.concatTokens(), as: "", expression: expr}); } return ret; } } |