How to create database view with cutom LIKE parameter? - java

I have JDBC Connection to my cloud MySQL database.
Utilizing the benefit of View's fetch time, I decided to create view for my user-queried searched results to be added into my JavaFX UI TableView.
I have following SQL View to run from method in UI.
CREATE VIEW `SEARCHED_QUERY_VIEW` AS
SELECT PRODUCTS.id,
PRODUCTS.name AS 'Product name',
MANUFACTURER.name AS 'Manufacturer name',
PACKAGE.name AS 'Package type',
SUB_PACKAGE.name AS 'Sub Package type'
FROM LOGISTICS.PRODUCTS
JOIN MANUFACTURER ON PRODUCTS.manid = MANUFACTURER.id
JOIN PACKAGE ON PRODUCTS.packageid = PACKAGE.id
JOIN SUB_PACKAGE ON PRODUCTS.subpackageid = SUB_PACKAGE.id
WHERE MANUFACTURER.name LIKE '%VIT%' OR PRODUCTS.name LIKE '%VIT%';
Now the problem lies with LIKE here.
I want LIKE to get an argument which will be passed from a parameterized method in JavaFX.
Can anybody advise solution on how to rework the view to receive custom LIKE parameter from Java Method?

You should move like parameter to select from the View.
So create the View without like condition but use LIKE in the query:
SELECT * FROM SEARCHED_QUERY_VIEW WHERE `Manufacturer name` LIKE '%VIT%' OR `Product name` LIKE '%VIT%';

Related

How to query current state of all Objects in a event driven database table?

I am currently implementing a blacklist feature for my application. Therefore I want to use a event based datatable, so I can also track, when a item has been blocked and by whom.
To give you a bit of a context: This is how the table looks like
id|object_id |object_type|change_time |change_type|
--|----------|-----------|-------------------|-----------|
0|1234567890|ITEM |2019-04-29 15:12:42|BLACKLISTED|
1|654321 |MATERIAL |2019-04-29 15:14:19|BLACKLISTED|
2|654321 |MATERIAL |2019-04-29 15:14:58|CLEARED |
As I am using spring and spring-data-jpa it is quite easy to get the current state of a single Item when querying for the first result ordered by time.
#Repository
public interface ItemFilterRepository extends JpaRepository<ItemFilterDpo, Integer> {
ItemFilterDpo findFirstByObjectIdAndObjectTypeOrderByChangeTimeDesc(String objectId, ItemFilterObjectTypeDpo type);
}
However, I can't find a nice solution for showing all Items that are currently blocked.
So I had a look here in stack overflow and found an answer using subqueries in the sql (SQL Query for current state of all entities in event table).
select
object_id, object_type, change_time, change_type as last_modified
from
item_filter ife
where
ife.change_time = (
select max(ife2.change_time)
from item_filter ife2
where ife.object_id=ife2.object_id
)
That gives me the following result, which I can filter for BLACKLISTED afterwards:
object_id |object_type|change_time |last_modified|
----------|-----------|-------------------|-------------|
1234567890|ITEM |2019-04-29 15:12:42|BLACKLISTED |
654321 |MATERIAL |2019-04-29 15:14:58|CLEARED |
To use that with spring-data, my first approach would be to create a view and query from that.
I'd really like to know, whether there is a better approach using spring-data to query the current state of all objects in a event datatable.
If an other framework suites better for my problem, I am happy to know.
Edit:
Using distinct on feels a bit better, however this doesn't solves my problem with spring-data.
select distinct on (object_id, object_type)
object_id, object_type, change_time, change_type as last_modified
from
item_filter bl
order by
object_id, object_type, change_time DESC;

JAva query from SQL-Server view

I have a Java aplication that retrieves data from SQL server.
The queries are of the form
SELECT field1, field2...
FROM [PRODUCTION].[INNOVATOR].[someTable]
WHERE NOT date = ''
This works fine.
However, I created for a user dedicated view, that has quite complex structure, including multiple JOINs.
I want to retrieve data from this view while avoiding the debug of this SQL in the Java environment.
So, I placed the name of the view instead of someTable.
Sound simple, but I get constantly an error message saying that the is not found.
Any idea why?
Check who the owner of the newly created view is. Since the fully qualified name of a view is Database.Owner.View, the query above will only work if the view owner is "INNOVATOR". Replace "INNOVATOR" with the view owner.

How do I specify column names in Liquibase when creating view?

I have a database view which selects two columns from two different tables with the same name.
For example generating a DDL for the view would generate the following:
create view MYSCHEMA.VPRODUCTS ("Name", "AlternameName"...) AS
A.Name,
B.Name,
from PRODUCTSA A
left join PRODUCTSB B
...
When I run the command to generate a Liquibase change set, the SQL in the tag only contains a select statement for the two columns and makes no distinction for the unique column name (i.e. "Name", "AlternameName").
<createView viewName="VPRODUCTS ">SELECT
A.Name
,B.Name
from PRODUCTSA A
left join PRODUCTSB B
...
As a result, whenever I try to execute the change set Liquibase complains about duplicate entries for columns with the same name.
I can fix this by adding SQL "AS" statements in the tag, but I'd prefer if there was a better way to do this.
<createView viewName="VPRODUCTS ">SELECT
A.Name
,B.Name AS AlternateName
from PRODUCTSA A
left join PRODUCTSB B
...
Is there an attribute or tag that would allow me to specify the column names when creating the view, or providing the raw SQL in the DDL?
There isn't another way. Liquibase simply passes the nested SELECT statement into the create view statement so whatever is required in the select needs to be part of the query. There is no built-in pre-processing of the view statement beyond pre-pending "CREATE VIEW X"
If you want to provide the raw SQL you can always use the tag.
Another way would be to use subselects, like this:
SELECT [COLUMNLIST 1:1 RENAMED] FROM (your view)
Or use WITH blocks along the same line of thinking.

Wso2 Data service giving java.lang.NullPointerException

I created a web service using wso2 data service. But when I try to test it it gives below error.
Below is the screenshot of the error.
You are getting this error, because you haven't defined the result of your select query. When you define a SELECT query, you are going to retrieve a resultset from the query. So you need to define the result using the output parameters.
In the Add Query page there is a section called 'Result (Output Mapping)' where you can define the resut.
Alternatively you can define the query with column names (ex: SELECT name, age from Student) and then just click on 'Generate Responses' which automatically generate the response[1].
[1]http://docs.wso2.org/display/DSS301/Generate+Response

Oracle ADF::How do I pass a parameter to named query via requestScope to another view object

Question::
How can i pass the value to parameter named “userParameter” in a query. using requestScope
Kindly help out with the steps to make a request scope work on the same page.
For now I know the following.
I have a page named UserRoomReservation
I have a data control (UserView)
Drag the first data control as a table.
convert the first column to a link
add actionlistener to that link
from: #{row.userId}
to: #{requestScope.userParameter}
I created another view object via sql query
I put a named parameter ":userParameter"
the sql query is as written below:::
SELECT DISTINCT Fullreservation.USERID,
Meetingrooms."roomName",
Meetingrooms."roomId" ,
COUNT (Fullreservation.roomid) AS countRoomUsage
FROM FULLRESERVATION Fullreservation, "meetingRooms" Meetingrooms
WHERE fullreservation.roomid = Meetingrooms."roomId"
AND Fullreservation.USERID = :userParameter
GROUP BY
Meetingrooms."roomName",
Meetingrooms."roomId",
Fullreservation.USERID
I dragged it to the same page as a graph.
but it doesn't work because the value hasn't been passed to the
You can get value from requestScope
ADFContext.getCurrent().getRequestScope().get(obj);
and then set this value in bind variable using NamedWhereClauseParam method
ViewObjectInstance.setNamedWhereClauseParam("BindVariabbleName",value);
ViewObjectInstance.executeQuery();

Categories