アットウィキロゴ
var atl = atl || {};
atl.sql = atl.sql || {};
atl.dom = atl.dom || {};
atl.sql.INDENT_STRING = "\t";
atl.sql.RESERVED_WORDS_UPPER_FLG = false;
atl.sql.RESERVED_WORDS_LOWER_FLG = false;
atl.sql.ATO_NO_SPACE_TOHKEN_KIGOH_MAP = {
    ".": true,
    "(": true,
    ",": false
};
atl.sql.MAE_NO_SPACE_TOHKEN_KIGOH_MAP = {
    ".": true,
    ",": true,
    ";": true,
    ")": true,
    "(*)": true,
    "(+)": true,
    "(": {
        "AND": false,
        "OR": false,
        "ON": false,
        "=": false,
        "-": false,
        "+": false,
        "/": false,
        "*": false,
        "<": false,
        "<=": false,
        "<>": false,
        ">": false,
        ">=": false
    }
};
atl.sql.ATO_BREAK_TOKEN_MAP = {
    "WITH": false,
    "SELECT": true,
    "UPDATE": true,
    "INTO": true,
    "FROM": true,
    "SET": true,
    "WHERE": true,
    "BY": true,
    "VALUES": true,
    "ON": false,
    "UNION": true,
    ",": true,
    "(": true,
    ";": true,
    "ALL": true,
    "AND": false,
    "OR": false,
    "JOIN": true,
    "MINUS": true,
    "INTERSECT": true,
    "USING": true,
    "UNION ALL": true,
    "WHEN MATCHED THEN": true,
    "WHEN NOT MATCHED THEN": true
};
atl.sql.MAE_BREAK_TOKEN_MAP = {
    "INSERT": true,
    "UPDATE": true,
    "FROM": true,
    "SET": true,
    "WHERE": true,
    "AND": true,
    "OR": true,
    "GROUP": true,
    "HAVING": true,
    "ORDER": true,
    "VALUES": true,
    "UNION": true,
    ")": true,
    ";": true,
    "WHEN": true,
    "ELSE": true,
    "END": true,
    "SELECT": true,
    ",": false,
    "ON": true,
    "WITH": false,
    "LEFT": true,
    "RIGHT": true,
    "INNER": true,
    "FULL": true,
    "NATURAL": true,
    "MINUS": true,
    "INTERSECT": true,
    "USING": true,
    "UNION ALL": true,
    "WHEN MATCHED THEN": true,
    "WHEN NOT MATCHED THEN": true
};
atl.sql.MAE_EXP_BREAK_TOKEN_MAP = {
    "^--": true
};
atl.sql.NOT_BREAK_TOKEN_MAP = {
    "BETWEEN": "AND"
};
atl.sql.INDENT_PLUS_TOKEN_MAP = {
    "WITH": false,
    "SELECT": true,
    "UPDATE": true,
    "INSERT": true,
    "FROM": true,
    "SET": true,
    "WHERE": true,
    "AND": true,
    "OR": true,
    "BY": true,
    "VALUES": true,
    "(": true,
    "CASE": true,
    "ON": true,
    "USING": true,
    "MATCHED": true,
    "MERGE": true,
    "WHEN MATCHED THEN": true,
    "WHEN NOT MATCHED THEN": true
};
atl.sql.INDENT_MINUS_TOKEN_MAP = {
    "SET": true,
    "AND": true,
    "OR": true,
    "VALUES": true,
    "UNION": true,
    "END": true,
    "ON": true,
    "MINUS": true,
    "INTERSECT": true,
    "USING": true,
    "UNION ALL": true,
    "WHEN MATCHED THEN": true,
    "WHEN NOT MATCHED THEN": true
};
atl.sql.KAKKO_BREAK_TOKEN_MAP = {
    "SELECT": true,
    "INSERT": true,
    "UPDATE": true,
    "FROM": true,
    "AND": true,
    "OR": true,
    "ON": true,
    "<": true,
    "<=": true,
    "<>": true,
    ">": true,
    ">=": true,
    "=": true,
    "!=": true
};
atl.sql.BLOCK_INDENT_NEXT_MINUS_ALL_PLUS_TOKEN_MAP = {
    "LEFT": true,
    "RIGHT": true,
    "INNER": true,
    "FULL": true,
    "NATURAL": true
};
atl.sql.BASE_INDENT_SEARCH_MAP = {
    "SELECT": true,
    "INSERT": true,
    "UPDATE": true,
    "DELETE": true
};
atl.sql.BASE_BLOCK_LEVEL_INDENT_MAP = {
    "FROM": true,
    "WHERE": true,
    "HAVING": true,
    "GROUP": true,
    "ORDER": true,
    "INSERT": true,
    "UPDATE": true
};
atl.sql.MACHING_INDENT_MAP = {
    "(": ")",
    "WHEN MATCHED THEN": "WHEN NOT MATCHED THEN"
};
atl.sql.SPECIAL_JOIN_TOKEN_MAP = {
    "UNION": "ALL",
    "UNION ALL": true,
    "WHEN": {
        "MATCHED": true,
        "NOT": true
    },
    "WHEN MATCHED": "THEN",
    "WHEN MATCHED THEN": true,
    "WHEN NOT": "MATCHED",
    "WHEN NOT MATCHED": "THEN",
    "WHEN NOT MATCHED THEN": true
};
atl.sql.SPECIAL_LAST_JOIN_TOKEN_MAP = {
    "AND": atl.sql.INDENT_STRING,
    "OR": atl.sql.INDENT_STRING,
    "ON": atl.sql.INDENT_STRING
};
atl.sql.RESERVED_WORDS_MAP = {
    "ABORT": true,
    "ABSOLUTE": true,
    "ACCELERATED": true,
    "ACTION": true,
    "ADD": true,
    "AFTER": true,
    "ALL": true,
    "ALTER": true,
    "AND": true,
    "ANY": true,
    "AS": true,
    "ASC": true,
    "ATOMIC": true,
    "AUTHORIZATION": true,
    "AVG": true,
    "BEFORE": true,
    "BEGIN/BEGINS": true,
    "BETWEEN": true,
    "BINARYNULL": true,
    "BIND": true,
    "BLANK": true,
    "BY": true,
    "CALL": true,
    "CASCADE": true,
    "CASE": true,
    "CHAIN": true,
    "CHARACTER": true,
    "CHECK": true,
    "CLOSE": true,
    "COBOL": true,
    "COLLATE": true,
    "COLUMN": true,
    "COMMIT": true,
    "COMMITTED": true,
    "CONDITION": true,
    "CONSTRAINT": true,
    "CONTINUE": true,
    "CONVERT": true,
    "COUNT": true,
    "CREATE": true,
    "CREATETAB": true,
    "CROSS": true,
    "CS": false,
    "CURDATE": true,
    "CURRENT": true,
    "CURSOR": true,
    "CURTIME": true,
    "DATABASE": true,
    "DCOMPRESS": true,
    "DECIMALSEPARATORCOMMA": true,
    "DECLARE": true,
    "DEFAULT": true,
    "DELETE": true,
    "DESC": true,
    "DIAGNOSTICS": true,
    "DICTIONARY": true,
    "DISTINCT": true,
    "DO": true,
    "DROP": true,
    "EACH": true,
    "ELSE": true,
    "ELSEIF": true,
    "END": true,
    "ESCAPE": true,
    "EX": false,
    "EXCLUSIVE": true,
    "EXEC": true,
    "EXECUTE": true,
    "EXISTS": true,
    "EXTERNAL": true,
    "FETCH": true,
    "FIRST": true,
    "FN": false,
    "FOR": true,
    "FOREIGN": true,
    "FROM": true,
    "FULL": true,
    "GRANT": true,
    "GROUP": true,
    "HANDLER": true,
    "HAVING": true,
    "HOUR": true,
    "IDENTITY": true,
    "IF": true,
    "IN": true,
    "INCLUDE": true,
    "INDEX": true,
    "INNER": true,
    "INOUT": true,
    "INSERT": true,
    "INTERNAL": true,
    "INTO": true,
    "IS": true,
    "ISOLATION": true,
    "INTERSECT": true,
    "JOIN": true,
    "KEY": true,
    "LANGUAGE": true,
    "LAST": true,
    "LEAVE": true,
    "LEFT": true,
    "LEGACYOWNERNAME": true,
    "LENGTH": true,
    "LEVEL": true,
    "LIKE": true,
    "LOGICAL": true,
    "LOGIN": true,
    "LOOP": true,
    "LOWER": true,
    "MAX": true,
    "MILLISECOND": true,
    "MIN": true,
    "MINUTE": true,
    "MODIFIABLE": true,
    "MODIFY": true,
    "MODULE": true,
    "MONEY": true,
    "MONTH": true,
    "MATCHED": true,
    "MINUS": true,
    "MERGE": true,
    "NEW": true,
    "NEXT": true,
    "NO": false,
    "NORMAL": true,
    "NOT": true,
    "NOTFOUND": true,
    "NOW": true,
    "NULL": true,
    "NATURAL": true,
    "OF": true,
    "OFF": true,
    "OLD": true,
    "ON": true,
    "ONLY": true,
    "OPEN": true,
    "OPENMODE": true,
    "OPTION": true,
    "OR": true,
    "ORDER": true,
    "OUT": true,
    "OUTER": true,
    "OWNER": true,
    "OWNERACCESS": true,
    "PAGESIZE": true,
    "PREALLOCATE": true,
    "PREPARE": true,
    "PRIMARY": true,
    "PRINT": true,
    "PRIOR": true,
    "PRIVILEGES": true,
    "PROCEDURE": true,
    "PUBLIC": true,
    "RANGE": true,
    "READ": true,
    "READONLY": true,
    "REFERENCES": true,
    "REFERENCING": true,
    "RELATIVE": true,
    "RELEASE": true,
    "REPEAT": true,
    "REPEATABLE": true,
    "REPLACE": true,
    "REPLICATION": true,
    "RESIGNAL": true,
    "RESTRICT": true,
    "RETURN": true,
    "RETURNS": true,
    "REVOKE": true,
    "RIGHT": true,
    "ROLLBACK": true,
    "ROW": true,
    "ROWCOUNT": true,
    "SAVEPOINT": true,
    "SCHEMA": true,
    "SCROLL": true,
    "SECURITY": true,
    "SEG": true,
    "SELECT": true,
    "SERIALIZABLE": true,
    "SET": true,
    "SIGNAL": true,
    "SIZE": true,
    "SOME": true,
    "SQLERROR": true,
    "SQLEXCEPTIONS": true,
    "SQLWARNINGS": true,
    "SQLSTATE": true,
    "SSP_EXPR": true,
    "SSP_PRED": true,
    "START": true,
    "STATEMENT": true,
    "STRINGNULL": true,
    "SUM": true,
    "SVBEGIN": true,
    "SVEND": true,
    "TABLE": true,
    "THEN": true,
    "THRESHOLD": true,
    "TO": true,
    "TOP": true,
    "TRANSACTION": true,
    "TRIGGER": true,
    "TRIGGERSTAMPMISC": true,
    "TRUENULLCREATE": true,
    "TS": false,
    "TYPE": true,
    "UNCOMMITTED": true,
    "UNDO": true,
    "UNION": true,
    "UNIQUE": true,
    "UNTIL": true,
    "UPDATABLE": true,
    "UPDATE": true,
    "UPPER": true,
    "USER": true,
    "USING": true,
    "VALUE": true,
    "VALUES": true,
    "VERIFY": true,
    "VIEW": true,
    "WHEN": true,
    "WHERE": true,
    "WHILE": true,
    "WITH": true,
    "WORK": true,
    "WRITE": true,
    "YEAR": true,
    "ZSTRING": true
};
atl.regMatch = function(a, b) {
    for (var c in b) {
        if (b[c] == true) {
            var d = new RegExp(c);
            if (a.match(d)) {
                return true
            }
        }
    }
    return false
};
atl.StringBuffer = function(a) {
    this.array = new Array();
    this.len = 0;
    this.stringCache = null;
    if (a) {
        this.append(a)
    }
};
atl.StringBuffer.prototype = {
    append: function(a) {
        this.array.push(a);
        this.len += a.length;
        this.stringCache = null
    },
    toString: function() {
        if (!this.stringCache) {
            this.stringCache = this.array.join("")
        }
        return this.stringCache
    },
    length: function() {
        return this.len
    },
    charAt: function(i) {
        return this.toString().substring(i)
    },
    clear: function() {
        this.len = 0;
        this.array.length = 0
    }
};
atl.dom.Node = function() {};
atl.dom.Node.prototype = {
    DOCUMENT_NODE: 0,
    ELEMENT_NODE: 1,
    TEXT_NODE: 2,
    appendChild: function(a) {
        if (!this.childArray) {
            this.childArray = new Array()
        }
        this.childArray.push(a)
    },
    getChildNodes: function() {
        return this.childArray
    },
    getNodeType: function() {
        return null
    },
    setNodeValue: function(a) {
        this.value = a
    },
    getNodeValue: function() {
        return this.value
    }
};
atl.dom.Element = function(a) {
    this.tagName = a;
    this.attributeHash = new Array();
    this.elementId = atl.dom.Element.prototype.elementId++
};
atl.dom.Element.prototype = new atl.dom.Node();
atl.dom.Element.prototype.elementId = 0;
atl.dom.Element.prototype.getNodeType = function() {
    return atl.dom.Node.ELEMENT_NODE
};
atl.dom.Element.prototype.getAttribute = function(a) {
    return this.attributeHash[a]
};
atl.dom.Element.prototype.setAttribute = function(a, b) {
    this.attributeHash[a] = b
};
atl.dom.Element.prototype.getTagName = function() {
    return this.tagName
};
atl.dom.Element.prototype.getElementId = function() {
    return this.elementId
};
atl.dom.Document = function() {};
atl.dom.Document.prototype = new atl.dom.Node();
atl.dom.Document.prototype.getNodeType = function() {
    return atl.dom.Node.DOCUMENT_NODE
};
atl.dom.Document.prototype.createElement = function(a) {
    return new atl.dom.Element(a)
};
atl.dom.Document.prototype.createTextNode = function(a) {
    return new atl.dom.Text(a)
};
atl.sql.SqlFormatter = function(a) {
    if (a != null) {
        this.tokenizer = new atl.sql.SqlTokenizer(a);
        this.sql = a
    }
    this.machingIndentArray = new Array();
    this.machingEndKeyMap = new Array();
    this.notBreakToken = null;
    this.oldUpperToken = "";
    this.indentCnt = 0;
    this.machingIndentMap = {};
    this.baseIndentMap = {};
    this.baseIndentPlusMinusFirstFlgMap = {}
};
atl.sql.SqlFormatter.prototype = {
    format: function(a) {
        if (a != null) {
            this.tokenizer = new atl.sql.SqlTokenizer(a);
            this.sql = a
        }
        if (this.sql == null || this.sql.length == 0) {
            return ""
        }
        var b = new atl.dom.Document();
        var c = b.createElement("root");
        c.setAttribute("BREAK", true);
        this.createSqlNode(b, c);
        var d = new atl.StringBuffer();
        this.makeSqlBuffer(d, c);
        this.rTrimBuffer(d);
        return d.toString()
    },
    rTrimBuffer: function(a) {
        if (a.length <= 0) {
            return
        }
        var b = a.array.pop();
        if (b) {
            a.append(b.replace(/([ \t]+$)/g, ''))
        }
    },
    isLastBreakBuffer: function(a) {
        var b = a.array[a.array.length - 1];
        if (b == null || b.charAt(b.length - 1) == "\n") {
            return true
        }
        return false
    },
    formatToken: function(a, b, c, d, e) {
        var f = c.toUpperCase();
        var g = false;
        var h = "";
        var i = atl.sql.MACHING_INDENT_MAP[f];
        if (i) {
            this.machingIndentArray.push(this.indentCnt);
            this.machingEndKeyMap[e + ":" + i] = true
        } else if (this.machingEndKeyMap[e + ":" + f]) {
            this.indentCnt = this.machingIndentArray.pop();
            delete this.machingEndKeyMap[f]
        } else if (f == ";") {
            this.indentCnt = 0;
            this.machingIndentArray.length = 0
        }
        var j = atl.sql.MAE_NO_SPACE_TOHKEN_KIGOH_MAP[f];
        if (j) {
            if (j == true || j[this.oldUpperToken] != false) {
                b = b.replace(/([ \t]+$)/g, '')
            }
        }
        if (atl.sql.INDENT_MINUS_TOKEN_MAP[f]) {
            if (this.indentCnt > 0) {
                this.indentCnt--
            }
        }
        if (atl.sql.BLOCK_INDENT_NEXT_MINUS_ALL_PLUS_TOKEN_MAP[f]) {
            if (this.baseIndentPlusMinusFirstFlgMap[e] == null) {
                this.baseIndentPlusMinusFirstFlgMap[e] = true
            } else {
                this.indentCnt--
            }
        }
        if (atl.sql.BASE_INDENT_SEARCH_MAP[f]) {
            this.baseIndentMap[e] = this.indentCnt
        }
        if (atl.sql.BASE_BLOCK_LEVEL_INDENT_MAP[f] && this.baseIndentMap[e] != null) {
            this.indentCnt = this.baseIndentMap[e]
        }
        if (atl.sql.NOT_BREAK_TOKEN_MAP[f]) {
            this.notBreakToken = atl.sql.NOT_BREAK_TOKEN_MAP[f]
        }
        if (atl.sql.MAE_BREAK_TOKEN_MAP[f] || atl.regMatch(f, atl.sql.MAE_EXP_BREAK_TOKEN_MAP)) {
            if (d && this.notBreakToken != f) {
                b = "\r\n"
            }
        }
        if (this.notBreakToken == f) {
            h = " ";
            this.notBreakToken = null
        } else if ((atl.sql.ATO_BREAK_TOKEN_MAP[f] && d) || f.match(/^\-\-/) || f.match(/\*\/$/)) {
            h = "\r\n"
        } else if (atl.sql.SPECIAL_LAST_JOIN_TOKEN_MAP[f]) {
            h = atl.sql.SPECIAL_LAST_JOIN_TOKEN_MAP[f]
        } else if (atl.sql.ATO_NO_SPACE_TOHKEN_KIGOH_MAP[f]) {
            h = ""
        } else {
            h = " "
        }
        if (b != null && b.charAt(b.length - 1) == '\n') {
            if (a.length() > 0) {
                a.append(b + this.getIndent(this.indentCnt) + c)
            } else {
                a.append(this.getIndent(this.indentCnt) + c)
            }
        } else {
            a.append((b == null ? "" : b) + c)
        }
        if (atl.sql.INDENT_PLUS_TOKEN_MAP[f] || atl.sql.BLOCK_INDENT_NEXT_MINUS_ALL_PLUS_TOKEN_MAP[f]) {
            this.indentCnt++
        }
        this.oldUpperToken = f;
        return h
    },
    makeSqlBuffer: function(a, b) {
        var c = b.getChildNodes();
        if (c == null) {
            return
        }
        for (var i = 0; i < c.length; i++) {
            if (c[i].getTagName() == "block") {
                this.makeSqlBuffer(a, c[i]);
                continue
            }
            var d = b.getAttribute("BREAK");
            var e = b.getElementId();
            this.staticSpliter = this.formatToken(a, this.staticSpliter, c[i].getNodeValue(), d, e)
        }
        return
    },
    createSqlNode: function(a, b) {
        var c;
        var d = b.getAttribute("BREAK") || false;
        var e = atl.sql.RESERVED_WORDS_UPPER_FLG;
        var f = atl.sql.RESERVED_WORDS_LOWER_FLG;
        while ((c = this.tokenizer.nextTokenJoin(atl.sql.SPECIAL_JOIN_TOKEN_MAP, e, f)) != null) {
            var g = a.createElement("token");
            g.setNodeValue(c);
            if (!d && atl.sql.KAKKO_BREAK_TOKEN_MAP[c.toUpperCase()]) {
                d = true;
                b.setAttribute("BREAK", true)
            }
            if (c == "(") {
                var h = a.createElement("block");
                h.appendChild(g);
                b.appendChild(h);
                this.createSqlNode(a, h);
                continue
            } else if (c == ")") {
                b.appendChild(g);
                return
            }
            b.appendChild(g)
        }
    },
    getIndent: function(a) {
        var b = "";
        for (var i = 0; i < a; i++) {
            b += atl.sql.INDENT_STRING
        }
        return b
    }
};
atl.sql.SqlTokenizer = function(a) {
    this.TOKEN_END_KIGOH_MAP = {
        ",": true,
        "(": true,
        ")": true,
        "!": true,
        "=": true,
        "<": true,
        ">": true,
        "+": true,
        "-": true,
        "*": true,
        "/": true,
        ".": true,
        ";": true,
        "|": true
    };
    this.TOKEN_END_MAP = {
        " ": true,
        "\t": true,
        "\r": true,
        "\n": true
    };
    this.TOKEN_RENKETSU_KIGOH_MAP = {
        "<": {
            "=": true,
            ">": true
        },
        "(": {
            "*": true,
            "+": true
        },
        ">": "=",
        "!": "=",
        "(+": ")",
        "(*": ")",
        "/": "*",
        "*": "/",
        "-": "-",
        "|": "|",
        "<=": true,
        ">=": true,
        "!=": true,
        "<>": true,
        "(+)": true,
        "(*)": true,
        "/*": true,
        "*/": true,
        "--": true,
        "||": true
    };
    this.specialJoinMap = null;
    this.sql = a;
    this.tokenIdx = 0
};
atl.sql.SqlTokenizer.prototype = {
    nextTokenJoin: function(a, b, c) {
        var d;
        var e;
        if (this.joinArray && this.joinArray.length > 0) {
            return this.joinArray.shift()
        }
        if (!this.joinArray) {
            this.joinArray = new Array()
        }
        while ((d = this.nextToken()) != null) {
            if (b || c) {
                upperToken = d.toUpperCase();
                if (atl.sql.RESERVED_WORDS_MAP[upperToken]) {
                    if (c) {
                        d = d.toLowerCase()
                    } else {
                        d = upperToken
                    }
                }
            }
            this.joinArray.push(d);
            e = this.joinArray.join(" ");
            var f = a[e.toUpperCase()];
            if (f == null) {
                return this.joinArray.shift()
            } else if (f == true) {
                break
            } else {
                continue
            }
            break
        }
        if (this.joinArray.length > 0) {
            this.joinArray.length = 0;
            return e
        } else {
            return null
        }
    },
    nextToken: function() {
        var a;
        var b;
        var c = new atl.StringBuffer();
        var d = false;
        var e = false;
        var f = false;
        var g = false;
        if (this.sql == null) {
            return null
        }
        for (var i = this.tokenIdx; i < this.sql.length; i++) {
            a = this.sql.charAt(i);
            if (i + 1 < this.sql.length) {
                b = this.sql.charAt(i + 1)
            } else {
                b = ""
            }
            if (!e && !f && !g && a == "\'") {
                d = !d;
                if (!d) {
                    c.append(a);
                    break
                }
            } else if (!d && !f && !g && a == "\"") {
                e = !e;
                if (!e) {
                    c.append(a);
                    break
                }
            } else if (!d && !e && !g && a == "-" && b == "-") {
                if (!f && c.length() > 0) {
                    i--;
                    break
                }
                f = true
            } else if (f && (a == "\r" || a == "\n")) {
                f = false;
                break
            } else if (!d && !e && !f && a == "/" && b == "*") {
                if (c.length() > 0) {
                    i--;
                    break
                }
                g = true
            } else if (g && a == "*" && b == "/") {
                g = false;
                c.append(a + b);
                i++;
                break
            }
            if (!d && !e && !f && !g) {
                if (this.TOKEN_END_KIGOH_MAP[a]) {
                    if (c.length() == 0) {
                        c.append(a);
                        if (!this.TOKEN_END_KIGOH_MAP[b] && !this.TOKEN_END_MAP[b]) {
                            break
                        }
                        continue
                    }
                    var h = this.TOKEN_RENKETSU_KIGOH_MAP[c.toString()];
                    if (h) {
                        if (h == a || h[a]) {
                            c.append(a);
                            if (this.TOKEN_RENKETSU_KIGOH_MAP[c.toString()] == true) {
                                break
                            }
                            continue
                        }
                    }
                    i--;
                    break
                } else if (this.TOKEN_END_MAP[a]) {
                    if (this.TOKEN_END_KIGOH_MAP[b] || this.TOKEN_END_MAP[b]) {
                        continue
                    }
                    if (c.length() > 0) {
                        break
                    } else {
                        continue
                    }
                } else {
                    if (this.TOKEN_END_KIGOH_MAP[b] || this.TOKEN_END_MAP[b]) {
                        c.append(a);
                        break
                    }
                }
            }
            c.append(a)
        }
        this.tokenIdx = i + 1;
        if (c.length() > 0) {
            return c.toString()
        } else {
            return null
        }
    }
};
最終更新:2015年12月23日 13:44