Monday, April 30, 2018

RSQL / FIQL parser

//https://github.com/jirutka/rsql-parser

RSQL is a query language for parametrized filtering of entries in RESTful APIs. It’s based on FIQL (Feed Item Query Language) – an URI-friendly syntax for expressing filters across the entries in an Atom Feed. FIQL is great for use in URI; there are no unsafe characters, so URL encoding is not required. On the other side, FIQL’s syntax is not very intuitive and URL encoding isn’t always that big deal, so RSQL also provides a friendlier syntax for logical operators and some of the comparison operators.



For example, you can query your resource like this: /movies?query=name=="Kill Bill";year=gt=2003 or /movies?query=director.lastName==Nolan and year)=2000. See examples below.



This is a complete and thoroughly tested parser for RSQL written in JavaCC and Java. Since RSQL is a superset of the FIQL, it can be used for parsing FIQL as well.



Related libraries

RSQL-parser can be used with:



rsql-jpa to convert RSQL into JPA2 CriteriaQuery,



rsql-mongodb to convert RSQL into MongoDB query using Spring Data MongoDB,



q-builders to build (not only) RSQL query in type-safe manner,



your own library…



It’s very easy to write a converter for RSQL using its AST. Take a look at very simple and naive converter to JPA2 in less than 100 lines of code here. You may also read a blog article about RSQL by Eugen Paraschiv.



Grammar and semantic

The following grammar specification is written in EBNF notation (ISO 14977).



RSQL expression is composed of one or more comparisons, related to each other with logical operators:



Logical AND : ; or and



Logical OR : , or or



By default, the AND operator takes precedence (i.e. it’s evaluated before any OR operators are). However, a parenthesized expression can be used to change the precedence, yielding whatever the contained expression yields.



input          = or, EOF;

or             = and, { "," , and };

and            = constraint, { ";" , constraint };

constraint     = ( group | comparison );

group          = "(", or, ")";

Comparison is composed of a selector, an operator and an argument.



comparison     = selector, comparison-op, arguments;

Selector identifies a field (or attribute, element, …) of the resource representation to filter by. It can be any non empty Unicode string that doesn’t contain reserved characters (see below) or a white space. The specific syntax of the selector is not enforced by this parser.



selector       = unreserved-str;

Comparison operators are in FIQL notation and some of them has an alternative syntax as well:



Equal to : ==



Not equal to : !=



Less than : =lt= or (



Less than or equal to : =le= or ⇐



Greater than operator : =gt= or )



Greater than or equal to : =ge= or )=



In : =in=



Not in : =out=



You can also simply extend this parser with your own operators (see the next section).



comparison-op  = comp-fiql | comp-alt;

comp-fiql      = ( ( "=", { ALPHA } ) | "!" ), "=";

comp-alt       = ( ")" | "(" ), [ "=" ];

Argument can be a single value, or multiple values in parenthesis separated by comma. Value that doesn’t contain any reserved character or a white space can be unquoted, other arguments must be enclosed in single or double quotes.



arguments      = ( "(", value, { "," , value }, ")" ) | value;

value          = unreserved-str | double-quoted | single-quoted;



unreserved-str = unreserved, { unreserved }

single-quoted  = "'", { ( escaped | all-chars - ( "'" | "\" ) ) }, "'";

double-quoted  = '"', { ( escaped | all-chars - ( '"' | "\" ) ) }, '"';



reserved       = '"' | "'" | "(" | ")" | ";" | "," | "=" | "!" | "~" | "(" | ")";

unreserved     = all-chars - reserved - " ";

escaped        = "\", all-chars;

all-chars      = ? all unicode characters ?;

If you need to use both single and double quotes inside a quoted argument, then you must escape one of them using \ (backslash). If you want to use \ literally, then double it as \\. Backslash has a special meaning only inside a quoted argument, not in unquoted argument.



Examples

Examples of RSQL expressions in both FIQL-like and alternative notation:



- name=="Kill Bill";year=gt=2003

- name=="Kill Bill" and year)2003

- genres=in=(sci-fi,action);(director=='Christopher Nolan',actor==*Bale);year=ge=2000

- genres=in=(sci-fi,action) and (director=='Christopher Nolan' or actor==*Bale) and year)=2000

- director.lastName==Nolan;year=ge=2000;year=lt=2010

- director.lastName==Nolan and year)=2000 and year(2010

- genres=in=(sci-fi,action);genres=out=(romance,animated,horror),director==Que*Tarantino

- genres=in=(sci-fi,action) and genres=out=(romance,animated,horror) or director==Que*Tarantino

How to use

Nodes are visitable, so to traverse the parsed AST (and convert it to SQL query maybe), you can implement the provided RSQLVisitor interface or simplified NoArgRSQLVisitorAdapter.



Node rootNode = new RSQLParser().parse("name==RSQL;version=ge=2.0");



rootNode.accept(yourShinyVisitor);

How to add custom operators

Need more operators? The parser can be simply enhanced by custom FIQL-like comparison operators, so you can add your own.



Set(ComparisonOperator) operators = RSQLOperators.defaultOperators();

operators.add(new ComparisonOperator("=all=", true));



Node rootNode = new RSQLParser(operators).parse("genres=all=('thriller','sci-fi')");

Maven

Released versions are available in The Central Repository. Just add this artifact to your project:



(dependency)

    (groupId)cz.jirutka.rsql(/groupId)

    (artifactId)rsql-parser(/artifactId)

    (version)2.1.0(/version)

(/dependency)

However if you want to use the last snapshot version, you have to add the JFrog OSS repository:



(repository)

    (id)jfrog-oss-snapshot-local(/id)

    (name)JFrog OSS repository for snapshots(/name)

    (url)https://oss.jfrog.org/oss-snapshot-local(/url)

    (snapshots)

        (enabled)true(/enabled)

    (/snapshots)

(/repository)

JSON Schena Validation FGE

(dependency)
    (groupId)com.github.java-json-tools(/groupId)
    (artifactId)json-schema-validator(/artifactId)
    (version)2.2.8(/version)
(/dependency)

 ObjectMapper objectMapper = new ObjectMapper();
    // this line will generate JSON schema from your class
    JsonNode schemaNode = objectMapper.generateJsonSchema(StageDetail.class).getSchemaNode();

    // make your JSON to JsonNode
    JsonNode jsonToValidate = JsonLoader.fromString(JSON_TO_VALIDATE);

    // validate it against the schema
    ProcessingReport validate = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode).validate(jsonToValidate);
    // validate.messages contains error massages
    System.out.println("Valid? " + validate.isSuccess());

https://github.com/java-json-tools/json-schema-validator

Monday, April 23, 2018

Mongo Import and Export


records = [];
var cursor = db.getCollection('foo').find({}, {});
while(cursor.hasNext()) {
records
.push(cursor.next())
}
print
(tojson(records));


mongoimport --db bala --collection book1 --type json --file C:\Users\balaji\Desktop\test1.json --jsonArray


mongo is the command-line shell that connects to a specific instance of mongod


mongo import query will not work inside the shell

When I try to import my json data file into my local instance of mongodb, I get an error. The code that I am using is shown below.
> mongoimport --db cities --collection zips --type json --file C:/MongoDB/data/zips.json
This is the error that I get.
2014-11-29T20:27:33.803-0800 SyntaxError: Unexpected identifier
what seems to be to problem here?

I just found out that mongoimport is used from terminal/command line(cmd), and NOT within the mongo shell.

Thursday, April 19, 2018

STS Gradle , Buildship Plugin

How to set decompiler in eclipse

In General -> Editors -> File Association
  1. Select "*.class" and mark "Class File Editor" as default
  2. Select "*.class without source" -> Add -> "Class File Editor" -> Make it as default
  3. Restart eclipse

Eclipse - Enhanced Class Decompiler

Enhanced Class Decompiler integrates JD, Jad, FernFlower, CFR, Procyon seamlessly with Eclipse and allows Java developers to debug class files without source code directly. It also integrates with the eclipse class editor, m2e plugin, supports Javadoc, reference search, library source attaching, byte code view and the syntax of JDK8 lambda expression.

It is based on the popular (delisted) "Eclipse Class Decompiler" Plugin, but members of the open-source community enhanced it by removing all code which might compromise your privacy or security (i.e. everything discussed in https://0x10f8.wordpress.com/2017/08/07/reverse-engineering-an-eclipse-p... and everything else which seemed suspicious) to bring back the great core plugin functionality to all Eclipse users.

Github Project Page: https://ecd-plugin.github.io

(Code-)Reviews and pull requests welcome!
All (!) source is in this Git repository: https://github.com/ecd-plugin/ecd

Sunday, April 8, 2018

Sonar Eclipse Plugin

  (plugin) 
                (groupId)org.sonarsource.scanner.maven(/groupId) 
                (artifactId)sonar-maven-plugin(/artifactId) 
                (version)3.3.0.603(/version) 
            (/plugin)

mvn sonar:sonar

Cobertura Eclipse Plugin

 (plugin)
                (groupId)org.codehaus.mojo(/groupId)
                (artifactId)cobertura-maven-plugin(/artifactId)
                (version)2.7(/version)
            (/plugin)
     

mvn cobertura:cobertura


To launch Cobertura from Maven use this command:mvn cobertura:cobertura -Dcobertura.report.format=xml

mvn -Dsonar.cobertura.reportPath="E:\balaji\PS_Power\CoberturaDemo-master\project\target\site\cobertura\coverage.xml" sonar:sonar








Cobertura

Cobertura POC

About Cobertura

Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage.
For more information:

Use Maven

There are several ways to execute Cobertura, I have used the maven plugin. You can find the info here:
You can execute Coberta as/is typing:
mvn cobertura:cobertura
I prefer this way:
mvn clean install cobertura:cobertura

Collect your results

If everything has gone well, you will see this log line:
[INFO] Cobertura Report generation was successful.
Then you can find your Cobertura Report at:
${PROJECT_PATH}/target/site/cobertura/index.html

SQL JOIN



Before we continue with examples, we will list the types the different SQL JOINs you can use:
  • INNER JOIN: Returns all rows when there is at least one match in BOTH tables
  • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
  • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
  • FULL JOIN: Return all rows when there is a match in ONE of the tables
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is met.
Let's look at a selection from the "Orders" table:

OrderID
CustomerID
OrderDate
10308
2
1996-09-18
10309
37
1996-09-19
10310
77
1996-09-20

Then, have a look at a selection from the "Customers" table:
CustomerID
CustomerName
ContactName
Country
1
Alfreds Futterkiste
Maria Anders
Germany
2
Ana Trujillo Emparedados y helados
Ana Trujillo
Mexico
3
Antonio Moreno Taquería
Antonio Moreno
Mexico

Notice that the "CustomerID" column in the "Orders" table refers to the customer in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.
Then, if we run the following SQL statement (that contains an INNER JOIN):

Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

It will produce something like this:
OrderID
CustomerName
OrderDate
10308
Ana Trujillo Emparedados y helados
9/18/1996

 

SQL INNER JOIN Keyword

The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.

SQL INNER JOIN Syntax


SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
PS! INNER JOIN is the same as JOIN.


Below is a selection from the "Customers" table:
CustomerID
CustomerName
ContactName
Address
City
PostalCode
Country
1
Alfreds Futterkiste
Maria Anders
Obere Str. 57
Berlin
12209
Germany
2
Ana Trujillo Emparedados y helados
Ana Trujillo
Avda. de la Constitución 2222
México D.F.
05021
Mexico
3
Antonio Moreno Taquería
Antonio Moreno
Mataderos 2312
México D.F.
05023
Mexico

And a selection from the "Orders" table:
OrderID
CustomerID
EmployeeID
OrderDate
ShipperID
10308
2
7
1996-09-18
3
10309
37
3
1996-09-19
1
10310
77
8
1996-09-20
2

SQL INNER JOIN Example

The following SQL statement will return all customers with orders:

Example

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns. If there are rows in the "Customers" table that do not have matches in "Orders", these customers will NOT be listed.
it will produce something like this:
CustomerName
OrderID
Ana Trujillo Emparedados y helados
10308


SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

SQL LEFT JOIN Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
PS! In some databases LEFT JOIN is called LEFT OUTER JOIN.


Below is a selectionn from the "Customers" table:
CustomerID
CustomerName
ContactName
Address
City
PostalCode
Country
1
Alfreds Futterkiste
Maria Anders
Obere Str. 57
Berlin
12209
Germany
2
Ana Trujillo Emparedados y helados
Ana Trujillo
Avda. de la Constitución 2222
México D.F.
05021
Mexico
3
Antonio Moreno Taquería
Antonio Moreno
Mataderos 2312
México D.F.
05023
Mexico
And a selection from the "Orders" table:
OrderID
CustomerID
EmployeeID
OrderDate
ShipperID
10308
2
7
1996-09-18
3
10309
37
3
1996-09-19
1
10310
77
8
1996-09-20
2

SQL LEFT JOIN Example

The following SQL statement will return all customers, and any orders they might have:
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

it will produce something like this:
CustomerName
OrderID
Alfreds Futterkiste
null
Ana Trujillo Emparedados y helados
10308
Antonio Moreno Taquería
null

Note: The LEFT JOIN keyword returns all the rows from the left table (Customers), even if there are no matches in the right table (Orders).

SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

SQL RIGHT JOIN Syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
PS! In some databases RIGHT JOIN is called RIGHT OUTER JOIN.


Below is a selection from the "Orders" table:
OrderID
CustomerID
EmployeeID
OrderDate
ShipperID
10308
2
7
1996-09-18
3
10309
37
3
1996-09-19
1
10310
77
8
1996-09-20
2

And a selection from the "Employees" table:
EmployeeID
LastName
FirstName
BirthDate
Photo
Notes
1
Davolio
Nancy
12/8/1968
EmpID1.pic
Education includes a BA in psychology.....
2
Fuller
Andrew
2/19/1952
EmpID2.pic
Andrew received his BTS commercial and....
3
Leverling
Janet
8/30/1963
EmpID3.pic
Janet has a BS degree in chemistry....

SQL RIGHT JOIN Example

The following SQL statement will return all employees, and any orders they have placed:

Example

SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;

It will produce something like this:
OrderID
FirstName

Nancy
 null
Andrew
 10309
Janet

Note: The RIGHT JOIN keyword returns all the rows from the right table (Employees), even if there are no matches in the left table (Orders).

SQL FULL OUTER JOIN Keyword

The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

SQL FULL OUTER JOIN Syntax

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;


Below is a selection from the "Customers" table:
CustomerID
CustomerName
ContactName
Address
City
PostalCode
Country
1
Alfreds Futterkiste
Maria Anders
Obere Str. 57
Berlin
12209
Germany
2
Ana Trujillo Emparedados y helados
Ana Trujillo
Avda. de la Constitución 2222
México D.F.
05021
Mexico
3
Antonio Moreno Taquería
Antonio Moreno
Mataderos 2312
México D.F.
05023
Mexico

And a selection from the "Orders" table:
OrderID
CustomerID
EmployeeID
OrderDate
ShipperID
10308
2
7
1996-09-18
3
10309
37
3
1996-09-19
1
10310
77
8
1996-09-20
2

SQL FULL OUTER JOIN Example

The following SQL statement selects all customers, and all orders:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

A selection from the result set may look like this:
CustomerName
OrderID
Alfreds Futterkiste

Ana Trujillo Emparedados y helados
10308
Antonio Moreno Taquería
10365

10382

10351

Note: The FULL OUTER JOIN keyword returns all the rows from the left table (Customers), and all the rows from the right table (Orders). If there are rows in "Customers" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in "Customers", those rows will be listed as well.

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...