Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

106-fix-driver-error-on-creating-table #107

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,15 @@ func (c *conn) exec(ctx context.Context, query string, args []driver.Value) (dri
return nil, err
}
body, err := c.doRequest(ctx, req)
if body != nil {
body.Close()
if err != nil {
if body != nil {
body.Close()
}

return emptyResult, err
}
return emptyResult, err

return emptyResult, noticeError(body)
}

func (c *conn) doRequest(ctx context.Context, req *http.Request) (io.ReadCloser, error) {
Expand Down
19 changes: 19 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clickhouse

import (
"bytes"
"io"
"net/http"
"strings"
"time"
Expand All @@ -11,6 +12,8 @@ var (
escaper = strings.NewReplacer(`\`, `\\`, `'`, `\'`)
dateFormat = "2006-01-02"
timeFormat = "2006-01-02 15:04:05"

exceptionMarker = "DB::Exception"
)

func escape(s string) string {
Expand Down Expand Up @@ -40,6 +43,22 @@ func readResponse(response *http.Response) (result []byte, err error) {
return
}

func noticeError(body io.ReadCloser) (err error) {
if body == nil {
return nil
}

buf := new(bytes.Buffer)
defer body.Close()
_, err = buf.ReadFrom(body)
msg := buf.String()
if strings.Contains(msg, exceptionMarker) {
return newError(msg)
}

return nil
}

func numOfColumns(data []byte) int {
var cnt int
for _, ch := range data {
Expand Down