How to parse SQL record to Enum - sql-server

Is there a way to parse enum value from table record. For example, I have class which contains user data, and on of them is Enum type. The data is passed from DataRow, but I have trouble parsing enum value.
I tried something like this,
uType= (EType) Enum.TryParse(typeof(row["userType"]));
but it wouldn't compile. Any tip?
Thanks.

try with this code
uType = (EType) Enum.Parse(typeof(EType), row["userType"].ToString(), true);

Enum.TryParse returns a boolean that indicates if the value could be parsed successfully.
Assuming that userType is a string in the DataTable:
EType eType;
bool canParse = Enum.TryParse(row.Field<String>("userType"), out eType);

Related

Filter db rows with GORM preload

Currently, I got this struct
type Token struct {
gorm.Model
Name string `gorm:"column:name"`
Enabled bool `gorm:"column:enabled"`
Symbol string `gorm:"column:symbol"`
TokenDetails []*EarnTokenDetail `gorm:"foreignkey:TokenID;references:ID"`
}
type EarnTokenDetail struct {
gorm.Model
Name string `gorm:"column:name"`
TokenID uint64 `gorm:"column:token_id"`
Enabled bool `gorm:"column:enabled"`
Chains *EarnChain `gorm:"foreignkey:ID;references:ChainID"`
}
type EarnChain struct {
ID uint64 `gorm:"primary_key column:id"`
Enabled bool `gorm:"column:enabled"`
Name string `gorm:"column:name"`
}
And this GORM query:
var tokens []*model.Token
result := e.db.
WithContext(ctx).
Preload("TokenDetails", "token_details.enabled = true").
Preload("TokenDetails.Chains", "chains.enabled = true").
Find(&tokens, "tokens.enabled = true")
It works fine when everything is enabled, but when I disable chains in the database, the result will still show the tokens with disabled chains, with the Chains field empty.
How can I filter out those rows while still using preload?
According the the GORM documentation, that's the expected behavior. It does one query after another, so there is no way to "drop" results of the former queries. If you don't want these results at all - not even with an empty Chain field, consider to use Join() to filter out those. I think I even found a comment pointing that out: How to multiple table joins in GORM

Using go-pg to retrieve virtual columns from Postgres

I'm using go-pg (https://github.com/go-pg/pg) and this code:
type Book struct {
id int
name string
}
var books []Book
err := db.Model(&books).Select()
and everything works good but I need to add a "virtual" column like this:
concat ('info:', 'id:', id, '...') AS info
and I tried to use:
query.ColumnExpr("concat ('info:', 'id:', id, '...') AS info")
but:
go-pg complains with: error="pg: can't find column=info in model=Book (try discard_unknown_columns)"
go-pg doesn't include anymore columns id and name in query: concat... ONLY!
I can understand that because now go-pg doesn't know how to bind data, but I really need that string which I can retrieve from DB only.
Is there a way?
Can I use a custom type like this below?
type CustomBook struct {
Info string
Book
}
Does this make sense?
this approach could work for you:
type Book struct {
ID int
Name string
Info string `pg:"-"`
}
...
db.Model(&books).ColumnExpr("book.*").ColumnExpr("CONCAT('id:', id, 'name:', name) AS info").Select()
pg:"-" ignores the struct field and it is not created nor it produces any errors
this ignored column is documented here: https://pg.uptrace.dev/models/
another approach, depending on your requirements could be like this:
var r []struct {
Name string
Info string
}
db.Model((*Book)(nil)).Column("name").ColumnExpr("CONCAT('id:', id, 'name:', name) AS info").Select(&r)
this second one is documented here: https://pg.uptrace.dev/queries/

Is there a simpler way to decode this json in Go?

I am trying to parse some JSON from Jira to variables. This is using the go-jira package (https://godoc.org/github.com/andygrunwald/go-jira)
Currently I have some code to get the developer:
dev := jiraIssue.Fields.Unknowns["customfield_11343"].(map[string]interface{})["name"]
and team := jiraIssue.Fields.Unknowns["customfield_12046"].([]interface{})[0].(map[string]interface{})["value"]
to get the team they are a part of from.
Getting the team they are on is a bit gross, is there a cleaner way to get the team besides having to type assert, set the index, then type assert again?
Here is the complete json (modified but structure is same, its way too long):
{
"expand":"renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
"id":"136944",
"self":"https://jira.redacted.com/rest/api/2/issue/136944",
"key":"RM-2506",
"fields":{
"customfield_11343":{
"self":"https://redacted.com/rest/api/2/user?username=flast",
"name":"flast",
"key":"flast",
"emailAddress":"flast#redacted.com",
"displayName":"first last",
"active":true,
"timeZone":"Europe/London"
},
"customfield_12046":[
{
"self":"https://jira.redacted.com/rest/api/2/customFieldOption/12045",
"value":"diy",
"id":"12045"
}
],
}
Thanks
The way I go about problems like this is:
Copy some JSON with things I am interested in and paste it into https://mholt.github.io/json-to-go/
Remove fields that arenĀ“t of interest.
Just read the data and unmarshal.
You might end up with something like this given the two custom fields of interest, but you can cut the structure down further if you just need the name.
type AutoGenerated struct {
Fields struct {
Customfield11343 struct {
Self string `json:"self"`
Name string `json:"name"`
Key string `json:"key"`
EmailAddress string `json:"emailAddress"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
TimeZone string `json:"timeZone"`
} `json:"customfield_11343"`
Customfield12046 []struct {
Self string `json:"self"`
Value string `json:"value"`
ID string `json:"id"`
} `json:"customfield_12046"`
} `json:"fields"`
}
The effect you get is that all extra information in the feed is discarded, but you get the data you want very cleanly.
This is a tough one since the second one is in an array form. It makes it hard to use a map.
For the first one, it's simple enough to use:
type JiraCustomField struct {
Self string `json:"self"`
Name string `json:"name"`
Key string `json:"key"`
EmailAddress string `json:"emailAddress"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
TimeZone string `json:"timeZone"`
}
type JiraPayload struct {
Expand string `json:"expand"`
ID string `json:"id"`
Key string `json:"key"`
Fields map[string]JiraCustomField `json:"fields"`
}
https://play.golang.org/p/y8-g6r0kInV
Specifically this part Fields map[string]JiraCustomField for the second case it looks like you need it in an array form like Fields map[string][]JiraCustomField.
In a case like this, I think you'll need to make your own Unmarshaler. This is a good tutorial: https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/
What you could do with your custom Unmarshal/marshaler, is use the Reflection package and check if it's an array or a struct. If it's a struct then put it into an array, and store it in Fields map[string][]JiraCustomField.

Decimal type in Go and Postgresql with gorm

So i'm creating an API and I needed to store the price of something.
I'm using gorm and gormigrate for my database migration.
I'm just wondering what proper type should I use for storing decimals. I've red somewhere that I shouldn't use floats when storing currencies.
type MyStruct struct {
Name string `json:"name" gorm:"not null"`
Description string `json:"description" gorm:"null"`
Price <what type should be here> `json:"price"`
}
So, based on the suggestion of #ain, I used shopspring/decimal. But it's giving me an error when I do automigrate.
It turns out that I only needed to set the type to numeric using a gorm tag to make it work:
type MyStruct struct {
Name string `json:"name" gorm:"not null"`
Description string `json:"description" gorm:"null"`
Price decimal.Decimal `json:"price" gorm:"type:numeric"`
}

Inserting NULL integer using VB.Net and EF5

Working on an application that relies on an older version of entity, and I'm trying to insert a NULL into an int field. The field in SQL Server is (int, null).
Here's the definition of the object in EF:
<EdmScalarPropertyAttribute(EntityKeyProperty:=false, IsNullable:=true)>
<DataMemberAttribute()>
Public Property application_id() As Nullable(Of Global.System.Int32)
...and here is where I'm trying to set it:
applications.application_id = IIf(IsNumeric(txtAppID.Text), CInt(txtAppID.Text), Nothing)
The error thrown in response is:
An exception of type 'System.InvalidCastException' occurred in ... but was not handled in user code
Additional information: Specified cast is not valid.
I can confirm that this issue is being thrown due to the Nothing portion because previously it was applications.application_id = CInt(txtAppID.Text) and all was fine.
I've tried DBNull.Value instead of Nothing, though the error reads the same. Done a fair bit of research though most issues relate to ES6 or datetime fields, and as such I felt my issue was specific enough to warrant its own question.
Thanks.
The IIf function doesn't short circuit, and therefore always evaluates both the true and false parts, so it's not going to work in that situation. The If keyword does short circuit, but you will probably run into issues with the return type and nullable value types (e.g. Dim x As Integer? = If(False, 1, Nothing) results in x = 0 because the If is returning Integer and not Integer?).
So, I would recommend either using a regular If statement:
If IsNumeric(txtAppID.Text) Then
applications.application_id = CInt(txtAppID.Text)
Else
applications.application_id = Nothing
End If
or you could create a helper function:
Function NullableCInt(value As String) As Integer?
If IsNumeric(value) Then Return CInt(value)
Return Nothing
End Function
and use that:
applications.application_id = NullableCInt(txtAppID.Text)
You can get working If method with casting
Dim temp As Integer
applications.application_id = If(Integer.TryParse(value, temp), temp, DirectCast(Nothing, Integer?))
For better readability you can introduce "default" value
Static DEFAULT_VALUE As Integer? = Nothing
Dim temp As Integer
applications.application_id = If(Integer.TryParse(value, temp), temp, DEFAULT_VALUE)
With Integer.TryParse you need "check/convert" string to integer only once.

Resources