행위

Sql script converter

DB CAFE

# -*- coding: utf-8 -*-

import sys, string
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        #self.setGeometry(400, 20, 1200, 800)
        self.setFixedSize(1200, 800)
        self.setWindowTitle("SQL Script Converter")
        self.setWindowIcon(QtGui.QIcon("c:\sqlscriptconverter/img/pythonlogo.png"))
        QtGui.QApplication.setStyle("Cleanlooks")

        font = self.font()                                           # set font
        font.setFamily("GulimChe")
        font.setPointSize(10)
        self.setFont(font)

        self.statusBar()                                             # set statusbar

        self.vListcount = 0                                          # set user variables
        self.vEditchanged = False
        self.vSQLScript = ""

        # -- create listwidget objects ------------------------------
        self.tListwidget = QtGui.QListWidget(self)                   # set listwidget
        self.tListwidget.setGeometry(QtCore.QRect(30, 66, 220, 650))
        self.tListwidget.setFont(font)                               # set font of listwidget

        self.tListwidget.setStatusTip("This is a QlistWidget object.")

        # -- open file and add item ---------------------------------
        tInitfile = open('c:\sqlscriptconverter\convertlist.ini', 'r')

        while True:
            vInitfile = tInitfile.readline()                         # no more line, return "None"
            if not vInitfile:
                break
            self.tListwidget.addItem(vInitfile.replace("\n", ""))    # add list item

        tInitfile.close()

        # -- create button objects ----------------------------------
        tButtonClear = QtGui.QPushButton("Clear", self)              # set clear button
        tButtonClear.clicked.connect(self.on_button_clear)
        tButtonClear.resize(100, 22)
        tButtonClear.move(30, 22)
        tButtonClear.setStatusTip("Clear text on the window.")

        tButtonConvert = QtGui.QPushButton("Convert(F5)", self)      # set convert button
        tButtonConvert.clicked.connect(self.on_button_convert)
        tButtonConvert.resize(100, 22)
        tButtonConvert.move(150, 22)
        tButtonConvert.setStatusTip("Convert your scripts on a rule basis. ")

        tButtonExit = QtGui.QPushButton("Exit", self)                # set exit button
        tButtonExit.clicked.connect(QtCore.QCoreApplication.instance().quit)
        tButtonExit.resize(100, 22)
        tButtonExit.move(1070, 22)
        tButtonExit.setStatusTip("Quit this application.")

        # -- create textedit objects -------------------------------
        self.tTextedit = QtGui.QTextEdit(self)                       # set textedit box
        self.tTextedit.move(270, 66)
        self.tTextedit.resize(900, 650)

        self.tTextedit.textChanged.connect(self.on_text_changed)
        self.tTextedit.setStatusTip("You can paste your script here and convert it.")

        # -- create progressbar objects -----------------------------
        self.tProgress = QtGui.QProgressBar(self)                    # set progressbar
        self.tProgress.setGeometry(30, 734, 1130, 20)

        self.on_load_sql()

    def on_load_sql(self):                                           # set initiate loading
        self.vSQLfile = open('c:\sqlscriptconverter\sql.ini', 'r')
        self.vSQLread = self.vSQLfile.read()

        self.tTextedit.setText(self.vSQLread)

        self.vSQLfile.close()

    def on_text_changed(self):
        self.vEditchanged = True

    def on_button_clear(self):                                       # set action of clear button
        self.vEditchanged = False
        self.vSQLScript = ""
        #self.on_load_sql()
        self.tTextedit.clear()
        self.tProgress.setValue(0)

    def keyPressEvent(self, e):                                      # system method for F-Key
        if e.key() == QtCore.Qt.Key_F5:
            self.on_button_convert()

    def on_button_convert(self):                                     # set actioni of vconvert button
        vKey1 = ""                                                   # 변환될 단어 할당(ex. select)
        vKey2 = ""                                                   # 변환할 단어 할당(ex. SELECT)
        vKeylist      = ["\n", "\t", "(", " ", ";", ",", ")"]        # 변환 단어 + 리스트 결합
        vKeylistcount = len(vKeylist)                                # 리스트 개수 저장
        vLoop = 0                                                    # 프로그래스바를 위한 Loop 카운팅

        self.vListcount = self.tListwidget.count()
        self.vSQLScript = self.tTextedit.toPlainText()

        vBarrate = 100. / (self.vListcount * vKeylistcount)

        for i in range(1, self.vListcount + 1):
            vKey1 = self.tListwidget.item(i-1).text()
            vKey2 = str(vKey1).upper()

            for j in range(1, vKeylistcount + 1):
                vLoop += 1
                vSQL1 = vKey1 + vKeylist[j - 1]
                vSQL2 = vKey2 + vKeylist[j - 1]

                self.vSQLScript = string.replace(self.vSQLScript, vSQL1, vSQL2)
                self.tProgress.setValue(vBarrate * vLoop)

        # -- 불용어 역전환
        xKey = ["skey", "wkey", "fkey", "tkey", "account", "quotas", "_key", ".ref", "_ref", "gkey", "dateend"]
        for xxKey in xKey:
            self.vSQLScript = string.replace(self.vSQLScript, xxKey, xxKey.lower())

        self.tTextedit.setText(self.vSQLScript)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    gui = Window()
    gui.show()
    sys.exit(app.exec_())