@@ -184,7 +184,7 @@ impl ViewRepresentationsBuilder {
184184 Self ( Vec :: new ( ) )
185185 }
186186
187- /// Add a representation to the list.
187+ /// Add a or overwrite a representation for a view
188188 ///
189189 /// SQL representations dialects must be unique (case insensitive). If a representation with the same
190190 /// dialect already exists, it will be overwritten.
@@ -200,7 +200,25 @@ impl ViewRepresentationsBuilder {
200200 self
201201 }
202202
203- /// Add a SQL representation to the list.
203+ /// Add a SQL representation for a view. Fails if a representation with the same dialect already exists.
204+ pub fn add_representation ( self , representation : ViewRepresentation ) -> Result < Self > {
205+ let dialect = match & representation {
206+ ViewRepresentation :: SqlViewRepresentation ( sql) => & sql. dialect ,
207+ } ;
208+ if self
209+ . 0
210+ . iter ( )
211+ . any ( |r| matches ! ( r, ViewRepresentation :: SqlViewRepresentation ( sql) if sql. dialect. eq_ignore_ascii_case( dialect) ) )
212+ {
213+ return Err ( Error :: new (
214+ ErrorKind :: DataInvalid ,
215+ format ! ( "Representation with dialect {} already exists" , dialect) ,
216+ ) ) ;
217+ }
218+ Ok ( self . add_or_overwrite_representation ( representation) )
219+ }
220+
221+ /// Add a or overwrite a SQL representation for a view
204222 ///
205223 /// SQL representations dialects must be unique. If a representation with the same
206224 /// dialect already exists, it will be overwritten.
@@ -210,6 +228,13 @@ impl ViewRepresentationsBuilder {
210228 ) )
211229 }
212230
231+ /// Add a SQL representation for a view. Fails if a representation with the same dialect already exists.
232+ pub fn add_sql_representation ( self , sql : String , dialect : String ) -> Result < Self > {
233+ self . add_representation ( ViewRepresentation :: SqlViewRepresentation (
234+ SqlViewRepresentation { sql, dialect } ,
235+ ) )
236+ }
237+
213238 /// Build the list of representations.
214239 pub fn build ( self ) -> ViewRepresentations {
215240 ViewRepresentations ( self . 0 )
0 commit comments