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

Add limit and offset to Builder/Assembler with some fixes #284

Merged
merged 6 commits into from
Oct 8, 2018
Merged

Add limit and offset to Builder/Assembler with some fixes #284

merged 6 commits into from
Oct 8, 2018

Conversation

c910335
Copy link
Contributor

@c910335 c910335 commented Oct 6, 2018

I found that the query builder which was introduced in #132 is very powerful when I was building Shale.
But I think it needs some improvement.

  1. Fix typo "include" => "extend" in src/granite/query/builder_methods.cr.
  2. Fix the weird whitespaces in the log.
    Thanks @Thellior for finding this bug.
  3. little refactoring
  4. Introduce limit and offset.
    They are very useful for pagination.

@c910335 c910335 requested a review from a team October 6, 2018 09:33
Copy link
Contributor

@Thellior Thellior left a comment

Choose a reason for hiding this comment

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

Looking good to me :)

Copy link
Member

@robacarp robacarp left a comment

Choose a reason for hiding this comment

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

Thanks @c910335! I appreciate you refactoring and picking up the querybuilder where I left off.

I left one comment for discussion about the sql_builder.

The weird whitespacing is a combination of two features integrating: unified query logger and this query builder. Without the logger, the querybuilder as it was attempted to output formatted SQL, something like this:

  SELECT field, field, field, field
    FROM monitor_results
   WHERE run_start_time = $1 AND host_id = $2
   ORDER BY id DESC

Nevertheless, formatted SQL was more helpful for writing the query builder framework than it will be moving forward, so I'm fine removing it.

def build
@clauses.join " "
end
end
Copy link
Member

Choose a reason for hiding this comment

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

This is a clever shortcut, but I'm not sure it needs to be a class. Why not just implement this in bulid_sql?

def build_sql(&block)
  clauses = [] of String
  yield clauses
  clauses.compact.join ' '
end

sql = build_sql do |s|
s << "SELECT COUNT(*)"
s << "FROM #{table_name}"
s << where if where
Copy link
Member

Choose a reason for hiding this comment

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

because of the logic in build_sql, can these if suffixes be dropped?

@group_by : String?

def where
return @where if @where
Copy link
Member

Choose a reason for hiding this comment

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

This code isn't fresh enough in my mind to remember why I had build_* methods instead, but assuming there isn't some DSL caveat I think this is a fine refactor.

Copy link
Member

@drujensen drujensen left a comment

Choose a reason for hiding this comment

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

looks good. thx!

@drujensen
Copy link
Member

@robacarp are you ok with merging this? I have time to do a patch release today and would like to include these if you approve.

@robacarp
Copy link
Member

robacarp commented Oct 6, 2018

@drujensen sure, if you like. I think there's room for more refactor along the comments I made, but it's functional as is, I think

Copy link
Member

@robacarp robacarp left a comment

Choose a reason for hiding this comment

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

Thanks @c910335

@robacarp robacarp merged commit bbe603f into amberframework:master Oct 8, 2018
This was referenced Oct 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants