@h-ai/reldb 0.1.0-alpha.10 → 0.1.0-alpha.12

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.js CHANGED
@@ -1886,6 +1886,7 @@ function createMysqlProvider() {
1886
1886
  };
1887
1887
  }
1888
1888
  var logger3 = core.logger.child({ module: "reldb", scope: "postgres" });
1889
+ var PG_DOLLAR_TAG_REGEX = /^\$([A-Z_]\w*)?\$/i;
1889
1890
  function createPostgresProvider() {
1890
1891
  let pool = null;
1891
1892
  const ctx = {
@@ -1893,8 +1894,94 @@ function createPostgresProvider() {
1893
1894
  logger: logger3
1894
1895
  };
1895
1896
  function convertPlaceholders(sql) {
1897
+ let out = "";
1896
1898
  let index = 0;
1897
- return sql.replace(/\?/g, () => `$${++index}`);
1899
+ let i = 0;
1900
+ const len = sql.length;
1901
+ while (i < len) {
1902
+ const ch = sql[i];
1903
+ if (ch === "'") {
1904
+ out += ch;
1905
+ i++;
1906
+ while (i < len) {
1907
+ const c = sql[i];
1908
+ out += c;
1909
+ i++;
1910
+ if (c === "'") {
1911
+ if (sql[i] === "'") {
1912
+ out += sql[i];
1913
+ i++;
1914
+ continue;
1915
+ }
1916
+ break;
1917
+ }
1918
+ }
1919
+ continue;
1920
+ }
1921
+ if (ch === '"') {
1922
+ out += ch;
1923
+ i++;
1924
+ while (i < len) {
1925
+ const c = sql[i];
1926
+ out += c;
1927
+ i++;
1928
+ if (c === '"') {
1929
+ if (sql[i] === '"') {
1930
+ out += sql[i];
1931
+ i++;
1932
+ continue;
1933
+ }
1934
+ break;
1935
+ }
1936
+ }
1937
+ continue;
1938
+ }
1939
+ if (ch === "$") {
1940
+ const tagMatch = sql.slice(i).match(PG_DOLLAR_TAG_REGEX);
1941
+ if (tagMatch) {
1942
+ const tag = tagMatch[0];
1943
+ const end = sql.indexOf(tag, i + tag.length);
1944
+ if (end === -1) {
1945
+ out += sql.slice(i);
1946
+ i = len;
1947
+ } else {
1948
+ out += sql.slice(i, end + tag.length);
1949
+ i = end + tag.length;
1950
+ }
1951
+ continue;
1952
+ }
1953
+ }
1954
+ if (ch === "-" && sql[i + 1] === "-") {
1955
+ const nl = sql.indexOf("\n", i);
1956
+ if (nl === -1) {
1957
+ out += sql.slice(i);
1958
+ i = len;
1959
+ } else {
1960
+ out += sql.slice(i, nl + 1);
1961
+ i = nl + 1;
1962
+ }
1963
+ continue;
1964
+ }
1965
+ if (ch === "/" && sql[i + 1] === "*") {
1966
+ const endIdx = sql.indexOf("*/", i + 2);
1967
+ if (endIdx === -1) {
1968
+ out += sql.slice(i);
1969
+ i = len;
1970
+ } else {
1971
+ out += sql.slice(i, endIdx + 2);
1972
+ i = endIdx + 2;
1973
+ }
1974
+ continue;
1975
+ }
1976
+ if (ch === "?") {
1977
+ out += `$${++index}`;
1978
+ i++;
1979
+ continue;
1980
+ }
1981
+ out += ch;
1982
+ i++;
1983
+ }
1984
+ return out;
1898
1985
  }
1899
1986
  function pgTxErrorMessage(detail) {
1900
1987
  return reldbM("reldb_postgresTxFailed", { params: { error: detail } });
@@ -2379,6 +2466,10 @@ function createSqliteProvider() {
2379
2466
  async close() {
2380
2467
  if (database) {
2381
2468
  try {
2469
+ try {
2470
+ await txChain;
2471
+ } catch {
2472
+ }
2382
2473
  database.close();
2383
2474
  } catch (error) {
2384
2475
  database = null;