Added GetByAliases function in db and repository #539
Conversation
Codecov Report
@@ Coverage Diff @@
## master #539 +/- ##
=========================================
+ Coverage 78.39% 78.9% +0.51%
=========================================
Files 35 35
Lines 847 896 +49
=========================================
+ Hits 664 707 +43
- Misses 153 156 +3
- Partials 30 33 +3
Continue to review full report at Codecov.
|
Thank you for your PR!. Please address my comments :) |
} | ||
defer stmt.Close() | ||
|
||
rows, err := stmt.Query(aliasesInterface...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't find the for
loop and QueryRow
in the PR. Please checkout this example mentioned in one of the previous articles:
db, err := sql.Open("postgres", "postgres://postgres:postgres@localhost/postgres?sslmode=disable")
if err != nil {
log.Fatal(err)
}
defer db.Close()
stmt, err := db.Prepare("select count(*) from pg_stat_activity where datname = $1")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var num int
start := time.Now()
for i := 0; i < 1000; i++ {
err = stmt.QueryRow("postgres").Scan(&num)
}
elapsed := time.Since(start)
fmt.Printf("got: %d in %s\n", num, elapsed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
according to official document for function QueryRow
:
// If the query selects no rows, the *Row's Scan will return ErrNoRows.
// Otherwise, the *Row's Scan scans the first selected row and discards
// the rest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems that the document is only selecting a single row for testing purpose.
Here we want to select a series of rows, I believe we have to use Query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please read this whole thread: golang/go#5171
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@byliuyang In that thread, they talk about bulk operations a way to insert multiple rows at once
.
Our case is a standard SQL query, which requires to call Query
and pass the list of args.
Please read this http://go-database-sql.org/prepared.html
Go creates prepared statements for you under the covers. A simple db.Query(sql, param1, param2), for example, works by preparing the sql, then executing it with the parameters and finally closing the statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@byliuyang Just added a TODO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@byliuyang sorry I still did not get why you think we should use QueryRow instead of Query, if we use QueryRow then it will cost us more connections with DB, we could definitely do performance test, but intuitively I feel it's slower
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dawntomm All queryRows in a single prepare statement share one connection.
@alldroll Tom addressed all your comments. I'll merge now to unblock him so that he can work on the next component. |
New Behavior
Description
Added GetByAliases function in db and repository.
User should be able to search the public and the private short links they created. In order to support that, we add RESTful API to search for all urls created by a given user. The first step is to add GetByAliases function to fetch URLs from database.