Convenient way to wrap long SQL statements in javascript

前端 未结 5 1495
别那么骄傲
别那么骄傲 2021-01-26 09:35

In python, one can use \"\"\" to wrap long MySQL statements. For example,

sql = \"\"\"CREATE TABLE EMPLOYEE (
         FIRST_NAME  CHAR(20) NOT NULL,
         L         


        
相关标签:
5条回答
  • 2021-01-26 10:11

    In this answer, I suggest staging your multi-line string via a string array. Then, once the staging is complete you finalize the multi-line string by using join to enforce lines are separated with a newline character.

    In Sumukh Barve's answer, the strings were carefully constructed with a whitespace character separator (instead of a newline character). In itself, it's not wrong, however, it could lead to malformed SQL statements. When maintaining such SQL statements, the maintainer could easily overlook the need for the whitespace character and could inadvertently malformed the SQL statement:

    "CREATE TABLE" +
    "EMPLOYEE (" + //...
    

    Would result in:

    "CREATE TABLEEMPLOYEE (" + //...
    

    The use of join can also help ensure that there is a mandatory newline character separating the lines without the need to remember to add it within the text of each line.

    [ "CREATE TABLE",
      "EMPLOYEE (" ].join("\n")
    

    Would result in:

    CREATE TABLE
    EMPLOYEE (
    

    Without a newline character, all errors will be reported on line 1. For example, on PSQL:

    CREATE TABLE EMP (ID);
    

    Would report:

    ERROR:  syntax error at or near ";"
    LINE 1: TABLE EMP(ID);
    

    Regardless of how long the SQL statement is, all errors will be reported against LINE 1.

    However, if a new line was added, e.g.

    CREATE TABLE EMP (
        ID);
    

    You will get better feedback on which line caused the error:

    ERROR:  syntax error at or near ";"
    LINE 2: ID);
    

    Also using join with the newline character aids in logging.

    Note that there is an implicit newline separator present in @user781486 answer because of the ES6 backtick notation. ES6 backtick notation looks simple. However, it rapidly loses it's simplicity when you decide to mix backtick notation with conditional business rules.

    Your mileage may vary.

    function buildSQL(includeAge) {
      let arr = [];
      arr.push( "CREATE TABLE EMPLOYEE (" );
      arr.push( "    FIRST_NAME  CHAR(20) NOT NULL," );
      arr.push( "    LAST_NAME   CHAR(20), " );
      if (includeAge) {
          arr.push( "    AGE         INT," );
      }
      arr.push( "    SEX         CHAR(1)," );
      arr.push( "    INCOME      FLOAT " );
      arr.push( "); " );
      let sql = arr.join("\n");
      return sql;
    }
    
    function refreshSQL() {
      let includeAge = $("#includeAge").is(':checked');
      let sql = buildSQL(includeAge);
      $("#resultSQL").val(sql);
    }
    
    $("#includeAge").on('change', refreshSQL);
    
    refreshSQL();
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <p><label><input type="checkbox" id="includeAge"/>includeAge</label></p>
    
    <p><textarea id="resultSQL" style="width:400px; height:150px">
    </textarea></p>

    0 讨论(0)
  • 2021-01-26 10:19

    For the future large SQL statments, one tip is to use functions or procedures in MySQL, and then call it in one line:

    var sql = select * from getEmployee(); // function getEmployee returns the query

    0 讨论(0)
  • 2021-01-26 10:23

    You can use a backslash \ at the end of the line to spread the string over more than one line.

    var sql = "CREATE TABLE EMPLOYEE ( \
               FIRST_NAME  CHAR(20) NOT NULL, \
               LAST_NAME  CHAR(20), \
               AGE INT, \
               SEX CHAR(1), \
               INCOME FLOAT )";
    document.write(sql);

    0 讨论(0)
  • 2021-01-26 10:25

    This shouldn't be a problem if you are using node.js that support ES6. ES6 javascript now supports multi-line string using syntax below.

    sql = `CREATE TABLE EMPLOYEE (
             FIRST_NAME  CHAR(20) NOT NULL,
             LAST_NAME  CHAR(20),
             AGE INT,  
             SEX CHAR(1),
             INCOME FLOAT )`
    
    0 讨论(0)
  • 2021-01-26 10:30

    Dealing with long strings in JavaScript:

    var sql = "CREATE TABLE EMPLOYEE (" +
                 " FIRST_NAME  CHAR(20) NOT NULL," +
                 " LAST_NAME  CHAR(20)," +
                 " AGE INT," +
                 " SEX CHAR(1)," +
                 " INCOME FLOAT )";
    

    Python's triple quotes are great! Unfortunately, in JavaScript, you have only two options:

    • + based concatenation, as above
    • \ based continuation, as proposed by @Nina Scholz

    Personally, I don't like using \ for line continuation (in any language.) Using + doesn't introduce unnecessary spaces in your string either.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题