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

Method format

src/ore/src/sql.rs:186–220  ·  view source on GitHub ↗

Formats this SQL fragment by replacing each `{}` with the next SQL argument. Use `{{` and `}}` to escape literal braces.

(self, args: impl IntoIterator<Item = Sql>)

Source from the content-addressed store, hash-verified

184 ///
185 /// Use `{{` and `}}` to escape literal braces.
186 pub fn format(self, args: impl IntoIterator<Item = Sql>) -> Result<Self, SqlFormatError> {
187 let mut args = args.into_iter();
188 let mut out = String::with_capacity(self.0.len());
189 let mut chars = self.0.chars().peekable();
190
191 while let Some(ch) = chars.next() {
192 match ch {
193 '{' => match chars.peek() {
194 Some('{') => {
195 chars.next();
196 out.push('{');
197 }
198 Some('}') => {
199 chars.next();
200 let arg = args.next().ok_or(SqlFormatError::MissingArgument)?;
201 out.push_str(arg.as_str());
202 }
203 _ => return Err(SqlFormatError::InvalidOpenBrace),
204 },
205 '}' => match chars.peek() {
206 Some('}') => {
207 chars.next();
208 out.push('}');
209 }
210 _ => return Err(SqlFormatError::InvalidCloseBrace),
211 },
212 _ => out.push(ch),
213 }
214 }
215
216 if args.next().is_some() {
217 return Err(SqlFormatError::ExtraArgument);
218 }
219 Ok(Sql(Cow::Owned(out)))
220 }
221
222 /// Like [`Sql::format`], but panics on invalid input instead of returning
223 /// an error.

Callers 15

formatBytesShortFunction · 0.45
formatCurrencyFunction · 0.45
SnapshotProgressFunction · 0.45
formatValueFunction · 0.45
prettyFormatAxisValueFunction · 0.45
applyMethod · 0.45
add_objectsMethod · 0.45
remove_objectsMethod · 0.45
log_environment_infoFunction · 0.45
connUrlMethod · 0.45
_check_frontiersFunction · 0.45
workflow_resetFunction · 0.45

Calls 8

SqlClass · 0.85
is_someMethod · 0.80
into_iterMethod · 0.45
lenMethod · 0.45
nextMethod · 0.45
peekMethod · 0.45
pushMethod · 0.45
as_strMethod · 0.45

Tested by 5

connUrlMethod · 0.36
setUpMethod · 0.36
setUpMethod · 0.36
setUpMethod · 0.36
setUpMethod · 0.36