-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
112 lines (97 loc) · 3.29 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2019 Satoshi Konno. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package postgresql
import (
"github.com/cybergarage/go-postgresql/postgresql/protocol/message"
"github.com/cybergarage/go-postgresql/postgresql/query"
)
// PreparedStatement represents a prepared statement.
type PreparedStatement struct {
*message.Parse
ParsedStatement *query.Statement
}
// Name returns a prepared statement name.
func (stmt *PreparedStatement) Name() string {
return stmt.Parse.Name
}
// PreparedStatementMap represents a prepared statement map.
type PreparedStatementMap map[string]*PreparedStatement
// NewPreparedStatementMap returns a new prepared statement map.
func NewPreparedStatementMap() PreparedStatementMap {
return make(PreparedStatementMap)
}
// PreparedStatement returns a prepared statement.
func (stmtMap PreparedStatementMap) PreparedStatement(name string) (*PreparedStatement, error) {
q, oK := stmtMap[name]
if !oK {
return nil, query.NewErrPreparedStatementNotExist(name)
}
return q, nil
}
// SetPreparedStatement sets a prepared statement.
func (stmtMap PreparedStatementMap) SetPreparedStatement(msg *message.Parse) error {
parser := query.NewParser()
stmts, err := parser.ParseString(msg.Query)
if err != nil {
return err
}
if 1 < len(stmts) {
return query.NewErrMultiplePreparedStatementNotSupported(msg.Query)
}
stmt := &PreparedStatement{
Parse: msg,
ParsedStatement: stmts[0],
}
stmtMap[msg.Name] = stmt
return nil
}
// RemovePreparedStatement removes a prepared statement.
func (stmtMap PreparedStatementMap) RemovePreparedStatement(name string) error {
_, oK := stmtMap[name]
if !oK {
return query.NewErrPreparedStatementNotExist(name)
}
delete(stmtMap, name)
return nil
}
// PreparedPortal represents a prepared query statement.
type PreparedPortal = message.Query
// PreparedPortalMap represents a prepared query statement map.
type PreparedPortalMap map[string]PreparedPortal
// NewPreparedPortalMap returns a new prepared query statement map.
func NewPreparedPortalMap() PreparedPortalMap {
return make(PreparedPortalMap)
}
// PreparedPortal returns a prepared query statement.
func (portalMap PreparedPortalMap) PreparedPortal(name string) (*PreparedPortal, error) {
q, oK := portalMap[name]
if !oK {
return nil, query.NewErrPreparedPortalNotExist(name)
}
return &q, nil
}
// SetPreparedPortal sets a prepared query statement.
func (portalMap PreparedPortalMap) SetPreparedPortal(name string, query *PreparedPortal) error {
portalMap[name] = *query
return nil
}
// RemovePreparedPortal removes a prepared query statement.
func (portalMap PreparedPortalMap) RemovePreparedPortal(name string) error {
_, oK := portalMap[name]
if !oK {
return query.NewErrPreparedPortalNotExist(name)
}
delete(portalMap, name)
return nil
}