@@ -94,13 +94,13 @@ def param(value: object, name: str | None = None) -> ParamInterpolation:
9494 return ParamInterpolation (value = value , expression = name )
9595
9696
97- def template (thing : object , / , ** ignored_kwargs ) -> OurTemplate :
97+ def template (thing : object , / , ** ignored_kwargs ) -> SqlTemplate :
9898 """Convert something to a Template-ish.
9999
100100 The rules are:
101101 - If the thing has a __duckdb_template__ method, call it and convert the
102102 resuling strings and IntoParams into a SqlTemplate.
103- - If the thing is a string , treat it as raw SQL and return a SqlTemplate with that string.
103+ - If the thing is a `str` , treat it as raw SQL and return a SqlTemplate with that string.
104104 - If the thing is an IntoTemplate, resolve it into a SqlTemplate by recursively resolving
105105 any inner IntoInterpolations and flattening any nested templates.
106106 - If the thing is an IntoInterpolation, resolve it into a SqlTemplate by recursively resolving
@@ -110,31 +110,32 @@ def template(thing: object, /, **ignored_kwargs) -> OurTemplate:
110110 if isinstance (thing , SupportsDuckdbTemplate ):
111111 raw = thing .__duckdb_template__ (** ignored_kwargs )
112112 parts = [raw ] if isinstance (raw , str ) or is_into_interpolation (raw ) else list (raw )
113- return OurTemplate (* parts )
113+ return SqlTemplate (* parts )
114114 if isinstance (thing , str ):
115- return OurTemplate (thing )
115+ return SqlTemplate (thing )
116116 if isinstance (thing , IntoTemplate ):
117- return OurTemplate (* thing )
117+ return SqlTemplate (* thing )
118118 if isinstance (thing , IntoInterpolation ):
119- return OurTemplate (thing )
120- return OurTemplate (param (value = thing ))
119+ return SqlTemplate (thing )
120+ return SqlTemplate (param (value = thing ))
121121
122122
123123def compile (thing : object ) -> CompiledSql :
124+ """Compile a thing into a final SQL string with named parameter placeholders, and a list of Params."""
124125 t = template (thing )
125126 resolved = resolve (t )
126127 return compile_parts (resolved )
127128
128129
129- def resolve (parts : Iterable [str | IntoInterpolation ]) -> OurTemplate [str | IntoInterpolation ]:
130+ def resolve (parts : Iterable [str | IntoInterpolation ]) -> SqlTemplate [str | IntoInterpolation ]:
130131 """Resolve an OurTemplate by recursively resolving any inner templates and interpolations."""
131132 resolved : list [str | IntoInterpolation ] = []
132133 for part in parts :
133134 if isinstance (part , str ):
134135 resolved .append (part )
135136 else :
136137 resolved .extend (resolve_interpolation (part ))
137- return OurTemplate (* resolved )
138+ return SqlTemplate (* resolved )
138139
139140
140141def resolve_interpolation (interp : IntoInterpolation ) -> Iterable [str | IntoInterpolation ]:
@@ -151,15 +152,15 @@ def resolve_interpolation(interp: IntoInterpolation) -> Iterable[str | IntoInter
151152 # Note that this is potentially unsafe if the value comes from an untrusted source,
152153 # since it could lead to SQL injection vulnerabilities, so it should be used with caution.
153154 if interp .conversion == "s" :
154- return OurTemplate (str (value ))
155+ return SqlTemplate (str (value ))
155156 elif interp .conversion == "r" :
156- return OurTemplate (repr (value ))
157+ return SqlTemplate (repr (value ))
157158 elif interp .conversion == "a" :
158- return OurTemplate (ascii (value ))
159+ return SqlTemplate (ascii (value ))
159160
160161 if isinstance (value , str ):
161162 # do NOT pass to template, since that would treat it as a raw SQL.
162- return OurTemplate (param (value , name = interp .expression ))
163+ return SqlTemplate (param (value , name = interp .expression ))
163164 templ = template (value )
164165 # resolved = resolve(templ)
165166 # If the resolved inner is just a single Interpolation, then just return
@@ -175,7 +176,7 @@ def resolve_interpolation(interp: IntoInterpolation) -> Iterable[str | IntoInter
175176 # eg with a friendly param name, rather than
176177 # "SELECT * FROM people WHERE age = $p0", with a param $p0=37
177178 if len (templ .strings ) == 2 and templ .strings [0 ] == "" and templ .strings [1 ] == "" and len (templ .interpolations ) == 1 :
178- return OurTemplate (param (value = value , name = interp .expression ))
179+ return SqlTemplate (param (value = value , name = interp .expression ))
179180 else :
180181 # We got something nested, eg
181182 # age = 37
@@ -240,7 +241,7 @@ def __init__(self, value: object, expression: str | None = None) -> None:
240241 self .format_spec = ""
241242
242243
243- class OurTemplate :
244+ class SqlTemplate :
244245 """A simple implementation of IntoTemplate, for testing purposes."""
245246
246247 def __init__ (
@@ -256,7 +257,7 @@ def __iter__(self) -> Iterator[str | IntoInterpolation]:
256257 yield i
257258 yield self .strings [- 1 ]
258259
259- def resolve (self ) -> OurTemplate :
260+ def resolve (self ) -> SqlTemplate :
260261 """Resolve any inner templates and interpolations, returning a new OurTemplate with only strings and ParamInterpolations."""
261262 return resolve (self )
262263
0 commit comments