Question: How to implement 'Schwartz Style'?
Closed this issue · 2 comments
Firstly, thanks for your work on this!
Secondly, apologies if a GH issue is not the right place to ask this question.
Rightly or wrongly, I tend to write SQL in a rather simple form, very similar to that of Baron Schwartz, and for a long time I've been hoping to be able to use Emacs to do so. Here's a short made-up example to illustrate:
with
current_students as (
select
name as student_name,
id as student_id
from
student_table
where
current_student is true and
start_date > '2015-01-01 00:07:00' and
start_date <= '2016-01-01' and
enrolled is true
),
classes as (
select
name as class_name,
id as class_id,
student_id
from
class_table
)
select
student_id,
student_name,
class_name,
class_id
from current_students as s
left join classes as c on s.student_id = c.student_id
Given the power and granularity of your package, I'm sure that such a style is possible. However, after spending more time than I'd like to admit reading the documentation and messing around, I haven't got close. Do you have any tips on how to set the sqlind-default-indentation-offsets-alist
variable to achieve this, or something like it?
Hi there! This is the right place to ask the question, as this is the only place I actually monitor :-)
I have added a new indentation helper function, so make sure you pull the new changes in, with those, the snipped below should indent the code example as you provided it. You can use it as a starting point to write a more complete indentation rules that conform to the coding standard you linked.
I know that the indentation rules are complex and I'm not quite sure how to make them simpler. There are lots of very different coding standards for SQL and sql-indent
simply reflects that complexity.
However, I would like to create a collection of indentation rules, so that people can hopefully just choose one that is closest to what they want and modify that one. You can see an example of that in the sql-indent-left.el
file. If you create a complete indentation style for the "Baron Schwartz" style, I would be happy to add it to the collection if you submit a pull request.
I would also appreciate some feedback on documentation. Since I wrote sql-indent
, everything is very obvious to me and it is hard to tell what level of documentation is adequate.
Thanks,
Alex.
(defvar p43-sql-indentation-offsets-alist
`((select-clause 0)
(insert-clause 0)
(delete-clause 0)
(update-clause 0)
(in-select-clause + sqlind-lineup-close-paren-to-open-indentation)
(select-table-continuation + sqlind-lineup-close-paren-to-open-indentation)
,@sqlind-default-indentation-offsets-alist))
(add-hook 'sqlind-minor-mode-hook
(lambda ()
(setq sqlind-indentation-offsets-alist
p43-sql-indentation-offsets-alist)))
I have added a new indentation helper function, so make sure you pull the new changes in, with those, the snipped below should indent the code example as you provided it. You can use it as a starting point to write a more complete indentation rules that conform to the coding standard you linked.
This worked a treat, thanks so much! The only thing that didn't quite seem to work was the new helper function (sqlind-lineup-close-paren-to-open-indentation
); it left the closing paren one level of indentation further to the right than I expected. I managed to make this (seem to) work by simply repeating the back-to-indentation
command an additional time, without really understanding much about elisp, or the function itself. I've included this as a PR in case it's useful (#45), but I'm not sure if it's the right thing to do.
However, I would like to create a collection of indentation rules, so that people can hopefully just choose one that is closest to what they want and modify that one. You can see an example of that in the sql-indent-left.el file. If you create a complete indentation style for the "Baron Schwartz" style, I
would be happy to add it to the collection if you submit a pull request.
This is an excellent idea. I'll road test this for a couple of weeks and then hopefully submit one.
I would also appreciate some feedback on documentation. Since I wrote sql-indent, everything is very obvious to me and it is hard to tell what level of documentation is adequate.
I think the main thing is probably just more examples, especially of what the resulting SQL would look like. The documentation is actually really good, it's just a complicated subject!
Thanks again!