将SQL作为字符串存储在多行中的Java代码将如何编写?

huangapple go评论85阅读模式
英文:

What will be the java code to make an SQL stored as String in multi line?

问题

以下是翻译好的部分:

<!-- language: lang-sql -->

CREATE TABLE BOOK (page number(20, 0),
                    author varchar2(255),
                    noOfLines number(100) 
                  )
String s = "CREATE TABLE BOOK (page number(20, 0), author varchar2(255), noOfLines number(100));";
s = s.replaceAll(", ", ",\n");

希望这能帮助你格式化SQL字符串。

英文:

I have an SQL which is stored in a String goes like

String s="CREATE TABLE BOOK (page number(20, 0), author varchar2(255), noOfLines number(100))";

I need to write a code that could format my sql as

<!-- language: lang-sql -->

CREATE TABLE BOOK (page number(20, 0),
                    author varchar2(255),
                    noOfLines number(100) 
                  )

What will be the java program to do it.

I tried

sql.replace(&quot;, &quot;,&quot;, \n&quot;);

but this code produced an output like

<!-- language: lang-sql -->

 CREATE TABLE BOOK (page number(20,
  0),
  author varchar2(255),
  noOfLines number(100) 
 )

As you can see the comma at the number(20, 0) was also considered, but I don't want that.

How to do this?

答案1

得分: 3

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {
    public static void main(String[] args) {
        final String regex = "[)P],?";
        final String string = "CREATE TABLE BOOK (page number(20, 0), author varchar2(255), noOfLines number(100))";
        final String subst = "$0\\\n                  ";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);

        // The substituted value will be contained in the result variable
        final String result = matcher.replaceAll(subst);

        System.out.println(result);
    }
}

我用链接创建了这个代码,这是一个非常有用的页面,你甚至可以导出到其他语言,比如Java、Python、PHP...希望它能帮助你并且我能够达到你期望的输出。


<details>
<summary>英文:</summary>

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class test {
        public static void main(String[] args) {
            final String regex = &quot;[)P],?&quot;;
            final String string = &quot;CREATE TABLE BOOK (page number(20, 0), author varchar2(255), noOfLines number(100))&quot;;
            final String subst = &quot;$0\\\n                  &quot;;
    
            final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
            final Matcher matcher = pattern.matcher(string);
    
    // The substituted value will be contained in the result variable
            final String result = matcher.replaceAll(subst);
    
            System.out.println(result);
        }
    }

I made it with 
[1] pretty useful page you can even export to other languages like java, python, php ... Hope it helps and I was able to achieve your desired output. [1]: https://regex101.com/ </details> # 答案2 **得分**: 1 使用您提供的示例SQL字符串,以下是来自一个SQL格式化网站的结果。 ```sql CREATE TABLE book ( page NUMBER(20, 0), author VARCHAR2(255), nooflines NUMBER(100) )

正如您所看到的,仅对于这个SQL语句,已经进行了一些工作。

  1. 将所有SQL关键字转换为大写。

  2. 将列名和列定义左对齐。

这意味着您需要识别SQL关键字、列名和列定义。不要忘记主要的SQL语句:SELECT、INSERT、UPDATE、DELETE以及INSERT UPDATE等组合。

如果这是您想要做的,您将需要编写相当数量的代码。一个正则表达式无法满足您的要求。

英文:

Using your example SQL string, this is the result from one SQL Formatter web site.

CREATE TABLE book 
  ( 
     page      NUMBER(20, 0), 
     author    VARCHAR2(255), 
     nooflines NUMBER(100) 
  )

As you can see, just for this one SQL statement, there was a bit of work done.

  1. Upper case all SQL keywords.

  2. Left justify the column names and the column definitions.

Meaning that you have to identify SQL keywords, column names, and column definitions. Let's not forget the major SQL statements; SELECT, INSERT, UPDATE, DELETE, and the combinations, like INSERT UPDATE.

If this is what you want to do, you're going to have to write a good bit of code. One regex isn't going to get you there.

huangapple
  • 本文由 发表于 2020年7月31日 17:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定