ScottMattes
Member³
Has anyone done anything with the LuaPlSql plugin?
There doesn't seem to be any 'how to' that comes with it.
There doesn't seem to be any 'how to' that comes with it.
local menuItem = AddMenu("Lua / Util / Test1", Test1)
-- trying to figure out if LUA is useful for me
local function Test1()
-- this one works, always show so that i know plugin still working
msg = SYS.TNSNAMES()
plsql.ShowMessage(msg)
-- this demos literals
-- msg = "Hello World"
-- plsql.ShowMessage("Hello World")
-- these demo functions, uncomment the trailing ShowMessage to see results
-- msg = SYS.OracleHome()
-- msg = IDE.GetBrowserItems("TABLES",1)
-- msg = IDE.GetWindowCount()
-- which = IDE.Connected()
-- msg = IDE.GetCursorWord()
-- msg = SQL.Execute("select count(*) from dual")
-- msg = IDE.SetStatusMessage("hello worldie")
-- msg = SQL.ErrorMessage()
-- plsql.ShowMessage(msg)
-- these demo procedures
-- IDE.CreateWindow(2,"Hello World")
-- IDE.SetCursor(10,47)
-- this sets a boolean
-- msg = IDE.GetSelectedText()
-- if which
-- then
-- msg = "Connected"
-- else
-- msg = "Not Connected"
-- end
-- plsql.ShowMessage(msg)
-- these don't work for some reason
-- IDE.ShowDialog("BREAKPOINTS",test) -- does not work
-- msg = SYS.DelphiVersion() -- does not work
end
-- Menus
local menuItem1 = AddMenu("Lua / Util / Wrap program unit", WrapUnit)
local menuItem2 = AddMenu("Lua / Util / Open TNS file", OpenTNSFile)
local menuItem3 = AddMenu("Lua / Util / Test1", Test1)
--The variable to access to the new menu items list
local AddMenu = ...
--Shortcuts for easy code writing
local plsql = plsql
local SYS, IDE, SQL = plsql.sys, plsql.ide, plsql.sql
--Available callbacks
return {
OnActivate,
OnDeactivate,
CanClose,
AfterStart,
OnBrowserChange,
OnWindowChange,
OnWindowCreate,
OnWindowCreated,
OnWindowClose,
OnConnectionChange,
OnPopup,
OnMainMenu,
OnTemplate,
OnFileLoaded,
OnFileSaved,
About,
CommandLine,
RegisterExport,
ExportInit,
ExportFinished,
ExportPrepare,
ExportData
}
do
local function SomeFunction()
--something to do
end
AddMenu(SomeFunction, "&Lua / Some Function Name")
end
local function OnActivate()
--to do some initialization
end
local function GetTypeMethod(ObjectName,SubObject)
--Получение информации об объекте для которого вызван метод
local OldSubObject=SubObject
--Построение графического интерфейса
-- Creates a list and sets its items, dropdown and amount of visible items
local list= iup.list { dropdown="NO", visible_items=10,EDITBOX ="YES",size="300x200y"
,VISIBLELINES = 10, SCROLLBAR = "YES"
}
-- Creates frame with dropdown list and sets its title
local frm_list = iup.frame {list
; title = ObjectName}
-- Creates a dialog with the the frames with three lists and sets its title
local dlg = iup.dialog {iup.hbox {frm_list}
; title = "Select method"}
--Populate method list
local function RefreshList()
local nvI=0;
local sql = [[
select distinct lower(t.method_name)
from dba_type_methods t
where t.type_name=upper(']].. ObjectName ..[[')
and upper(t.method_name) like upper(']].. SubObject ..[[%')
order by lower(t.method_name)
]]
list[1]=nil
if SQL.Execute(sql) ~= 0 then
ShowMessage(SQL.ErrorMessage())
else
while true do
local svName = SQL.Field(1)
if SQL.Eof() then break end
nvI=nvI+1
list[nvI]=svName
SQL.Next()
end
end
--If the list contains only one item select one
if nvI==1 then
list["VALUE"]=string.lower(list[1])
list["CARET"]=string.len(list[1])+1
else
list["VALUE"]=string.lower(SubObject)
list["CARET"]=string.len(SubObject)+1
end
end
function list:k_any(k)
if k==iup.K_CR then
dlg:hide()
--delete old method imputed before
if OldSubObject ~= "" then
IDE.KeyPress(8,4)
--IDE.Refresh()
end
do
--Insert selected method
IDE.InsertText(list["VALUE"])
end
end
if k==iup.K_ESC then
--exit
dlg:hide()
end;
if k==iup.K_sCR then
--Refresh list by new filter
SubObject=list["VALUE"]
RefreshList()
end
return k
end
-- Show dialog
dlg:showxy(iup.CENTER, iup.CENTER)
RefreshList()
iup.MainLoop()
dlg:destroy()
end
local function AutoReplaceSTM()
--Get Type name and method
local nvX = IDE.GetCursorX()
local nvY =IDE.GetCursorY()
IDE.SetSelection(IDE.LineIndex(nvY-1),IDE.GetSelection())
local svText=IDE.GetSelectedText()
IDE.SetCursor(nvX,nvY)
local NewLine=lpeg.P("\r\n")
local Com1 = lpeg.P("--")*((lpeg.P(1)-NewLine)^0)*NewLine
local Com2 = lpeg.P("/*")* ( (lpeg.P(1)-lpeg.P("*/"))^0 ) *lpeg.P("*/")
local Space = lpeg.S(" \r\n\t:=[]()")+Com1+Com2
local Delim=lpeg.S(".")
local Any=(lpeg.P(1)-Space-Delim)
local Tp=lpeg.C(Any^0)*((Delim*lpeg.C(Any^0)))*(-lpeg.P(1))
local G=(Space^0*((Any+Delim)-Tp)*Space^0)^0*Space^0*Tp
local svRes=lpeg.match(lpeg.Ct(G),svText)
-- ShowMessage(svRes[1])
--ShowMessage(svRes[2])
if svRes==nil then
ShowMessage(svText)
else
local svON,svOS
if svRes[1]==nil then
svON=""
else
svON=svRes[1]
end
if svRes[2]==nil then
svON=""
else
svOS=svRes[2]
end
if svON~="" then
--Show dialog for selecting type method
GetTypeMethod(svON,svOS)
end
end
end
iup.key_open()
AddMenu(AutoReplaceSTM,"Lua / Edit / AutoReplace static type method")
end
-- Open AfterConnect File
do
local function OpenAfterConnectFile()
IDE.OpenFile(plsql.WindowType.SQL, SYS.RootDir() .. "\AfterConnect.sql")
end
AddMenu(OpenAfterConnectFile, "&Lua / Utilities / Open AfterConnect File")
end
-- Open Login File
do
local function OpenAfterConnectFile()
IDE.OpenFile(plsql.WindowType.SQL, SYS.RootDir() .. "\Login.sql")
end
AddMenu(OpenAfterConnectFile, "&Lua / Utilities / Open Login File")
end
do
local function Format()
local svCurFile=IDE.Filename()
local nvWT=IDE.GetWindowType()
if nvWT==plsql.WindowType["Test"] then
local svText=string.gsub(IDE.GetText(),"(\r\n)","\n")
local fl=io.open("c:\buf\reg\reg8.rg","w")
fl:write(svText)
fl:close()
os.execute("PlugIns\lua\ManUtl\\Format.ahk")
fl=io.open("c:\buf\reg\reg8.rg","r")
svText=fl:read("*a")
fl:close()
svText=string.gsub(svText,"(\n)","\r\n")
IDE.SetText(svText)
else
syslib.remove("c:\buf\reg\reg8.rg")
IDE.SetFilename("c:\buf\reg\reg8.rg")
IDE.SaveFile()
os.execute("PlugIns\lua\ManUtl\\Format.ahk")
IDE.ReloadFile()
IDE.SetFilename(svCurFile)
end
plsql.SetForegroundWindow(IDE.GetAppHandle())
end;
AddMenu(Format,"Lua / Edit / Format")
end;
; format.ahk
; AutoHotkey Version: 1.x
; Script Function:
; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;
f_regprefix=c:\buf\reg\reg
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
run, "C:\Program Files\Quest Software\SQL Navigator 5.5\FmtPlus.exe" %f_regprefix%8.rg
WinWaitActive Formatter Plus
SendPlay {alt}ef^s
UniqueFmtID := WinExist("A")
WinClose, ahk_id %UniqueFmtID%
WinWaitClose, ahk_id %UniqueFmtID%
static luaL_reg plsql_idelib[] = {
{"MenuState", plsql_ide_MenuState},
{"Connected", plsql_ide_Connected},
{"GetConnectionInfo", plsql_ide_GetConnectionInfo},
{"GetBrowserInfo", plsql_ide_GetBrowserInfo},
{"GetWindowType", plsql_ide_GetWindowType},
{"GetAppHandle", plsql_ide_GetAppHandle},
{"GetWindowHandle", plsql_ide_GetWindowHandle},
{"GetClientHandle", plsql_ide_GetClientHandle},
{"GetChildHandle", plsql_ide_GetChildHandle},
{"Refresh", plsql_ide_Refresh},
//and so on .....
{NULL, NULL}
};
local function OnTemplate(FileName, Data)
local ObjectType,ObjectOwner,ObjectName=IDE.GetWindowObject()
if ObjectType==nil then
ObjectType=""
end
if string.upper(ObjectType)=="TYPE BODY" or
string.upper(ObjectType)=="PACKAGE BODY" then
local Slash = lpeg.P("\")
local Any = (lpeg.P(1)-Slash)^0
local Path=(Any*Slash)^0
local Templ=Path*lpeg.C(Any)
--Get the name of the template
local svText=lpeg.match(Templ,FileName)
if string.upper(svText)==string.upper("header.tpl") and string.upper(ObjectType)=="TYPE BODY" then
return IDE.GetObjectSource("TYPE",string.upper(ObjectOwner),string.upper(ObjectName))
end
return true
end
return true
end;
do
local function Refresh()
local function RefreshSrc()
local ObjectType,ObjectOwner,ObjectName=IDE.GetWindowObject()
if string.len(ObjectName)>0 then
IDE.SetText(
IDE.GetObjectSource(
string.upper(ObjectType)
,string.upper(ObjectOwner),string.upper(ObjectName)
)
)
end
end;
local nvWT=IDE.GetWindowType()
if nvWT==plsql.WindowType["Procedure"] then
local nvX = IDE.GetCursorX()
local nvY =IDE.GetCursorY()
if string.len(IDE.TabInfo(0))>0 then
local nvTab=0
local nvCurTab=IDE.TabIndex(-1)
while string.len(IDE.TabInfo(nvTab))>0 do
IDE.TabIndex(nvTab)
RefreshSrc()
nvTab=nvTab+1
end
IDE.TabIndex(nvCurTab)
else
RefreshSrc()
end
IDE.SetCursor(nvX,nvY)
end
end
AddMenu(Refresh, "Lua / Edit / Refresh procedure")
end;
local plsql = plsql
local SYS, IDE, SQL = plsql.sys, plsql.ide, plsql.sql
require'lpeg'
local syslib = require("sys")
local function SetClipBoardText( spText)
plsql.SetClipboardText(spText)
end
local ShowMessage = plsql.ShowMessage
--Return text between branches
local function GetSubQuery()
--Get cursor coordinates
local nvX = IDE.GetCursorX()
local nvY =IDE.GetCursorY()
IDE.SetSelection(IDE.GetSelection(), -1)
--Recieve the text to be parsed
local svText=IDE.GetSelectedText()
--Retrun the cursor
IDE.SetCursor(nvX,nvY)
--lpeg gramma
local Space = lpeg.S(" \r\n\t")^0
local Open = "("
local Close = ")"
local NewLine=lpeg.S("\n")
local EmptyLine=((lpeg.S(" \t\r"))^0*lpeg.P("\n") )^0
local Com1 = lpeg.P("--")*((lpeg.P(1)-NewLine)^0)*NewLine
local Com2 = lpeg.P("/*")* ( (lpeg.P(1)-lpeg.P("*/"))^0 ) *lpeg.P("*/")
local Exp, Term = lpeg.V"Exp", lpeg.V"Term"
local Any= lpeg.V"Any"
local AnyR= lpeg.V"AnyR"
G = lpeg.P{ Exp,
Exp = Any*Open*EmptyLine*lpeg.C(Any*Term^0*Any)*Close*lpeg.P(1)^0;
Term = Any*Open*(Any*Term^0*Any)*Close*Any;
Any = ( Com1+Com2+( (lpeg.P(1)-Open-Close-lpeg.P("--")-lpeg.P("/*") )^1 ) )^0
}
--Get subquery
svText=lpeg.match(G,svText)
return svText
end
do
--Menu item for copy subquery
local function CopySubQuery()
local svText=GetSubQuery()
if svText==nil then
ShowMessage("Sub query is not found.")
else
SetClipBoardText(svText)
end
end
pvCopySubQuery=AddMenu(CopySubQuery, "Lua / Edit / Copy SubQuery")
end;
local syslib = require("sys")
require( "iuplua" )
local AddMenu = ...
local function AutoComplitStaticTypeMethod(ObjectName,SubObject)
--Better to put in into on Activate event
iup.key_open()
local OldSubObject=SubObject
-- Creates a list and sets its items, dropdown and amount of visible items
local list= iup.list { dropdown="NO", visible_items=10,EDITBOX ="YES",size="300x200y"
,VISIBLELINES = 10, SCROLLBAR = "YES"
}
-- Creates frame with dropdown list and sets its title
local frm_list = iup.frame {list
; title = string.upper(ObjectName) .. " (Shift+Enter - Refresh)"}
-- Creates a dialog with the the frames with three lists and sets its title
local dlg = iup.dialog {iup.hbox {frm_list}
; title = "Select method (Enter)"}
--Populate list by method
local function RefreshList()
local nvI=0;
local sql = [[
select distinct lower(t.method_name)
from dba_type_methods t
where t.type_name=upper(:ObjectName)
and upper(t.method_name) like upper(:SubObject||'%')
order by lower(t.method_name)
]]
SQL.SetVariable("ObjectName", ObjectName)
SQL.SetVariable("SubObject", SubObject)
list[1]=nil
if SQL.Execute(sql) ~= 0 then
ShowMessage(SQL.ErrorMessage())
else
while true do
local svName = SQL.Field(1)
if SQL.Eof() then break end
nvI=nvI+1
list[nvI]=svName
SQL.Next()
end
end
SQL.ClearVariables()
--If there is only one item put in into curent value
if nvI==1 then
list["VALUE"]=string.lower(list[1])
list["CARET"]=string.len(list[1])+1
else
list["VALUE"]=string.lower(SubObject)
list["CARET"]=string.len(SubObject)+1
end
end
--Key event
function list:k_any(k)
if k==iup.K_CR then
dlg:hide()
--Delete sumobject in editor
if OldSubObject ~= "" then
IDE.KeyPress(8,4)
--IDE.Refresh()
end
do
IDE.InsertText(list["VALUE"])
end
end
if k==iup.K_ESC then
--Exit
dlg:hide()
end;
if k==iup.K_sCR then
--Refresh
SubObject=list["VALUE"]
RefreshList()
end
return k
end
-- ShowDiolog
dlg:showxy(iup.CENTER, iup.CENTER)
--RefreshList
RefreshList()
--Message loop
iup.MainLoop()
dlg:destroy()
end
require 'iuplua'
btn = iup.button{title = "Click me!"}
function btn:action ()
iup.Message("Note","I have been clicked!")
return iup.DEFAULT
end
dlg = iup.dialog{btn; title="Simple Dialog",size="QUARTERxQUARTER"}
dlg:show()
iup.MainLoop()
-- Pl/Sql Developer Lua Plug-In Addon: Test Harness
-- Variables
local AddMenu = ...
local plsql = plsql
local SYS, IDE, SQL = plsql.sys, plsql.ide, plsql.sql
-- code to test goes below this
require 'iuplua'
do
local upMenuItem
local function btns()
btn = iup.button{title = "Click me!"}
function btn:action ()
iup.Message("Note","I have been clicked!")
return iup.DEFAULT
end
dlg = iup.dialog{btn; title="Simple Dialog",size="QUARTERxQUARTER"}
dlg:show()
iup.MainLoop()
end
upMenuItem = AddMenu(btns, "&Lua / Tests / Btns")
AddMenu(btns, "&Lua / Tests / Btns")
end
-- code to test goes above this
local function ShowFileCont(spFileName)
local hwnd=IDE.GetWindowHandle()
plsql.EnableWindow(hwnd,false);
...
dlg:show()
iup.MainLoop()
dlg:destroy()
...
plsql.EnableWindow(hwnd,true)
plsql.SetForegroundWindow(hwnd)
end