forked from panoplyio/pgsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request panoplyio#41 from panoplyio/revert-39-revert-34-ex…
…tended-query-messages Revert "Revert "Extended query message handling""
- Loading branch information
Showing
19 changed files
with
1,104 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package protocol | ||
|
||
// ParseComplete is sent when backend parsed a prepared statement successfully | ||
var ParseComplete = []byte{'1', 0, 0, 0, 4} | ||
|
||
// BindComplete is sent when backend prepared a portal and finished planning the query | ||
var BindComplete = []byte{'2', 0, 0, 0, 4} | ||
|
||
// CreatesTransaction tells weather this is a frontend message that should start/continue a transaction | ||
func (m *Message) CreatesTransaction() bool { | ||
return m.Type() == Parse || m.Type() == Bind | ||
} | ||
|
||
// EndsTransaction tells weather this is a frontend message that should end the current transaction | ||
func (m *Message) EndsTransaction() bool { | ||
return m.Type() == Query || m.Type() == Sync | ||
} | ||
|
||
// transaction represents a sequence of frontend and backend messages | ||
// that apply only on commit. the purpose of transaction is to support | ||
// extended query flow. | ||
type transaction struct { | ||
transport *Transport | ||
in []Message // TODO: asses if we need it after implementation of prepared statements and portals is done | ||
out []Message // TODO: add size limit | ||
} | ||
|
||
// Read uses Transport to read the next message into the transaction's incoming messages buffer | ||
func (t *transaction) Read() (msg Message, err error) { | ||
if msg, err = t.transport.Read(); err == nil { | ||
t.in = append(t.in, msg) | ||
} | ||
return | ||
} | ||
|
||
// Write writes the provided message into the transaction's outgoing messages buffer | ||
func (t *transaction) Write(msg Message) error { | ||
t.out = append(t.out, msg) | ||
return nil | ||
} | ||
|
||
func (t *transaction) flush() (err error) { | ||
for len(t.out) > 0 { | ||
err = t.transport.write(t.out[0]) | ||
if err != nil { | ||
break | ||
} | ||
t.out = t.out[1:] | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package protocol | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestTransaction_Read(t *testing.T) { | ||
buf := bytes.Buffer{} | ||
comm := bufio.NewReadWriter(bufio.NewReader(&buf), bufio.NewWriter(&buf)) | ||
p := &Transport{W: comm, R: comm, initialized: true} | ||
trans := &transaction{transport: p, in: []Message{}, out: []Message{}} | ||
|
||
_, err := comm.Write([]byte{'P', 0, 0, 0, 4}) | ||
require.NoError(t, err) | ||
|
||
err = comm.Flush() | ||
require.NoError(t, err) | ||
|
||
m, err := trans.Read() | ||
require.NoError(t, err) | ||
require.NotNil(t, m, | ||
"expected to receive message from transaction. got nil") | ||
|
||
require.Equalf(t, 1, len(trans.in), | ||
"expected exactly 1 message in transaction incoming buffer. actual: %d", len(trans.in)) | ||
|
||
require.Equalf(t, byte('P'), trans.in[0].Type(), | ||
"expected type of the only message in transaction incoming buffer to be 'P'. actual: %c", trans.in[0].Type()) | ||
|
||
require.Equalf(t, 0, len(trans.out), | ||
"expected no message to exist in transaction's outgoing message buffer. actual buffer length: %d", len(trans.out)) | ||
|
||
err = trans.Write(CommandComplete("")) | ||
require.NoError(t, err) | ||
|
||
require.Equalf(t, 1, len(trans.out), | ||
"expected exactly one message in transaction's outgoind message buffer. actual messages count: %d", len(trans.out)) | ||
} |
Oops, something went wrong.