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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 | 242x 242x 242x 242x 242x 242x 242x 210x 210x 210x 1751x 1751x 680x 680x 1071x 1071x 42x 42x 42x 332x 331x 42x 1x 1x 1x 41x 41x 289x 289x 41x 41x 1029x 4x 4x 4x 4x 4x 4x 1025x 1025x 219x 219x 218x 219x 219x 806x 806x 494x 494x 494x 2984x 312x 311x 311x 207x 207x 242x 207x 207x 207x 207x 207x 207x 17x 17x 2x 2x 13x 13x 163x 163x 161x 170x 23x 23x 23x 28x 28x 28x 5x 5x 5x 23x 23x 1x 25x 25x 15x 25x 3x 3x 1x 24x 18x 22x 22x 22x 2x 20x 7020x 537x 537x 537x 2610x 2610x 2288x 175x 175x 43x 3x 3x 3250x 3250x 259x 259x 2991x 30x 1x 1x 253x 253x 214x 258x 258x 219x 282x 278x 278x 278x 376x 376x 93x 93x 283x 14x 14x 14x 14x 269x 376x 269x 9x 9x 8x 8x 14x 7x 8x 8x 260x 15x 15x 15x 15x 245x 6x 6x 6x 6x 239x 399x 399x 403x 403x 40x 439x 439x 406x 406x 3x 442x 442x 411x 411x 5x 447x 447x 423x 423x 12x 459x 459x 423x 462x 462x 460x 460x 460x 294x 255x 9x 9x 9x 9x 246x 237x 233x 1x 232x 232x 56x 56x 55x 55x 55x 231x 3x 228x 4x 228x 228x 228x 692x 173x 158x 213x 228x 11x 202x 6x 2x 210x 210x 210x 49x 782x 580x 282x 439x 439x 364x 364x 293x 75x 75x 57x 56x 56x 244x 4x 4x 3x 43x 516x 2x 2x 345x 41x 40x 39x 290x 290x 290x 290x 290x 47x 46x 45x 290x 290x 290x 278x 278x 290x 440x 426x 61x 40x 40x 242x 242x 236x 236x 242x 19x 19x 19x 14x 14x 14x 19x 14x 14x 68x 68x 1x 1x 67x 51x 50x 14x 123x 79x 18x 18x 18x 44x 1x 16x 3x 20x 10x 1x 1x 3x 3x 3x 3x 1244x 386x 439x 10x 10x 8x 8x 2x 2x 342x 342x 44x 44x 44x 9x 298x 253x 45x 45x 45x 45x 44x 12x 12x 19x 19x 17x 6x 4x 19x 19x 19x 19x 19x 19x 19x 9x 9x 9x 9x 27x 326x 83x 16x 8x 16x 67x 67x 225x 51x 51x 15x 60x 60x 84x 7x 6x 60x 15x 71x 83x 83x 12x 59x 50x 51x | /**
* The SQL `WHERE` subset beatoraja random-course stages are written in, parsed and evaluated against a neutral song
* record. beatoraja runs each stage's clause against its song database (`SQLiteSongDatabaseAccessor.getSongDatas`),
* appending it to `... WHERE ` over `song` joined with the player's `score` and `scorelog` (and `information` when song
* information is on); Be-Music has no database, so the clause is evaluated per candidate with SQLite's comparison
* rules over the candidate's song columns, its best record (`score`) and its song information (`information`), and a
* trailing `ORDER BY … LIMIT …` orders and cuts the selection ({@link selectCourseCharts}).
*
* Anything outside the subset — an unknown column, a column the joined tables make ambiguous, a function call, a
* sub-query — is a parse error, so a stage with such a filter selects nothing (beatoraja's query fails and returns no
* song the same way) instead of throwing at play time.
*/
/**
* One chart as a random-course filter sees it: the `song` table columns of beatoraja's song database that Be-Music
* can supply, and the rows beatoraja joins to it, `score` (the player's best record) and `information` (the chart's
* song information). A missing text column reads as `''` and a missing integer column as `0`, the values beatoraja's
* `SongData` stores for a chart that leaves them unset.
*/
export interface CourseChartRecord {
md5?: string;
sha256?: string;
title?: string;
subtitle?: string;
artist?: string;
subartist?: string;
genre?: string;
tag?: string;
/** The chart file path. */
path?: string;
/** `#PLAYLEVEL` as an integer (0 when it is not a number). */
level?: number;
/** `#DIFFICULTY` (1 BEGINNER … 5 INSANE, 0 undefined). */
difficulty?: number;
/** beatoraja's `Mode.id`: 5, 7, 9, 10, 14, 25 (24 keys) or 50 (24 keys double). */
mode?: number;
notes?: number;
minbpm?: number;
maxbpm?: number;
/** Chart length in milliseconds. */
length?: number;
/** `#RANK` / bmson `judge_rank`. */
judge?: number;
/** beatoraja's `SongData.FEATURE_*` bits (LN 8, CN 16, HCN 32, mines 2, `#RANDOM` 4, STOP 64, scroll 128). */
feature?: number;
/** beatoraja's `SongData.CONTENT_*` bits (text 1, BGA 2, preview 4, no key sound 128). */
content?: number;
/**
* The `score` row: the player's best record on the chart. Absent for a chart never played, so every `score.*` column
* reads as NULL, as beatoraja's `LEFT OUTER JOIN` gives.
*/
score?: CourseChartScore;
/** The `information` row: the chart's song information. Absent, every `information.*` column reads as NULL. */
information?: CourseChartInformation;
}
/**
* The `score` columns Be-Music keeps (beatoraja's `ScoreDatabaseAccessor` table). A field left out reads as NULL.
* `exscore` is Be-Music's own: beatoraja keeps the judgment counts instead (`epg`, `lpg`, …), which Be-Music does not.
*/
export interface CourseChartScore {
/** The clear lamp as beatoraja's `ClearType.id`: 0 NO PLAY, 1 FAILED, 3 ASSIST EASY … 10 MAX. */
clear?: number;
exscore?: number;
/** The max combo. */
combo?: number;
/** The fewest BP. */
minbp?: number;
playcount?: number;
/** The chart's note count. */
notes?: number;
}
/** The `information` columns (beatoraja's `SongInformation`). A field left out reads as NULL. */
export interface CourseChartInformation {
/** Taps on the keys. */
n?: number;
/** Long notes on the keys (a CN / HCN counts its end too). */
ln?: number;
/** Taps on the scratch. */
s?: number;
/** Long notes on the scratch. */
ls?: number;
total?: number;
/** Notes per second over the busy seconds. */
density?: number;
/** The busiest second. */
peakdensity?: number;
/** The busiest 5-second average from the TOTAL border on. */
enddensity?: number;
mainbpm?: number;
}
/** A table a filter column belongs to. */
export type CourseChartTable = 'song' | 'score' | 'information';
type CourseChartTextColumn =
| 'md5'
| 'sha256'
| 'title'
| 'subtitle'
| 'artist'
| 'subartist'
| 'genre'
| 'tag'
| 'path';
type CourseChartSongColumn = Exclude<keyof CourseChartRecord, 'score' | 'information'>;
type CourseChartIntegerColumn = Exclude<CourseChartSongColumn, CourseChartTextColumn>;
/** A column a filter can read: a `song` column ({@link CourseChartRecord}), a `score` or an `information` one. */
export type CourseChartColumn = CourseChartSongColumn | keyof CourseChartScore | keyof CourseChartInformation;
type ColumnType = 'text' | 'integer' | 'real';
const SONG_COLUMN_TYPES: Readonly<Record<CourseChartSongColumn, ColumnType>> = {
md5: 'text',
sha256: 'text',
title: 'text',
subtitle: 'text',
artist: 'text',
subartist: 'text',
genre: 'text',
tag: 'text',
path: 'text',
level: 'integer',
difficulty: 'integer',
mode: 'integer',
notes: 'integer',
minbpm: 'integer',
maxbpm: 'integer',
length: 'integer',
judge: 'integer',
feature: 'integer',
content: 'integer',
} satisfies Record<CourseChartTextColumn, 'text'> & Record<CourseChartIntegerColumn, 'integer'>;
/** `score.sha256` / `information.sha256` read the song's hash on a row that exists. */
const SCORE_COLUMN_TYPES: Readonly<Record<keyof CourseChartScore | 'sha256', ColumnType>> = {
sha256: 'text',
clear: 'integer',
exscore: 'integer',
combo: 'integer',
minbp: 'integer',
playcount: 'integer',
notes: 'integer',
};
const INFORMATION_COLUMN_TYPES: Readonly<Record<keyof CourseChartInformation | 'sha256', ColumnType>> = {
sha256: 'text',
n: 'integer',
ln: 'integer',
s: 'integer',
ls: 'integer',
total: 'real',
density: 'real',
peakdensity: 'real',
enddensity: 'real',
mainbpm: 'real',
};
const COLUMN_TYPES: Readonly<Record<CourseChartTable, Readonly<Record<string, ColumnType>>>> = {
song: SONG_COLUMN_TYPES,
score: SCORE_COLUMN_TYPES,
information: INFORMATION_COLUMN_TYPES,
};
/**
* Every column of the tables beatoraja joins (`SQLiteSongDatabaseAccessor`, `ScoreDatabaseAccessor`,
* `ScoreLogDatabaseAccessor`, `SongInformationAccessor` at beatoraja 83574048), whether Be-Music supplies it or not,
* plus Be-Music's `score.exscore`. An unqualified column resolves to the one table that has it, as SQLite does; one
* that more than one table has is refused (`ambiguous column name`), so the stage selects nothing as in beatoraja:
* write `song.mode`, not `mode`. A column Be-Music has no source for (`song.date`, `score.epg`, every `scorelog`
* column, …) is an unknown column rather than a made-up value.
*/
const BEATORAJA_COLUMNS: Readonly<Record<CourseChartTable | 'scorelog', readonly string[]>> = {
song: [
'md5',
'sha256',
'title',
'subtitle',
'genre',
'artist',
'subartist',
'tag',
'path',
'folder',
'stagefile',
'banner',
'backbmp',
'preview',
'parent',
'level',
'difficulty',
'maxbpm',
'minbpm',
'length',
'mode',
'judge',
'feature',
'content',
'date',
'favorite',
'adddate',
'notes',
'charthash',
],
score: [
'sha256',
'mode',
'clear',
'epg',
'lpg',
'egr',
'lgr',
'egd',
'lgd',
'ebd',
'lbd',
'epr',
'lpr',
'ems',
'lms',
'notes',
'combo',
'minbp',
'avgjudge',
'playcount',
'clearcount',
'trophy',
'ghost',
'option',
'seed',
'random',
'date',
'state',
'scorehash',
'exscore',
],
scorelog: [
'sha256',
'mode',
'clear',
'oldclear',
'score',
'oldscore',
'combo',
'oldcombo',
'minbp',
'oldminbp',
'avgjudge',
'oldavgjudge',
'date',
],
information: [
'sha256',
'n',
'ln',
's',
'ls',
'total',
'density',
'peakdensity',
'enddensity',
'mainbpm',
'distribution',
'speedchange',
'lanenotes',
],
};
const TABLE_NAMES = Object.keys(BEATORAJA_COLUMNS) as ReadonlyArray<CourseChartTable | 'scorelog'>;
/** An SQL value: SQLite's INTEGER / REAL (`number`), TEXT (`string`) or NULL. */
export type CourseSQLValue = number | string | null;
/** The parsed form of a filter. */
export type CourseChartFilterExpr =
| {
type: 'literal';
value: CourseSQLValue;
/** A number written with a `.` or an exponent: SQLite REAL. */ real?: true;
}
| { type: 'column'; table: CourseChartTable; column: CourseChartColumn }
| { type: 'unary'; op: '-' | '+' | 'not'; operand: CourseChartFilterExpr }
| {
type: 'binary';
op: 'or' | 'and' | '=' | '!=' | '<' | '<=' | '>' | '>=' | '&' | '|' | '+' | '-' | '*' | '/' | '%' | '||';
left: CourseChartFilterExpr;
right: CourseChartFilterExpr;
}
| { type: 'in'; operand: CourseChartFilterExpr; values: CourseChartFilterExpr[]; negated: boolean }
| {
type: 'like';
operand: CourseChartFilterExpr;
pattern: CourseChartFilterExpr;
escape?: CourseChartFilterExpr;
negated: boolean;
}
| {
type: 'between';
operand: CourseChartFilterExpr;
low: CourseChartFilterExpr;
high: CourseChartFilterExpr;
negated: boolean;
}
| { type: 'isNull'; operand: CourseChartFilterExpr; negated: boolean };
/** What an `ORDER BY` term sorts by: an expression, or SQLite's `RANDOM()`, which takes the injected random source. */
export type CourseChartOrderKey = CourseChartFilterExpr | { type: 'random' };
/** One `ORDER BY` term. */
export interface CourseChartOrderTerm {
key: CourseChartOrderKey;
descending: boolean;
/** `NULLS FIRST` / `NULLS LAST`; absent, SQLite's default: NULL sorts first ascending and last descending. */
nulls?: 'first' | 'last';
}
/** A whole stage filter: the `WHERE` clause and the trailing `ORDER BY` / `LIMIT` / `OFFSET` it may carry. */
export interface CourseChartQuery {
where: CourseChartFilterExpr;
/** Empty without `ORDER BY`: the selection keeps the candidates' order. */
orderBy: CourseChartOrderTerm[];
/** `LIMIT`; absent (or negative in the SQL) for none. */
limit?: number;
/** `OFFSET` (or the first number of `LIMIT m, n`); 0 without one, a negative one reading as 0, as in SQLite. */
offset: number;
}
export type CourseChartFilterParseResult =
| {
ok: true;
/** The `WHERE` clause alone ({@link evaluateCourseChartFilter}). */
filter: CourseChartFilterExpr;
/** The whole filter, ordering and limit included ({@link selectCourseCharts}). */
query: CourseChartQuery;
}
| { ok: false; error: string };
type Token =
| { kind: 'number'; value: number; real: boolean; at: number }
| { kind: 'string'; value: string; at: number }
| { kind: 'word'; value: string; at: number }
| { kind: 'quoted'; value: string; at: number }
| { kind: 'op'; value: string; at: number }
| { kind: 'end'; at: number };
class FilterSyntaxError extends Error {}
const OPERATORS = [
'<>',
'!=',
'==',
'<=',
'>=',
'||',
'=',
'<',
'>',
'(',
')',
',',
'.',
'+',
'-',
'*',
'/',
'%',
'&',
'|',
];
function tokenize(sql: string): Token[] {
const tokens: Token[] = [];
let i = 0;
while (i < sql.length) {
const c = sql[i]!;
if (/\s/u.test(c)) {
i++;
continue;
}
const at = i;
if (c === "'") {
let value = '';
i++;
for (;;) {
if (i >= sql.length) throw new FilterSyntaxError(`unterminated string at ${at}`);
if (sql[i] === "'") {
if (sql[i + 1] === "'") {
value += "'";
i += 2;
continue;
}
i++;
break;
}
value += sql[i];
i++;
}
tokens.push({ kind: 'string', value, at });
continue;
}
if (c === '"' || c === '`' || c === '[') {
const close = c === '[' ? ']' : c;
const end = sql.indexOf(close, i + 1);
Iif (end < 0) throw new FilterSyntaxError(`unterminated identifier at ${at}`);
tokens.push({ kind: 'quoted', value: sql.slice(i + 1, end), at });
i = end + 1;
continue;
}
const number = /^(?:0[xX][0-9a-fA-F]+|(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)/u.exec(sql.slice(i));
if (number !== null) {
const text = number[0];
if (/^[A-Za-z_]/u.test(sql.slice(i + text.length))) throw new FilterSyntaxError(`bad number at ${at}`);
tokens.push({ kind: 'number', value: Number(text), real: !/^0[xX]/u.test(text) && /[.eE]/u.test(text), at });
i += text.length;
continue;
}
const word = /^[A-Za-z_][A-Za-z0-9_]*/u.exec(sql.slice(i));
if (word !== null) {
tokens.push({ kind: 'word', value: word[0], at });
i += word[0].length;
continue;
}
const op = OPERATORS.find((candidate) => sql.startsWith(candidate, i));
if (op === undefined) throw new FilterSyntaxError(`unexpected '${c}' at ${at}`);
tokens.push({ kind: 'op', value: op, at });
i += op.length;
}
tokens.push({ kind: 'end', at: sql.length });
return tokens;
}
const RESERVED = new Set(['and', 'or', 'not', 'in', 'like', 'between', 'is', 'null', 'escape', 'order', 'limit']);
class Parser {
private index = 0;
// A plain field, not a constructor parameter property: Node's type stripping (the bench runner) rejects those.
private readonly tokens: Token[];
constructor(tokens: Token[]) {
this.tokens = tokens;
}
parse(): CourseChartQuery {
const where = this.or();
const orderBy = this.orderBy();
let limit: number | undefined;
let offset = 0;
if (this.acceptKeyword('limit')) {
const first = this.integer('LIMIT');
if (this.acceptOp(',') !== undefined) {
// `LIMIT m, n`: SQLite reads the first number as the offset.
offset = first;
limit = this.integer('LIMIT');
} else {
limit = first;
if (this.acceptKeyword('offset')) offset = this.integer('OFFSET');
}
}
const next = this.peek();
if (next.kind !== 'end') throw new FilterSyntaxError(`unexpected ${describe(next)} at ${next.at}`);
return {
where,
orderBy,
...(limit !== undefined && limit >= 0 ? { limit } : {}),
offset: Math.max(0, offset),
};
}
/** `ORDER BY term [ASC | DESC] [NULLS FIRST | LAST], …`, or nothing. */
private orderBy(): CourseChartOrderTerm[] {
if (!this.acceptKeyword('order')) return [];
this.expectKeyword('by');
const terms: CourseChartOrderTerm[] = [];
do {
let key: CourseChartOrderKey;
const at = this.peek().at;
const after = this.peek(1);
if (this.isKeyword('random') && after.kind === 'op' && after.value === '(') {
this.index += 2;
this.expectOp(')');
key = { type: 'random' };
} else {
key = this.or();
// SQLite reads a bare integer as a result-column number, which depends on beatoraja's column list.
if (key.type === 'literal' && typeof key.value === 'number') {
throw new FilterSyntaxError(`ORDER BY a column number is not supported at ${at}`);
}
}
let descending = false;
if (this.acceptKeyword('desc')) descending = true;
else this.acceptKeyword('asc');
let nulls: 'first' | 'last' | undefined;
if (this.acceptKeyword('nulls')) {
Iif (this.acceptKeyword('first')) nulls = 'first';
else if (this.acceptKeyword('last')) nulls = 'last';
else throw new FilterSyntaxError(`expected FIRST or LAST at ${this.peek().at}`);
}
terms.push({ key, descending, ...(nulls !== undefined ? { nulls } : {}) });
} while (this.acceptOp(',') !== undefined);
return terms;
}
/** A `LIMIT` / `OFFSET` count: an integer literal, optionally signed. */
private integer(clause: string): number {
const sign = this.acceptOp('-', '+');
const token = this.next();
if (token.kind !== 'number' || token.real || !Number.isSafeInteger(token.value)) {
throw new FilterSyntaxError(`${clause} takes an integer at ${token.at}`);
}
return sign === '-' ? -token.value : token.value;
}
private peek(offset = 0): Token {
return this.tokens[Math.min(this.index + offset, this.tokens.length - 1)]!;
}
private next(): Token {
const token = this.peek();
if (token.kind !== 'end') this.index++;
return token;
}
private isKeyword(name: string, offset = 0): boolean {
const token = this.peek(offset);
return token.kind === 'word' && token.value.toLowerCase() === name;
}
private acceptKeyword(name: string): boolean {
if (!this.isKeyword(name)) return false;
this.index++;
return true;
}
private expectKeyword(name: string): void {
if (!this.acceptKeyword(name)) {
const token = this.peek();
throw new FilterSyntaxError(`expected ${name.toUpperCase()} at ${token.at}`);
}
}
private acceptOp(...ops: string[]): string | undefined {
const token = this.peek();
if (token.kind === 'op' && ops.includes(token.value)) {
this.index++;
return token.value;
}
return undefined;
}
private expectOp(op: string): void {
if (this.acceptOp(op) === undefined) {
const token = this.peek();
throw new FilterSyntaxError(`expected '${op}' at ${token.at}`);
}
}
private or(): CourseChartFilterExpr {
let left = this.and();
while (this.acceptKeyword('or')) left = { type: 'binary', op: 'or', left, right: this.and() };
return left;
}
private and(): CourseChartFilterExpr {
let left = this.not();
while (this.acceptKeyword('and')) left = { type: 'binary', op: 'and', left, right: this.not() };
return left;
}
private not(): CourseChartFilterExpr {
if (this.acceptKeyword('not')) return { type: 'unary', op: 'not', operand: this.not() };
return this.equality();
}
/** SQLite's equality tier: `=`, `==`, `!=`, `<>`, `IS [NOT] NULL`, `[NOT] IN`, `[NOT] LIKE`, `[NOT] BETWEEN`. */
private equality(): CourseChartFilterExpr {
let left = this.relational();
for (;;) {
const op = this.acceptOp('=', '==', '!=', '<>');
if (op !== undefined) {
left = { type: 'binary', op: op === '=' || op === '==' ? '=' : '!=', left, right: this.relational() };
continue;
}
if (this.acceptKeyword('is')) {
const negated = this.acceptKeyword('not');
this.expectKeyword('null');
left = { type: 'isNull', operand: left, negated };
continue;
}
const negated = this.isKeyword('not') && ['in', 'like', 'between'].some((name) => this.isKeyword(name, 1));
if (negated) this.index++;
if (this.acceptKeyword('in')) {
this.expectOp('(');
if (this.isKeyword('select')) throw new FilterSyntaxError(`sub-queries are not supported`);
const values: CourseChartFilterExpr[] = [];
if (this.acceptOp(')') === undefined) {
do values.push(this.or());
while (this.acceptOp(',') !== undefined);
this.expectOp(')');
}
left = { type: 'in', operand: left, values, negated };
continue;
}
if (this.acceptKeyword('like')) {
const pattern = this.relational();
const escape = this.acceptKeyword('escape') ? this.relational() : undefined;
left = { type: 'like', operand: left, pattern, ...(escape !== undefined ? { escape } : {}), negated };
continue;
}
if (this.acceptKeyword('between')) {
const low = this.relational();
this.expectKeyword('and');
left = { type: 'between', operand: left, low, high: this.relational(), negated };
continue;
}
return left;
}
}
private relational(): CourseChartFilterExpr {
let left = this.bitwise();
for (;;) {
const op = this.acceptOp('<', '<=', '>', '>=') as '<' | '<=' | '>' | '>=' | undefined;
if (op === undefined) return left;
left = { type: 'binary', op, left, right: this.bitwise() };
}
}
private bitwise(): CourseChartFilterExpr {
let left = this.additive();
for (;;) {
const op = this.acceptOp('&', '|') as '&' | '|' | undefined;
if (op === undefined) return left;
left = { type: 'binary', op, left, right: this.additive() };
}
}
private additive(): CourseChartFilterExpr {
let left = this.multiplicative();
for (;;) {
const op = this.acceptOp('+', '-') as '+' | '-' | undefined;
if (op === undefined) return left;
left = { type: 'binary', op, left, right: this.multiplicative() };
}
}
private multiplicative(): CourseChartFilterExpr {
let left = this.concat();
for (;;) {
const op = this.acceptOp('*', '/', '%') as '*' | '/' | '%' | undefined;
if (op === undefined) return left;
left = { type: 'binary', op, left, right: this.concat() };
}
}
private concat(): CourseChartFilterExpr {
let left = this.unary();
while (this.acceptOp('||') !== undefined) left = { type: 'binary', op: '||', left, right: this.unary() };
return left;
}
private unary(): CourseChartFilterExpr {
const op = this.acceptOp('-', '+') as '-' | '+' | undefined;
if (op !== undefined) return { type: 'unary', op, operand: this.unary() };
return this.primary();
}
private primary(): CourseChartFilterExpr {
const token = this.next();
if (token.kind === 'number') return { type: 'literal', value: token.value, ...(token.real ? { real: true } : {}) };
if (token.kind === 'string') return { type: 'literal', value: token.value };
if (token.kind === 'op' && token.value === '(') {
Iif (this.isKeyword('select')) throw new FilterSyntaxError(`sub-queries are not supported`);
const inner = this.or();
this.expectOp(')');
return inner;
}
if (token.kind === 'word' && token.value.toLowerCase() === 'null') return { type: 'literal', value: null };
if (token.kind === 'word' || token.kind === 'quoted') {
if (token.kind === 'word' && RESERVED.has(token.value.toLowerCase())) {
throw new FilterSyntaxError(`unexpected ${token.value.toUpperCase()} at ${token.at}`);
}
let name = token.value;
let table: string | undefined;
if (this.acceptOp('.') !== undefined) {
table = name.toLowerCase();
if (!(TABLE_NAMES as readonly string[]).includes(table)) throw new FilterSyntaxError(`no such table: ${name}`);
const column = this.next();
Iif (column.kind !== 'word' && column.kind !== 'quoted') {
throw new FilterSyntaxError(`expected a column name at ${column.at}`);
}
name = column.value;
}
if (this.peek().kind === 'op' && (this.peek() as { value: string }).value === '(') {
throw new FilterSyntaxError(`functions are not supported: ${name}`);
}
return resolveColumn(table, name);
}
throw new FilterSyntaxError(`unexpected ${describe(token)} at ${token.at}`);
}
}
/**
* The column `table.name` (or `name` alone) names, resolved as SQLite resolves it over beatoraja's joined tables:
* unqualified, it must belong to exactly one of them. A column Be-Music has no source for is an unknown column.
*/
function resolveColumn(table: string | undefined, name: string): CourseChartFilterExpr {
const column = name.toLowerCase();
let owner = table;
if (owner === undefined) {
const owners = TABLE_NAMES.filter((candidate) => BEATORAJA_COLUMNS[candidate].includes(column));
if (owners.length > 1) throw new FilterSyntaxError(`ambiguous column name: ${name}`);
owner = owners[0] ?? 'song';
}
const label = table === undefined ? name : `${table}.${name}`;
if (owner === 'scorelog' || !Object.hasOwn(COLUMN_TYPES[owner as CourseChartTable], column)) {
throw new FilterSyntaxError(`unknown column '${label}'`);
}
return { type: 'column', table: owner as CourseChartTable, column: column as CourseChartColumn };
}
function describe(token: Token): string {
if (token.kind === 'end') return 'end of filter';
return `'${String(token.value)}'`;
}
/**
* Parses a random-course stage filter. Supported: column names (optionally qualified with `song.`, `score.` or
* `information.`, quoted with `"`, `` ` `` or `[]`), string / number / `NULL` literals, `=` `==` `!=` `<>` `<` `<=` `>` `>=`, `AND` `OR` `NOT`, parentheses,
* `[NOT] IN (…)`, `[NOT] LIKE … [ESCAPE …]`, `[NOT] BETWEEN … AND …`, `IS [NOT] NULL`, `+ - * / %`, `& |` and `||`,
* then an optional `ORDER BY expr | RANDOM() [ASC | DESC] [NULLS FIRST | LAST], …` and `LIMIT n [OFFSET m]` /
* `LIMIT m, n`. Anything else — an unknown column, an unqualified column beatoraja's joined tables share (`mode`,
* `notes`, `sha256`, …), a column Be-Music has no source for (`scorelog.*`, `score.epg`, …), a function, a sub-query — gives `{ ok: false }` with the reason.
*/
export function parseCourseChartFilter(sql: string): CourseChartFilterParseResult {
try {
const query = new Parser(tokenize(sql)).parse();
return { ok: true, filter: query.where, query };
} catch (error) {
Eif (error instanceof FilterSyntaxError) return { ok: false, error: error.message };
throw error;
}
}
/** A column's affinity: TEXT, or NUMERIC for an INTEGER or a REAL column; `none` for any other expression. */
type Affinity = 'text' | 'numeric' | 'none';
function columnType(expr: Extract<CourseChartFilterExpr, { type: 'column' }>): ColumnType {
return COLUMN_TYPES[expr.table][expr.column]!;
}
function affinityOf(expr: CourseChartFilterExpr): Affinity {
if (expr.type !== 'column') return 'none';
return columnType(expr) === 'text' ? 'text' : 'numeric';
}
function columnValue(
record: CourseChartRecord,
expr: Extract<CourseChartFilterExpr, { type: 'column' }>,
): CourseSQLValue {
const type = columnType(expr);
if (expr.table === 'song') {
const value = record[expr.column as CourseChartSongColumn];
if (type === 'text') return typeof value === 'string' ? value : '';
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
}
// A joined row the record lacks reads as NULL in every column, as an outer join gives; so does a field it lacks.
const row = record[expr.table];
if (row === undefined) return null;
if (expr.column === 'sha256') return record.sha256 ?? '';
const value = (row as Readonly<Record<string, unknown>>)[expr.column];
return typeof value === 'number' && Number.isFinite(value) ? value : null;
}
/** SQLite's NUMERIC affinity: a text value that reads as a number in full becomes that number. */
function applyNumericAffinity(value: CourseSQLValue): CourseSQLValue {
if (typeof value !== 'string') return value;
const trimmed = value.trim();
if (trimmed.length === 0 || !/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/u.test(trimmed)) return value;
return Number(trimmed);
}
function toText(value: number | string): string {
return typeof value === 'string' ? value : String(value);
}
/** SQLite's conversion of a value to a number for arithmetic: the longest numeric prefix of a text, else 0. */
function toNumber(value: number | string): number {
if (typeof value === 'number') return value;
const prefix = /^\s*[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/u.exec(value);
return prefix === null ? 0 : Number(prefix[0]);
}
/** SQLite's sort order: numbers before text, numbers by value, text by code unit (BINARY collation). */
function compareValues(a: number | string, b: number | string): number {
if (typeof a === 'number' && typeof b === 'number') return a < b ? -1 : a > b ? 1 : 0;
if (typeof a === 'number') return -1;
if (typeof b === 'number') return 1;
return a < b ? -1 : a > b ? 1 : 0;
}
/** The operands of a comparison with SQLite's affinity rules applied (INTEGER beats TEXT beats none). */
function comparisonOperands(
left: CourseChartFilterExpr,
right: CourseChartFilterExpr,
record: CourseChartRecord,
): [CourseSQLValue, CourseSQLValue] {
let a = evaluate(left, record);
let b = evaluate(right, record);
const la = affinityOf(left);
const ra = affinityOf(right);
if (la === 'numeric' && ra !== 'numeric') b = applyNumericAffinity(b);
else if (ra === 'numeric' && la !== 'numeric') a = applyNumericAffinity(a);
else if (la === 'text' && ra === 'none' && typeof b === 'number') b = toText(b);
else Iif (ra === 'text' && la === 'none' && typeof a === 'number') a = toText(a);
return [a, b];
}
function compare(
op: '=' | '!=' | '<' | '<=' | '>' | '>=',
left: CourseChartFilterExpr,
right: CourseChartFilterExpr,
record: CourseChartRecord,
): CourseSQLValue {
const [a, b] = comparisonOperands(left, right, record);
if (a === null || b === null) return null;
const order = compareValues(a, b);
const result =
op === '='
? order === 0
: op === '!='
? order !== 0
: op === '<'
? order < 0
: op === '<='
? order <= 0
: op === '>'
? order > 0
: order >= 0;
return result ? 1 : 0;
}
/** Three-valued truth: `true`, `false`, or `null` (unknown). */
function truth(value: CourseSQLValue): boolean | null {
if (value === null) return null;
return toNumber(value) !== 0;
}
function fromTruth(value: boolean | null): CourseSQLValue {
return value === null ? null : value ? 1 : 0;
}
/** SQLite folds ASCII letters only for `LIKE`. */
function asciiLower(text: string): string {
// Most titles and patterns have no capital, or only ASCII: return those as they are, or through `toLowerCase`,
// which folds exactly the ASCII capitals in ASCII-only text. Only text with non-ASCII characters needs the
// letter-by-letter fold that leaves them alone.
let upper = false;
for (let index = 0; index < text.length; index += 1) {
const code = text.charCodeAt(index);
if (code > 0x7f) return text.replace(/[A-Z]/gu, (c) => c.toLowerCase());
if (code >= 0x41 && code <= 0x5a) upper = true;
}
return upper ? text.toLowerCase() : text;
}
const likePatterns = new Map<string, RegExp>();
function like(text: string, pattern: string, escape: string | undefined): boolean {
const key = `${escape ?? ''}\u0000${pattern}`;
let regex = likePatterns.get(key);
if (regex === undefined) {
regex = likePattern(pattern, escape);
// A filter set has a handful of patterns; the cap only stops an adversarial one from growing without bound.
Iif (likePatterns.size >= 256) likePatterns.clear();
likePatterns.set(key, regex);
}
return regex.test(text);
}
function likePattern(pattern: string, escape: string | undefined): RegExp {
let source = '^';
for (let i = 0; i < pattern.length; i++) {
const c = pattern[i]!;
if (escape !== undefined && c === escape && i + 1 < pattern.length) {
i++;
source += pattern[i]!.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
} else if (c === '%') source += '[\\s\\S]*';
else if (c === '_') source += '[\\s\\S]';
else source += c.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
}
return new RegExp(`${source}$`, 'u');
}
/** Whether an arithmetic operand is REAL by type: a REAL literal or column, or arithmetic over one. */
function isRealExpr(expr: CourseChartFilterExpr): boolean {
if (expr.type === 'literal') return expr.real === true;
if (expr.type === 'column') return columnType(expr) === 'real';
Iif (expr.type === 'unary') return expr.op !== 'not' && isRealExpr(expr.operand);
Eif (expr.type === 'binary' && ['+', '-', '*', '/'].includes(expr.op))
return isRealExpr(expr.left) || isRealExpr(expr.right);
return false;
}
function arithmetic(op: '+' | '-' | '*' | '/' | '%' | '&' | '|', a: number, b: number, real: boolean): CourseSQLValue {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b === 0) return null;
return !real && Number.isInteger(a) && Number.isInteger(b) ? Math.trunc(a / b) : a / b;
case '%': {
const divisor = Math.trunc(b);
return divisor === 0 ? null : Math.trunc(a) % divisor;
}
case '&':
case '|': {
// SQLite's bitwise operators work on 64-bit integers.
Iif (!Number.isFinite(a) || !Number.isFinite(b)) return null;
const x = BigInt.asIntN(64, BigInt(Math.trunc(a)));
const y = BigInt.asIntN(64, BigInt(Math.trunc(b)));
return Number(op === '&' ? x & y : x | y);
}
}
}
function evaluate(expr: CourseChartFilterExpr, record: CourseChartRecord): CourseSQLValue {
switch (expr.type) {
case 'literal':
return expr.value;
case 'column':
return columnValue(record, expr);
case 'unary': {
const value = evaluate(expr.operand, record);
if (expr.op === 'not') {
const t = truth(value);
return fromTruth(t === null ? null : !t);
}
Iif (value === null) return null;
return expr.op === '-' ? -toNumber(value) : expr.op === '+' ? value : null;
}
case 'binary': {
const { op } = expr;
if (op === 'and' || op === 'or') {
const a = truth(evaluate(expr.left, record));
const b = truth(evaluate(expr.right, record));
if (op === 'and') return fromTruth(a === false || b === false ? false : a === null || b === null ? null : true);
return fromTruth(a === true || b === true ? true : a === null || b === null ? null : false);
}
if (op === '=' || op === '!=' || op === '<' || op === '<=' || op === '>' || op === '>=') {
return compare(op, expr.left, expr.right, record);
}
const a = evaluate(expr.left, record);
const b = evaluate(expr.right, record);
Iif (a === null || b === null) return null;
if (op === '||') return toText(a) + toText(b);
return arithmetic(op, toNumber(a), toNumber(b), isRealExpr(expr.left) || isRealExpr(expr.right));
}
case 'in': {
let sawNull = false;
for (const value of expr.values) {
const result = compare('=', expr.operand, value, record);
if (result === null) sawNull = true;
else if (result === 1) return expr.negated ? 0 : 1;
}
if (evaluate(expr.operand, record) === null || sawNull) return null;
return expr.negated ? 1 : 0;
}
case 'like': {
const text = evaluate(expr.operand, record);
const pattern = evaluate(expr.pattern, record);
const escape = expr.escape !== undefined ? evaluate(expr.escape, record) : undefined;
Iif (text === null || pattern === null || escape === null) return null;
const escapeChar = escape !== undefined ? toText(escape) : undefined;
const matched = like(
asciiLower(toText(text)),
asciiLower(toText(pattern)),
escapeChar !== undefined ? asciiLower(escapeChar) : undefined,
);
return matched !== expr.negated ? 1 : 0;
}
case 'between': {
const low = truth(compare('>=', expr.operand, expr.low, record));
const high = truth(compare('<=', expr.operand, expr.high, record));
const inside = low === false || high === false ? false : low === null || high === null ? null : true;
return fromTruth(inside === null ? null : inside !== expr.negated);
}
case 'isNull':
return (evaluate(expr.operand, record) === null) !== expr.negated ? 1 : 0;
}
}
/** Whether a record passes a parsed filter: the clause is true (an unknown / NULL result does not pass, as in SQL). */
export function evaluateCourseChartFilter(filter: CourseChartFilterExpr, record: CourseChartRecord): boolean {
return truth(evaluate(filter, record)) === true;
}
/** SQLite's `ORDER BY` order of two values for one term: NULL first ascending, last descending, unless `nulls` says. */
function compareOrderValues(a: CourseSQLValue, b: CourseSQLValue, term: CourseChartOrderTerm): number {
if (a === null || b === null) {
if (a === b) return 0;
const nullsFirst = term.nulls !== undefined ? term.nulls === 'first' : !term.descending;
return (a === null) === nullsFirst ? -1 : 1;
}
const order = compareValues(a, b);
return term.descending ? -order : order;
}
/**
* The candidates a whole stage filter selects, in its order: those its `WHERE` clause passes, sorted by its
* `ORDER BY` (ties keep the candidates' order, so the result is deterministic), then cut by its `OFFSET` / `LIMIT`.
* `recordOf` gives each candidate's {@link CourseChartRecord}. `ORDER BY RANDOM()` draws one value per selected
* candidate, in the candidates' order, from `random` (values in `[0, 1)` like `Math.random`), and throws without it.
*/
export function selectCourseCharts<T>(
query: CourseChartQuery,
candidates: readonly T[],
recordOf: (candidate: T) => CourseChartRecord,
random?: () => number,
): T[] {
const selected = candidates.filter((candidate) => evaluateCourseChartFilter(query.where, recordOf(candidate)));
let ordered = selected;
if (query.orderBy.length > 0) {
const keyed = selected.map((candidate) => {
const record = recordOf(candidate);
const keys = query.orderBy.map(({ key }): CourseSQLValue => {
if (key.type !== 'random') return evaluate(key, record);
if (random === undefined) throw new Error('ORDER BY RANDOM() needs a random source');
return random();
});
return { candidate, keys };
});
keyed.sort((a, b) => {
for (const [index, term] of query.orderBy.entries()) {
const order = compareOrderValues(a.keys[index]!, b.keys[index]!, term);
if (order !== 0) return order;
}
return 0;
});
ordered = keyed.map(({ candidate }) => candidate);
}
const end = query.limit !== undefined ? query.offset + query.limit : undefined;
return ordered.slice(query.offset, end);
}
|