Code

proto: Added EscapeString().
authorSebastian Harl <sh@tokkee.org>
Thu, 20 Nov 2014 21:05:24 +0000 (22:05 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 20 Nov 2014 21:05:24 +0000 (22:05 +0100)
This function may be used to escape and quote a string suitable for use in a
query.

proto/proto.go

index ee47fcfcf0442d78e4a89f076491bc629dc427da..5e93e472adbdb53c50c9bb0b10061dfeabec2419 100644 (file)
@@ -33,6 +33,7 @@ import (
        "encoding/json"
        "fmt"
        "io"
        "encoding/json"
        "fmt"
        "io"
+       "strings"
 )
 
 // Network byte order.
 )
 
 // Network byte order.
@@ -178,4 +179,12 @@ func Unmarshal(m *Message, v interface{}) error {
        return json.Unmarshal(m.Raw[4:], v)
 }
 
        return json.Unmarshal(m.Raw[4:], v)
 }
 
+// EscapeString returns the quoted and escaped string s suitable for use
+// in a query.
+func EscapeString(s string) string {
+       // Currently, the server only handles double-quotes.
+       // Backslashes do not serve any special purpose.
+       return "'" + strings.Replace(s, "'", "''", -1) + "'"
+}
+
 // vim: set tw=78 sw=4 sw=4 noexpandtab :
 // vim: set tw=78 sw=4 sw=4 noexpandtab :