You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2019/04/16 19:15:12 UTC

[GitHub] [trafficcontrol] rob05c opened a new issue #3494: Change TO Large Selects to Embed Non-Struct Fields for Sqlx

rob05c opened a new issue #3494: Change TO Large Selects to Embed Non-Struct Fields for Sqlx
URL: https://github.com/apache/trafficcontrol/issues/3494
 
 
   So, we went through all this debate about adding the massive `sqlx` library for its one `StructScan` function, to avoid things like:
   
   ```
   	resultRows, err := tx.Query(insertQuery(), &ds.Active, &ds.AnonymousBlockingEnabled, &ds.CacheURL, &ds.CCRDNSTTL, &ds.CDNID, &ds.CheckPath, &ds.ConsistentHashRegex, &deepCachingType, &ds.DisplayName, &ds.DNSBypassCNAME, &ds.DNSBypassIP, &ds.DNSBypassIP6, &ds.DNSBypassTTL, &ds.DSCP, &ds.EdgeHeaderRewrite, &ds.GeoLimitRedirectURL, &ds.GeoLimit, &ds.GeoLimitCountries, &ds.GeoProvider, &ds.GlobalMaxMBPS, &ds.GlobalMaxTPS, &ds.FQPacingRate, &ds.HTTPBypassFQDN, &ds.InfoURL, &ds.InitialDispersion, &ds.IPV6RoutingEnabled, &ds.LogsEnabled, &ds.LongDesc, &ds.LongDesc1, &ds.LongDesc2, &ds.MaxDNSAnswers, &ds.MidHeaderRewrite, &ds.MissLat, &ds.MissLong, &ds.MultiSiteOrigin, &ds.OriginShield, &ds.ProfileID, &ds.Protocol, &ds.QStringIgnore, &ds.RangeRequestHandling, &ds.RegexRemap, &ds.RegionalGeoBlocking, &ds.RemapText, &ds.RoutingName, &ds.SigningAlgorithm, &ds.SSLKeyVersion, &ds.TenantID, &ds.TRRequestHeaders, &ds.TRResponseHeaders, &ds.TypeID, &ds.XMLID)
   ```
   
   And we're not even avoiding them. The worse ones are still using the standard library:
   
   https://github.com/apache/trafficcontrol/blob/master/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go#L143
   
   Because we need fields not in the struct, and `sqlx` isn't capable of that. Note the `&deepCachingType` in the above query.
   
   But this is possible, and not even that difficult. All we have to do is create a new struct, which anonymously embeds the real struct. Like so:
   
   ```
   type CreateDS struct {
     tc.DeliveryServiceNullable
     DeepCachingType string
   }
   createDS := &CreateDS{DeliveryServiceNullable: ds, DeepCachingType: deepCachingType}
   resultRows, err := tx.StructScan(insertQuery(), createDS)
   ds = &updateDS.DeliveryServiceNullable
   ```
   
   As well as updating `insertQuery()` to use `sqlx` `active=:active` syntax rather than standard SQL `active=$1`.
   
   In this particular case, it's also possible to solve the problem by making `tc.DeliveryService.DeepCachingType` implement `sql.Valuer` and `sql.Scanner`; but the above solution works universally.
   
   In fact, it would probably take less time to do, than it took me to make this issue.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services