首先,我嘗試了過去stackoverflow答案的解決方案,這些答案與我的問題有關,但沒有任何效果,這就是為什么我將它作為一個單獨的問題提出。
我在 golang 中有兩個結構
type otherPayments struct {
DebitTo int `json:"debit_To" binding:"required"`
CreditFrom int `json:"credit_from" binding:"required"`
OverallType string `json:"overall_type" binding:"required"`
}
type advanceAndRoomPayment struct {
PmID int `json:"pm_id" binding:"required"` //Payment method id
PmName string `json:"pm_name" binding:"required"` //Payment method name
DebitTo int `json:"debit_To" binding:"required"` //The ledger to debit from
CreditFrom int `json:"credit_from" binding:"required"` //The ledger to credit from
OverallType string `json:"overall_type" binding:"required"` //Overall transaction type
}
我的postgresql 表中有5SQL 列booking_settings
initial列,型別 =otherPayments,JSONBcancellation, 型別 =otherPayments,JSONBupdation, 型別 =otherPayments,JSONBadvance_payment型別 =advanceAndRoomPayment,JSONB []room_payment, 型別 =advanceAndRoomPayment,JSONB []
查詢SELECT如下
SELECT initial, cancellation, updation advance_payment, room_payment FROM booking_settings WHERE hotel_id = $1
我使用的 sql 包是https://jmoiron.github.io/sqlx/
我正在嘗試將上面的列掃描到它們適當的結構變數中,到目前為止,我只能設法掃描initial, cancellation and updation,但不能掃描JSONB [] advance_payment and room_payment
非常感謝任何幫助,謝謝
uj5u.com熱心網友回復:
以防萬一您不知道,jsonb[]是一個PostgreSQL 陣列型別,其元素型別為jsonb. 它不是“json 陣列”型別。
如果要將 JSON 資料存盤在列中,則應使用json/jsonb型別,無論您希望該資料包含標量、物件還是陣列JSON 值。
因此,除非您在考慮某些特定用例的情況下選擇 PostgreSQL Array 型別,否則將列的型別從 更改為 可能會jsonb[]更好jsonb。
如果您不能或不想更改列型別,那么您仍然可以在 SELECT 查詢中將 PostgreSQL 陣列轉換為 JSON 陣列,然后在您的自定義 Gosql.Scanner實作中,用于json.Unmarshal解碼 db 資料.
SELECT to_jsonb(advance_payment) FROM booking_settings WHERE hotel_id = $1
-- or
SELECT array_to_json(advance_payment)::jsonb FROM booking_settings WHERE hotel_id = $1
type advanceAndRoomPaymentList []advanceAndRoomPayment
func (ls *advanceAndRoomPaymentList) Scan(src any) error {
var data []byte
switch v := src.(type) {
case string:
data = []byte(v)
case []byte:
data = v
}
return json.Unmarshal(data, ls)
}
如果您有許多參考 PostgreSQL 陣列列的查詢并且您不想更新每一個來進行轉換,您可以自己決議 PostgreSQL 陣列然后解組各個元素,或者您可以將這項作業委托給一些第三方實施。
這是一個未經測驗的示例,使用pq.GenericArray:
// I haven't tested the following but I'd assume it ought to work,
// if not, then perhaps maybe small tweaks are needed here and there...
type advanceAndRoomPaymentList []advanceAndRoomPayment
func (ls *advanceAndRoomPaymentList) Scan(src any) error {
return pq.GenericArray{ls}.Scan(src)
}
// implement Scanner for the element type of the slice
func (a *advanceAndRoomPayment) Scan(src any) error {
var data []byte
switch v := src.(type) {
case string:
data = []byte(v)
case []byte:
data = v
}
return json.Unmarshal(data, a)
}
如果您想自己決議 PostgreSQL 陣列,那么您需要了解用于表示此類陣列的語法。您可以在此處找到相關檔案:
陣列值的外部文本表示由根據陣列元素型別的 I/O 轉換規則解釋的項以及指示陣列結構的裝飾組成。裝飾由陣列值周圍的花括號({ 和})加上相鄰項之間的分隔符組成。分隔符通常是逗號 (,),但也可以是其他字符:它由陣列元素型別的 typdelim 設定確定。在 PostgreSQL 發行版中提供的標準資料型別中,所有型別都使用逗號,但 type box 除外,它使用分號 (;)。
因此,例如,如果您的 pg-array 包含一個 json-object、一個 json-array、一個 json-string 和一個 json-bool,并且您選擇了它,那么將傳遞給sql.Scanner實作的陣串列示將看起來像:
{"{\"foo\": \"bar\"}","[\"foo\", \"bar\"]","\"foo bar\"",true}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/504853.html
標籤:PostgreSQL 去 去金酒 sqlx
