MCPcopy Create free account
hub / github.com/Effect-TS/effect / exec

Method exec

packages/sql/sqlite-do/test/Client.test.ts:41–119  ·  view source on GitHub ↗
(sql: string, ...params: ReadonlyArray<unknown>)

Source from the content-addressed store, hash-verified

39 }
40
41 exec(sql: string, ...params: ReadonlyArray<unknown>) {
42 this.statements.push(sql)
43 const statement = normalizeSql(sql)
44 if (/^(BEGIN|COMMIT|ROLLBACK|SAVEPOINT)\b/i.test(statement)) {
45 throw new Error(`Unsupported transaction SQL: ${statement}`)
46 }
47
48 if (/^CREATE TABLE\b/i.test(statement)) {
49 const match = /^CREATE TABLE(?: IF NOT EXISTS)?\s+("[^"]+"|\w+)\s*\((.*)\)$/i.exec(statement)
50 if (match !== null) {
51 const table = unquote(match[1])
52 if (!this.tables.has(table)) {
53 this.tables.set(table, [])
54 this.columns.set(
55 table,
56 match[2].split(",").map((part) => unquote(part.trim().split(/\s+/)[0])).filter((column) =>
57 column.length > 0 && column.toUpperCase() !== "PRIMARY"
58 )
59 )
60 }
61 }
62 return new FakeCursor([])
63 }
64
65 if (/^INSERT INTO\b/i.test(statement)) {
66 const match = /^INSERT INTO\s+("[^"]+"|\w+)\s*\(([^)]+)\)\s+VALUES\s+(.+)$/i.exec(statement)
67 if (match !== null) {
68 const table = unquote(match[1])
69 const columns = match[2].split(",").map((column) => unquote(column.trim()))
70 const rows = this.tables.get(table) ?? []
71 const tableColumns = this.columns.get(table) ?? [...columns]
72 if (!this.tables.has(table)) {
73 this.tables.set(table, rows)
74 this.columns.set(table, tableColumns)
75 }
76 let paramIndex = 0
77 for (const group of valueGroups(match[3])) {
78 const values = group.split(",").map((value) => parseValue(value.trim(), params, () => paramIndex++))
79 const row: Record<string, unknown> = {}
80 if (tableColumns.includes("id") && !columns.includes("id")) {
81 row.id = rows.length + 1
82 }
83 for (let i = 0; i < columns.length; i++) {
84 row[columns[i]] = values[i]
85 }
86 if (tableColumns.includes("created_at") && row.created_at === undefined) {
87 row.created_at = "current_timestamp"
88 }
89 rows.push(row)
90 }
91 }
92 return new FakeCursor([])
93 }
94
95 if (/^SELECT\b/i.test(statement)) {
96 const match =
97 /^SELECT\s+(.+)\s+FROM\s+("[^"]+"|\w+)(?:\s+WHERE\s+("[^"]+"|\w+)\s*=\s*(\?|('[^']*')|\d+))?(?:\s+ORDER BY\s+("[^"]+"|\w+)\s+DESC)?$/i
98 .exec(statement)

Callers 15

extractModuleMetadataFunction · 0.45
transformFunction · 0.45
snippetsFunction · 0.45
registryClientFunction · 0.45
githubClientFunction · 0.45
negatedClassPatternFunction · 0.45
parseTagsFunction · 0.45
parseExampleFunction · 0.45
main.tsFile · 0.45
importMapReplacementsFunction · 0.45
markdownSafetyIssuesFunction · 0.45
selectionNamesFunction · 0.45

Calls 13

normalizeSqlFunction · 0.85
valueGroupsFunction · 0.85
trimMethod · 0.80
unquoteFunction · 0.70
parseValueFunction · 0.70
pushMethod · 0.65
hasMethod · 0.65
setMethod · 0.65
getMethod · 0.65
NumberFunction · 0.50
filterMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected