How to find particular table in which class in whole webApplication - java

In a webApplication have 900pages java class & jsp pages, 920 tables,250 procedure,430 functions. now my question is that suppose that there is table "employee_Table" now i wanna know that in which class using this table and in which jsp page using this table, and in which procedure and also in which functions using this table.
I am working on it from 2 days but unable to get any logic so kindly help it.

A good starting point is probably to search all the sources (Java, DDL and SQL) for the string employee_Table (case insensitive, please). The reasoning here is that this string is pretty unique so you won't get many unwanted search results.
The next step is to use an enum or create a new type TableName and replace all string literals with it. That way, you can now tell your IDE to search for all places where the enum or TableName constant is being used.

Related

Splitting the string , mapping and building the sql in java

I have some task to do and can't figure out how to reach the result.
For example i have some STRING like this :
key1=value1&key2=value2|key3=value3
This string like key=value with options AND or OR.
I have also some map that contains following things:
key1=ke1Mapped; key2=key2Mapped; key3=key3Mapped;
I want to build some sql Select * from table1 where
And insert mapped keys and its values, but also to take into account & and | , i.e. AND or OR to use in sql query when it's needed.
Actually, finally sql query should be:
Select * from table where ke1Mapped=value1 AND key2Mapped=value2 OR key3Mapped=value3
I tried to split the main string and convert to MAP and then to loop over the list of mapped values and build dynamically sql. But i don't know what is AND or OR.
Tried to push it in two stacks , also were complicated.
I try now to understand how to build only architecture of this flow. And can't figure out what approach is needed here.
Anyone can suggest something?
Thanks

How to get result set by checking a specific element in an aggregated array using JOOQ?

I want to filter results by a specific value in the aggregated array in the query.
Here is a little description of the problem.
Section belongs to the garden. Garden belongs to District and District belongs to the province.
Users have multiple sections. Those sections belong to their gardens and they are to their Districts and them to Province.
I want to get user ids that have value 2 in district array.
I tried to use any operator but it doesn't work properly. (syntax error)
Any help would be appreciated.
ps: This is possible writing using plain SQL
rs = dslContext.select(
field("user_id"),
field("gardens_array"),
field("province_array"),
field("district_array"))
.from(table(select(
arrayAggDistinct(field("garden")).as("gardens_array"),
arrayAggDistinct(field("province")).as("province_array"),
arrayAggDistinct(field("distict")).as("district_array"))
.from(table("lst.user"))
.leftJoin(table(select(
field("section.user_id").as("user_id"),
field("garden.garden").as("garden"),
field("garden.province").as("province"),
field("garden.distict").as("distict"))
.from(table("lst.section"))
.leftJoin("lst.garden")
.on(field("section.garden").eq(field("garden.garden")))
.leftJoin("lst.district")
.on(field("district.district").eq(field("garden.district")))).as("lo"))
.on(field("user.user_id").eq(field("lo.user_id")))
.groupBy(field("user.user_id"))).as("joined_table"))
.where(val(2).equal(DSL.any("district_array"))
.fetch()
.intoResultSet();
Your code is calling DSL.any(T...), which corresponds to the expression any(?) in PostgreSQL, where the bind value is a String[] in your case. But you don't want "district_array" to be a bind value, you want it to be a column reference. So, either, you assign your arrayAggDistinct() expression to a local variable and reuse that, or you re-use your field("district_array") expression or replicate it:
val(2).equal(DSL.any(field("district_array", Integer[].class)))
Notice that it's usually a good idea to be explicit about data types (e.g. Integer[].class) when working with the plain SQL templating API, or even better, use the code generator.

Accessing Neo4j List in Java After Query

I don't have much code to post, and I'm quite confused on where to start. There's a lot of documentation online and I can't seem to find what I'm looking for.
Suppose I have this query result saved into a StatementResult variable:
result = session.run("MATCH (n:person {tag1: 'Person1'})"
+ "RETURN [(n)-->(b) WHERE b:type1 | m.tag2]")
In the Neo4j browser, this returns a list of exactly what I'm looking for. My question is how we can access this in Java. I know how to access single values, but not a list of this type.
Any help would be appreciated.
Thanks.
Usually you just iterate over the statement results, to access each record and then with each record you can access each named column. You didn't use any names.
The column will return Value objects which you then can turn into the types you expect, so in your case into a list with asList().
See the API docs for StatementResult and Value.asList()
also your statement is not correct you probably meant b where you wrote m and you need to name your column to access it

Create table query using JDBC doesn't working with different database engines

I write a scala program which interoperates with some database engines (for example MySQL, PostgreSQL).
I use JDBC api to handle SQL queries. I use queries to create table, and I want to create a table with the fields given by users, then these fields are names which can contains spaces or words with accent.
For example, create a table dummy with 2 fields, 'column 1' and 'column 2' as varchar fields.
Writing this query for MySQL database while preserving the spaces contained in the fields, we need to use backticks in the query like :
CREATE TABLE dummy (`column 1` varchar(20), `column 2` varchar(20));
In the same way, the right way to write this query for PostgreSQL while preserving the spaces is :
CREATE TABLE dummy ("column 1" varchar(20), "column 2" varchar(20));
Maybe for another database engine, there is a different way to write this query.
Is there any standard way to write this query with the constrainsts above and using JDBC so that it works with any database engines ?
Thank in advance for your answers.
This doesn't directly address your question as asked, but I think that you would be better off not naming your columns this way. I would 'normalize' the column names (by, e.g. replacing spaces with underscores).
The column names should probably not be exposed directly to the users anyway.
If you need 'human readable' names for columns, I would store them in another table. Or, if it is as simple as preserving spaces, just reverse the process, replacing underscores with spaces.
As already mentioned you should not use spaces, special characters or reserved words as column or table names. To play it safe you can generally put quotes around table and column names to avoid case-sensitivy issues accross databases.
CREATE TABLE "foo" ("id" VARCHAR(32), "bar" VARCHAR(64))
According to SQL-99 standard double quotes (") are used to delimit identifiers. Case sensitivy actually relates to the type of database used and it's settings. There are dbs that will upper- or lowercase your table and column names if they are not quoted but sometimes only in CREATE TABLE or ALTER TABLE commands. Which can lead to runtime errors.
For example CREATE TABLE foo might actually create a table named FOO. When doing a SELECT * FROM foo you might get an error because table foo does not exist but table FOO does exist. Having worked accross lots of DBMS I tend to use quotes for table and column names.
The important part is that you have to stick to writing lowercase or uppercase when using quotes because "foo" is not equal to "FOO" but foo might be equal to FOO depending on the used DBMS. Either do lowercase or uppercase but stick to it if you're using quotes.
You should also avoid database specific column types (stick to ANSI SQL whenever possible).
But doing database migrations by hand is very tedious and error-prone. I would suggest to use migration tools (flyway db) or let the migrations get created by libraries like slick.
As you mentioned scala, please have a look at Slick (http://slick.lightbend.com/) which is a great functional database layer for scala. There are others too but that one I use heavily and can recommend it.

How to determine if a column name must be referred in quotes in a SQL statement?

I am writing a web service that essentially allows users to submit queries to pre-existing tables in various SQL databases against advertised columns.
I have a PostgreSQL table defined like that:
CREATE TABLE stpg.test (
test integer,
"Test" integer,
"TEST" integer
);
insert into stpg.test values (1,2,3);
To determined the names of the available columns I run the following Java code:
ResultSet rs = dbmd.getColumns(null, "stpg", "test", null);
while (rs.next()) {
System.out.println(rs.getString("COLUMN_NAME"));
}
I get:
test
Test
TEST
If a user submits a query, referring to the columns as they were returned, like
select test, Test, TEST from stpg.test he will get 1 1 1 instead of expected 1 2 3.
Is this a bug?
I know that doing select test, "Test", "TEST" from stpg.testreturns results correctly. But my users would not know that to fetch the values of "capitalized" columns that were defined in quotes they need to use quotes in the query.
Is there a way I could could determine that a column name is case sensitive so that I could report its name in quotes? I need to do that generically against different databases, so JDBC api approach is preferable. I tried using ResultSetMetaData and invoking getColumnName and getColumnLabel but they return the names without the quotes. Calling isCaseSensitive always returns false.
Is there a way I could could determine that a column name is case sensitive so that I could report its name in quotes?
It looks like you are saying that a column name needs to be quoted if it contains any upper-case letters. In that case:
if (!name.equals(name.toLowercase())) {
// needs quoting.
}
But this is moot:
if you just quote all column names, or
if you treat user-supplied column names as case insensitive.
(On the latter point, having column names where case sensitivity matters is probably a bad design. Case sensitivity is certainly not something that you'd want your website users to have to worry about ...)
I tried using ResultSetMetaData and invoking getColumnName and getColumnLabel but they return the names without the quotes.
As they should! The quotes are not part of the column names! They are part of the (Postgres) SQL syntax for identifiers (in general). The name is the stuff within the quotes.
Calling isCaseSensitive always returns false.
To be honest, it is not entirely clear (from the javadoc) what the result of that method means. But it sounds like you might have found a bug in the JDBC driver you are using. (Or maybe you are just mistaken. The code for that implements that method in the current Postgres does consult the column type information ...)
I would suggest to always quote the column names. There is no real reason why you would remove the quotes. And, more importantly, the code to decide whether to quote or not is certainly going to span over 10-15ish lines with no added value. That's about 15 lines of code which can introduce new bugs, typos, conceptual errors.
Just quoting each column is straight-forward and always correct!
Also, regarding to your question if the result of select test, Test, TEST from stpg.test is a bug: It's not. It's the default behaviour of PostgreSQL. All column names (or, db-object names) are always lowered except if they are enclosed in quotes. This also leads us to isCaseSensitive. It is always false because it is not case-sensitive.
A more important note: If you let your users type in SQL queries, you will likely run into other weird problems. You will never know what kind of shenanigans your users type. Either by design or by accident ;)
If this is one of the first times you allow users to enter SQL queries, consider your plan of action carefully! Users type errors, mistakes (full-cartesian products on 5 tables with millions of rows? And only then apply filters?... fun times...), or might even try to play with your DB. If you decide on really doing this, buckle up! :) It all depends on the technical knowledge of you user-base.
Also, in Postgres I find it useful to keep everything lower-cased and user underscores to separate words. Like user_account instead of UserAccount.

Categories