Format MySQL code inside PHP string

后端 未结 16 1786
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 00:29

Is there any program IDE or not that can format MySQL code inside PHP string e.g. I use PHPStorm IDE and it cannot do it.

It does that for PHP and MYSQL but not for MYS

16条回答
  •  被撕碎了的回忆
    2021-02-04 01:31

    Since your problem is to format an existing body of code, you don't need an IDE. You need a script to batch-process all your files once, and then you can forget about it. This is what the script needs to do:

    1. Correctly parse PHP strings, including strings with all sorts of embedded or escaped quotes. This should be bullet-proof, and should only care about PHP syntax.
    2. Check each PHP string with an algorithm that detects SQL commands. This can be made as smart as you need it to be (no need to blindly accept every string containing the word "insert", for example).
    3. Feed the identified strings through an SQL pretty-printer.
    4. Edit the input file, substituting the formatted string for the original one.

    Ideally, parts 1 and 3 should be handled by off-the-shelf modules. The rest should be easy to put together yourself, right?

    Update: Explaining this made it sound so simple, I decided to do it myself. Here's a quick solution. It's in python, but if you were willing to switch IDEs you can deal with installing python, right?

    Drag-and-drop any number of php files onto the script, or call it from the commandline, and it will filter the SQL bits through the SQLFormat API that @Parahat suggested. It edits the files in place, so keep a copy!

    """Format sql strings embedded in php code, editing source IN PLACE"""
    import sys, re, urllib, urllib2
    
    def processfile(fname):
        with open(fname) as fp:
            text = fp.read()
    
        with open(fname, "w") as out:
            for token in chunk(text):
                if is_sql_string(token):
                    token = token[0] + sqlformat(token[1:-1]) + token[0]
                out.write(token)
    
    def sqlformat(query):
        sqlapi = 'http://sqlformat.appspot.com/format/?keyword_case=upper&reindent=1&n_indents=4&'
        return urllib2.urlopen(sqlapi+urllib.urlencode({'data':query})).read()
    
    php_mode = False # global, so that is_sql_string() can examine it
    def chunk(src):
        """Chunk the php file into interesting units"""
        global php_mode
        while src:
            if not php_mode: # Read up to the next php group, if any
                m = re.match(r".*?<\?php", src, re.S)
                if m:
                    tok, src = _usematch(m, src)
                    yield tok
                    php_mode = True
                else: # No more php groups: EOF
                    yield src
                    return
    
            else:  # Reading php code
                # PHP ends without another string?
                m = re.match(r"[^'\"]*?\?>", src, re.S)
                if m:
                    tok, src = _usematch(m, src)
                    yield tok
                    php_mode = False
                    continue
    
                # at non-string material?
                m = re.match(r"[^'\"]+", src) 
                if m:
                    tok, src = _usematch(m, src)
                    yield tok
                    continue
    
                # Parse a string: Smallest quote-delimited sequence,
                # where closing quote is not preceded by backslash
                m = re.match(r'".*?(?

提交回复
热议问题