MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / doExecuteSql

Function doExecuteSql

console/src/api/materialize/executeSql.tsx:102–196  ·  view source on GitHub ↗
(
  environmentdHttpAddress: string,
  request: SqlRequest,
  requestOptions?: RequestInit,
)

Source from the content-addressed store, hash-verified

100}
101
102const doExecuteSql = async (
103 environmentdHttpAddress: string,
104 request: SqlRequest,
105 requestOptions?: RequestInit,
106): Promise<ExecuteSqlOutput> => {
107 const url = buildExecuteSqlUrl(environmentdHttpAddress);
108 // Optional session vars that will be set before running the request.
109 //
110 // Note: the JSON object is automatically URI encoded by the URL object.
111 const options = buildSessionVariables({
112 cluster: request.cluster,
113 cluster_replica: request.replica,
114 transaction_isolation:
115 request.transactionIsolation ?? "strict serializable",
116 });
117 url.searchParams.append("options", JSON.stringify(options));
118
119 const { headers, ...requestOpts } = requestOptions ?? {};
120 const response = await apiClient.mzApiFetch(
121 new Request(url.toString(), {
122 method: "POST",
123 headers: {
124 "Content-Type": "application/json",
125 ...headers,
126 },
127 body: JSON.stringify({
128 queries: request.queries,
129 }),
130 ...requestOpts,
131 }),
132 );
133
134 const responseText = await response.text();
135
136 if (!response.ok) {
137 if (response.status === 401) {
138 throw new Error(UNAUTHORIZED_ERROR);
139 }
140 if (response.status === 403 && isJsonObject(responseText)) {
141 const responseError = JSON.parse(responseText);
142 return {
143 errorMessage: responseError.message,
144 code: responseError.code,
145 detail: responseError.detail,
146 status: response.status,
147 };
148 }
149 return {
150 status: response.status,
151 errorMessage: responseText || DEFAULT_QUERY_ERROR,
152 };
153 } else {
154 const parsedResponse = JSON.parse(responseText);
155 const { results } = parsedResponse;
156 const outResults = [];
157 for (const oneResult of results) {
158 // Queries like `CREATE TABLE` or `CREATE CLUSTER` returns a null inside the results array
159 const { error: resultsError, rows } = oneResult || {};

Callers 1

executeSqlFunction · 0.85

Calls 8

buildExecuteSqlUrlFunction · 0.85
buildSessionVariablesFunction · 0.85
isJsonObjectFunction · 0.85
appendMethod · 0.45
parseMethod · 0.45
mapMethod · 0.45
getMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected