Skip to content
This repository has been archived by the owner on Apr 4, 2021. It is now read-only.

Commit

Permalink
pqの代わりにpgxを使う
Browse files Browse the repository at this point in the history
  • Loading branch information
syou6162 committed Apr 22, 2020
1 parent cc320e1 commit 633d293
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 20 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ go 1.12
require (
github.com/PuerkitoBio/goquery v1.5.1
github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195 // indirect
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/fatih/set v0.2.1 // indirect
github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/gorilla/websocket v1.4.0 // indirect
github.com/ikawaha/kagome v1.11.2
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgx v3.6.2+incompatible
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43 // indirect
github.com/jdkato/prose v0.0.0-20181022194337-a179b97cfa6f
github.com/jmoiron/sqlx v1.2.0
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lib/pq v1.3.0
github.com/lib/pq v1.3.0 // indirect
github.com/mackerelio/mackerel-client-go v0.9.1
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
Expand All @@ -27,6 +31,7 @@ require (
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/pkg/errors v0.9.1
github.com/shogo82148/go-shuffle v0.0.0-20180218125048-27e6095f230d // indirect
github.com/shopspring/decimal v0.0.0-20200419222939-1884f454f8ea // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/syou6162/GoOse v0.0.0-20190108170554-09969ebeb09f
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5z
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195 h1:c4mLfegoDw6OhSJXTd2jUEQgZUQuJWtocudb97Qn9EM=
github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195/go.mod h1:SLqhdZcd+dF3TEVL2RMoob5bBP5R1P1qkox+HtCBgGI=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand All @@ -19,12 +21,18 @@ github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/ikawaha/kagome v1.11.2 h1:eCWpLqv5Euqa5JcwkaobUSy6uGM8rwwMw5Su3eRepBI=
github.com/ikawaha/kagome v1.11.2/go.mod h1:lHwhkGuuWqKWTxeQMppD0EmQAfKbc39QKx9qoWqgo+A=
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 h1:vr3AYkKovP8uR8AvSGGUK1IDqRa5lAAvEkZG1LKaCRc=
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ=
github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o=
github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43 h1:jTkyeF7NZ5oIr0ESmcrpiDgAfoidCBF4F5kJhjtaRwE=
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
github.com/jdkato/prose v0.0.0-20181022194337-a179b97cfa6f h1:AQ+AwWeEFf6NsjaMzhuVKLfxZH1+i7aoHuYXObQAzDo=
Expand Down Expand Up @@ -69,6 +77,8 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shogo82148/go-shuffle v0.0.0-20180218125048-27e6095f230d h1:rUbV6LJa5RXK3jT/4jnJUz3UkrXzW6cqB+n9Fkbv9jY=
github.com/shogo82148/go-shuffle v0.0.0-20180218125048-27e6095f230d/go.mod h1:2htx6lmL0NGLHlO8ZCf+lQBGBHIbEujyywxJArf+2Yc=
github.com/shopspring/decimal v0.0.0-20200419222939-1884f454f8ea h1:jaXWVFZ98/ihXniiDzqNXQgMSgklX4kjfDWZTE3ZtdU=
github.com/shopspring/decimal v0.0.0-20200419222939-1884f454f8ea/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
Expand Down
12 changes: 6 additions & 6 deletions lib/repository/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io"
"time"

"github.com/lib/pq"
"github.com/jackc/pgx"
"github.com/syou6162/go-active-learning/lib/feature"
"github.com/syou6162/go-active-learning/lib/model"
"github.com/syou6162/go-active-learning/lib/util/file"
Expand Down Expand Up @@ -89,7 +89,7 @@ func (r *repository) UpdateFeatureVector(e *model.Example) error {
if _, err = r.db.Exec(`DELETE FROM feature WHERE example_id = $1;`, id); err != nil {
return err
}
_, err = r.db.Exec(`INSERT INTO feature (example_id, feature) VALUES ($1, unnest(cast($2 AS TEXT[])));`, id, pq.Array(e.Fv))
_, err = r.db.Exec(`INSERT INTO feature (example_id, feature) VALUES ($1, unnest(cast($2 AS TEXT[])));`, id, pgx.Array(e.Fv))
return err
}

Expand Down Expand Up @@ -214,15 +214,15 @@ func (r *repository) FindExampleById(id int) (*model.Example, error) {
func (r *repository) SearchExamplesByUlrs(urls []string) (model.Examples, error) {
// ref: https://godoc.org/github.com/lib/pq#Array
query := `SELECT * FROM example WHERE url = ANY($1);`
return r.searchExamples(query, pq.Array(urls))
return r.searchExamples(query, pgx.Array(urls))
}

func (r *repository) SearchExamplesByIds(ids []int) (model.Examples, error) {
if len(ids) == 0 {
return model.Examples{}, nil
}
query := fmt.Sprintf(`%s FROM example WHERE id = ANY($1);`, buildSelectQuery(true))
return r.searchExamples(query, pq.Array(ids))
return r.searchExamples(query, pgx.Array(ids))
}

func (r *repository) SearchExamplesByKeywords(keywords []string, aggregator string, limit int) (model.Examples, error) {
Expand All @@ -234,7 +234,7 @@ func (r *repository) SearchExamplesByKeywords(keywords []string, aggregator stri
regexList = append(regexList, fmt.Sprintf(`.*%s.*`, w))
}
query := fmt.Sprintf(`%s FROM example WHERE title ~* %s($1) AND label != -1 ORDER BY (label, score) DESC LIMIT $2;`, buildSelectQuery(true), aggregator)
return r.searchExamples(query, pq.Array(regexList), limit)
return r.searchExamples(query, pgx.Array(regexList), limit)
}

func (r *repository) countExamplesByLabel(label model.LabelType) (int, error) {
Expand Down Expand Up @@ -296,7 +296,7 @@ func (r *repository) SearchFeatureVector(examples model.Examples) (map[int]featu

query := `SELECT example_id, feature FROM feature WHERE example_id = ANY($1);`
pairs := make([]Pair, 0)
err = r.db.Select(&pairs, query, pq.Array(ids))
err = r.db.Select(&pairs, query, pgx.Array(ids))
if err != nil {
return fvById, err
}
Expand Down
6 changes: 3 additions & 3 deletions lib/repository/hatena_bookmark.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package repository

import (
"github.com/lib/pq"
"github.com/jackc/pgx"
"github.com/syou6162/go-active-learning/lib/model"
)

Expand Down Expand Up @@ -59,7 +59,7 @@ func (r *repository) SearchHatenaBookmarks(examples model.Examples, limitForEach
}

query := `SELECT * FROM hatena_bookmark WHERE example_id = ANY($1);`
err := r.db.Select(&hatenaBookmarks, query, pq.Array(exampleIds))
err := r.db.Select(&hatenaBookmarks, query, pgx.Array(exampleIds))
if err != nil {
return hatenaBookmarks, err
}
Expand All @@ -75,7 +75,7 @@ func (r *repository) SearchHatenaBookmarks(examples model.Examples, limitForEach

bookmarks := make([]*model.Bookmark, 0)
query = `SELECT * FROM bookmark WHERE hatena_bookmark_id = ANY($1) ORDER BY timestamp LIMIT $2;`
err = r.db.Select(&bookmarks, query, pq.Array(hatenaBookmarkIds), limitForEachExample)
err = r.db.Select(&bookmarks, query, pgx.Array(hatenaBookmarkIds), limitForEachExample)
if err != nil {
return hatenaBookmarks, err
}
Expand Down
4 changes: 2 additions & 2 deletions lib/repository/recommendation.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package repository

import (
"github.com/lib/pq"
"github.com/jackc/pgx"
"github.com/syou6162/go-active-learning/lib/model"
)

func (r *repository) UpdateRecommendation(rec model.Recommendation) error {
if _, err := r.db.Exec(`DELETE FROM recommendation WHERE list_type = $1;`, rec.RecommendationListType); err != nil {
return err
}
if _, err := r.db.Exec(`INSERT INTO recommendation (list_type, example_id) VALUES ($1, unnest(cast($2 AS INT[])));`, rec.RecommendationListType, pq.Array(rec.ExampleIds)); err != nil {
if _, err := r.db.Exec(`INSERT INTO recommendation (list_type, example_id) VALUES ($1, unnest(cast($2 AS INT[])));`, rec.RecommendationListType, pgx.Array(rec.ExampleIds)); err != nil {
return err
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions lib/repository/related_example.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package repository

import (
"github.com/lib/pq"
"github.com/jackc/pgx"
"github.com/syou6162/go-active-learning/lib/model"
)

func (r *repository) UpdateRelatedExamples(related model.RelatedExamples) error {
if _, err := r.db.Exec(`DELETE FROM related_example WHERE example_id = $1;`, related.ExampleId); err != nil {
return err
}
if _, err := r.db.Exec(`INSERT INTO related_example (example_id, related_example_id) VALUES ($1, unnest(cast($2 AS INT[])));`, related.ExampleId, pq.Array(related.RelatedExampleIds)); err != nil {
if _, err := r.db.Exec(`INSERT INTO related_example (example_id, related_example_id) VALUES ($1, unnest(cast($2 AS INT[])));`, related.ExampleId, pgx.Array(related.RelatedExampleIds)); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion lib/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"bufio"

_ "github.com/lib/pq"
_ "github.com/jackc/pgx"
"github.com/syou6162/go-active-learning/lib/classifier"
"github.com/syou6162/go-active-learning/lib/feature"
"github.com/syou6162/go-active-learning/lib/model"
Expand Down
4 changes: 2 additions & 2 deletions lib/repository/top_accessed_example.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package repository

import (
"github.com/lib/pq"
"github.com/jackc/pgx"
)

func (r *repository) UpdateTopAccessedExampleIds(exampleIds []int) error {
if _, err := r.db.Exec(`DELETE FROM top_accessed_example;`); err != nil {
return err
}
if _, err := r.db.Exec(`INSERT INTO top_accessed_example (example_id) VALUES (unnest(cast($1 AS INT[])));`, pq.Array(exampleIds)); err != nil {
if _, err := r.db.Exec(`INSERT INTO top_accessed_example (example_id) VALUES (unnest(cast($1 AS INT[])));`, pgx.Array(exampleIds)); err != nil {
return err
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions lib/repository/tweet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package repository
import (
"time"

"github.com/lib/pq"
"github.com/jackc/pgx"
"github.com/syou6162/go-active-learning/lib/model"
)

Expand Down Expand Up @@ -59,7 +59,7 @@ func (r *repository) SearchReferringTweetsList(examples model.Examples, limitFor

exampleIdsWithTweetsCount := make([]exampleIdWithTweetsCount, 0)
tweetsCountByExampleQuery := `SELECT example_id, COUNT(*) AS tweets_count FROM tweet WHERE example_id = ANY($1) GROUP BY example_id ORDER BY tweets_count DESC;`
err := r.db.Select(&exampleIdsWithTweetsCount, tweetsCountByExampleQuery, pq.Array(exampleIds))
err := r.db.Select(&exampleIdsWithTweetsCount, tweetsCountByExampleQuery, pgx.Array(exampleIds))
if err != nil {
return referringTweetsByExampleId, err
}
Expand All @@ -81,7 +81,7 @@ func (r *repository) SearchReferringTweetsList(examples model.Examples, limitFor

tweets := make([]*model.Tweet, 0)
query := `SELECT * FROM tweet WHERE example_id = ANY($1) AND label != -1 AND score > -1.0 AND (lang = 'en' OR lang = 'ja') ORDER BY favorite_count DESC LIMIT $2;`
err = r.db.Select(&tweets, query, pq.Array(exampleIds), limitForEachExample)
err = r.db.Select(&tweets, query, pgx.Array(exampleIds), limitForEachExample)
if err != nil {
return referringTweetsByExampleId, err
}
Expand Down

0 comments on commit 633d293

Please sign in to comment.