清理 CREATE TABLE 列布局

发布于 2024-09-14 07:25:48 字数 748 浏览 3 评论 0原文

我有一系列如下所示的 sql 脚本:

CREATE TABLE table_one
(
    column_one            int   not null,
    column_two    varchar(100)               not null,
    column_three_four_five int,
    column_six    decimal(16,4) null,
    PRIMARY KEY ( column_one, column_three_four_five)
);

我想清理布局以使其更易于扫描,如下所示:

CREATE TABLE table_one
(
    column_one             int          not null,
    column_two             varchar(100) not null,
    column_three_four_five int,
    column_six             decimal(16,4)     null,
    PRIMARY KEY
    (
        column_one,
        column_three_four_five
    )
);

确切的布局不如能够创建干净的外观以提高可读性重要。 (请阅读:请不要对格式本身进行攻击)-grin-

编写此脚本的好方法是什么(我正在看着你,Perl 大神......)?

I've got a series of sql scripts that look like this:

CREATE TABLE table_one
(
    column_one            int   not null,
    column_two    varchar(100)               not null,
    column_three_four_five int,
    column_six    decimal(16,4) null,
    PRIMARY KEY ( column_one, column_three_four_five)
);

I'd like to clean up the layout to be easier to scan, something like this:

CREATE TABLE table_one
(
    column_one             int          not null,
    column_two             varchar(100) not null,
    column_three_four_five int,
    column_six             decimal(16,4)     null,
    PRIMARY KEY
    (
        column_one,
        column_three_four_five
    )
);

The exact layout is less important than being able to create a clean look to improve readability. (read: please don't flame the formatting itself) -grin-

What would be a good way to script this (I'm looking at you, Perl gods...)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

前事休说 2024-09-21 07:25:49

嗯,不能说它适用于所有文件,但类似这样的东西就可以完成这项工作...

#!/usr/bin/perl
use strict; 
use warnings;

my $default_padding = 30;
my $my_file = 'data.sql';
my $my_new_file = 'data_new.sql';

open (my $fh, '<', $my_file) or die $!;
my @sql = <$fh>;
close($fh);

open (my $fhsave, '>>', $my_new_file) or die $!;
foreach my $line (@sql) {
    print $fhsave "$line" if ($line !~ /^\s+/);
    $line =~ s/\s+/ /ig;
    print $fhsave sprintf("   %-*s %s\n", $default_padding, $1, $2) if ($line =~ /^\s+(.+?)\s+(.+)/);
}
close ($fhsave);

示例文件 data.sql

CREATE TABLE table_one
(
    column_one            int   not null,
    column_two    varchar(100)               not null,
    column_three_four_five int,
    column_six    decimal(16,4) null
);

输出:

CREATE TABLE table_one
(
   column_one                     int not null, 
   column_two                     varchar(100) not null, 
   column_three_four_five         int, 
   column_six                     decimal(16,4) null 
);

Hmmmmm well can't say it will work for all your files but something like this would do the job ...

#!/usr/bin/perl
use strict; 
use warnings;

my $default_padding = 30;
my $my_file = 'data.sql';
my $my_new_file = 'data_new.sql';

open (my $fh, '<', $my_file) or die $!;
my @sql = <$fh>;
close($fh);

open (my $fhsave, '>>', $my_new_file) or die $!;
foreach my $line (@sql) {
    print $fhsave "$line" if ($line !~ /^\s+/);
    $line =~ s/\s+/ /ig;
    print $fhsave sprintf("   %-*s %s\n", $default_padding, $1, $2) if ($line =~ /^\s+(.+?)\s+(.+)/);
}
close ($fhsave);

sample file data.sql

CREATE TABLE table_one
(
    column_one            int   not null,
    column_two    varchar(100)               not null,
    column_three_four_five int,
    column_six    decimal(16,4) null
);

output:

CREATE TABLE table_one
(
   column_one                     int not null, 
   column_two                     varchar(100) not null, 
   column_three_four_five         int, 
   column_six                     decimal(16,4) null 
);
丑疤怪 2024-09-21 07:25:49

我还没有尝试过其中任何一个,但 CPAN 有 SQL::Beautify 和 < a href="http://search.cpan.org/perldoc?SQL::QueryBuilder::Pretty" rel="nofollow noreferrer">SQL::QueryBuilder::Pretty。

I haven't tried either one, but CPAN has SQL::Beautify and SQL::QueryBuilder::Pretty.

调妓 2024-09-21 07:25:49

http://www.dpriver.com/pp/sqlformat.htm

不是脚本。但我现在不想编写任何代码。

对于任何语言中的此类内容,请查找格式化程序或“漂亮”一词来表示预构建的垃圾。

http://www.dpriver.com/pp/sqlformat.htm

Not a script. But I don't want to code anything right now.

For stuff like this in any language look for formatters or the word 'pretty' for prebuilt junk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文