datagrip 根据 sql 导出 示例

独自空忆成欢 提交于 2019-11-27 05:04:16

例如 sql

select 'operator_scenic_staytime_day', t.citycode, countycode, sceniccode, uuid, scenicname, sceniclevel, maxload, sceniclogo, staytime, people_num, format_time, updatetime, create_time
from operator_scenic_staytime_day t
where countycode = '130724'
  AND scenicname != 'xxxx';
'operator_scenic_staytime_day' 为表的名字grooy 代码:
import com.intellij.database.model.DasTable

SEP = ", "
QUOTE     = "\'"
NEWLINE   = System.getProperty("line.separator")

KEYWORDS_LOWERCASE = com.intellij.database.util.DbSqlUtil.areKeywordsLowerCase(PROJECT)
KW_INSERT_INTO = KEYWORDS_LOWERCASE ? "insert into " : "INSERT INTO "
KW_VALUES = KEYWORDS_LOWERCASE ? ") values (" : ") VALUES ("
KW_NULL = KEYWORDS_LOWERCASE ? "null" : "NULL"

def record(columns, dataRow) {
    OUT.append(KW_INSERT_INTO)

    if (TABLE == null) {    // 第一列 作为 表的名字!
        OUT.append(FORMATTER.format(dataRow, columns[0]))
    }
    else {
    OUT.append(TABLE.getParent().getName()).append(".").append(TABLE.getName())
    }
    OUT.append(" (")

    columns.eachWithIndex { column, idx ->
       if(idx == 0){      // 排除第一列
            return;
        }
        OUT.append(column.name()).append(idx != columns.size() - 1 ? SEP : "")
    }

    OUT.append(KW_VALUES)
    columns.eachWithIndex { column, idx ->
        if(idx == 0){           // 排除第一列
            return;
        }

        def value = dataRow.value(column)
        def skipQuote = value.toString().isNumber() || value == null
        def stringValue = value != null ? FORMATTER.format(dataRow, column) : KW_NULL
        if (DIALECT.getDbms().isMysql()) stringValue = stringValue.replace("\\", "\\\\")
        OUT.append(skipQuote ? "": QUOTE).append(stringValue.replace(QUOTE, QUOTE + QUOTE))
           .append(skipQuote ? "": QUOTE).append(idx != columns.size() - 1 ? SEP : "")
    }
    OUT.append(");").append(NEWLINE)
}

ROWS.each { row -> record(COLUMNS, row) }

 

 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!