-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support new query types * Fix
- Loading branch information
Showing
7 changed files
with
236 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package proc | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
) | ||
|
||
type CatMessage struct { | ||
ProtoMessage | ||
Decrypt bool | ||
Name string | ||
} | ||
|
||
func NewCatMessage(name string, decrypt bool) *CatMessage { | ||
return &CatMessage{ | ||
Name: name, | ||
Decrypt: decrypt, | ||
} | ||
} | ||
|
||
func (c *CatMessage) Encode() []byte { | ||
bt := []byte{ | ||
byte(MessageTypeCat), | ||
0, | ||
0, | ||
0, | ||
} | ||
|
||
if c.Decrypt { | ||
bt[1] = byte(DecryptMessage) | ||
} else { | ||
bt[1] = byte(NoDecryptMessage) | ||
} | ||
|
||
bt = append(bt, []byte(c.Name)...) | ||
bt = append(bt, 0) | ||
ln := len(bt) + 8 | ||
|
||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, uint64(ln)) | ||
return append(bs, bt...) | ||
} | ||
|
||
func GetCatName(b []byte) string { | ||
buff := bytes.NewBufferString("") | ||
|
||
for i := 0; i < len(b); i++ { | ||
if b[i] == 0 { | ||
break | ||
} | ||
buff.WriteByte(b[i]) | ||
} | ||
|
||
return buff.String() | ||
} |
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,25 @@ | ||
package proc | ||
|
||
import "encoding/binary" | ||
|
||
type CommandCompleteMessage struct { | ||
} | ||
|
||
func NewCommandCompleteMessage() *CommandCompleteMessage { | ||
return &CommandCompleteMessage{} | ||
} | ||
|
||
func (cc *CommandCompleteMessage) Encode() []byte { | ||
bt := []byte{ | ||
byte(MessageTypeCommandComplete), | ||
0, | ||
0, | ||
0, | ||
} | ||
|
||
ln := len(bt) + 8 | ||
|
||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, uint64(ln)) | ||
return append(bs, bt...) | ||
} |
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,31 @@ | ||
package proc | ||
|
||
type ProtoMessage interface { | ||
Decode([]byte) | ||
Encode() []byte | ||
} | ||
|
||
type MessageType byte | ||
|
||
type RequestEncryption byte | ||
|
||
const ( | ||
MessageTypeCat = MessageType(42) | ||
MessageTypePut = MessageType(43) | ||
MessageTypeCommandComplete = MessageType(44) | ||
MessageTypeReadyForQuery = MessageType(45) | ||
|
||
DecryptMessage = RequestEncryption(1) | ||
NoDecryptMessage = RequestEncryption(0) | ||
|
||
EncryptMessage = RequestEncryption(1) | ||
NoEncryptMessage = RequestEncryption(0) | ||
) | ||
|
||
func (m MessageType) String() string { | ||
switch m { | ||
case MessageTypeCat: | ||
return "CAT" | ||
} | ||
return "UNKNOWN" | ||
} |
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,39 @@ | ||
package proc | ||
|
||
import "encoding/binary" | ||
|
||
type PutMessage struct { | ||
ProtoMessage | ||
Encrypt bool | ||
Name string | ||
} | ||
|
||
func NewPutMessage(name string, encrypt bool) *PutMessage { | ||
return &PutMessage{ | ||
Name: name, | ||
Encrypt: encrypt, | ||
} | ||
} | ||
|
||
func (c *PutMessage) Encode() []byte { | ||
bt := []byte{ | ||
byte(MessageTypeCat), | ||
0, | ||
0, | ||
0, | ||
} | ||
|
||
if c.Encrypt { | ||
bt[1] = byte(EncryptMessage) | ||
} else { | ||
bt[1] = byte(NoEncryptMessage) | ||
} | ||
|
||
bt = append(bt, []byte(c.Name)...) | ||
bt = append(bt, 0) | ||
ln := len(bt) + 8 | ||
|
||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, uint64(ln)) | ||
return append(bs, bt...) | ||
} |
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,25 @@ | ||
package proc | ||
|
||
import "encoding/binary" | ||
|
||
type ReadyForQueryMessage struct { | ||
} | ||
|
||
func NewReadyForQueryMessage() *ReadyForQueryMessage { | ||
return &ReadyForQueryMessage{} | ||
} | ||
|
||
func (cc *ReadyForQueryMessage) Encode() []byte { | ||
bt := []byte{ | ||
byte(MessageTypeReadyForQuery), | ||
0, | ||
0, | ||
0, | ||
} | ||
|
||
ln := len(bt) + 8 | ||
|
||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, uint64(ln)) | ||
return append(bs, bt...) | ||
} |