简体   繁体   English

SQL固定长度插入语句格式化程序

[英]SQL fixed-length insert statement formatter

Lets assume I have this long insert statement 让我们假设我有这个长插入语句

insert into table1 (id, name, phone, very_long_col_name, ...) 
            values (1, 'very long name indeed...', '555-555-5555', 1, ...)

As you can see above, it gets hard to tell values from their column since their length is uneven 正如您在上面所看到的,由于它们的长度不均匀,很难从列中分辨出值

I'm looking for something (eg command line util) to format the above (not just SQL format) to this: 我正在寻找一些东西(例如命令行util)来将上面的格式(不仅仅是SQL格式)格式化为:

insert into table1 (id, name                      , phone         , very_long_col_name, ...) 
            values (1 , 'very long name indeed...', '555-555-5555', 1                 , ...)

This way I can see which value goes with which column easily 通过这种方式,我可以轻松查看哪个值与哪个列相关

It can be a plugin to notepad++, a java utility, a plugin to an SQL IDE, what ever does the trick... 它可以是记事本++的插件,一个java实用程序,一个SQL IDE的插件,它的诀窍......

Prepared statements, T-SQL parameters, Hibernate, JPA etc is not an option right now 准备语句,T-SQL参数,Hibernate,JPA等现在不是一个选项

Not suggesting a plugin, but I mostly see this kind of thing formatted this way: 不是建议一个插件,但我大多看到这种方式格式化:

insert into table1 
       (
         id, 
         name,
         phone, 
         very_long_col_name, 
         ...
       ) 
values 
       (
         1, 
         'very long name indeed...', 
         '555-555-5555', 
         1, 
         ...
       )

I find this more readable than scrolling through a very long line. 我发现这比滚动很长的一行更具可读性。

Your better bet is to use SQL prepared statements. 最好的办法是使用SQL预处理语句。 This lets you separate the SQL query syntax from your data, so you'd first prepare the statement: 这使您可以将SQL查询语法与数据分开,因此您首先要准备语句:

$statement = mysqli_prepare("INSERT INTO `blah` (`id`,`phone`,`name`) VALUES(?,?,?)");

Then you bind the data to the statement: 然后数据绑定到语句:

$statement->bind('iss', 1234, "(555) 123-4567", "Kris");

I used PHP as an example, and the 'iss' in the above code says it's binding an Int and 2 strings in that order. 我以PHP为例,上面代码中的'iss'表示它按顺序绑定了一个Int和2个字符串。

Have you considered the following alternative syntax ? 您是否考虑过以下替代语法

INSERT INTO `table` SET
    `id` = 1,
    `name` = 'very long name indeed...',
    `phone` = '555-555-5555',
    `very_long_col_name` = 1,
    `...` = '...'
;

If the place that contains your SQL statement contains the DATA that you want to insert, then you are most probably doing something very, very, very wrong. 如果包含您的SQL语句的位置包含您要插入的DATA,那么您很可能正在做一些非常非常非常错误的事情。

What do you want to achieve? 你想达到什么目的? Do you want to format the query, so that you can dump it in a pretty style for debugging purposes? 是否要格式化查询,以便您可以将其转储为漂亮的样式以进行调试? Well, this is easy, just add strlen(some_string)-some_fixed_number number of whitespace at the appropriate places in your code. 好吧,这很简单,只需在代码中的适当位置添加strlen(some_string)-some_fixed_number数量的空格。 I can not suggest actual code here, because I do not know what language you use or what coding styles you prefer and so on... 我不能在这里建议实际的代码,因为我不知道你使用的语言或你喜欢的编码风格等等......

But even if I wanted to, I do not see any value in this. 但即使我想,我也没有看到任何价值。 You should separate SQL queries and the data that you use in your SQL queries (eg for inserting). 您应该将SQL查询与您在SQL查询中使用的数据分开(例如,用于插入)。

Building SQL query strings dynamically is out of fashion for some very good reasons (quoting, sql injection and so on...). 由于一些非常好的理由(引用,sql注入等等),动态构建SQL查询字符串已经过时了。

EDIT: If you want to format an SQL dump or some INSERT statements that prepare a database, then you can just use CSV formatted data. 编辑:如果要格式化SQL转储或某些准备数据库的INSERT语句,则可以使用CSV格式的数据。 It is easier to read than SQL statements. 它比SQL语句更容易阅读。

Variables? 变量?

insert into
table1 ( id,  name,  phone,  very_long_col_name, ...) 
values (@id, @name, @phone, @long_val, ...)

(obviously you need to declare and set / select these too) (显然你需要declareset / select这些)

In Oracle (since we can select from dual) I like to make these into insert into select from so I can alias the columns and make it easier to read: 在Oracle中(因为我们可以从双重选择)我喜欢将这些insert into select from因此我可以对列进行别名并使其更易于阅读:

insert into table1 
(
 id, 
 name, 
 phone, 
 very_long_col_name,
 ...
) 
select 1 id, 
       'very long name indeed...' name, 
       '555-555-5555' phone, 
       1 very_long_col_name, 
       ...
  from dual;       

A desired the same thing so I built this javascript tool: 期望同样的事情,所以我建立了这个JavaScript工具:

SQL Insert Formatter SQL插入格式化程序

It does precisely what you ask and handles multiple statements. 它完全符合您的要求并处理多个语句。 It is client-side only, so no need to worry about your data being uploaded. 它只是客户端,因此无需担心您的数据被上传。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM