Editor extensions for SQL Developer - plugins

I'm attempting to write an (editor) extension for Oracle SQL Developer. I'm having trouble finding information about it, but I've pieced the following together from the few tutorials and blog entries that I've found:
<?xml version="1.0" encoding="UTF-8"?>
<displays>
<display enable="true" objectType="SEQUENCE" style="null" type="editor">
<name><![CDATA[Details]]></name>
<query>
<sql>
<![CDATA[
SELECT 'sequence_name' AS "NAME", sequence_name AS "VALUE" FROM :NAME
UNION
SELECT 'last_value' AS "NAME", CAST(last_value AS text) AS "VALUE" FROM :NAME
UNION
SELECT 'start_value' AS "NAME", CAST(start_value AS text) AS "VALUE" FROM :NAME
UNION
SELECT 'increment_by' AS "NAME", CAST(increment_by AS text) AS "VALUE" FROM :NAME
UNION
SELECT 'max_value' AS "NAME", CAST(max_value AS text) AS "VALUE" FROM :NAME
UNION
SELECT 'min_value' AS "NAME", CAST(min_value AS text) AS "VALUE" FROM :NAME
UNION
SELECT 'cache_value' AS "NAME", CAST(cache_value AS text) AS "VALUE" FROM :NAME
UNION
SELECT 'log_cnt' AS "NAME", CAST(log_cnt AS text) "VALUE" FROM :NAME
UNION
SELECT 'is_cycled' AS "NAME", CAST(is_cycled AS text) AS "VALUE" FROM :NAME
UNION
SELECT 'is_called' AS "NAME", CAST(is_called AS text) AS "VALUE" FROM :NAME
]]>
</sql>
</query>
<CustomValues>
<TYPE>vertical</TYPE>
</CustomValues>
</display>
</displays>
This works, in that extra tabs do show up for sequences on Oracle db connections. However, they do not show up for Postgres db connections. For sequences to even show in pg databases in SQL Dev, I have to use a second extension script:
<?xml version="1.0" encoding="windows-1252" ?>
<navigator RESOURCE_FILE="oracle.dbtools.raptor.navigator.OracleNavigatorResource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="navigator.xsd">
<objectType connType="PostgreSQL" id="SEQUENCE" includesSyns="true" weight="100.0">
<folder>
<icon RSKEY="SEQUENCE_FOLDER_ICON"/>
<label RSKEY="Sequences"/>
<queries>
<query minversion="8">
<sql constrained="true"><![CDATA[SELECT relname, nspname FROM pg_class JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace WHERE relkind = 'S' AND nspname = :SCHEMA]]></sql>
</query>
<columns>
<column filterable="true" id="SEQUENCENAME" sortable="true">
<colName><![CDATA[relname]]></colName>
</column>
<column filterable="true" id="NAME" sortable="true">
<colName><![CDATA[relname]]></colName>
</column>
</columns>
</queries>
</folder>
<node>
<icon RSKEY="OracleIcons.SEQUENCE"/>
</node>
</objectType>
</navigator>
Is there a way to specify that this extension only applies to Postgres sequences, and not to Oracle sequences?

The correct answer here seems to be to have a connType="PostgreSQL" attribute in the display element. This limits it to only Postgres sequences. This was trial and error, I can't find editor.xsd and it seems like Oracle doesn't make it available.

Related

Variable in comma separated in the IN CLAUSE of SP of DB2 for Z/OS

I have a SP in which I have a In clause like mentioned below
value1 is a Int,Variable1 is varchar
Suppose SP started
Variable1=(value1,value2,value3)--getting from another table
Select * from tableA where Column1 in (Variable1).
The just above statement is not working ,needed a work around for this ,Please help
Here is a small example of using an XML list to select an arbitrary number of values:
SELECT * FROM "tableA"
WHERE "Column1" IN (
SELECT * FROM
XMLTABLE('$X/set/row' PASSING XMLPARSE('
<set>
<row item="1"/>
<row item="2"/>
<row item="3"/>
<!-- add as many "row" as you need here -->
</set>
')
AS "X"
COLUMNS
"item" INT PATH '#item'
) AS X
)
Then you can parameterize the query, where ? is a parameter of type XML
SELECT * FROM "tableA"
WHERE "Column1" IN (
SELECT * FROM
XMLTABLE('$X/set/row' PASSING ? AS "X" COLUMNS "item" INT PATH '#item') AS X
)

XQuery modify with variable

I have the follow XML
<root>
<business name="LM" id="1" total_pes="0">
</business>
<business name="KO" id="354" total_pes="0">
</business>
<business name="TUI" id="889" total_pes="0">
</business>
</root>
I want to update the total_pes attribute with a record count of other table tbl_logs:
id | log
1 | A
1 | A
1 | A
354 | A
354 | A
889 | A
My output XML would be this:
<root>
<business name="LM" id="1" total_pes="3">
</business>
<business name="KO" id="354" total_pes="2">
</business>
<business name="TUI" id="889" total_pes="1">
</business>
</root>
This is what I already done:
DECLARE #total_pes_new int
DECLARE #ID INT
SET #ID = (SELECT TOP 1 ID FROM #IDS)
WHILE #ID IS NOT NULL
BEGIN
set #total_pes_new = ( SELECT COUNT(A.PES) FROM TBL A
WHERE A.ID = #ID)
SET #XML.modify('replace value of (/root/business[#id=sql:variable("#ID")]/#total_pes)[1] with sql:variable("#total_pes_new")')
SET #ID = (SELECT TOP 1 ID FROM #IDS WHERE ID > #ID)
END
I will have problems with this loop. Can anyone help me to do a better solution?
Tks
If your XML is in an XML database, then you also have the option of using XQuery Update, see http://www.w3.org/TR/xquery-update-10/.

How can I convert this T-SQL to Fetch-XML?

I want to use this SQL in a Dynamics CRM report. I can't work out how to convert it to Fetch-XML.
select p.ProductNumber "Plan Number",p.Name,p.price "Monthly Rate",count(*) "Group", '0' "Direct Debit"
from contact c,product p
where c.integ_schemeid = p.ProductId
and c.ParentCustomerId is not null
group by p.ProductNumber,p.Name,p.price
union
select p.ProductNumber "Plan Number",p.Name,p.price "Monthly Rate", '0' "Group", count(*) "Direct Debit"
from contact c,product p
where c.integ_schemeid = p.ProductId
and c.ParentCustomerId is null
group by p.ProductNumber,p.Name,p.price
http://www.sql2fetchxml.com fails on this occasion.
You can't convert that query to FetchXML, because unions aren't supported. What you'll need to do is look into merging the queries into 1 if possible. Even if it means duplicating columns and using conditional statements within your report to display the relevant data instead. For example, if you simplified the query to this:
select p.ProductNumber "Plan Number", p.Name, p.price, c.ParentCustomerId
from product p
inner join contact c on c.integ_schemeid = p.ProductId
This can be converted to the following fetchxml:
<fetch mapping="logical">
<entity name="product">
<attribute name="ProductNumber" alias="Plan Number" />
<attribute name="Name" />
<attribute name="price" />
<link-entity name="contact" to="ProductId" from="integ_schemeid" alias="c" link-type="inner">
<attribute name="ParentCustomerId" />
</link-entity>
</entity>
</fetch>
Then, because you have all of the data in 1 dataset including contacts with null ParentCustomerIds, you can group within your report instead of within the query.

How to use SQL XML and XMLAGG to return multiple xmlelements

In SQL, I need to create xml code that looks like this:
<Phone>
<PhoneTypeCode tc="12">Mobile</PhoneTypeCode>
<Area>801</Area>
<DialNumber>9996666</DialNumber>
</Phone>
<Phone>
<PhoneTypeCode tc="2">Business</PhoneTypeCode>
<Area>801</Area>
<DialNumber>1113333</DialNumber>
</Phone>
using xmlagg, but it is throwing an error on the ',' after p.desc
How does this IBM DB2 SQL function need to be fixed to achieve the above xml?
select
xmlelement(Name "Phone",
xmlagg(xmlelement(name "PhoneTypeCode",
xmlattributes(trim(p.phtype) as "tc"), trim(p.desc)),
xmlelement(name "AreaCode", p.area),
xmlelement(name "DialNumber", p.phone)
)
) as xml
from phone p
where p.entityid = #entity_id
I also wanted to add that it does compile and run with this:
select
xmlelement(Name "Phone",
xmlagg(xmlelement(name "PhoneTypeCode",
xmlattributes(trim(p.phtype) as "tc"), trim(p.desc))
)
) as xml
from phone p
where p.entityid = #entity_id
Here is what it returns:
<Phone>
<PhoneTypeCode tc="12">Mobile</PhoneTypeCode>
<PhoneTypeCode tc="2">Business</PhoneTypeCode>
</Phone>
But of course, I need the Area and DialNumber. It is as if you can't have more than one xmlelement within an xmlagg.
How does this IBM DB2 SQL function need to be fixed to achieve the above xml?
Firstly, you may want to count your parentheses. Normally one would want as many closing parentheses as there are opening parentheses.
Secondly, you don't need XMLAGG() at all. You'd use it when inserting multiple elements of the same type, based on multiple relational records, into a single outer element, like
<phones>
<phone no="1" .../>
<phone no="2" .../>
...
</phones>
For you something like this should work:
with phone (phtype, desc, area, phone) as
(values ('home','blah','555','555-5555'),('office','blah','555','555-1111'))
select
xmlelement(
Name "Phone",
xmlelement(
name "PhoneTypeCode",
xmlattributes(
trim(p.phtype) as "tc"
),
trim(p.desc)
),
xmlelement(name "AreaCode", p.area),
xmlelement(name "DialNumber", p.phone)
) as xml
from phone p

Generate XML from Table

I have a table like this:
ID Name Column1 Column2 Column3
1 ABC 202.2 1500 34000
2 IJK 104 10000 27000
I want to generate XML like this:
<doc>
<record ID="1" Name="ABC" Column1="202.2" Column2="15000" Column3="34000" />
<record ID="2" Name="IJK" Column1="104" Column2="10000" Column3="27000" />
</doc>
I have got some clue from this forum post and used this code:
CREATE TABLE #tmp (column1 VARCHAR(20), column2 VARCHAR(20), column3 VARCHAR(20))
INSERT INTO #tmp VALUES ( 'data1', 'data2', 'data3' )
INSERT INTO #tmp VALUES ( 'data11', 'data21', 'data31' )
-- FOR XML PATH with ELEMENTS will automatically unpivot the data for you
-- Then reshape your XML using nested FLWOR loops
SELECT
(
SELECT *
FROM #tmp t
FOR XML PATH, ELEMENTS, TYPE
).query('
for $e in row
return
<row>{
for $f in $e/*
return <field name="{local-name($f)}">{data($f)}</field>
}
</row>
')
I tried the following modified version:
SELECT (SELECT * FROM cte_temp t FOR XML PATH, ELEMENTS, TYPE)
.query('for $e in row return
<doc>
{
for $f in $e return <record {local-name($f)}="{data($f)}" />
}
</doc>')
But I'm getting error:
XQuery [query()]: Invalid source character 0x7b found in an identifier
near 'return'.
Why you trying to get fancy.
Select * from #tmp as record
FOR XML AUTO, root('doc')