@@ -230,8 +230,21 @@ def field_names(cls, *, exclude: FieldNamesSet = ()) -> list[str]:
230230
231231 @classmethod
232232 def field_names_sql (
233- cls , * , prefix : Optional [str ] = None , exclude : FieldNamesSet = ()
233+ cls ,
234+ * ,
235+ prefix : Optional [str ] = None ,
236+ exclude : FieldNamesSet = (),
237+ as_prepended : Optional [str ] = None ,
234238 ) -> list [Fragment ]:
239+ if as_prepended :
240+ return [
241+ sql (
242+ "{} AS {}" ,
243+ sql .identifier (f , prefix = prefix ),
244+ sql .identifier (f"{ as_prepended } { f } " ),
245+ )
246+ for f in cls .field_names (exclude = exclude )
247+ ]
235248 return [
236249 sql .identifier (f , prefix = prefix ) for f in cls .field_names (exclude = exclude )
237250 ]
@@ -300,6 +313,16 @@ def from_mapping(cls: type[T], mapping: Mapping[str, Any], /) -> T:
300313 cls .from_mapping = from_mapping_fn # type: ignore
301314 return from_mapping_fn (mapping )
302315
316+ @classmethod
317+ def from_prepended_mapping (
318+ cls : type [T ], mapping : Mapping [str , Any ], prepend : str
319+ ) -> T :
320+ filtered_dict : dict [str , Any ] = {}
321+ for k , v in mapping .items ():
322+ if k .startswith (prepend ):
323+ filtered_dict [k [len (prepend ) :]] = v
324+ return cls .from_mapping (filtered_dict )
325+
303326 @classmethod
304327 def ensure_model (cls : type [T ], row : Union [T , Mapping [str , Any ]]) -> T :
305328 if isinstance (row , cls ):
0 commit comments