@tinycloud/sdk-services 2.3.0-beta.6 → 2.3.0-beta.7

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
@@ -1798,7 +1798,7 @@ var SQLService = class extends BaseService {
1798
1798
  try {
1799
1799
  const response = await this.invokeSQL(
1800
1800
  dbName,
1801
- SQLAction.READ,
1801
+ this.actionForSql(sql, SQLAction.READ),
1802
1802
  { action: "query", sql, params: params ?? [] },
1803
1803
  options?.signal
1804
1804
  );
@@ -1828,7 +1828,7 @@ var SQLService = class extends BaseService {
1828
1828
  }
1829
1829
  const response = await this.invokeSQL(
1830
1830
  dbName,
1831
- SQLAction.WRITE,
1831
+ this.actionForSql(sql, SQLAction.WRITE),
1832
1832
  body,
1833
1833
  options?.signal
1834
1834
  );
@@ -1850,7 +1850,7 @@ var SQLService = class extends BaseService {
1850
1850
  try {
1851
1851
  const response = await this.invokeSQL(
1852
1852
  dbName,
1853
- SQLAction.WRITE,
1853
+ this.actionForSqlBatch(statements),
1854
1854
  { action: "batch", statements },
1855
1855
  options?.signal
1856
1856
  );
@@ -1927,6 +1927,14 @@ var SQLService = class extends BaseService {
1927
1927
  signal: this.combineSignals(signal)
1928
1928
  });
1929
1929
  }
1930
+ actionForSql(sql, fallback) {
1931
+ return firstSqlToken(sql) === "pragma" ? SQLAction.ADMIN : fallback;
1932
+ }
1933
+ actionForSqlBatch(statements) {
1934
+ return statements.some(
1935
+ (statement) => this.actionForSql(statement.sql, SQLAction.WRITE) === SQLAction.ADMIN
1936
+ ) ? SQLAction.ADMIN : SQLAction.WRITE;
1937
+ }
1930
1938
  async handleErrorResponse(response, operation) {
1931
1939
  const errorText = await response.text();
1932
1940
  let errorBody = {};
@@ -1972,6 +1980,33 @@ var SQLService = class extends BaseService {
1972
1980
  }
1973
1981
  };
1974
1982
  SQLService.serviceName = "sql";
1983
+ function firstSqlToken(sql) {
1984
+ let index = 0;
1985
+ while (index < sql.length) {
1986
+ while (index < sql.length && /\s/.test(sql[index])) {
1987
+ index++;
1988
+ }
1989
+ if (sql.startsWith("--", index)) {
1990
+ const newline = sql.indexOf("\n", index + 2);
1991
+ if (newline === -1) {
1992
+ return void 0;
1993
+ }
1994
+ index = newline + 1;
1995
+ continue;
1996
+ }
1997
+ if (sql.startsWith("/*", index)) {
1998
+ const end = sql.indexOf("*/", index + 2);
1999
+ if (end === -1) {
2000
+ return void 0;
2001
+ }
2002
+ index = end + 2;
2003
+ continue;
2004
+ }
2005
+ break;
2006
+ }
2007
+ const match = /^[A-Za-z_]+/.exec(sql.slice(index));
2008
+ return match?.[0].toLowerCase();
2009
+ }
1975
2010
 
1976
2011
  // src/duckdb/DuckDbDatabaseHandle.ts
1977
2012
  var DuckDbDatabaseHandle = class {