Oracle Apex Refreshing Chart is not working - sql

I have created a dialog page in my apex application. On this page a user is possible to select several filters for a chart. After clicking on a button the user is redirected to the previous page and the region which contains the chart is refreshed. The problem I have is that setting the filters doesn't work. After being redirected to the previous page the chart is being refreshed endless until an error occurs that says: Bad Gateway. So I don't see any result of the chart. Is it possible that the query of my chart is too complex? The dialog page contains four items of type shuttle and here is the query of my chart.
select COUNT(TRIGGER_TABLE.DATUM_UHRZEIT) as Anzahl_Trigger,
TEST.ID as ID
from BRIDGE_SYSTEM_TRIGGER, SYSTEM_TABLE, TRIGGER_TABLE, FAHRT, TEST, MITARBEITER
where BRIDGE_SYSTEM_TRIGGER.SYSTEM_TABLE_SYSTEM_ID = SYSTEM_TABLE.SYSTEM_ID
and BRIDGE_SYSTEM_TRIGGER.TRIGGER_TABLE_TRIGGER_ID = TRIGGER_TABLE.TRIGGER_ID
and TRIGGER_TABLE.FAHRT_FAHRT_ID = FAHRT.FAHRT_ID
and MITARBEITER.QNUMMER = FAHRT.MITARBEITER_QNUMMER
and FAHRT.TEST_ID = TEST_ID
and TRIGGER_TABLE.PRIORITAET = 0
or ((instr(':' || upper(:P26_Tests) || ':', upper(Test.Test_ID)) > 0)
or (instr(':' || upper(:P26_UEBERSYSTEM) || ':',':' || upper(system_table.system_name) ||':') > 0) or (instr(':' || upper(:P26_UNTERSYSTEM) || ':',':' || upper(system_table.system_name) ||':') > 0)
or (instr(':' || upper(:P26_COUNTRIES) || ':', upper(FAHRT.LAND)) > 0)
or (instr(':' || upper(:P26_REF) || ':', upper(Test.REF)) > 0)
or (instr(':' || upper(:P26_DRIVER) || ':', upper(MITARBEITER.QNUMMER)) > 0))
GROUP BY TEST.ID
ORDER BY TEST_ID;
The query doesn't throw any errors. Another problem I thought about is that something with the refresh is not correctly defined or not working.

I could solve my problem! There was no mistake in the refresh. Although I could not proove why the refresh was not loading, I think it was a runtime-issue because the query was too complex.
I changed the last five or-conditions of my query to and-clauses. After doing that the refresh works again.
select COUNT(TRIGGER_TABLE.DATUM_UHRZEIT) as Anzahl_Trigger,
TEST.ID as ID
from BRIDGE_SYSTEM_TRIGGER, SYSTEM_TABLE, TRIGGER_TABLE, FAHRT, TEST, MITARBEITER
where BRIDGE_SYSTEM_TRIGGER.SYSTEM_TABLE_SYSTEM_ID = SYSTEM_TABLE.SYSTEM_ID
and BRIDGE_SYSTEM_TRIGGER.TRIGGER_TABLE_TRIGGER_ID = TRIGGER_TABLE.TRIGGER_ID
and TRIGGER_TABLE.FAHRT_FAHRT_ID = FAHRT.FAHRT_ID
and MITARBEITER.QNUMMER = FAHRT.MITARBEITER_QNUMMER
and FAHRT.TEST_ID = TEST_ID
and TRIGGER_TABLE.PRIORITAET = 0
and ((instr(':' || upper(:P26_Tests) || ':', upper(Test.Test_ID)) > 0)
and (instr(':' || upper(:P26_UEBERSYSTEM) || ':',':' || upper(system_table.system_name) ||':') > 0) or (instr(':' || upper(:P26_UNTERSYSTEM) || ':',':' || upper(system_table.system_name) ||':') > 0)
and (instr(':' || upper(:P26_COUNTRIES) || ':', upper(FAHRT.LAND)) > 0)
and (instr(':' || upper(:P26_REF) || ':', upper(Test.REF)) > 0)
and (instr(':' || upper(:P26_DRIVER) || ':', upper(MITARBEITER.QNUMMER)) > 0))
GROUP BY TEST.ID
ORDER BY TEST_ID;
The query has definetely to look like that because I want to filter and that's why I need the and-condition. Each element has to be true, otherwise wrong results will appear.

Related

How to reference function in Node.js Postgres API Call

I have this function called pvt():
Create Or Replace Function pvt()
RETURNS void
LANGUAGE 'plpgsql'
AS '
declare
sqlColumn varchar;
qr varchar;
columnlist varchar;
Begin
sqlColumn= ''select distinct D.sys_cat from (select Row_Number() Over (Partition By Project,Date_ Order By System_) as sys_cat From your_table) D order by D.sys_cat;'';
qr=''prepare pvtstmt as Select D.Project,D.Date_,'';
For columnlist In EXECUTE sqlColumn
Loop
qr=qr || ''
Max(Case When sys_cat=''|| chr(39) || columnlist || chr(39) ||'' Then System_ ||'' ||chr(39)||''-''||chr(39)||''|| Result_ Else '' ||chr(39)||chr(39)||'' End) As System'' || columnlist || '' , '';
END LOOP;
qr=substr(qr, 0, length(qr) - 1);
qr=qr || ''From
(select *, Row_Number() Over (Partition By Project,Date_ Order By System_) as sys_cat From your_table) D Where D.Project='' || chr(39) || ''Proj1'' || chr(39) || ''Group By D.Project,D.Date_ Order By D.Project,D.Date_'';
Deallocate All;
EXECUTE qr;
End;
'
I need to execute these SQL statements:
Select pvt();
Execute pvtstmt;
Currently, I have a queries.js file where I have defined:
const getRunsByProjectTable = `
Select pvt();
Execute pvtstmt;
`
However, when I try to make the API call I do not receive any data back:
const getRunsByProjectTable = (req, res) => {
pool.query(queries.getRunsByProjectTable, (error, results) => {
if (error) throw error;
res.status(200).json(results.rows);
})
}
Does anyone know what the issue may be? Any help would be much appreciated!

DBMS_ASSERT.enquote_name() in Oracle

I have a sample code from http://phil-sqltips.blogspot.com/2015/07/beware-of-empty-partitions.html and I'd like to understand this xmlgen sql.
WITH t AS (
SELECT table_owner
, table_name
, partition_name
, TO_NUMBER (EXTRACTVALUE (XMLTYPE (DBMS_XMLGEN.getxml ('SELECT COUNT(*) AS rows_exist FROM '
|| DBMS_ASSERT.enquote_name (str => table_owner)
|| '.'
|| DBMS_ASSERT.enquote_name (str => table_name)
|| ' PARTITION ('
|| DBMS_ASSERT.enquote_name (str => partition_name)
|| ') WHERE ROWNUM <= 1'
)
)
, '/ROWSET/ROW/ROWS_EXIST'
)
) AS rows_exist
FROM all_tab_partitions
WHERE table_owner = 'WH'
AND table_name IN ('POINT_OF_SALE_FACTS')
ORDER BY table_owner
, table_name
, partition_position
)
SELECT 'ALTER TABLE '
|| DBMS_ASSERT.enquote_name (str => table_owner)
|| '.'
|| DBMS_ASSERT.enquote_name (str => table_name)
|| ' DROP PARTITION '
|| DBMS_ASSERT.enquote_name (str => partition_name)
|| ';' AS stmt
, t.*
FROM t
WHERE rows_exist = 0
;
I've found most of them except this,
DBMS_ASSERT.enquote_name (str => table_owner).
What is the syntax of arrow in (str => table_owner) inside of enquote_name?
I found this https://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_assert.htm#BABDEHBC but there is no such arrow syntax, and couldn't find web sites for this syntax.
That's the named parameter syntax. Here's the relevant documentation. So str => table_name says to set the str parameter to table_name.
In this case, it isn't necessary, since str is the first parameter for DBMS_ASSERT.ENQUOTE_NAME. So you could omit it and just call DBMS_ASSERT.enquote_name(table_owner).
But in some cases it can be very useful. For example, when there are multiple optional parameters (with defaults) and you only want to specify one of them. Or when you want to clarify the purpose of a parameter by displaying the parameter name next to the value.

LINQ Query to search for users with partial name

I have this SQL query:
SELECT
*
FROM
employeeTable
WHERE
(
concat(first_name, ' ', last_name) like concat('%', replace(#MatchString, ' ', '%'), '%')
or
concat(last_name, ' ', first_name) like concat('%', replace(#MatchString, ' ', '%'), '%')
)
It works perfectly by searching both name and last name with partial values., by searching users with partial strings. So for example:
"ric jon" will find Rick Jones, Richard Jonesy, and Jonathan Prichter.
I have the following Linq Query, using Entity Framework:
from employee in context.Employee
where employee.first_name.Contains(matchString)
|| employee.last_name.Contains(matchString)
select employee
But the string "ric jon" does not find anything.
I can't seem to make that linq query work the same as the SQL query.
That SQL wouldn't in fact find those records you sampled with the given "ric jon". What you are asking for is a special filter which IMHO requires a function that is not natively supported by the backend (you didn't specify your backend but anyway at least I don't know a backend that would support this natively). Something like this would work (note that you are getting the Employee records to local, this is not done at backend level):
Func<Employee, string[], bool> match = (emp, _parts) => {
return
((emp.FirstName.IndexOf(_parts[0],StringComparison.CurrentCultureIgnoreCase ) != -1) &&
(_parts.Length == 1 || emp.LastName.IndexOf(_parts[1], StringComparison.CurrentCultureIgnoreCase) != -1)) ||
((_parts.Length == 1 || emp.FirstName.IndexOf(_parts[1], StringComparison.CurrentCultureIgnoreCase) != -1) &&
(emp.LastName.IndexOf(_parts[0], StringComparison.CurrentCultureIgnoreCase) != -1));
};
string search = "ric jon";
var result = context.Employee.AsEnumerable()
.Where(n => match(n, search.Split()));
Update:
You can use this one which would be supported by many backends:
string search = "ric jon";
string[] parts = search.ToLowerInvariant().Split();
string p1 = parts.Length < 1 ? "" :parts[0];
string p2 = parts.Length < 2 ? "" :parts[1];
var result = context.Employee.Where(n =>
((n.FirstName.ToLowerInvariant().Contains(p1) &&
n.LastName.ToLowerInvariant().Contains(p2))) ||
((n.FirstName.ToLowerInvariant().Contains(p2) &&
n.LastName.ToLowerInvariant().Contains(p1))));

MVC SQL invalid

I have the following code in my MVC Index function. It is called from a search page that allows the user to search for various string values:
public ViewResult Index(string sortOrder, string YearString,string MonthString,string BudgetTypeString,string DescriptionString)
{
ViewBag.BudgetTypeSortParm = String.IsNullOrEmpty(sortOrder) ? "BudgetType_desc" : "";
ViewBag.MonthSortParm = sortOrder == "Date" ? "Month_desc" : "Month";
ViewBag.YearSortParm = sortOrder == "Date" ? "Year_desc" : "Year"; // 12-24-2014 JR added
ViewBag.DescriptionSortParm = sortOrder == "Description" ? "Description_desc" : "Description";
var budg = from s in db.budgets
select s;
if (!String.IsNullOrEmpty(YearString) ||
!String.IsNullOrEmpty(MonthString) ||
!String.IsNullOrEmpty(BudgetTypeString) ||
!String.IsNullOrEmpty(DescriptionString))
{
budg = budg.Where(s => s.BudgetType.ToUpper().Contains(BudgetTypeString.ToUpper()) ||
String.IsNullOrEmpty(BudgetTypeString) || s.Description.ToUpper().Contains(DescriptionString.ToUpper()) ||
String.IsNullOrEmpty(DescriptionString) ||
s.Month.ToUpper().Contains(MonthString.ToUpper()) ||
String.IsNullOrEmpty(MonthString) ||
s.Year.ToUpper().Contains(YearString.ToUpper()) ||
String.IsNullOrEmpty(YearString));
budg = budg.OrderBy(s => s.BudgetType);
return View(db.budgets.ToList());
}
}
Here is the actual SQL that is converted from the above code:
budg {SELECT
[Extent1].[id] AS [id],
[Extent1].[BudgetType] AS [BudgetType],
[Extent1].[Description] AS [Description],
[Extent1].[Amount] AS [Amount],
[Extent1].[Month] AS [Month],
[Extent1].[Year] AS [Year],
[Extent1].[DateStamp] AS [DateStamp]
FROM [dbo].[budget] AS [Extent1]
WHERE (( CAST(CHARINDEX(UPPER(#p__linq__0),
UPPER([Extent1].[BudgetType])) AS int)) > 0) OR
(( CAST(CHARINDEX(UPPER(#p__linq__1), UPPER([Extent1].[Description])) AS int)) > 0) OR
(( CAST(CHARINDEX(UPPER(#p__linq__2), UPPER([Extent1].[Month])) AS int)) > 0) OR
(( CAST(CHARINDEX(UPPER(#p__linq__3), UPPER([Extent1].[Year])) AS int)) > 0)}
Does anyone know why my strings are incorrectly being converted into integers and how to correct it so the search strings on my search page work correctly?
Let's take a look at just one of the Cast statements:
( CAST(CHARINDEX(UPPER(#p__linq__1), UPPER([Extent1].[Description])) AS int)) > 0)
Take a look at the inner statement. It's actually getting the CharIndex of one string in the other. Basically, does "Description" contain #p__linq__1. CharIndex returns 0 if if the first expression is not found in the other. The result of CharIndex is what is being Cast as int, and then compared to see if it is greater than 0. Which is exactly what you asked it to do when you use .Contains

owa_util.mime_header Issue - Trying to extract a Excel File from a query

'm having a problem with my one procedure that suppose to generate an Excel file, the procedure is this one:
CREATE OR REPLACE PROCEDURE GENERATE_REPORT_P(P_CONTRACT_NUM IN VARCHAR2 DEFAULT NULL,
P_CUSTOMER_NAME IN VARCHAR2 DEFAULT NULL,
P_CUSTOMER_NUM IN VARCHAR2 DEFAULT NULL,
P_UPDATE_DATE_START IN DATE DEFAULT NULL,
P_UPDATE_DATE_END IN DATE DEFAULT NULL,
P_ORDER_NUM IN VARCHAR2 DEFAULT NULL) IS
vRecords BOOLEAN := FALSE;
BEGIN
FOR i IN (SELECT
a.contract_num,
d.customer_abt_number,
d.customer_name,
c.doms_order_num order_num,
e.service_tag asset,
e.sku,
e.field,
e.previous_value,
e.new_value,
e.update_date, --to_char(i.update_date, 'DD-MON-RRRR HH12:MI:SS')
e.updated_by
FROM contracts a,
order_extension b,
orders c,
customers d,
abt_cisi_log e
WHERE a.id = b.contract_id
AND c.id = b.order_id
AND d.id = a.customer_id
AND c.id = e.order_id
--parameters
AND a.contract_num LIKE NVL(p_contract_num, a.contract_num)
AND d.customer_abt_number LIKE
NVL(p_customer_num, d.customer_abt_number)
AND d.customer_name LIKE
NVL(p_customer_name, d.customer_name)
AND e.update_date between
NVL(to_date(p_update_date_start, 'dd-mon-rrrr'), '01-JAN-1900') AND
NVL(to_date(p_update_date_end, 'dd-mon-rrrr'), '31-DEC-2199')
AND c.doms_order_num LIKE
NVL(p_order_num, c.doms_order_num)
ORDER BY a.contract_num,
c.doms_order_num,
e.service_tag,
e.sku) LOOP
IF NOT vRecords THEN
vRecords := TRUE;
owa_util.mime_header(ccontent_type => 'application/vnd.ms-excel');
htp.htmlopen;
htp.bodyopen;
htp.tableopen(cattributes => 'border=1');
htp.tablerowopen;
htp.p('<td><b>Contract</b></td>');
htp.p('<td><b>Customer Number</b></td>');
htp.p('<td><b>Customer</b></td>');
htp.p('<td><b>Order#</b></td>');
htp.p('<td><b>Asset</b></td>');
htp.p('<td><b>SKU</b></td>');
htp.p('<td><b>Field</b></td>');
htp.p('<td><b>Previous Value</b></td>');
htp.p('<td><b>New Value</b></td>');
htp.p('<td><b>Update Date</b></td>');
htp.p('<td><b>Updated By</b></td>');
htp.tablerowclose;
END IF;
htp.tablerowopen;
htp.p('<td>' || i.contract_num || '</td>');
htp.p('<td>' || i.customer_abt_number || '</td>');
htp.p('<td>' || i.customer_name || '</td>');
htp.p('<td>' || i.order_num || '</td>');
htp.p('<td>' || i.asset || '</td>');
htp.p('<td>' || i.sku || '</td>');
htp.p('<td>' || i.field || '</td>');
htp.p('<td>' || i.previous_value || '</td>');
htp.p('<td>' || i.new_value || '</td>');
htp.p('<td>' || to_char(i.update_date, 'DD-MON-RRRR HH12:MI:SS') ||
'</td>');
htp.p('<td>' || i.updated_by || '</td>');
htp.tablerowclose;
END LOOP;
IF vRecords THEN
htp.tableclose;
htp.bodyclose;
htp.htmlclose;
END IF;
END GENERATE_REPORT_P;
But when i execute this procedure by
Begin
GENERATE_REPORT_P();
END;
I ALWAYS get this error:
I was trying to figure it out, but not much luclk until now, I've tryed to comment all the code into the procedure and just call the owa_util.mime_header(ccontent_type => 'application/vnd.ms-excel'); To see what happens, and checked the data type from my table, well, pretty much all the basic stuff.
If anyone could give me some help, ou tip, i really appreciate that.
My Regards!
Note: I extract the procedure from a packge, and ajust one or two things just to place at StackOverflow, so, i u see the error message and figere it out, my bad!
To resolve this particular issue put this piece of code somewhere before the call to generate_report_p:
owa.num_cgi_vars := NVL(owa.num_cgi_vars, 0);