반응형
func (a *Account) Insert(db *sql.DB) {
    now := time.Now()
    sql_statement := "INSERT INTO account (account_id, device_model, memory_size, created_at) VALUES ($1, $2, $3, $4);"
    _, err := db.Exec(sql_statement, a.AccountId, a.DeviecModel, a.MemorySize, now)
    if err != nil {
        sql_statement = "UPDATE account SET device_model = $1, memory_size = $2, created_at = $3 WHERE account_id = $4;"
        result, _ := db.Exec(sql_statement, a.DeviecModel, a.MemorySize, now, a.AccountId)

        row, err := result.RowsAffected()

        if row == 0 || err != nil {
            log.Panic().Stack().Err(err).Msg("Insert")
        }
    }
}

+ Recent posts