亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PlaceHolderFormat 不會在 SQL 期間使用 pgx 驅動程序替換參數值的美元符號

PlaceHolderFormat 不會在 SQL 期間使用 pgx 驅動程序替換參數值的美元符號

Go
森林海 2022-09-05 10:39:34
我是Go的新手,并且正在嘗試根據postgresql數據庫中的用戶名檢查密碼。我無法讓美元替換發生,寧愿不訴諸于連接字符串。我目前正在使用松鼠,但也嘗試過,沒有,沒有太多的運氣。我有以下代碼:    package datalayerimport (    "database/sql"    "encoding/json"    "fmt"    "net/http"    sq "github.com/Masterminds/squirrel"    _ "github.com/jackc/pgx/v4/stdlib"    "golang.org/x/crypto/bcrypt"    "github.com/gin-gonic/gin")var (    // for the database    db *sql.DB)func InitDB(sqlDriver string, dataSource string) error {    var err error    // Connect to the postgres db  (sqlDriver is literal string "pgx")    db, err = sql.Open(sqlDriver, dataSource)    if err != nil {        panic(err)    }    return db.Ping()}// Create a struct that models the structure of a user, both in the request body, and in the DBtype Credentials struct {    Password string `json:"password", db:"password"`    Username string `json:"username", db:"username"`}func Signin(c *gin.Context) {    // Parse and decode the request body into a new `Credentials` instance    creds := &Credentials{}    err := json.NewDecoder(c.Request.Body).Decode(creds)    if err != nil {        // If there is something wrong with the request body, return a 400 status        c.Writer.WriteHeader(http.StatusBadRequest)        return    }    query := sq.        Select("password").        From("users").        Where("username = $1", creds.Username).        PlaceholderFormat(sq.Dollar)        // The line below doesn't substitute the $ sign, it shows this:  SELECT password FROM users WHERE username = $1 [rgfdgfd] <nil>    fmt.Println(sq.        Select("password").        From("users").        Where("username = $1", creds.Username).        PlaceholderFormat(sq.Dollar).ToSql())    rows, sqlerr := query.RunWith(db).Query()    if sqlerr != nil {        panic(fmt.Sprintf("QueryRow failed: %v", sqlerr))    }    if err != nil {        // If there is an issue with the database, return a 500 error        c.Writer.WriteHeader(http.StatusInternalServerError)        return    }  當我檢查pgAdmin時,我看到以下內容,顯示美元符號未被替換:
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

占位符的替換是由postgres服務器完成的,不應該是Go代碼或松鼠的工作來做替換。

在執行采用參數的查詢時,數據庫驅動程序必須執行的操作的大致概述如下所示:

  1. 使用查詢字符串(占位符保持不變),將向 postgres 服務器發送請求以創建預準備語句parse

  2. 使用參數值和新創建的語句的標識符,發送一個請求,通過創建門戶使語句準備好執行。門戶(類似于游標,但不相同)表示準備執行或已部分執行的語句,并填充任何缺少的參數值。bind

  3. 使用門戶的標識符將請求發送到服務器,然后服務器執行門戶的查詢。execute

請注意,上述步驟只是一個粗略的概述,實際上數據庫客戶端和服務器之間涉及更多的請求 - 響應周期。

就目前而言,我相信它向您顯示的是請求創建的準備好的聲明,盡管由于我不熟悉它,因此我無法確定。pgAdminparse


從理論上講,像 這樣的幫助程序庫或類似的驅動程序庫可以實現參數本身的替換,然后向服務器發送一個簡單的查詢。但是,一般來說,考慮到SQL注入的可能性,在我看來,最好將其留給postgres服務器的權威。squirrelpgx


的工作是簡單地將占位符轉換為指定的格式。例如,您可以使用MySQL格式編寫SQL,然后調用該方法將其轉換為PostgreSQL格式。PlaceholderFormat(?,?,...)PlaceholderFormat(sql.Dollar)($1,$2,...)


查看完整回答
反對 回復 2022-09-05
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號