首页 励志语录 文学杂读 农贸分析

AutoIT生成SQL语句

2024-10-21


#cs ____________________________________

 Au3 版本:
 脚本作者:
    Email:
    QQ/TM:
 脚本版本:
 脚本功能:

#ce _______________脚本开始_________________
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>

Opt("GUICloseOnESC", 0)

Global $db_version,$db_comb,$ii_num,$uu_num,$dd_num,$ss_num,$db_lable,$time
Global $i_num,$u_num,$d_num,$s_num,$table_name,$row_name,$row_type,$row_len,$add_btn
Global $sql_num = 1,$sql[10000][2][2],$tablename
_main_gui()

Func _main_gui()
    Opt("GUICoordMode", 2)
    Opt("GUIResizeMode", 1)
    Opt("GUIOnEventMode", 1)
    $gui = GUICreate("创建SQL语句",400,400)
    GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
    GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")
    
    GUICtrlCreateLabel("DataBase:",20,15,80)
    GUICtrlSetFont(-1,14)
    $db_comb = GUICtrlCreateCombo("",10,-1,120,45,$CBS_DROPDOWNLIST + $WS_VSCROLL)
    GUICtrlSetFont(-1,14)
    GUICtrlSetData(-1,"|Oracle|SQLServer|MySQL|DB2|Infomix|Sybase|Teradata|Netezza|Postgres|DaMeng|KingBase|GBase","")
    GUICtrlSetOnEvent($GUI_EVENT_PRIMARYDOWN,"_choose_client_name")
    
    $db_version = GUICtrlCreateCombo("",10,-1,100,45,$CBS_DROPDOWNLIST + $WS_VSCROLL)
    GUICtrlSetFont(-1,14)
    
    GUICtrlCreateLabel("表名:",-330,0,45)
    GUICtrlSetFont(-1,14)
    $table_name = GUICtrlCreateInput("",10,-1,100,25)
    GUICtrlSetFont(-1,14)
    GUICtrlSetState(-1,$GUI_DISABLE)
    GUICtrlCreateLabel("列名:",20,-1,45)
    GUICtrlSetFont(-1,14)
    $row_name = GUICtrlCreateInput("",10,-1,100,25)
    GUICtrlSetFont(-1,14)
    GUICtrlSetState(-1,$GUI_DISABLE)
    GUICtrlCreateLabel("类型:",-330,20,45)
    GUICtrlSetFont(-1,14)
    $row_type = GUICtrlCreateCombo("",10,-1,120,45,$CBS_DROPDOWNLIST + $WS_VSCROLL)
    GUICtrlSetFont(-1,14)
    GUICtrlSetState(-1,$GUI_DISABLE)
    GUICtrlCreateLabel("长度:",0,-1,45)
    GUICtrlSetFont(-1,14)
    $row_len = GUICtrlCreateInput("",10,-1,80,25)
    GUICtrlSetFont(-1,14)
    GUICtrlSetState(-1,$GUI_DISABLE)
    $add_btn = GUICtrlCreateButton("添加",0,-1,40,25)
    GUICtrlSetFont(-1,14)
    GUICtrlSetOnEvent($add_btn,"_add_now")
    GUICtrlSetState(-1,$GUI_DISABLE)
    
    GUICtrlCreateLabel("Insert次数:",-330,20,170)
    GUICtrlSetFont(-1,14)
    $i_num = GUICtrlCreateInput("100",10,-1,150,25)
    GUICtrlSetFont(-1,14)
    
    GUICtrlCreateLabel("Update次数:",-330,20,170)
    GUICtrlSetFont(-1,14)
    $u_num = GUICtrlCreateInput("100",10,-1,150,25)
    GUICtrlSetFont(-1,14)
    
    GUICtrlCreateLabel("Delete次数:",-330,20,170)
    GUICtrlSetFont(-1,14)
    $d_num = GUICtrlCreateInput("100",10,-1,150,25)
    GUICtrlSetFont(-1,14)
    
    GUICtrlCreateLabel("Select次数:",-330,20,170)
    GUICtrlSetFont(-1,14)
    $s_num = GUICtrlCreateInput("10",10,-1,150,25)
    GUICtrlSetFont(-1,14)
    
    $db_btn = GUICtrlCreateButton("确定",-180,20,60,30)
    GUICtrlSetFont(-1,14)
    GUICtrlSetOnEvent($db_btn,"_execute_now")
    
    $db_lable = GUICtrlCreateLabel("",-150,20,300)
    GUICtrlSetFont(-1,14)
    GUICtrlSetColor(-1,0xff0000)
    
    GUISetState()
    While 1
        Sleep(10)
    WEnd
    
EndFunc
;~ 向表中添加字段。
Func _add_now()
    $tablename = StringStripWS(GUICtrlRead($table_name),8)
    $rowname = StringStripWS(GUICtrlRead($row_name),8)
    $rowtype = StringStripWS(GUICtrlRead($row_type),8)
    $rowlen = StringStripWS(GUICtrlRead($row_len),8)
    If StringLen($tablename) > 30 Then
        MsgBox(0,"INFO","表名长度超过30,错误。")
        Return
    EndIf
    If StringLen($tablename) == 0 Then
        MsgBox(0,"INFO","要添加的表名为空,错误!")
        Return
    EndIf
    If Not StringRegExp($tablename,'^[a-zA-Z]{1}[a-zA-Z0-9_$#]{0,}',0) Then
        MsgBox(0,"INFO","表名定义错误,应该是字母为首的数字、字母、# $ _")
        Return
    EndIf
    GUICtrlSetState($table_name,$GUI_DISABLE)
    
    If StringLen($rowname) == 0 Then
        MsgBox(0,"INFO","要添加的列名为空,错误!")
        Return
    EndIf
    If StringLen($rowtype) == 0 Then
        MsgBox(0,"INFO","要添加的字段类型为空,错误!")
        Return
    EndIf
    If StringLen($rowlen) <> 0 Then
        If Not StringRegExp($rowlen,'[0-9]{1,}',0) Then
            MsgBox(0,"INFO","要添加的字段长度为非数字,错误。")
            Return
        EndIf
    EndIf
    For $i = 1 To $sql_num - 1
        If $sql[$i][0][0] == $rowname Then
            MsgBox(0,"INFO","列名与已知列名重复,错误!")
            Return
        EndIf
    Next
    $sql[$sql_num][0][0] = $rowname
    $sql[$sql_num][1][0] = $rowtype
    $sql[$sql_num][1][1] = $rowlen
    $sql_num = $sql_num + 1
EndFunc
;选择数据库
Func _choose_client_name()
    $client_name = StringStripWS(GUICtrlRead(@GUI_CtrlId),8)
    GUICtrlSetState($table_name,$GUI_ENABLE)
    GUICtrlSetState($row_name,$GUI_ENABLE)
    GUICtrlSetState($row_type,$GUI_ENABLE)
    GUICtrlSetState($row_len,$GUI_ENABLE)
    GUICtrlSetState($add_btn,$GUI_ENABLE)
    Switch $client_name
        Case StringCompare($client_name,"Oracle") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($db_version,"11.1.0.1|11.1.0.2|11.1.0.3|11.1.0.4|11.1.0.5|11.1.0.6|11.1.0.7|11.2.0.1|11.2.0.2|11.2.0.3|11.2.0.4|11.2.0.5|10.1.0.1|10.1.0.2|10.1.0.3|10.1.0.4|10.1.0.5|10.2.0.1|10.2.0.2|10.2.0.3|10.2.0.4|10.2.0.5|9.1.0.0|9.2.0.1|9.2.0.4|9.2.0.6|9.2.0.8")
            GUICtrlSetData($row_type,"CHAR|VARCHAR2|NCHAR|NVARCHAR2|DATE|LONG|RAW|LONG RAW|BLOB|CLOB|NCLOB|BFILE|ROWID|NROWID|NUMBER(P,S)|DECIMAL(P,S)|INTEGER|FLOAT|REAL")
            GUICtrlSetData($db_lable,"创建Oracle数据库的SQL语句.")
        Case StringCompare($client_name,"SQLServer") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($row_type,"IMAGE|BINARY|VARBINARY|BIGINT|INT|SMALLINT|TINYINT|DECIMAL|NUMERIC|FLOAT|REAL|CHAR|VARCHAR|TEXT|NCHAR|NVARCHAR|NTEXT|DATETIME|SMALLDATETIME|MONEY|SMALLMONEY|BIT|TIMESTAMP|UNIQUEIDENTIFIER")
            GUICtrlSetData($db_version,"2005|2008")
            GUICtrlSetData($db_lable,"创建SQLServer数据库的SQL语句.")
        Case StringCompare($client_name,"MySQL") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($row_type,"TINYINT|SMALLINT|MEDIUMINT|INT|BIGINT|FLOAT|DOUBLE|DECIMAL|CHAR|VARCHAR|TINYBLOB|BLOB|MEDIUMBLOB|LONGBLOB|TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|ENUM|SET|DATE|TIME|DATETIME|TIMESTAMP|YEAR")
            GUICtrlSetData($db_version,"5.6|5.5|5.1|5.0|4.1|4.0")
            GUICtrlSetData($db_lable,"创建MySQL数据库的SQL语句.")
        Case StringCompare($client_name,"DB2") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($row_type,"CHAR|VARCHAR|LONG VARCHAR|GRAPHICS|VARGRAPHICS|LONG VARGRAPHIC|TIMESTAMP|DATE|TIME|INT|SMALLINT|DOUBLE|FLOAT|NUMERIC(P,S)|DECIMAL(P,S)|TEXT|BLOB")
            GUICtrlSetData($db_version,"10.1|9.7|9.5|9.1|8.2|8.1")
            GUICtrlSetData($db_lable,"创建DB2数据库的SQL语句.")
        Case StringCompare($client_name,"Infomix") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($db_version,"11|10|9")
        Case StringCompare($client_name,"Sybase") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($db_version,"11|12|15")
        Case StringCompare($client_name,"Teradata") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($db_version,"7|8")
        Case StringCompare($client_name,"Netezza") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($db_version,"all")
        Case StringCompare($client_name,"Postgres") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($db_version,"9")
        Case StringCompare($client_name,"DaMeng") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($row_type,"CHAR|VARCHAR|CHARACTER|VARCHAR2|NUMERIC|DECIMAL|DEC|NUMBER|INTEGER|INT|BIGINT|TINYINT|BYTE|SMALLINT|BINARY|VARBINARY|FLOAT|DOUBLE|REAL|DOUBLE PRECISION|BIT|DATE|TIME|TIMESTAMP|INTERVAL YEAR TO MONTH|INTERVAL YEAR|INTERVAL MONTH|INTERVAL DAY|INTERVAL DAY TO HOUR|INTERVAL DAY TO MINUTE|INTERVAL DAY TO SECOND|INTERVAL HOUR|INTERVAL HOUR TO MINUTE|INTERVAL HOUR TO SECOND|INTERVAL MINUTE|INTERVAL MINUTE TO SECOND|INTERVAL SECOND|TIME WITH TIME ZONE|TIMESTAMP WITH TIME ZONE|TIMESTAMP WITH LOCAL TIME ZONE|TEXT|LONGVARCHAR|IMAGE|LONGVARBINARY|BLOB|CLOB")
            GUICtrlSetData($db_version,"6|7")
            GUICtrlSetData($db_lable,"创建DaMeng数据库的SQL语句.")
        Case StringCompare($client_name,"KingBase") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($db_version,"7")
        Case StringCompare($client_name,"GBase") == 0
            GUICtrlSetData($db_lable,"")
            GUICtrlSetData($db_version,"")
            GUICtrlSetData($table_name,"")
            GUICtrlSetData($row_name,"")
            GUICtrlSetData($row_type,"")
            GUICtrlSetData($row_len,"")
            GUICtrlSetData($db_version,"8")
            GUICtrlSetData($db_lable,"创建GBase数据库的SQL语句.")
    EndSwitch
EndFunc


Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            Exit
        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
    EndSelect
EndFunc

Func _execute_now()
    GUICtrlSetState($table_name,$GUI_ENABLE)
    GUICtrlSetData($table_name,"")
    GUICtrlSetData($row_name,"")
    GUICtrlSetData($row_type,"")
    GUICtrlSetData($row_len,"")
    
    $time = @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & @MIN & @SEC
    $client_name = StringStripWS(GUICtrlRead($db_comb),8)
    If StringLen($client_name) == 0 Then
        MsgBox(0,"INFO","需要一个数据库类型才能执行!")
        Return
    EndIf
    $ii_num = StringStripWS(GUICtrlRead($i_num),8)
    If Not StringRegExp($ii_num,'^[0-9]{1,}',0) Then
        MsgBox(0,"INFO","填入数据非数字,请检查!")
        Return
    EndIf
    $uu_num = StringStripWS(GUICtrlRead($u_num),8)
    If Not StringRegExp($uu_num,'^[0-9]{1,}',0) Then
        MsgBox(0,"INFO","填入数据非数字,请检查!")
        Return
    EndIf
    $dd_num = StringStripWS(GUICtrlRead($d_num),8)
    If Not StringRegExp($dd_num,'^[0-9]{1,}',0) Then
        MsgBox(0,"INFO","填入数据非数字,请检查!")
        Return
    EndIf
    $ss_num = StringStripWS(GUICtrlRead($s_num),8)
    If Not StringRegExp($ss_num,'^[0-9]{1,}',0) Then
        MsgBox(0,"INFO","填入数据非数字,请检查!")
        Return
    EndIf
    
    $client_version = StringStripWS(GUICtrlRead($db_version),8)
    Switch $client_name
        Case StringCompare($client_name,"Oracle") == 0
            _Oracle_do()
            MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"SQLServer") == 0
            _SQLServer_do()
            MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"MySQL") == 0
            _MySQL_do()
            MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"DB2") == 0
            _DB2_do()
            MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"Infomix") == 0
;~             _Infomix_do()
;~             MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"Sybase") == 0
;~             _Sybase_do()
;~             MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"Teradata") == 0
;~             _Teradata_do()
;~             MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"Netezza") == 0
;~             _Netezza_do()
;~             MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"Postgres") == 0
;~             _Postgres_do()
;~             MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"DaMeng") == 0
            _DaMeng_do()
            MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"KingBase") == 0
;~             _KingBase_do()
;~             MsgBox(0,"INFO","OK!生成语句成功!")
        Case StringCompare($client_name,"GBase") == 0
            _GBase_do()
            MsgBox(0,"INFO","OK!生成语句成功!")
    EndSwitch
    $sql_num = 1
EndFunc

Func _write_sql($msg)
    $client_name = StringStripWS(GUICtrlRead($db_comb),8)
    $client_version = StringStripWS(GUICtrlRead($db_version),8)
    $file = @ScriptDir & "\" & $client_name & "_" & $client_version & "_" & $time & ".sql"
    FileWriteLine($file,$msg)
EndFunc

Func _DaMeng_do()
    
EndFunc

Func _SQLServer_do()
    
EndFunc

Func _GBase_do()
    
EndFunc

Func _Oracle_do()
    
EndFunc

Func _MySQL_do()
    
EndFunc

Func _DB2_do()
    
EndFunc

;创建一个随机的数字字符串
Func _num_create($n)
    Local $char[10] = ['1','2','3','4','5','6','7','8','9','0']
    Local $str = "",$num
    For $i = 0 To $n - 1
        If $i == 0 Then
            $num = Random(0,8,1)
        Else
            $num = Random(0,9,1)
        EndIf
        $str = $str & $char[$num]
    Next
    Return Number($str)
EndFunc
;创建一个包含数字字母的随机字符串
Func _string_create($n)
    Dim $char[62] = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    Local $str = ""
    For $i = 0 To $n - 1
        $num = Random(0,61,1)
        $str = $str & $char[$num]
    Next
    Return $str
EndFunc
;创建一个字母的字符串
Func _chara_create($n)
    Dim $char[52] = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    Local $str = ""
    For $i = 0 To $n - 1
        $num = Random(0,51,1)
        $str = $str & $char[$num]
    Next
    Return $str
EndFunc
;创建一个中文的随机短语
Func _chinese_create($n)
    Dim $char[22] = ['人总是要有梦想的,万一哪天实现了呢?','没有正义,就没有和平。','我是白领:今天领了薪水,交了房租水电,买了油米泡面,摸了口袋,感叹一声,这个月工资又白领了','心里放不过自己,是没有智慧;心里放不过别人,是没有慈悲。','要替别人着想,但为自己而活。','有空学风水去,死后占个好墓也算弥补了生前买不起好房的遗憾。','有没有那么一瞬间,沵也会想起涐。','怀才就象怀孕,时间久了会让人看出来。','看别人不顺眼,是自己修养不够。','没有比脚更远的路,没有比人更高的山!','谈恋爱就像剥洋葱,总有一层会让你流泪。','人生两大悲剧:一是万念俱灰,一是踌躇满志。','人之所以活得累,是因为放不下架子,撕不开面子,解不开情节。','不是人人都能活的低调,可以低调的基础是随时都能高调。','有人能让你痛苦,说明你的修行还不够。','让未来到来,让过去过去。','如果说我懂的道理比别人多一点,那是因为我犯的错误比别人多一点。','痛苦源于欲望。','废话是人际关系的第一句。','世界上只有想不通的人,没有走不通的路。','人,长得漂亮不如活的漂亮。','真坏人并不可怕,可怕的是假好人。']
    Local $str = ""
    For $i = 0 To $n - 1
        $num = Random(0,21,1)
        $str = $str & $char[$num]
    Next
    Return $str
EndFunc

热门文章

随机推荐