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

Override view name in querier #167

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft

Override view name in querier #167

wants to merge 9 commits into from

Conversation

AlekSi
Copy link
Member

@AlekSi AlekSi commented Jul 31, 2018

By @bitxel.
Closes #154.

TODO

  • More tests.
  • Support for INSERT/UPDATE/DELETE.

@AlekSi AlekSi added the feature label Jul 31, 2018
@AlekSi AlekSi added this to the v1.4.0 milestone Jul 31, 2018
@AlekSi AlekSi self-assigned this Jul 31, 2018
@AlekSi AlekSi changed the base branch from v1-stable to v1-4 July 31, 2018 07:19
querier_examples_test.go Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Jul 31, 2018

Codecov Report

Merging #167 into v1-stable will increase coverage by 0.18%.
The diff coverage is 100%.

Impacted file tree graph

@@              Coverage Diff              @@
##           v1-stable     #167      +/-   ##
=============================================
+ Coverage       68.4%   68.58%   +0.18%     
=============================================
  Files             19       19              
  Lines           1557     1566       +9     
=============================================
+ Hits            1065     1074       +9     
  Misses           442      442              
  Partials          50       50
Impacted Files Coverage Δ
querier_selects.go 97.29% <100%> (+0.07%) ⬆️
querier.go 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8019070...27faa85. Read the comment docs.

func (q *Querier) WithView(viewName string) *Querier {
newQ := newQuerier(q.dbtx, q.Dialect, q.Logger)
newQ.viewName = viewName
return newQ
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are losing tag here.

querier.go Outdated
// QualifiedView returns quoted qualified view name.
func (q *Querier) QualifiedView(view View) string {
v := q.QuoteIdentifier(view.Name())
if q.viewName != "" {
v = q.QuoteIdentifier(q.viewName)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh. view parameter is ignored when q.viewName is set.

@@ -38,7 +38,11 @@ func (s *ReformDBSuite) TestInit() {

fis, err := ioutil.ReadDir(dir)
s.Require().NoError(err)
s.Require().Len(fis, 4)
if s.db.Dialect == sqlite3.Dialect {
s.Require().Len(fis, 4) // generate 4 struct files for sqlite3
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use views for SQLite3 too.

querier_selects.go Show resolved Hide resolved
querier_test.go Show resolved Hide resolved
querier_test.go Show resolved Hide resolved
querier_test.go Show resolved Hide resolved
querier_examples_test.go Show resolved Hide resolved
querier_examples_test.go Show resolved Hide resolved
@AlekSi
Copy link
Member Author

AlekSi commented Aug 7, 2018

@bitxel May you please check this branch? It should work for all SELECTs without nasty side-effects.

from = q.qualifiedViewName + " AS " + from
}

return fmt.Sprintf("%s %s FROM %s %s", query, strings.Join(q.QualifiedColumns(view), ", "), from, tail)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we only change table name in FROM clause and alias it to the old name. That should do the trick for all SELECTs. @bitxel, please check it out.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understand, it's a neat way that only change the view name when making SQL,
but it may lose the schema name, so it's caller's responsibility to add and quote the schema name

@AlekSi AlekSi closed this Dec 28, 2018
@AlekSi AlekSi changed the base branch from v1-4 to v1-stable December 28, 2018 07:12
@AlekSi AlekSi reopened this Dec 28, 2018
@AlekSi AlekSi marked this pull request as draft July 16, 2020 18:33
@AlekSi AlekSi modified the milestones: v1.4.0, v1.5.0 Jul 19, 2020
@AlekSi AlekSi changed the base branch from v1-stable to main July 19, 2020 11:52
// QualifiedView returns quoted qualified view name.
// WithQualifiedViewName returns a copy of Querier with set qualified view name.
// Returned Querier is tied to the same DB or TX.
// TODO Support INSERT/UPDATE/DELETE. More test.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional linters] reported by reviewdog 🐶
querier.go:73: Line contains TODO/BUG/FIXME: "TODO Support INSERT/UPDATE/DELETE. More ..." (godox)

@@ -85,6 +85,20 @@ func ExampleQuerier_WithContext() {
_, _ = DB.WithContext(ctx).SelectAllFrom(ProjectTable, "")
}

func ExampleQuerier_WithQualifiedViewName() {
_, err := DB.WithQualifiedViewName("people_0").FindByPrimaryKeyFrom(PersonTable, 1)
if err != reform.ErrNoRows {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional linters] reported by reviewdog 🐶
err113: do not compare errors directly, use errors.Is() instead: "err != reform.ErrNoRows" (goerr113)

s.Equal(`"people"`, s.q.QualifiedView(PersonTable))
s.Equal(`"people"`, s.q.WithQualifiedViewName("ignored").QualifiedView(PersonTable))

case mssql.Dialect, sqlserver.Dialect:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional linters] reported by reviewdog 🐶
SA1019: mssql.Dialect is deprecated: Use sqlserver.Dialect instead. https://github.com/denisenkom/go-mssqldb#deprecated (staticcheck)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Override table name when executing SQL
6 participants