@atscript/db-mysql 0.1.173 → 0.1.175

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -634,6 +634,72 @@ function buildWhere(filter, opts) {
634
634
  return (0, _atscript_db_sql_tools.buildWhere)(mysqlDialect, filter, opts);
635
635
  }
636
636
  //#endregion
637
+ //#region src/innodb-word-chars.ts
638
+ /** A regular-expression character class of InnoDB's word characters (`u` flag). */
639
+ const INNODB_WORD_CLASS = [
640
+ "[",
641
+ "\\u0030-\\u0039\\u0041-\\u005A\\u005F\\u0061-\\u007A\\u00AA\\u00B2\\u00B3\\u00B5\\u00B9\\u00BA",
642
+ "\\u00BC-\\u00BE\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02C1\\u02C6-\\u02D1\\u02E0-\\u02E4\\u02EE",
643
+ "\\u0300-\\u036F\\u037A-\\u037D\\u0386\\u0388-\\u038A\\u038C\\u038E-\\u03A1\\u03A3-\\u03CE",
644
+ "\\u03D0-\\u03F5\\u03F7-\\u0481\\u0483-\\u0486\\u0488-\\u0513\\u0531-\\u0556\\u0559\\u0561-\\u0587",
645
+ "\\u0591-\\u05BD\\u05BF\\u05C1\\u05C2\\u05C4\\u05C5\\u05C7\\u05D0-\\u05EA\\u05F0-\\u05F2",
646
+ "\\u0610-\\u0615\\u0621-\\u063A\\u0640-\\u065E\\u0660-\\u0669\\u066E-\\u06D3\\u06D5-\\u06DC",
647
+ "\\u06DE-\\u06E8\\u06EA-\\u06FC\\u06FF\\u0710-\\u074A\\u074D-\\u076D\\u0780-\\u07B1\\u07C0-\\u07F5",
648
+ "\\u07FA\\u0901-\\u0939\\u093C-\\u094D\\u0950-\\u0954\\u0958-\\u0963\\u0966-\\u096F\\u097B-\\u097F",
649
+ "\\u0981-\\u0983\\u0985-\\u098C\\u098F\\u0990\\u0993-\\u09A8\\u09AA-\\u09B0\\u09B2\\u09B6-\\u09B9",
650
+ "\\u09BC-\\u09C4\\u09C7\\u09C8\\u09CB-\\u09CE\\u09D7\\u09DC\\u09DD\\u09DF-\\u09E3\\u09E6-\\u09F1",
651
+ "\\u09F4-\\u09F9\\u0A01-\\u0A03\\u0A05-\\u0A0A\\u0A0F\\u0A10\\u0A13-\\u0A28\\u0A2A-\\u0A30",
652
+ "\\u0A32\\u0A33\\u0A35\\u0A36\\u0A38\\u0A39\\u0A3C\\u0A3E-\\u0A42\\u0A47\\u0A48\\u0A4B-\\u0A4D",
653
+ "\\u0A59-\\u0A5C\\u0A5E\\u0A66-\\u0A74\\u0A81-\\u0A83\\u0A85-\\u0A8D\\u0A8F-\\u0A91\\u0A93-\\u0AA8",
654
+ "\\u0AAA-\\u0AB0\\u0AB2\\u0AB3\\u0AB5-\\u0AB9\\u0ABC-\\u0AC5\\u0AC7-\\u0AC9\\u0ACB-\\u0ACD\\u0AD0",
655
+ "\\u0AE0-\\u0AE3\\u0AE6-\\u0AEF\\u0B01-\\u0B03\\u0B05-\\u0B0C\\u0B0F\\u0B10\\u0B13-\\u0B28",
656
+ "\\u0B2A-\\u0B30\\u0B32\\u0B33\\u0B35-\\u0B39\\u0B3C-\\u0B43\\u0B47\\u0B48\\u0B4B-\\u0B4D",
657
+ "\\u0B56\\u0B57\\u0B5C\\u0B5D\\u0B5F-\\u0B61\\u0B66-\\u0B6F\\u0B71\\u0B82\\u0B83\\u0B85-\\u0B8A",
658
+ "\\u0B8E-\\u0B90\\u0B92-\\u0B95\\u0B99\\u0B9A\\u0B9C\\u0B9E\\u0B9F\\u0BA3\\u0BA4\\u0BA8-\\u0BAA",
659
+ "\\u0BAE-\\u0BB9\\u0BBE-\\u0BC2\\u0BC6-\\u0BC8\\u0BCA-\\u0BCD\\u0BD7\\u0BE6-\\u0BF2\\u0C01-\\u0C03",
660
+ "\\u0C05-\\u0C0C\\u0C0E-\\u0C10\\u0C12-\\u0C28\\u0C2A-\\u0C33\\u0C35-\\u0C39\\u0C3E-\\u0C44",
661
+ "\\u0C46-\\u0C48\\u0C4A-\\u0C4D\\u0C55\\u0C56\\u0C60\\u0C61\\u0C66-\\u0C6F\\u0C82\\u0C83",
662
+ "\\u0C85-\\u0C8C\\u0C8E-\\u0C90\\u0C92-\\u0CA8\\u0CAA-\\u0CB3\\u0CB5-\\u0CB9\\u0CBC-\\u0CC4",
663
+ "\\u0CC6-\\u0CC8\\u0CCA-\\u0CCD\\u0CD5\\u0CD6\\u0CDE\\u0CE0-\\u0CE3\\u0CE6-\\u0CEF\\u0D02\\u0D03",
664
+ "\\u0D05-\\u0D0C\\u0D0E-\\u0D10\\u0D12-\\u0D28\\u0D2A-\\u0D39\\u0D3E-\\u0D43\\u0D46-\\u0D48",
665
+ "\\u0D4A-\\u0D4D\\u0D57\\u0D60\\u0D61\\u0D66-\\u0D6F\\u0D82\\u0D83\\u0D85-\\u0D96\\u0D9A-\\u0DB1",
666
+ "\\u0DB3-\\u0DBB\\u0DBD\\u0DC0-\\u0DC6\\u0DCA\\u0DCF-\\u0DD4\\u0DD6\\u0DD8-\\u0DDF\\u0DF2\\u0DF3",
667
+ "\\u0E01-\\u0E3A\\u0E40-\\u0E4E\\u0E50-\\u0E59\\u0E81\\u0E82\\u0E84\\u0E87\\u0E88\\u0E8A\\u0E8D",
668
+ "\\u0E94-\\u0E97\\u0E99-\\u0E9F\\u0EA1-\\u0EA3\\u0EA5\\u0EA7\\u0EAA\\u0EAB\\u0EAD-\\u0EB9",
669
+ "\\u0EBB-\\u0EBD\\u0EC0-\\u0EC4\\u0EC6\\u0EC8-\\u0ECD\\u0ED0-\\u0ED9\\u0EDC\\u0EDD\\u0F00",
670
+ "\\u0F18\\u0F19\\u0F20-\\u0F33\\u0F35\\u0F37\\u0F39\\u0F3E-\\u0F47\\u0F49-\\u0F6A\\u0F71-\\u0F84",
671
+ "\\u0F86-\\u0F8B\\u0F90-\\u0F97\\u0F99-\\u0FBC\\u0FC6\\u1000-\\u1021\\u1023-\\u1027\\u1029\\u102A",
672
+ "\\u102C-\\u1032\\u1036-\\u1039\\u1040-\\u1049\\u1050-\\u1059\\u10A0-\\u10C5\\u10D0-\\u10FA\\u10FC",
673
+ "\\u1100-\\u1159\\u115F-\\u11A2\\u11A8-\\u11F9\\u1200-\\u1248\\u124A-\\u124D\\u1250-\\u1256\\u1258",
674
+ "\\u125A-\\u125D\\u1260-\\u1288\\u128A-\\u128D\\u1290-\\u12B0\\u12B2-\\u12B5\\u12B8-\\u12BE\\u12C0",
675
+ "\\u12C2-\\u12C5\\u12C8-\\u12D6\\u12D8-\\u1310\\u1312-\\u1315\\u1318-\\u135A\\u135F\\u1369-\\u137C",
676
+ "\\u1380-\\u138F\\u13A0-\\u13F4\\u1401-\\u166C\\u166F-\\u1676\\u1681-\\u169A\\u16A0-\\u16EA",
677
+ "\\u16EE-\\u16F0\\u1700-\\u170C\\u170E-\\u1714\\u1720-\\u1734\\u1740-\\u1753\\u1760-\\u176C",
678
+ "\\u176E-\\u1770\\u1772\\u1773\\u1780-\\u17B3\\u17B6-\\u17D3\\u17D7\\u17DC\\u17DD\\u17E0-\\u17E9",
679
+ "\\u17F0-\\u17F9\\u180B-\\u180D\\u1810-\\u1819\\u1820-\\u1877\\u1880-\\u18A9\\u1900-\\u191C",
680
+ "\\u1920-\\u192B\\u1930-\\u193B\\u1946-\\u196D\\u1970-\\u1974\\u1980-\\u19A9\\u19B0-\\u19C9",
681
+ "\\u19D0-\\u19D9\\u1A00-\\u1A1B\\u1B00-\\u1B4B\\u1B50-\\u1B59\\u1B6B-\\u1B73\\u1D00-\\u1DCA",
682
+ "\\u1DFE-\\u1E9B\\u1EA0-\\u1EF9\\u1F00-\\u1F15\\u1F18-\\u1F1D\\u1F20-\\u1F45\\u1F48-\\u1F4D",
683
+ "\\u1F50-\\u1F57\\u1F59\\u1F5B\\u1F5D\\u1F5F-\\u1F7D\\u1F80-\\u1FB4\\u1FB6-\\u1FBC\\u1FBE",
684
+ "\\u1FC2-\\u1FC4\\u1FC6-\\u1FCC\\u1FD0-\\u1FD3\\u1FD6-\\u1FDB\\u1FE0-\\u1FEC\\u1FF2-\\u1FF4",
685
+ "\\u1FF6-\\u1FFC\\u2070\\u2071\\u2074-\\u2079\\u207F-\\u2089\\u2090-\\u2094\\u20D0-\\u20EF\\u2102",
686
+ "\\u2107\\u210A-\\u2113\\u2115\\u2119-\\u211D\\u2124\\u2126\\u2128\\u212A-\\u212D\\u212F-\\u2139",
687
+ "\\u213C-\\u213F\\u2145-\\u2149\\u214E\\u2153-\\u2184\\u2460-\\u249B\\u24EA-\\u24FF\\u2776-\\u2793",
688
+ "\\u2C00-\\u2C2E\\u2C30-\\u2C5E\\u2C60-\\u2C6C\\u2C74-\\u2C77\\u2C80-\\u2CE4\\u2CFD\\u2D00-\\u2D25",
689
+ "\\u2D30-\\u2D65\\u2D6F\\u2D80-\\u2D96\\u2DA0-\\u2DA6\\u2DA8-\\u2DAE\\u2DB0-\\u2DB6\\u2DB8-\\u2DBE",
690
+ "\\u2DC0-\\u2DC6\\u2DC8-\\u2DCE\\u2DD0-\\u2DD6\\u2DD8-\\u2DDE\\u3005-\\u3007\\u3021-\\u302F",
691
+ "\\u3031-\\u3035\\u3038-\\u303C\\u3041-\\u3096\\u3099\\u309A\\u309D-\\u309F\\u30A1-\\u30FA",
692
+ "\\u30FC-\\u30FF\\u3105-\\u312C\\u3131-\\u318E\\u3192-\\u3195\\u31A0-\\u31B7\\u31F0-\\u31FF",
693
+ "\\u3220-\\u3229\\u3251-\\u325F\\u3280-\\u3289\\u32B1-\\u32BF\\u3400-\\u4DB5\\u4E00-\\u9FA5\\u9FBB",
694
+ "\\uA000-\\uA48C\\uA717-\\uA71A\\uA800-\\uA827\\uA840-\\uA873\\uAC00-\\uD7A3\\uF900-\\uFA2D",
695
+ "\\uFA30-\\uFA6A\\uFA70-\\uFAD9\\uFB00-\\uFB06\\uFB13-\\uFB17\\uFB1D-\\uFB28\\uFB2A-\\uFB36",
696
+ "\\uFB38-\\uFB3C\\uFB3E\\uFB40\\uFB41\\uFB43\\uFB44\\uFB46-\\uFBB1\\uFBD3-\\uFD3D\\uFD50-\\uFD8F",
697
+ "\\uFD92-\\uFDC7\\uFDF0-\\uFDFB\\uFE00-\\uFE0F\\uFE20-\\uFE23\\uFE70-\\uFE74\\uFE76-\\uFEFC",
698
+ "\\uFF10-\\uFF19\\uFF21-\\uFF3A\\uFF41-\\uFF5A\\uFF66-\\uFFBE\\uFFC2-\\uFFC7\\uFFCA-\\uFFCF",
699
+ "\\uFFD2-\\uFFD7\\uFFDA-\\uFFDC",
700
+ "]"
701
+ ].join("");
702
+ //#endregion
637
703
  //#region src/mysql-adapter.ts
638
704
  /** A column's own `@db.mysql.charset`, `@db.mysql.collate` and `@db.column.collate` (lowercased, `""` when absent). */
639
705
  function declaredCharsetCollation(field) {
@@ -705,6 +771,64 @@ const nonStrictChecked = /* @__PURE__ */ new WeakSet();
705
771
  * loading the time zone tables fixes a running server without a restart.
706
772
  */
707
773
  const convertibleZones = /* @__PURE__ */ new WeakMap();
774
+ /** Connections whose COMMIT failed: the transaction is over, no ROLLBACK may follow. */
775
+ const endedTx = /* @__PURE__ */ new WeakSet();
776
+ /**
777
+ * ER_LOCK_DEADLOCK rolled the whole transaction back; ER_LOCK_WAIT_TIMEOUT /
778
+ * ER_LOCK_NOWAIT (3572, a `NOWAIT` locking read) failed the statement.
779
+ */
780
+ const MYSQL_CONTENTION_CODES = {
781
+ 1213: "DEADLOCK",
782
+ 1205: "LOCK_TIMEOUT",
783
+ 3572: "LOCK_TIMEOUT"
784
+ };
785
+ /**
786
+ * `error` as the retryable `DbError` of its contention errno (deadlock,
787
+ * lock wait timeout, `NOWAIT` lock refusal), else `error` as is; the driver
788
+ * error is kept as `cause`. Applied to every statement and to COMMIT.
789
+ */
790
+ function mapContention(error) {
791
+ if (!error || typeof error !== "object") return error;
792
+ const err = error;
793
+ const code = typeof err.errno === "number" ? MYSQL_CONTENTION_CODES[err.errno] : void 0;
794
+ if (!code) return error;
795
+ return new _atscript_db.DbError(code, [{
796
+ path: "",
797
+ message: err.sqlMessage ?? err.message
798
+ }], void 0, { cause: error });
799
+ }
800
+ /**
801
+ * Whether the server reported `error` (a positive `errno`), as opposed to a
802
+ * client-side failure — a timeout, a lost socket — after which the server's
803
+ * transaction state is unknown.
804
+ */
805
+ function isServerError(error) {
806
+ const errno = error && typeof error === "object" ? error.errno : void 0;
807
+ return typeof errno === "number" && errno > 0;
808
+ }
809
+ /** Closes `conn` — or, for a driver without `destroy()`, rolls it back (best effort) and releases it. */
810
+ async function discardConnection(conn) {
811
+ if (conn.destroy) {
812
+ conn.destroy();
813
+ return;
814
+ }
815
+ try {
816
+ await conn.exec("ROLLBACK");
817
+ } catch {}
818
+ conn.release();
819
+ }
820
+ /** `exec` whose statements reject with the retryable `DbError` on contention (any statement can be the deadlock victim). */
821
+ function contentionExec(exec) {
822
+ const contention = (error) => {
823
+ throw mapContention(error);
824
+ };
825
+ return {
826
+ run: (sql, params) => exec.run(sql, params).catch(contention),
827
+ all: (sql, params) => exec.all(sql, params).catch(contention),
828
+ get: (sql, params) => exec.get(sql, params).catch(contention),
829
+ exec: (sql) => exec.exec(sql).catch(contention)
830
+ };
831
+ }
708
832
  /** The `ALTER TABLE` that re-creates `fks`, all on `table`, as they were. */
709
833
  function addForeignKeysSql(table, fks) {
710
834
  return `ALTER TABLE ${table} ${fks.map((fk) => `ADD CONSTRAINT ${qi(fk.name)} FOREIGN KEY (${fk.columns.map(qi).join(", ")}) REFERENCES ${fk.refTable} (${fk.refColumns.map(qi).join(", ")}) ON DELETE ${fk.onDelete} ON UPDATE ${fk.onUpdate}`).join(", ")}`;
@@ -774,6 +898,8 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
774
898
  /** Field path → `@db.mysql.charset` of the column. */
775
899
  _charsetFields = /* @__PURE__ */ new Map();
776
900
  _space;
901
+ _poolExec;
902
+ _txExecs = /* @__PURE__ */ new WeakMap();
777
903
  /** Memo of {@link _keyTableCharsets} — it also makes its warning fire once. */
778
904
  _keyTableCharsetsMemo;
779
905
  /** Whether the connected MySQL instance supports native VECTOR type (MySQL 9.0+). */
@@ -843,12 +969,17 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
843
969
  try {
844
970
  this._log("COMMIT");
845
971
  await conn.exec("COMMIT");
846
- } finally {
847
- conn.release();
972
+ } catch (error) {
973
+ endedTx.add(conn);
974
+ if (isServerError(error)) conn.release();
975
+ else await discardConnection(conn);
976
+ throw mapContention(error);
848
977
  }
978
+ conn.release();
849
979
  }
850
980
  async _rollbackTransaction(state) {
851
981
  const conn = state;
982
+ if (endedTx.delete(conn)) return;
852
983
  try {
853
984
  this._log("ROLLBACK");
854
985
  await conn.exec("ROLLBACK");
@@ -861,7 +992,11 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
861
992
  * otherwise the pool-based driver.
862
993
  */
863
994
  _exec() {
864
- return this._txConnection() ?? this.driver;
995
+ const conn = this._txConnection();
996
+ if (!conn) return this._poolExec ??= contentionExec(this.driver);
997
+ let exec = this._txExecs.get(conn);
998
+ if (!exec) this._txExecs.set(conn, exec = contentionExec(conn));
999
+ return exec;
865
1000
  }
866
1001
  /**
867
1002
  * Runs `fn` on one connection (the transaction's, or a dedicated one) with
@@ -1102,10 +1237,7 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
1102
1237
  const columns = (message.match(/FOREIGN KEY \(([^)]+)\)/)?.[1])?.split(",").map((c) => c.trim().replace(/^`|`$/g, ""));
1103
1238
  throw (0, _atscript_db.constraintViolation)("FK_VIOLATION", err.errno === 1451 ? { referencedBy: referencing } : { columns }, message);
1104
1239
  }
1105
- if (err.errno === 1213 || err.errno === 1205 || err.errno === 3572) throw new _atscript_db.DbError(err.errno === 1213 ? "DEADLOCK" : "LOCK_TIMEOUT", [{
1106
- path: "",
1107
- message: err.sqlMessage ?? err.message
1108
- }]);
1240
+ throw mapContention(error);
1109
1241
  }
1110
1242
  throw error;
1111
1243
  }
@@ -2625,7 +2757,8 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
2625
2757
  * The search predicate (AND the request filter) and its relevance order
2626
2758
  * (`rank`, since 0.1.165) — used as the ORDER BY when the read has no
2627
2759
  * `$sort`: exact-number hits first, then `MATCH … AGAINST` relevance, then
2628
- * the primary key. Grouped searches use only the predicate.
2760
+ * the primary key; the primary key alone under `$rank: false`. Grouped
2761
+ * searches use only the predicate.
2629
2762
  */
2630
2763
  _buildSearchWhere(text, query, indexName) {
2631
2764
  const fulltextIndex = this._getFulltextIndex(indexName);
@@ -2637,10 +2770,11 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
2637
2770
  const all = fulltextIndex.match === "all";
2638
2771
  const matchClause = `MATCH(${matchCols}) AGAINST(? IN ${all ? "BOOLEAN" : "NATURAL LANGUAGE"} MODE)`;
2639
2772
  const term = all ? mysqlRequireEveryTerm(text) : text;
2773
+ const ranked = textFields.length > 0 && query.controls?.$rank !== false;
2640
2774
  const rank = (0, _atscript_db_sql_tools.searchRankOrder)(mysqlDialect, {
2641
2775
  exactEqualities: n === void 0 ? [] : integerFields.map((f) => `${qi(f.name)} = ?`),
2642
2776
  n,
2643
- textRank: textFields.length > 0 ? {
2777
+ textRank: ranked ? {
2644
2778
  sql: `${matchClause} DESC`,
2645
2779
  params: [term]
2646
2780
  } : void 0,
@@ -2956,31 +3090,48 @@ function vectorToString(vector) {
2956
3090
  }
2957
3091
  /** InnoDB's default full-text stop words (`INFORMATION_SCHEMA.INNODB_FT_DEFAULT_STOPWORD`). */
2958
3092
  const INNODB_DEFAULT_STOPWORDS = new Set("a about an are as at be by com de en for from how i in is it la of on or that the this to was what when where who will with und www".split(" "));
3093
+ /** A word as InnoDB's full-text parser reads it: a run of {@link INNODB_WORD_CLASS}. */
3094
+ const INNODB_WORD = new RegExp(`${INNODB_WORD_CLASS}+`, "gu");
3095
+ /** A word character right before a run of `*` at the end of a chunk: a prefix. */
3096
+ const INNODB_PREFIX = new RegExp(`${INNODB_WORD_CLASS}\\*+$`, "u");
3097
+ /** `innodb_ft_min_token_size` default: shorter words are not indexed. */
3098
+ const INNODB_MIN_TOKEN_SIZE = 3;
3099
+ /**
3100
+ * Whether InnoDB indexes `word` (an {@link INNODB_WORD} match) by default —
3101
+ * required, it can match a row. Word characters are all in the BMP: `length`
3102
+ * counts characters.
3103
+ */
3104
+ function innodbIndexes(word) {
3105
+ return word.length >= INNODB_MIN_TOKEN_SIZE && !INNODB_DEFAULT_STOPWORDS.has(word.toLowerCase());
3106
+ }
2959
3107
  /**
2960
3108
  * A search text as a boolean-mode `AGAINST` term with every word required
2961
- * (`@db.index.fulltext.match "all"`, since 0.1.168): `+word` per word, a
2962
- * `"quoted phrase"` stays one required phrase, a trailing `*` keeps its prefix
2963
- * meaning. The user's own boolean operators (`+ - < > ( ) ~ * " @`) are text,
2964
- * not syntax. A word InnoDB does not index by default — shorter than 3
2965
- * characters (`innodb_ft_min_token_size`) or on its default stop-word list —
2966
- * stays optional: requiring it would match no row.
3109
+ * (`@db.index.fulltext.match "all"`, since 0.1.168). The text is split into
3110
+ * words the way InnoDB splits it ({@link INNODB_WORD}), so the user's own
3111
+ * boolean operators (`+ - < > ( ) ~ * " @`) and punctuation are text, never
3112
+ * syntax: `the, quick` is the words `the` and `quick`. Each word becomes `+word`; a
3113
+ * `"quoted phrase"` stays one required phrase; a `*` right after a word keeps
3114
+ * its prefix meaning (`+dog*`). A word InnoDB does not index by default —
3115
+ * shorter than 3 characters (`innodb_ft_min_token_size`) or on its default
3116
+ * stop-word list — stays optional (requiring it would match no row), as does
3117
+ * a phrase made only of such words. A prefix is required whatever its length:
3118
+ * it matches the indexed words that start with it.
2967
3119
  */
2968
3120
  function mysqlRequireEveryTerm(text) {
2969
3121
  const parts = [];
2970
3122
  for (const match of text.matchAll(/"([^"]*)"|(\S+)/g)) {
2971
3123
  const phrase = match[1];
2972
3124
  if (phrase !== void 0) {
2973
- const clean = phrase.trim();
2974
- if (clean) parts.push(`+"${clean}"`);
3125
+ const words = phrase.match(INNODB_WORD);
3126
+ if (words) parts.push(`${words.some(innodbIndexes) ? "+" : ""}"${words.join(" ")}"`);
2975
3127
  continue;
2976
3128
  }
2977
- const word = match[2];
2978
- const core = word.replace(/\*+$/, "");
2979
- const pieces = core.split(/[+\-<>()~*"@]+/).filter(Boolean);
2980
- pieces.forEach((piece, i) => {
2981
- const star = i === pieces.length - 1 && core.length < word.length ? "*" : "";
2982
- const required = [...piece].length >= 3 && !INNODB_DEFAULT_STOPWORDS.has(piece.toLowerCase());
2983
- parts.push(`${required ? "+" : ""}${piece}${star}`);
3129
+ const chunk = match[2];
3130
+ const words = chunk.match(INNODB_WORD) ?? [];
3131
+ const prefix = chunk.endsWith("*") && INNODB_PREFIX.test(chunk);
3132
+ words.forEach((word, i) => {
3133
+ if (prefix && i === words.length - 1) parts.push(`+${word}*`);
3134
+ else parts.push(`${innodbIndexes(word) ? "+" : ""}${word}`);
2984
3135
  });
2985
3136
  }
2986
3137
  return parts.join(" ");
package/dist/index.d.cts CHANGED
@@ -136,6 +136,8 @@ declare class MysqlAdapter extends BaseDbAdapter {
136
136
  /** Field path → `@db.mysql.charset` of the column. */
137
137
  private _charsetFields;
138
138
  private _space?;
139
+ private _poolExec?;
140
+ private _txExecs;
139
141
  /** Memo of {@link _keyTableCharsets} — it also makes its warning fire once. */
140
142
  private _keyTableCharsetsMemo?;
141
143
  /** Whether the connected MySQL instance supports native VECTOR type (MySQL 9.0+). */
@@ -581,7 +583,8 @@ declare class MysqlAdapter extends BaseDbAdapter {
581
583
  * The search predicate (AND the request filter) and its relevance order
582
584
  * (`rank`, since 0.1.165) — used as the ORDER BY when the read has no
583
585
  * `$sort`: exact-number hits first, then `MATCH … AGAINST` relevance, then
584
- * the primary key. Grouped searches use only the predicate.
586
+ * the primary key; the primary key alone under `$rank: false`. Grouped
587
+ * searches use only the predicate.
585
588
  */
586
589
  private _buildSearchWhere;
587
590
  private _getFulltextIndex;
package/dist/index.d.mts CHANGED
@@ -136,6 +136,8 @@ declare class MysqlAdapter extends BaseDbAdapter {
136
136
  /** Field path → `@db.mysql.charset` of the column. */
137
137
  private _charsetFields;
138
138
  private _space?;
139
+ private _poolExec?;
140
+ private _txExecs;
139
141
  /** Memo of {@link _keyTableCharsets} — it also makes its warning fire once. */
140
142
  private _keyTableCharsetsMemo?;
141
143
  /** Whether the connected MySQL instance supports native VECTOR type (MySQL 9.0+). */
@@ -581,7 +583,8 @@ declare class MysqlAdapter extends BaseDbAdapter {
581
583
  * The search predicate (AND the request filter) and its relevance order
582
584
  * (`rank`, since 0.1.165) — used as the ORDER BY when the read has no
583
585
  * `$sort`: exact-number hits first, then `MATCH … AGAINST` relevance, then
584
- * the primary key. Grouped searches use only the predicate.
586
+ * the primary key; the primary key alone under `$rank: false`. Grouped
587
+ * searches use only the predicate.
585
588
  */
586
589
  private _buildSearchWhere;
587
590
  private _getFulltextIndex;
package/dist/index.mjs CHANGED
@@ -633,6 +633,72 @@ function buildWhere(filter, opts) {
633
633
  return buildWhere$1(mysqlDialect, filter, opts);
634
634
  }
635
635
  //#endregion
636
+ //#region src/innodb-word-chars.ts
637
+ /** A regular-expression character class of InnoDB's word characters (`u` flag). */
638
+ const INNODB_WORD_CLASS = [
639
+ "[",
640
+ "\\u0030-\\u0039\\u0041-\\u005A\\u005F\\u0061-\\u007A\\u00AA\\u00B2\\u00B3\\u00B5\\u00B9\\u00BA",
641
+ "\\u00BC-\\u00BE\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02C1\\u02C6-\\u02D1\\u02E0-\\u02E4\\u02EE",
642
+ "\\u0300-\\u036F\\u037A-\\u037D\\u0386\\u0388-\\u038A\\u038C\\u038E-\\u03A1\\u03A3-\\u03CE",
643
+ "\\u03D0-\\u03F5\\u03F7-\\u0481\\u0483-\\u0486\\u0488-\\u0513\\u0531-\\u0556\\u0559\\u0561-\\u0587",
644
+ "\\u0591-\\u05BD\\u05BF\\u05C1\\u05C2\\u05C4\\u05C5\\u05C7\\u05D0-\\u05EA\\u05F0-\\u05F2",
645
+ "\\u0610-\\u0615\\u0621-\\u063A\\u0640-\\u065E\\u0660-\\u0669\\u066E-\\u06D3\\u06D5-\\u06DC",
646
+ "\\u06DE-\\u06E8\\u06EA-\\u06FC\\u06FF\\u0710-\\u074A\\u074D-\\u076D\\u0780-\\u07B1\\u07C0-\\u07F5",
647
+ "\\u07FA\\u0901-\\u0939\\u093C-\\u094D\\u0950-\\u0954\\u0958-\\u0963\\u0966-\\u096F\\u097B-\\u097F",
648
+ "\\u0981-\\u0983\\u0985-\\u098C\\u098F\\u0990\\u0993-\\u09A8\\u09AA-\\u09B0\\u09B2\\u09B6-\\u09B9",
649
+ "\\u09BC-\\u09C4\\u09C7\\u09C8\\u09CB-\\u09CE\\u09D7\\u09DC\\u09DD\\u09DF-\\u09E3\\u09E6-\\u09F1",
650
+ "\\u09F4-\\u09F9\\u0A01-\\u0A03\\u0A05-\\u0A0A\\u0A0F\\u0A10\\u0A13-\\u0A28\\u0A2A-\\u0A30",
651
+ "\\u0A32\\u0A33\\u0A35\\u0A36\\u0A38\\u0A39\\u0A3C\\u0A3E-\\u0A42\\u0A47\\u0A48\\u0A4B-\\u0A4D",
652
+ "\\u0A59-\\u0A5C\\u0A5E\\u0A66-\\u0A74\\u0A81-\\u0A83\\u0A85-\\u0A8D\\u0A8F-\\u0A91\\u0A93-\\u0AA8",
653
+ "\\u0AAA-\\u0AB0\\u0AB2\\u0AB3\\u0AB5-\\u0AB9\\u0ABC-\\u0AC5\\u0AC7-\\u0AC9\\u0ACB-\\u0ACD\\u0AD0",
654
+ "\\u0AE0-\\u0AE3\\u0AE6-\\u0AEF\\u0B01-\\u0B03\\u0B05-\\u0B0C\\u0B0F\\u0B10\\u0B13-\\u0B28",
655
+ "\\u0B2A-\\u0B30\\u0B32\\u0B33\\u0B35-\\u0B39\\u0B3C-\\u0B43\\u0B47\\u0B48\\u0B4B-\\u0B4D",
656
+ "\\u0B56\\u0B57\\u0B5C\\u0B5D\\u0B5F-\\u0B61\\u0B66-\\u0B6F\\u0B71\\u0B82\\u0B83\\u0B85-\\u0B8A",
657
+ "\\u0B8E-\\u0B90\\u0B92-\\u0B95\\u0B99\\u0B9A\\u0B9C\\u0B9E\\u0B9F\\u0BA3\\u0BA4\\u0BA8-\\u0BAA",
658
+ "\\u0BAE-\\u0BB9\\u0BBE-\\u0BC2\\u0BC6-\\u0BC8\\u0BCA-\\u0BCD\\u0BD7\\u0BE6-\\u0BF2\\u0C01-\\u0C03",
659
+ "\\u0C05-\\u0C0C\\u0C0E-\\u0C10\\u0C12-\\u0C28\\u0C2A-\\u0C33\\u0C35-\\u0C39\\u0C3E-\\u0C44",
660
+ "\\u0C46-\\u0C48\\u0C4A-\\u0C4D\\u0C55\\u0C56\\u0C60\\u0C61\\u0C66-\\u0C6F\\u0C82\\u0C83",
661
+ "\\u0C85-\\u0C8C\\u0C8E-\\u0C90\\u0C92-\\u0CA8\\u0CAA-\\u0CB3\\u0CB5-\\u0CB9\\u0CBC-\\u0CC4",
662
+ "\\u0CC6-\\u0CC8\\u0CCA-\\u0CCD\\u0CD5\\u0CD6\\u0CDE\\u0CE0-\\u0CE3\\u0CE6-\\u0CEF\\u0D02\\u0D03",
663
+ "\\u0D05-\\u0D0C\\u0D0E-\\u0D10\\u0D12-\\u0D28\\u0D2A-\\u0D39\\u0D3E-\\u0D43\\u0D46-\\u0D48",
664
+ "\\u0D4A-\\u0D4D\\u0D57\\u0D60\\u0D61\\u0D66-\\u0D6F\\u0D82\\u0D83\\u0D85-\\u0D96\\u0D9A-\\u0DB1",
665
+ "\\u0DB3-\\u0DBB\\u0DBD\\u0DC0-\\u0DC6\\u0DCA\\u0DCF-\\u0DD4\\u0DD6\\u0DD8-\\u0DDF\\u0DF2\\u0DF3",
666
+ "\\u0E01-\\u0E3A\\u0E40-\\u0E4E\\u0E50-\\u0E59\\u0E81\\u0E82\\u0E84\\u0E87\\u0E88\\u0E8A\\u0E8D",
667
+ "\\u0E94-\\u0E97\\u0E99-\\u0E9F\\u0EA1-\\u0EA3\\u0EA5\\u0EA7\\u0EAA\\u0EAB\\u0EAD-\\u0EB9",
668
+ "\\u0EBB-\\u0EBD\\u0EC0-\\u0EC4\\u0EC6\\u0EC8-\\u0ECD\\u0ED0-\\u0ED9\\u0EDC\\u0EDD\\u0F00",
669
+ "\\u0F18\\u0F19\\u0F20-\\u0F33\\u0F35\\u0F37\\u0F39\\u0F3E-\\u0F47\\u0F49-\\u0F6A\\u0F71-\\u0F84",
670
+ "\\u0F86-\\u0F8B\\u0F90-\\u0F97\\u0F99-\\u0FBC\\u0FC6\\u1000-\\u1021\\u1023-\\u1027\\u1029\\u102A",
671
+ "\\u102C-\\u1032\\u1036-\\u1039\\u1040-\\u1049\\u1050-\\u1059\\u10A0-\\u10C5\\u10D0-\\u10FA\\u10FC",
672
+ "\\u1100-\\u1159\\u115F-\\u11A2\\u11A8-\\u11F9\\u1200-\\u1248\\u124A-\\u124D\\u1250-\\u1256\\u1258",
673
+ "\\u125A-\\u125D\\u1260-\\u1288\\u128A-\\u128D\\u1290-\\u12B0\\u12B2-\\u12B5\\u12B8-\\u12BE\\u12C0",
674
+ "\\u12C2-\\u12C5\\u12C8-\\u12D6\\u12D8-\\u1310\\u1312-\\u1315\\u1318-\\u135A\\u135F\\u1369-\\u137C",
675
+ "\\u1380-\\u138F\\u13A0-\\u13F4\\u1401-\\u166C\\u166F-\\u1676\\u1681-\\u169A\\u16A0-\\u16EA",
676
+ "\\u16EE-\\u16F0\\u1700-\\u170C\\u170E-\\u1714\\u1720-\\u1734\\u1740-\\u1753\\u1760-\\u176C",
677
+ "\\u176E-\\u1770\\u1772\\u1773\\u1780-\\u17B3\\u17B6-\\u17D3\\u17D7\\u17DC\\u17DD\\u17E0-\\u17E9",
678
+ "\\u17F0-\\u17F9\\u180B-\\u180D\\u1810-\\u1819\\u1820-\\u1877\\u1880-\\u18A9\\u1900-\\u191C",
679
+ "\\u1920-\\u192B\\u1930-\\u193B\\u1946-\\u196D\\u1970-\\u1974\\u1980-\\u19A9\\u19B0-\\u19C9",
680
+ "\\u19D0-\\u19D9\\u1A00-\\u1A1B\\u1B00-\\u1B4B\\u1B50-\\u1B59\\u1B6B-\\u1B73\\u1D00-\\u1DCA",
681
+ "\\u1DFE-\\u1E9B\\u1EA0-\\u1EF9\\u1F00-\\u1F15\\u1F18-\\u1F1D\\u1F20-\\u1F45\\u1F48-\\u1F4D",
682
+ "\\u1F50-\\u1F57\\u1F59\\u1F5B\\u1F5D\\u1F5F-\\u1F7D\\u1F80-\\u1FB4\\u1FB6-\\u1FBC\\u1FBE",
683
+ "\\u1FC2-\\u1FC4\\u1FC6-\\u1FCC\\u1FD0-\\u1FD3\\u1FD6-\\u1FDB\\u1FE0-\\u1FEC\\u1FF2-\\u1FF4",
684
+ "\\u1FF6-\\u1FFC\\u2070\\u2071\\u2074-\\u2079\\u207F-\\u2089\\u2090-\\u2094\\u20D0-\\u20EF\\u2102",
685
+ "\\u2107\\u210A-\\u2113\\u2115\\u2119-\\u211D\\u2124\\u2126\\u2128\\u212A-\\u212D\\u212F-\\u2139",
686
+ "\\u213C-\\u213F\\u2145-\\u2149\\u214E\\u2153-\\u2184\\u2460-\\u249B\\u24EA-\\u24FF\\u2776-\\u2793",
687
+ "\\u2C00-\\u2C2E\\u2C30-\\u2C5E\\u2C60-\\u2C6C\\u2C74-\\u2C77\\u2C80-\\u2CE4\\u2CFD\\u2D00-\\u2D25",
688
+ "\\u2D30-\\u2D65\\u2D6F\\u2D80-\\u2D96\\u2DA0-\\u2DA6\\u2DA8-\\u2DAE\\u2DB0-\\u2DB6\\u2DB8-\\u2DBE",
689
+ "\\u2DC0-\\u2DC6\\u2DC8-\\u2DCE\\u2DD0-\\u2DD6\\u2DD8-\\u2DDE\\u3005-\\u3007\\u3021-\\u302F",
690
+ "\\u3031-\\u3035\\u3038-\\u303C\\u3041-\\u3096\\u3099\\u309A\\u309D-\\u309F\\u30A1-\\u30FA",
691
+ "\\u30FC-\\u30FF\\u3105-\\u312C\\u3131-\\u318E\\u3192-\\u3195\\u31A0-\\u31B7\\u31F0-\\u31FF",
692
+ "\\u3220-\\u3229\\u3251-\\u325F\\u3280-\\u3289\\u32B1-\\u32BF\\u3400-\\u4DB5\\u4E00-\\u9FA5\\u9FBB",
693
+ "\\uA000-\\uA48C\\uA717-\\uA71A\\uA800-\\uA827\\uA840-\\uA873\\uAC00-\\uD7A3\\uF900-\\uFA2D",
694
+ "\\uFA30-\\uFA6A\\uFA70-\\uFAD9\\uFB00-\\uFB06\\uFB13-\\uFB17\\uFB1D-\\uFB28\\uFB2A-\\uFB36",
695
+ "\\uFB38-\\uFB3C\\uFB3E\\uFB40\\uFB41\\uFB43\\uFB44\\uFB46-\\uFBB1\\uFBD3-\\uFD3D\\uFD50-\\uFD8F",
696
+ "\\uFD92-\\uFDC7\\uFDF0-\\uFDFB\\uFE00-\\uFE0F\\uFE20-\\uFE23\\uFE70-\\uFE74\\uFE76-\\uFEFC",
697
+ "\\uFF10-\\uFF19\\uFF21-\\uFF3A\\uFF41-\\uFF5A\\uFF66-\\uFFBE\\uFFC2-\\uFFC7\\uFFCA-\\uFFCF",
698
+ "\\uFFD2-\\uFFD7\\uFFDA-\\uFFDC",
699
+ "]"
700
+ ].join("");
701
+ //#endregion
636
702
  //#region src/mysql-adapter.ts
637
703
  /** A column's own `@db.mysql.charset`, `@db.mysql.collate` and `@db.column.collate` (lowercased, `""` when absent). */
638
704
  function declaredCharsetCollation(field) {
@@ -704,6 +770,64 @@ const nonStrictChecked = /* @__PURE__ */ new WeakSet();
704
770
  * loading the time zone tables fixes a running server without a restart.
705
771
  */
706
772
  const convertibleZones = /* @__PURE__ */ new WeakMap();
773
+ /** Connections whose COMMIT failed: the transaction is over, no ROLLBACK may follow. */
774
+ const endedTx = /* @__PURE__ */ new WeakSet();
775
+ /**
776
+ * ER_LOCK_DEADLOCK rolled the whole transaction back; ER_LOCK_WAIT_TIMEOUT /
777
+ * ER_LOCK_NOWAIT (3572, a `NOWAIT` locking read) failed the statement.
778
+ */
779
+ const MYSQL_CONTENTION_CODES = {
780
+ 1213: "DEADLOCK",
781
+ 1205: "LOCK_TIMEOUT",
782
+ 3572: "LOCK_TIMEOUT"
783
+ };
784
+ /**
785
+ * `error` as the retryable `DbError` of its contention errno (deadlock,
786
+ * lock wait timeout, `NOWAIT` lock refusal), else `error` as is; the driver
787
+ * error is kept as `cause`. Applied to every statement and to COMMIT.
788
+ */
789
+ function mapContention(error) {
790
+ if (!error || typeof error !== "object") return error;
791
+ const err = error;
792
+ const code = typeof err.errno === "number" ? MYSQL_CONTENTION_CODES[err.errno] : void 0;
793
+ if (!code) return error;
794
+ return new DbError(code, [{
795
+ path: "",
796
+ message: err.sqlMessage ?? err.message
797
+ }], void 0, { cause: error });
798
+ }
799
+ /**
800
+ * Whether the server reported `error` (a positive `errno`), as opposed to a
801
+ * client-side failure — a timeout, a lost socket — after which the server's
802
+ * transaction state is unknown.
803
+ */
804
+ function isServerError(error) {
805
+ const errno = error && typeof error === "object" ? error.errno : void 0;
806
+ return typeof errno === "number" && errno > 0;
807
+ }
808
+ /** Closes `conn` — or, for a driver without `destroy()`, rolls it back (best effort) and releases it. */
809
+ async function discardConnection(conn) {
810
+ if (conn.destroy) {
811
+ conn.destroy();
812
+ return;
813
+ }
814
+ try {
815
+ await conn.exec("ROLLBACK");
816
+ } catch {}
817
+ conn.release();
818
+ }
819
+ /** `exec` whose statements reject with the retryable `DbError` on contention (any statement can be the deadlock victim). */
820
+ function contentionExec(exec) {
821
+ const contention = (error) => {
822
+ throw mapContention(error);
823
+ };
824
+ return {
825
+ run: (sql, params) => exec.run(sql, params).catch(contention),
826
+ all: (sql, params) => exec.all(sql, params).catch(contention),
827
+ get: (sql, params) => exec.get(sql, params).catch(contention),
828
+ exec: (sql) => exec.exec(sql).catch(contention)
829
+ };
830
+ }
707
831
  /** The `ALTER TABLE` that re-creates `fks`, all on `table`, as they were. */
708
832
  function addForeignKeysSql(table, fks) {
709
833
  return `ALTER TABLE ${table} ${fks.map((fk) => `ADD CONSTRAINT ${qi(fk.name)} FOREIGN KEY (${fk.columns.map(qi).join(", ")}) REFERENCES ${fk.refTable} (${fk.refColumns.map(qi).join(", ")}) ON DELETE ${fk.onDelete} ON UPDATE ${fk.onUpdate}`).join(", ")}`;
@@ -773,6 +897,8 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
773
897
  /** Field path → `@db.mysql.charset` of the column. */
774
898
  _charsetFields = /* @__PURE__ */ new Map();
775
899
  _space;
900
+ _poolExec;
901
+ _txExecs = /* @__PURE__ */ new WeakMap();
776
902
  /** Memo of {@link _keyTableCharsets} — it also makes its warning fire once. */
777
903
  _keyTableCharsetsMemo;
778
904
  /** Whether the connected MySQL instance supports native VECTOR type (MySQL 9.0+). */
@@ -842,12 +968,17 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
842
968
  try {
843
969
  this._log("COMMIT");
844
970
  await conn.exec("COMMIT");
845
- } finally {
846
- conn.release();
971
+ } catch (error) {
972
+ endedTx.add(conn);
973
+ if (isServerError(error)) conn.release();
974
+ else await discardConnection(conn);
975
+ throw mapContention(error);
847
976
  }
977
+ conn.release();
848
978
  }
849
979
  async _rollbackTransaction(state) {
850
980
  const conn = state;
981
+ if (endedTx.delete(conn)) return;
851
982
  try {
852
983
  this._log("ROLLBACK");
853
984
  await conn.exec("ROLLBACK");
@@ -860,7 +991,11 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
860
991
  * otherwise the pool-based driver.
861
992
  */
862
993
  _exec() {
863
- return this._txConnection() ?? this.driver;
994
+ const conn = this._txConnection();
995
+ if (!conn) return this._poolExec ??= contentionExec(this.driver);
996
+ let exec = this._txExecs.get(conn);
997
+ if (!exec) this._txExecs.set(conn, exec = contentionExec(conn));
998
+ return exec;
864
999
  }
865
1000
  /**
866
1001
  * Runs `fn` on one connection (the transaction's, or a dedicated one) with
@@ -1101,10 +1236,7 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
1101
1236
  const columns = (message.match(/FOREIGN KEY \(([^)]+)\)/)?.[1])?.split(",").map((c) => c.trim().replace(/^`|`$/g, ""));
1102
1237
  throw constraintViolation("FK_VIOLATION", err.errno === 1451 ? { referencedBy: referencing } : { columns }, message);
1103
1238
  }
1104
- if (err.errno === 1213 || err.errno === 1205 || err.errno === 3572) throw new DbError(err.errno === 1213 ? "DEADLOCK" : "LOCK_TIMEOUT", [{
1105
- path: "",
1106
- message: err.sqlMessage ?? err.message
1107
- }]);
1239
+ throw mapContention(error);
1108
1240
  }
1109
1241
  throw error;
1110
1242
  }
@@ -2624,7 +2756,8 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
2624
2756
  * The search predicate (AND the request filter) and its relevance order
2625
2757
  * (`rank`, since 0.1.165) — used as the ORDER BY when the read has no
2626
2758
  * `$sort`: exact-number hits first, then `MATCH … AGAINST` relevance, then
2627
- * the primary key. Grouped searches use only the predicate.
2759
+ * the primary key; the primary key alone under `$rank: false`. Grouped
2760
+ * searches use only the predicate.
2628
2761
  */
2629
2762
  _buildSearchWhere(text, query, indexName) {
2630
2763
  const fulltextIndex = this._getFulltextIndex(indexName);
@@ -2636,10 +2769,11 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
2636
2769
  const all = fulltextIndex.match === "all";
2637
2770
  const matchClause = `MATCH(${matchCols}) AGAINST(? IN ${all ? "BOOLEAN" : "NATURAL LANGUAGE"} MODE)`;
2638
2771
  const term = all ? mysqlRequireEveryTerm(text) : text;
2772
+ const ranked = textFields.length > 0 && query.controls?.$rank !== false;
2639
2773
  const rank = searchRankOrder(mysqlDialect, {
2640
2774
  exactEqualities: n === void 0 ? [] : integerFields.map((f) => `${qi(f.name)} = ?`),
2641
2775
  n,
2642
- textRank: textFields.length > 0 ? {
2776
+ textRank: ranked ? {
2643
2777
  sql: `${matchClause} DESC`,
2644
2778
  params: [term]
2645
2779
  } : void 0,
@@ -2955,31 +3089,48 @@ function vectorToString(vector) {
2955
3089
  }
2956
3090
  /** InnoDB's default full-text stop words (`INFORMATION_SCHEMA.INNODB_FT_DEFAULT_STOPWORD`). */
2957
3091
  const INNODB_DEFAULT_STOPWORDS = new Set("a about an are as at be by com de en for from how i in is it la of on or that the this to was what when where who will with und www".split(" "));
3092
+ /** A word as InnoDB's full-text parser reads it: a run of {@link INNODB_WORD_CLASS}. */
3093
+ const INNODB_WORD = new RegExp(`${INNODB_WORD_CLASS}+`, "gu");
3094
+ /** A word character right before a run of `*` at the end of a chunk: a prefix. */
3095
+ const INNODB_PREFIX = new RegExp(`${INNODB_WORD_CLASS}\\*+$`, "u");
3096
+ /** `innodb_ft_min_token_size` default: shorter words are not indexed. */
3097
+ const INNODB_MIN_TOKEN_SIZE = 3;
3098
+ /**
3099
+ * Whether InnoDB indexes `word` (an {@link INNODB_WORD} match) by default —
3100
+ * required, it can match a row. Word characters are all in the BMP: `length`
3101
+ * counts characters.
3102
+ */
3103
+ function innodbIndexes(word) {
3104
+ return word.length >= INNODB_MIN_TOKEN_SIZE && !INNODB_DEFAULT_STOPWORDS.has(word.toLowerCase());
3105
+ }
2958
3106
  /**
2959
3107
  * A search text as a boolean-mode `AGAINST` term with every word required
2960
- * (`@db.index.fulltext.match "all"`, since 0.1.168): `+word` per word, a
2961
- * `"quoted phrase"` stays one required phrase, a trailing `*` keeps its prefix
2962
- * meaning. The user's own boolean operators (`+ - < > ( ) ~ * " @`) are text,
2963
- * not syntax. A word InnoDB does not index by default — shorter than 3
2964
- * characters (`innodb_ft_min_token_size`) or on its default stop-word list —
2965
- * stays optional: requiring it would match no row.
3108
+ * (`@db.index.fulltext.match "all"`, since 0.1.168). The text is split into
3109
+ * words the way InnoDB splits it ({@link INNODB_WORD}), so the user's own
3110
+ * boolean operators (`+ - < > ( ) ~ * " @`) and punctuation are text, never
3111
+ * syntax: `the, quick` is the words `the` and `quick`. Each word becomes `+word`; a
3112
+ * `"quoted phrase"` stays one required phrase; a `*` right after a word keeps
3113
+ * its prefix meaning (`+dog*`). A word InnoDB does not index by default —
3114
+ * shorter than 3 characters (`innodb_ft_min_token_size`) or on its default
3115
+ * stop-word list — stays optional (requiring it would match no row), as does
3116
+ * a phrase made only of such words. A prefix is required whatever its length:
3117
+ * it matches the indexed words that start with it.
2966
3118
  */
2967
3119
  function mysqlRequireEveryTerm(text) {
2968
3120
  const parts = [];
2969
3121
  for (const match of text.matchAll(/"([^"]*)"|(\S+)/g)) {
2970
3122
  const phrase = match[1];
2971
3123
  if (phrase !== void 0) {
2972
- const clean = phrase.trim();
2973
- if (clean) parts.push(`+"${clean}"`);
3124
+ const words = phrase.match(INNODB_WORD);
3125
+ if (words) parts.push(`${words.some(innodbIndexes) ? "+" : ""}"${words.join(" ")}"`);
2974
3126
  continue;
2975
3127
  }
2976
- const word = match[2];
2977
- const core = word.replace(/\*+$/, "");
2978
- const pieces = core.split(/[+\-<>()~*"@]+/).filter(Boolean);
2979
- pieces.forEach((piece, i) => {
2980
- const star = i === pieces.length - 1 && core.length < word.length ? "*" : "";
2981
- const required = [...piece].length >= 3 && !INNODB_DEFAULT_STOPWORDS.has(piece.toLowerCase());
2982
- parts.push(`${required ? "+" : ""}${piece}${star}`);
3128
+ const chunk = match[2];
3129
+ const words = chunk.match(INNODB_WORD) ?? [];
3130
+ const prefix = chunk.endsWith("*") && INNODB_PREFIX.test(chunk);
3131
+ words.forEach((word, i) => {
3132
+ if (prefix && i === words.length - 1) parts.push(`+${word}*`);
3133
+ else parts.push(`${innodbIndexes(word) ? "+" : ""}${word}`);
2983
3134
  });
2984
3135
  }
2985
3136
  return parts.join(" ");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-mysql",
3
- "version": "0.1.173",
3
+ "version": "0.1.175",
4
4
  "description": "MySQL adapter for @atscript/db with mysql2 driver support.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -41,21 +41,21 @@
41
41
  "access": "public"
42
42
  },
43
43
  "dependencies": {
44
- "@atscript/db-sql-tools": "^0.1.173"
44
+ "@atscript/db-sql-tools": "^0.1.175"
45
45
  },
46
46
  "devDependencies": {
47
- "@atscript/core": "^0.2.9",
48
- "@atscript/typescript": "^0.2.9",
49
- "@uniqu/core": "^0.2.1",
47
+ "@atscript/core": "^0.2.11",
48
+ "@atscript/typescript": "^0.2.11",
49
+ "@uniqu/core": "^0.2.2",
50
50
  "mysql2": "^3.11.0",
51
- "unplugin-atscript": "^0.2.9"
51
+ "unplugin-atscript": "^0.2.11"
52
52
  },
53
53
  "peerDependencies": {
54
- "@atscript/core": "^0.2.9",
55
- "@atscript/typescript": "^0.2.9",
56
- "@uniqu/core": "^0.2.1",
54
+ "@atscript/core": "^0.2.11",
55
+ "@atscript/typescript": "^0.2.11",
56
+ "@uniqu/core": "^0.2.2",
57
57
  "mysql2": ">=3.0.0",
58
- "@atscript/db": "^0.1.173"
58
+ "@atscript/db": "^0.1.175"
59
59
  },
60
60
  "peerDependenciesMeta": {
61
61
  "mysql2": {