Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #SingleInstance,force
- ;#Persistent
- global GUIW:=510
- global GUIH:=490
- global nDisplacement=200
- global bInputShown:=true
- global bCMDOutputShown:=false
- global bTableListShown:=false
- global bTableListOnColumn:=true
- global BackupList
- global SearchString
- gui,INPUT:new,,SQL Input
- gui,INPUT:default
- gui,INPUT:font,,Courier
- Gui,INPUT:Add,Edit,vSearchString gRunSearch -WantReturn -multi
- Gui,INPUT:Add,Button,gCancelSearch vCancelSearch,X
- Gui,Input:Add,ListView,vTableList gTableList +NoSort,Table
- UpdateTableList()
- Gui,INPUT:Add,Edit,multi -wrap x0 y0 w500 r15 vSQLSCRIPT,
- Gui,INPUT:Add,Button,x0 vRUNSQL gRUNSQL,Run SQL Script
- Gui,INPUT:Add,Button,x0 vHideTableList gHideTableList,<
- Gui,INPUT:Add,Button,x0 vHideScript gHideScript,^
- Gui,INPUT:Add,Text,x0 vRowsE,Awaiting Command
- Gui,Input:Add,ListView,vSQLLIST +NoSort gColumnSelect x0,
- Gui,INPUT:Add,Edit,vCMDOutput +ReadOnly
- Gui,INPUT:Add,Button,vHideOutput gHideOutput,X
- GuiControl,Hide,CMDOutput
- GuiControl,Hide,HideOutput
- GUI,INPUT:+Resize +LabelINPUT
- gui,INPUT:show,W%GUIW% H%GUIH%
- GuiControl,Focus,SQLSCRIPT
- OnMessage(0x0202,"DragRelease")
- OnMessage(0x0100,"KeyPressed")
- return
- KeyPressed(wParam)
- {
- ;if (wParam=0x0D) and (A_GuiControl="TableList")
- ;msgbox,YEAH!!!
- }
- DragRelease()
- {
- Gui,INPUT:ListView,TableList
- if (A_GuiControl="SQLSCRIPT") and CopyRow
- {
- tempClip:=Clipboard
- GuiControl,Focus,SQLSCRIPT
- LV_GetText(sTable,CopyRow,1)
- Clipboard:=sTable
- send,^v
- sleep,10
- Clipboard:=tempClip
- }
- CopyRow=
- }
- CancelSearch:
- ClearSearch()
- Return
- ClearSearch()
- {
- if (SearchString="") and bTableListOnColumn
- {
- UpdateTableList()
- }
- else if (SearchString<>"")
- {
- if BackupList
- TableViewContents:=BackupList
- GuiControl,,SearchString,
- ShowTableList(TableViewContents)
- }
- }
- RunSearch:
- Gui,INPUT:Submit,NoHide
- Gui,INPUT:ListView,TableList
- TableViewContents:=[BackupList[1]]
- loop,% BackupList.MaxIndex()-1
- if RegExMatch(BackupList[A_Index+1],"i)" . SearchString)
- {
- TableViewContents.Insert(BackupList[A_Index+1])
- }
- ShowTableList(TableViewContents)
- Return
- TableList:
- Gui,ListView,TableList
- if A_GuiEvent=DoubleClick
- {
- LV_GetText(sTable,A_EventInfo,1)
- UpdateTableList(sTable)
- }
- else if A_GuiEvent=D
- global CopyRow:=A_EventInfo
- ;else
- ;ToolTip,% A_GuiEvent . " - " . A_EventInfo
- return
- UpdateTableList(sTable:="")
- {
- Gui,ListView,TableList
- If bTableListOnColumn
- {
- sOutput:=RegExReplace(RunCMDSQL("select name from sys.tables order by name"),"^[^`n`r]+[`n`r]+[^`n`r]+[`n`r]+","",,1)
- global TableViewContents=["Tables"]
- bTableListOnColumn:=false
- }
- else
- {
- sCMD:="select COL.name from sys.columns as COL inner join sys.tables as TAB on COL.object_id=TAB.object_id where TAB.name='" . RegExReplace(sTable,"\R","") . "' order by COL.name"
- sOutput:=RegExReplace(RunCMDSQL(sCMD),"^[^`n`r]+[`n`r]+[^`n`r]+[`n`r]+","",,1)
- global TableViewContents=["Columns in " . sTable]
- bTableListOnColumn:=true
- }
- sOutput:=RegExReplace(sOutput,"\R\(\d+ rows affected\)","")
- atPos:=1
- while true
- {
- sMatch:=RegExReplace(sOutput,"sUD)^([^\n]+)\s*`n.*$","$1",,1)
- sOutput:=RegExReplace(sOutput,"U)^([^\n]+)\s*`n","",,1)
- if not sMatch
- break
- TableViewContents.Insert(sMatch)
- }
- ;GuiControl,,SearchString,
- BackupList:=TableViewContents
- ShowTableList(TableViewContents)
- LV_ModifyCol(1,"AutoHdr")
- }
- ShowTableList(tList)
- {
- Gui,ListView,TableList
- LV_Delete()
- while LV_DeleteCol(1)
- {
- }
- LV_InsertCol(,"",tList[1])
- loop,% tList.MaxIndex()-1
- {
- if (RegExMatch(tList[A_Index+1],"^\R*$")=0)
- {
- ;if bTableListOnColumn
- ;msgbox,% A_Index . "/" . tList.MaxIndex() . " = " . tList[A_Index+1]
- LV_Add("",tList[A_Index+1])
- }
- if (tList.MaxIndex()-A_Index-1)=0
- break
- }
- }
- INPUTClose:
- If Clipboard=%strOutput%
- if bCMDOutputShown
- Clipboard:=tempClip
- ExitApp
- return
- INPUTSize:
- if ErrorLevel=1
- return
- global GUIW:=A_GuiWidth
- global GUIH:=A_GuiHeight
- InputResize()
- return
- RunCMDSQL(sCMD)
- {
- IfExist,SQLOUTPUT
- FileDelete,SQLOUTPUT
- RunWait,% ComSpec " /C sqlcmd -s ^| -U aura -P pos -d bbsql -Q """ . sCMD . """>SQLOUTPUT",,hide
- FileRead,sOutput,SQLOUTPUT
- FileDelete,SQLOUTPUT
- return sOutput
- }
- InputResize()
- {
- GUIW:=GUIW-(bTableListShown ? nDisplacement : 0)
- SearchStringX:=0
- SearchStringY:=0
- SearchStringW:=(bTableListShown ? nDisplacement-25 : 0)
- SearchStringH:=25
- CancelSearchX:=SearchStringW
- CancelSearchY:=0
- CancelSearchW:=25
- CancelSearchH:=25
- TableListX:=0
- TableListY:=SearchStringH
- TableListW:=(bTableListShown ? nDisplacement : 0)
- TableListH:=GUIH-TableListY
- SQLSCRIPTX:=(bTableListShown ? nDisplacement : 0)
- SQLSCRIPTY:=0
- SQLSCRIPTW:=GUIW
- SQLSCRIPTH:=(bInputShown ? GUIH/2-25 : 0)
- RUNSQLX:=(bTableListShown ? nDisplacement : 0)
- RUNSQLY:=SQLSCRIPTH
- RUNSQLW:=GUIW
- RUNSQLH:=(bInputShown ? 25 : 0)
- HideTableListX:=(bTableListShown ? nDisplacement : 0)
- HideTableListY:=RUNSQLY+RUNSQLH
- HideTableListW:=25
- HideTableListH:=25
- HideScriptX:=HideTableListW+(bTableListShown ? nDisplacement : 0)
- HideScriptY:=HideTableListY
- HideScriptW:=GUIW-HideTableListW
- HideScriptH:=25
- RowsEX:=(bTableListShown ? nDisplacement : 0)
- RowsEY:=HideScriptY+HideScriptH
- RowsEW:=GUIW
- RowsEH:=15
- SQLLISTX:=(bTableListShown ? nDisplacement : 0)
- SQLLISTY:=RowsEY+RowsEH
- SQLLISTW:=GUIW
- SQLLISTH:=GUIH-SQLLISTY-(bCMDOutputShown ? 25 : 0)
- CMDOutputX:=(bTableListShown ? nDisplacement : 0)
- CMDOutputY:=SQLLISTY+SQLLISTH
- CMDOutputW:=GUIW-25
- CMDOutputH:=25
- HideOutputX:=CMDOutputX+CMDOutputW
- HideOutputY:=CMDOutputY
- HideOutputW:=25
- HideOutputH:=25
- GuiControl,Move,SearchString,X%SearchStringX% Y%SearchStringY% W%SearchStringW% H%SearchStringH%
- GuiControl,Move,CancelSearch,X%CancelSearchX% Y%CancelSearchY% W%CancelSearchW% H%CancelSearchH%
- GuiControl,Move,TableList,X%TableListX% Y%TableListY% W%TableListW% H%TableListH%
- GuiControl,Move,SQLSCRIPT,X%SQLSCRIPTX% Y%SQLSCRIPTY% W%SQLSCRIPTW% H%SQLSCRIPTH%
- GuiControl,Move,RUNSQL,X%RUNSQLX% Y%RUNSQLY% W%RUNSQLW% H%RUNSQLH%
- GuiControl,MoveDraw,HideTableList,X%HideTableListX% Y%HideTableListY% W%HideTableListW% H%HideTableListH%
- GuiControl,MoveDraw,HideScript,X%HideScriptX% Y%HideScriptY% W%HideScriptW% H%HideScriptH%
- GuiControl,MoveDraw,RowsE,X%RowsEX% Y%RowsEY% W%RowsEW% H%RowsEH%
- GuiControl,MoveDraw,SQLLIST,X%SQLLISTX% Y%SQLLISTY% W%SQLLISTW% H%SQLLISTH%
- if bCMDOutputShown
- {
- GuiControl,MoveDraw,CMDOutput,X%CMDOutputX% Y%CMDOutputY% W%CMDOutputW% H%CMDOutputH%
- GuiControl,MoveDraw,HideOutput,X%HideOutputX% Y%HideOutputY% W%HideOutputW% H%HideOutputH%
- }
- GUIW:=GUIW+(bTableListShown ? nDisplacement : 0)
- }
- ColumnSelect:
- Gui,ListView,SQLLIST
- if A_GuiEvent=DoubleClick
- {
- global strOutput
- global RowSelected:=A_EventInfo
- LV_GetText(strOutput,RowSelected,1)
- while true
- {
- LV_GetText(ColOutput,RowSelected,A_Index+1)
- if ColOutput=
- break
- strOutput:=strOutput . ", " . ColOutput
- }
- GuiControl,,CMDOutput,% strOutput
- if not bCMDOutputShown
- global tempClip:=Clipboard
- bCMDOutputShown:=true
- Clipboard:=strOutput
- GuiControl,Show,CMDOutput
- GuiControl,Show,HideOutput
- InputResize()
- }
- else if A_GuiEvent=ColClick
- {
- if bCMDOutputShown
- {
- LV_GetText(strOutput,RowSelected,A_EventInfo)
- GuiControl,,CMDOutput,% strOutput
- Clipboard:=strOutput
- }
- }
- Return
- HideOutput:
- bCMDOutputShown:=false
- GuiControl,Hide,CMDOutput
- GuiControl,Hide,HideOutput
- If Clipboard=%strOutput%
- Clipboard:=tempClip
- InputResize()
- Return
- FormatSQL(ByRef sOutput,ByRef sRowsEffected,ByRef sColumns,ByRef isError)
- {
- sOutput:=RegExReplace(sOutput,"\R`t","")
- sOutput:=RegExReplace(sOutput,"\s*([\|`n])\s*","$1")
- isError:=RegExMatch(sOutput,"^Msg \d+, Level \d+, State \d+, Server ")
- if isError
- return false
- sColumns:=RegExReplace(sOutput,"sxU)`n(.*)`n\-.*?","$1")
- sOutput:=RegExReplace(sOutput,"sx)^.*?\-[\-\|]*","",,1)
- sRowsEffected:=RegExReplace(sOutput,".*(\(\d+ rows affected\)).*","$1")
- sOutput:=RegExReplace(sOutput,"\R\(\d+ rows affected\)","")
- }
- clear()
- {
- LV_Delete()
- while LV_DeleteCol(1)
- {
- }
- }
- RUNSQL:
- gui,INPUT:Submit,nohide
- Gui,Listview,SQLLIST
- sOutput:=RunCMDSQL(RegExReplace(SQLSCRIPT,"\R"," "))
- FormatSQL(sOutput,sRowsEffected,sColumns,isError)
- nPos:=1
- clear()
- if (isError=0)
- {
- RegExReplace(sColumns,"\|","",nCount)
- nCount:=nCount+1
- if (RegExMatch(sColumns,"[^\|`n]")<>0)
- {
- while (sColumns<>"")
- {
- RegExMatch(sColumns,"^([^\|`n]*)",sColTitle)
- sColumns:=RegExReplace(sColumns,"^([^\|`n]*[\|`n])","",,1)
- LV_InsertCol(A_Index,,sColTitle)
- }
- }
- else
- {
- loop,% nCount
- {
- LV_InsertCol(A_Index,,"")
- }
- }
- while (sOutput<>"")
- {
- oRow:=[]
- RegExMatch(sOutput,"^(.+?)`n",sRow)
- sOutput:=RegExReplace(sOutput,"^.+?`n","",,1)
- while (sRow<>"")
- {
- sMatch:=RegExReplace(sRow,"^(.+?)[`n\|].*$","$1")
- if not sMatch
- break
- oRow.Insert(sMatch)
- sRow:=RegExReplace(sRow,"^.+?[`n\|]","",,1)
- }
- LV_Add("",oRow*)
- }
- loop,% nCount
- {
- LV_ModifyCol(A_Index,"AutoHdr")
- }
- GuiControl,,RowsE,% sRowsEffected
- }
- else
- {
- GuiControl,,RowsE,% RegExReplace(sOutput,"`n"," | ")
- }
- return
- HideScript:
- if bInputShown
- {
- bInputShown:=false
- GuiControl,,HideScript,v
- GuiControl,Hide,SQLSCRIPT
- GuiControl,Hide,RUNSQL
- }
- else
- {
- bInputShown:=true
- GuiControl,,HideScript,^
- GuiControl,Show,SQLSCRIPT
- GuiControl,Show,RUNSQL
- }
- InputResize()
- Return
- HideTableList:
- Gui,INPUT:+LastFound
- WinGetPos,WinX,WinY
- WinGet,isMax,MinMax
- If bTableListShown
- {
- GuiControl,,HideTableList,% "<"
- bTableListShown:=false
- WinX:=WinX+nDisplacement
- GUIW:=GUIW-nDisplacement
- if isMax<>1
- gui,INPUT:show,X%WinX% W%GUIW%
- InputResize()
- }
- else
- {
- GuiControl,,HideTableList,% ">"
- bTableListShown:=true
- WinX:=WinX-nDisplacement
- GUIW:=GUIW+nDisplacement
- if isMax<>1
- gui,INPUT:show,X%WinX% W%GUIW%
- InputResize()
- }
- return
Advertisement
Add Comment
Please, Sign In to add comment