본문으로 바로가기
반응형

파이썬을 이용한 샘플 코드 조각 생성소스

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

"""
INSERT, SELECT, UPDATE, DELETE Sample Query
Column is up to you, but value is only Camel

author : Jeffrey Oh
"""

# table_name = "테이블명"

# c = ["컬럼명", "컬럼명", "...", "컬럼명"]

# sample

table_name = "adm_member"

c = ["seq", "user_id", "user_pw", "user_pw_enc", "name", "tel", "phone", "email", "zipCode", "addr1", "addr2", "remoteIp"]


insert = "INSERT INTO "+table_name+" ("

i1 = ") VALUES ("

i2 = ")"

cc = ""

s = ""

size = len(c)



for i in range(0, size) :

  if(i + 1 < size) :

    cc += c[i] + ", "

  else :

    cc += c[i]

cc += i1



for i in range(0, len(c)) :

    ts = ""

    if(i + 1 < len(c)) :

        if(c[i].find('_') != -1) :

            ts = c[i][:(c[i].find('_'))] + c[i][(c[i].find('_'))+1:].capitalize()

            if(c[i].count('_') > 1) :

                for j in range(0, c[i].count('_')-1) :
                    ts = ts[:(ts.find('_'))] + ts[(ts.find('_'))+1:].capitalize()

            s += "#{" + ts + "}, "

        else :

            s += "#{" + c[i] + "}, "

    else :

        if(c[i].find('_') != -1) :

            ts = c[i][:(c[i].find('_'))] + c[i][(c[i].find('_'))+1:].capitalize()

            if(c[i].count('_') > 1) :

                for j in range(0, c[i].count('_')-1) :
                    ts = ts[:(ts.find('_'))] + ts[(ts.find('_'))+1:].capitalize()

            s += "#{" + ts + "}, "

        else :

            s += "#{" + c[i] + "}"

cc = cc + s + i2



print(insert+cc+"\n")



delete = "DELETE FROM " + table_name + " WHERE " + c[0]



if(c[0].find('_') != -1):

    delete += "=#{" + c[0][:(c[0].find('_'))] + c[0][(c[0].find('_'))+1:].capitalize() + "}"

else :

    delete += "=#{" + c[0] + "}"





print(delete+"\n")



update = "UPDATE "+table_name+" SET "

cc = ""

s = ""

for i in range(0, len(c)) :

    ts = ""

    if(i + 1 < len(c)) :

        if(c[i].find('_') != -1) :

            ts = c[i][:(c[i].find('_'))] + c[i][(c[i].find('_'))+1:].capitalize()

            if(c[i].count('_') > 1) :

                for j in range(0, c[i].count('_')-1) :
                    ts = ts[:(ts.find('_'))] + ts[(ts.find('_'))+1:].capitalize()

            s += c[i] + "=#{" + ts + "}, "

        else :

            s += c[i] + "=#{" + c[i] + "}, "

    else :

        if(c[i].find('_') != -1) :

            ts = c[i][:(c[i].find('_'))] + c[i][(c[i].find('_'))+1:].capitalize()

            if(c[i].count('_') > 1) :

                for j in range(0, c[i].count('_')-1) :
                    ts = ts[:(ts.find('_'))] + ts[(ts.find('_'))+1:].capitalize()

            s += c[i] + "=#{" + ts + "}, "

        else :

            s += c[i] + "=#{" + c[i] + "}"

cc += s + " WHERE " + c[0]

if(c[0].find('_') != -1):

    cc += "=#{" + c[0][:(c[0].find('_'))] + c[0][(c[0].find('_'))+1:].capitalize() + "}"

else :

    cc += "=#{" + c[0] + "}"



print(update+cc+"\n")



select = "SELECT "

cc = ""

for i in range(0, size) :

    if(i + 1 < size) :

        cc += c[i] + ", "

    else :

        cc += c[i]

cc += " FROM " +table_name+ " WHERE " + c[0] + "=#{" + c[0] + "}"



print(select+cc)
INSERT INTO adm_member (seq, user_id, user_pw, user_pw_enc, name, tel, phone, email, zipCode, addr1, addr2, remoteIp) VALUES (#{seq}, #{userId}, #{userPw}, #{userPwEnc}, #{name}, #{tel}, #{phone}, #{email}, #{zipCode}, #{addr1}, #{addr2}, #{remoteIp})

DELETE FROM adm_member WHERE seq=#{seq}

UPDATE adm_member SET seq=#{seq}, user_id=#{userId}, user_pw=#{userPw}, user_pw_enc=#{userPwEnc}, name=#{name}, tel=#{tel}, phone=#{phone}, email=#{email}, zipCode=#{zipCode}, addr1=#{addr1}, addr2=#{addr2}, remoteIp=#{remoteIp} WHERE seq=#{seq}

SELECT seq, user_id, user_pw, user_pw_enc, name, tel, phone, email, zipCode, addr1, addr2, remoteIp FROM adm_member WHERE seq=#{seq}
반응형