好吧,我已经陷入了僵局。
在我的开源项目(基于 .NET 的 Oracle 数据库浏览器)中,我实现了一堆重构工具。到目前为止,一切都很好。我真正希望实现的一个功能是一个大型的“全局重新格式化”,它将使代码(脚本、函数、过程、包、视图等)符合标准。 (我一直为缺乏像样的 SQL 重构工具而感到悲伤,并想对此做点什么。)
不幸的是,我发现似乎没有任何一种广泛使用或广泛使用的工具,这让我很懊恼 。甚至是“普遍接受”的 PL-SQL 标准。这对我的实施计划造成了影响。
我的搜索相当详尽。我发现了很多相互矛盾的文档、线索和文章,而且意见也相当多样化。 (逗号的放置,在所有事情中,似乎引起了相当多的争论。)
所以我面临着几个选择:
- 添加一个功能,让用户自定义标准,然后根据该标准重新格式化代码。
—或者—
- 添加一项功能,让用户可以自定义标准并像 StyleCop 一样简单地生成违规列表,而 SQL 保持不变。
在我看来,第一个选项为最终用户节省了大量工作,但存在以可能不需要的方式修改 SQL 的风险。第二种选择存在产生大量警告并且不执行任何操作的风险。 (这通常会很烦人。)
无论哪种情况,我仍然没有可遵循的标准。我需要从你们那里知道的是一些民意调查,但又不是。如果您打算使用这种性质的工具,您希望它警告您或修复 SQL 代码的哪些部分?
再说一遍,由于缺乏有凝聚力的标准,我感到不知所措。鉴于 Oracle 还没有正式发布任何内容,我认为社区可以对此进行权衡。此外,考虑到 SO 的投票方式,投票将有助于确定特定“重构”的受欢迎程度。
PS 该引擎将 SQL 解析为表达式树,因此可以稳健地分析 SQL 并重新格式化它。我们应该做很多事情来纠正 SQL 的格式。但我认为对于这个东西的第一个版本,布局是首要关注的问题。但值得注意的是,该东西已经进行了重构,将关键字转换为大写,将标识符转换为小写。
Okay, I have reached a sort of an impasse.
In my open source project, a .NET-based Oracle database browser, I've implemented a bunch of refactoring tools. So far, so good. The one feature I was really hoping to implement was a big "Global Reformat" that would make the code (scripts, functions, procedures, packages, views, etc.) standards compliant. (I've always been saddened by the lack of decent SQL refactoring tools, and wanted to do something about it.)
Unfortunatey, I am discovering, much to my chagrin, that there doesn't seem to be any one widely-used or even "generally accepted" standard for PL-SQL. That kind of puts a crimp on my implementation plans.
My search has been fairly exhaustive. I've found lots of conflicting documents, threads and articles and the opinions are fairly diverse. (Comma placement, of all things, seems to generate quite a bit of debate.)
So I'm faced with a couple of options:
- Add a feature that lets the user customize the standard and then reformat the code according to that standard.
—OR—
- Add a feature that lets the user customize the standard and simply generate a violations list like StyleCop does, leaving the SQL untouched.
In my mind, the first option saves the end-users a lot of work, but runs the risk of modifying SQL in potentially unwanted ways. The second option runs the risk of generating lots of warnings and doing no work whatsoever. (It'd just be generally annoying.)
In either scenario, I still have no standard to go by. What I'd need to know from you guys is kind of poll-ish, but kind of not. If you were going to use a tool of this nature, what parts of your SQL code would you want it to warn you about or fix?
Again, I'm just at a loss due to a lack of a cohesive standard. And given that there isn't anything out there that's officially published by Oracle, I think this is something the community could weigh in on. Also, given the way that voting works on SO, the votes would help to establish the popularity of a given "refactoring."
P.S. The engine parses SQL into an expression tree so it can robustly analyze the SQL and reformat it. There should be quite a bit that we can do to correct the format of the SQL. But I am thinking that for the first release of the thing, layout is the primary concern. Though it is worth noting that the thing already has refactorings for converting keywords to upper case, and identifiers to lower case.
发布评论
评论(4)
PL/SQL 是 Ada 的衍生品,但是 Ada 的风格指南几乎和大多数“老派”DB 人员喜欢的风格指南一样令人反感。 (你必须认为他们的大写锁定卡得很厉害)
坚持使用你已经从 .Net 中知道的内容,这意味着合理标识符,而不将一半数据库加密/压缩为 30 个字符。< br>
您可以使用字典并拆分驼峰式或下划线标识符部分,并检查它们是否是真实的单词。有点像 FxCop 所做的。
不过可能有点烦人。因为一般的 Oracle 数据库都有最糟糕和不一致的命名准则,这些准则甚至在 30 年前就已经过时了。
因此,我认为您不会达到在项目(或用户的)中随处获得干净标识符的目标,
因为 PL/SQL 不区分大小写,并且列优先于同名的本地变量,所以您必须使更多的权衡。你可以参考其他Pascal衍生品的风格指南(Ada基于Modula,Modula又基于Pascal),比如Delphi 感觉更接近 PL/SQL(我混合使用 .Net 和 Delphi)。
特别是参数的“aPrefix”可以成为救星,因为这样就不会与列名发生冲突:
如果没有前缀,oracle 将始终选择列“Name”而不是参数“Name”。 (这很烦人,因为列可以用别名限定...)
我将 PL/SQL Devloper 配置为将所有关键字设为小写,但是,我将普通 SQL 中使用的关键字设为大写(SELECT、地点等)
结果,SQL 被排除在代码之外,但并非我的所有代码都必须受到全大写关键字的残酷对待。 (无论如何,它们都会突出显示,那么全上迷恋是怎么回事?;-))
当您的工具能够识别普通 SQL 并提供一些视觉线索时,甚至 SQL 关键字也不需要有不同的大小写。
顺便说一句,我很想看看。您可以发布一个网址,还是仍然“隐藏”?
干杯,
罗伯特
PL/SQL is an Ada derivative, however Ada's style guide is almost as gut-twisting disgusting as the one most "old-school" DB-people prefer. (The one where you have to think their caps lock got stuck pretty bad)
Stick with what you already know from .Net, which means sensible identifiers, without encrypting/compressing half the database into 30 chars.
You could use a dictionary and split camel-cased or underscored identifier parts and check if they are real words. Kinda like what FxCop does.
Could be bit annoying, though. Since the average Oracle database has the most atrocious and inconsistent naming guidelines that were obsolete even 30 years ago.
So, I don't think you'll reach the goal of getting clean identifiers everywhere in your projects (or your user's)
Since PL/SQL is case insensitive and columns are preferred over equally named local vars, you'll have to make even more tradeoffs. You can take parts of the style guide of other pascal derivatives (Ada is based on Modula, which is based on Pascal), like Delphi which feel a bit closer to home for PL/SQL (I use a mixture of .Net & Delphi).
Especially the "aPrefix" for parameters can be a life saver, because you won't collide with column names that way:
Without the prefix, oracle would always pick the column "Name" and not the parameter "Name". (Which is pretty annoying, since columns can be qualified with an alias...)
I configured my PL/SQL Devloper to make all keywords in lowercase, however, I made the ones that are used in plain SQL to be uppercased (SELECT,WHERE, etc)
As a result, SQLs are sticking out of the code, but not all my code has to be brutalized by all-upper keywords. (They are highlighted anyways, so what's with the all-upper fetish? ;-) )
When your tool is capable of identifying plain SQLs and give some visual clue, then even the SQL keywords wouldn't need to have a different casing.
btw, I'd love to take a look at it. Can you post an url, or is still "under cover"?
Cheers,
Robert
TOAD 有一个“漂亮的打印机”,并使用大量选项让用户对所做的事情有一定的发言权。 (但它变得如此复杂,以至于我仍然无法获得我想要的结果。)
对我来说,有些选项看起来很可怕,但似乎有些人喜欢它们。合理的默认值在 80% 的情况下应该是可以的,但由于这是宗教战争的问题,我确信您可以花费完全不合理的时间来获得相当小的结果。我建议编写一些代码来处理您提到的 10 年前的 sp,并包含诸如
TOAD has a "pretty printer" and uses a ton of options to give the user some say in what is done. (But it has gotten so complicated that I still can't manage to get the results I would like.)
For me, some options look downward horrible, but it seems that some people like them. A sensible default should be okay for 80% of the time, but as this is an issue of religious wars, I'm sure that you can spend a totally unreasonable amount of time for pretty small results. I'd suggest to code some things to handle the 10-year-old sp you mentioned, and to include something like a
<pre>
tag that the pretty printer leaves alone.我喜欢汤姆·凯特(在他的书中)的“标准”。这意味着一切都是小写的。对眼睛最容易。
I like the "standard" Of Tom Kyte (in his books). That means everything in lowercase. Most easy for the eyes.
如果您所做的只是重新排列空格以使代码看起来始终干净,那么就没有更改 SQL 结果的风险。
然而,作为过去 8 年的 Oracle/PLSQL 开发人员,我几乎可以保证,无论您给它多少选项,我都不会使用您的工具。原则上,代码的批量重新格式化听起来很棒,但随后您就完全破坏了重新格式化之前和之后的修订之间的版本控制差异。
If all you're doing is rearranging whitespace to make the code look consistently clean, then there's no risk of changing SQL results.
However, as an Oracle/PLSQL developer for the past 8 years, I can almost guarantee I wouldn't use your tool no matter how many options you give it. Bulk reformatting of code sounds great in principle, but then you've totally destroyed its diffability in version control between revisions prior to and after the reformat.