11using Microsoft . CodeAnalysis . CSharp . Syntax ;
22using Plugin ;
33using SqlcGenCsharp . Drivers . Generators ;
4+ using System ;
45using System . Collections . Generic ;
56using System . Linq ;
67using System . Text . RegularExpressions ;
@@ -14,7 +15,7 @@ public NpgsqlDriver(
1415 Options options ,
1516 string defaultSchema ,
1617 Dictionary < string , Dictionary < string , Table > > tables ,
17- Dictionary < string , Dictionary < string , Enum > > enums ,
18+ Dictionary < string , Dictionary < string , Plugin . Enum > > enums ,
1819 IList < Query > queries ) :
1920 base ( options , defaultSchema , tables , enums , queries )
2021 {
@@ -153,51 +154,58 @@ public NpgsqlDriver(
153154 {
154155 { "point" , new DbTypeInfo ( NpgsqlTypeOverride : "NpgsqlDbType.Point" ) }
155156 } ,
156- ordinal => $ "reader.GetFieldValue<NpgsqlPoint>({ ordinal } )"
157+ ordinal => $ "reader.GetFieldValue<NpgsqlPoint>({ ordinal } )",
158+ usingDirective : "NpgsqlTypes"
157159 ) ,
158160 [ "NpgsqlLine" ] = new ColumnMapping (
159161 new Dictionary < string , DbTypeInfo >
160162 {
161163 { "line" , new DbTypeInfo ( NpgsqlTypeOverride : "NpgsqlDbType.Line" ) }
162164 } ,
163- ordinal => $ "reader.GetFieldValue<NpgsqlLine>({ ordinal } )"
165+ ordinal => $ "reader.GetFieldValue<NpgsqlLine>({ ordinal } )",
166+ usingDirective : "NpgsqlTypes"
164167 ) ,
165168 [ "NpgsqlLSeg" ] = new ColumnMapping (
166169 new Dictionary < string , DbTypeInfo >
167170 {
168171 { "lseg" , new DbTypeInfo ( NpgsqlTypeOverride : "NpgsqlDbType.LSeg" ) }
169172 } ,
170- ordinal => $ "reader.GetFieldValue<NpgsqlLSeg>({ ordinal } )"
173+ ordinal => $ "reader.GetFieldValue<NpgsqlLSeg>({ ordinal } )",
174+ usingDirective : "NpgsqlTypes"
171175 ) ,
172176 [ "NpgsqlBox" ] = new ColumnMapping (
173177 new Dictionary < string , DbTypeInfo >
174178 {
175179 { "box" , new DbTypeInfo ( NpgsqlTypeOverride : "NpgsqlDbType.Box" ) }
176180 } ,
177- ordinal => $ "reader.GetFieldValue<NpgsqlBox>({ ordinal } )"
181+ ordinal => $ "reader.GetFieldValue<NpgsqlBox>({ ordinal } )",
182+ usingDirective : "NpgsqlTypes"
178183 ) ,
179184 [ "NpgsqlPath" ] = new ColumnMapping (
180185 new Dictionary < string , DbTypeInfo >
181186 {
182187 { "path" , new DbTypeInfo ( NpgsqlTypeOverride : "NpgsqlDbType.Path" ) }
183188 } ,
184- ordinal => $ "reader.GetFieldValue<NpgsqlPath>({ ordinal } )"
189+ ordinal => $ "reader.GetFieldValue<NpgsqlPath>({ ordinal } )",
190+ usingDirective : "NpgsqlTypes"
185191 ) ,
186192 [ "NpgsqlPolygon" ] = new ColumnMapping (
187193 new Dictionary < string , DbTypeInfo >
188194 {
189195 { "polygon" , new DbTypeInfo ( NpgsqlTypeOverride : "NpgsqlDbType.Polygon" ) }
190196 } ,
191- ordinal => $ "reader.GetFieldValue<NpgsqlPolygon>({ ordinal } )"
197+ ordinal => $ "reader.GetFieldValue<NpgsqlPolygon>({ ordinal } )",
198+ usingDirective : "NpgsqlTypes"
192199 ) ,
193200 [ "NpgsqlCircle" ] = new ColumnMapping (
194201 new Dictionary < string , DbTypeInfo >
195202 {
196203 { "circle" , new DbTypeInfo ( NpgsqlTypeOverride : "NpgsqlDbType.Circle" ) }
197204 } ,
198- ordinal => $ "reader.GetFieldValue<NpgsqlCircle>({ ordinal } )"
205+ ordinal => $ "reader.GetFieldValue<NpgsqlCircle>({ ordinal } )",
206+ usingDirective : "NpgsqlTypes"
199207 ) ,
200- [ "object[] " ] = new ColumnMapping (
208+ [ "object" ] = new ColumnMapping (
201209 new Dictionary < string , DbTypeInfo >
202210 {
203211 { "anyarray" , new DbTypeInfo ( ) }
@@ -206,14 +214,45 @@ public NpgsqlDriver(
206214 )
207215 } ;
208216
217+ protected sealed override Dictionary < string , Tuple < string , string ? > > KnownMappings { get ; } = new ( )
218+ {
219+ {
220+ "NpgsqlPoint" ,
221+ new ( "RegisterNpgsqlTypeHandler<NpgsqlPoint>();" , null )
222+ } ,
223+ {
224+ "NpgsqlLine" ,
225+ new ( "RegisterNpgsqlTypeHandler<NpgsqlLine>();" , null )
226+ } ,
227+ {
228+ "NpgsqlLSeg" ,
229+ new ( "RegisterNpgsqlTypeHandler<NpgsqlLSeg>();" , null )
230+ } ,
231+ {
232+ "NpgsqlBox" ,
233+ new ( "RegisterNpgsqlTypeHandler<NpgsqlBox>();" , null )
234+ } ,
235+ {
236+ "NpgsqlPath" ,
237+ new ( "RegisterNpgsqlTypeHandler<NpgsqlPath>();" , null )
238+ } ,
239+ {
240+ "NpgsqlPolygon" ,
241+ new ( "RegisterNpgsqlTypeHandler<NpgsqlPolygon>();" , null )
242+ } ,
243+ {
244+ "NpgsqlCircle" ,
245+ new ( "RegisterNpgsqlTypeHandler<NpgsqlCircle>();" , null )
246+ }
247+ } ;
248+
209249 public override string TransactionClassName => "NpgsqlTransaction" ;
210250
211251 public override ISet < string > GetUsingDirectivesForQueries ( )
212252 {
213253 return base . GetUsingDirectivesForQueries ( ) . AddRange (
214254 [
215255 "Npgsql" ,
216- "NpgsqlTypes" ,
217256 "System.Data"
218257 ] ) ;
219258 }
@@ -222,7 +261,6 @@ public override ISet<string> GetUsingDirectivesForModels()
222261 {
223262 return base . GetUsingDirectivesForModels ( ) . AddRange (
224263 [
225- "NpgsqlTypes" ,
226264 "System"
227265 ] ) ;
228266 }
@@ -280,17 +318,7 @@ private static void RegisterNpgsqlTypeHandler<T>(){{optionalDotnetCoreSuffix}}
280318
281319 protected override ISet < string > GetConfigureSqlMappings ( )
282320 {
283- return base . GetConfigureSqlMappings ( ) . AddRange (
284- [
285- "RegisterNpgsqlTypeHandler<NpgsqlPoint>();" ,
286- "RegisterNpgsqlTypeHandler<NpgsqlLine>();" ,
287- "RegisterNpgsqlTypeHandler<NpgsqlLSeg>();" ,
288- "RegisterNpgsqlTypeHandler<NpgsqlBox>();" ,
289- "RegisterNpgsqlTypeHandler<NpgsqlPath>();" ,
290- "RegisterNpgsqlTypeHandler<NpgsqlPolygon>();" ,
291- "RegisterNpgsqlTypeHandler<NpgsqlCircle>();" ,
292- "SqlMapper.AddTypeHandler(typeof(JsonElement), new JsonElementTypeHandler());"
293- ] ) ;
321+ return base . GetConfigureSqlMappings ( ) . AddRange ( KnownMappings . Values . Select ( x => x . Item1 ) ) ;
294322 }
295323
296324 // TODO different operations require different types of connections - improve code and docs to make it clearer
0 commit comments