<commit_before>JDBC Sniffer
============

[![Join the chat at https://gitter.im/bedrin/jdbc-sniffer](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bedrin/jdbc-sniffer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![CI Status](https://travis-ci.org/bedrin/jdbc-sniffer.svg?branch=master)](https://travis-ci.org/bedrin/jdbc-sniffer)
[![Coverage Status](https://coveralls.io/repos/bedrin/jdbc-sniffer/badge.png?branch=master)](https://coveralls.io/r/bedrin/jdbc-sniffer?branch=master)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.bedrin/jdbc-sniffer/badge.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/com.github.bedrin/jdbc-sniffer)
[![Download](https://api.bintray.com/packages/bedrin/github/jdbc-sniffer/images/download.svg) ](https://bintray.com/bedrin/github/jdbc-sniffer/_latestVersion)

JDBC Sniffer counts the number of executed SQL queries and provides an API for validating them
It is designed for unit tests and allows you to test if particular method doesn't make more than N SQL queries
Especially it's useful to catch the ORM [N+1 problem](http://stackoverflow.com/questions/97197/what-is-the-n1-selects-issue) at early stages 

```java
try (Spy s = Sniffer.expectAtMostOnce(Query.SELECT).expectNever(Threads.OTHERS);
     Statement statement = connection.createStatement()) {
    statement.execute("SELECT 1 FROM DUAL");
    // JDBC Sniffer will throw an Exception if you execute query other than SELECT or uncomment line below
    //statement.execute("SELECT 1 FROM DUAL");
}
```

You can also use JDBC Sniffer in your test environments to see the number of SQL queries executed by each HTTP request.
Just add it to your `web.xml` file:
```xml
<filter>
    <filter-name>sniffer</filter-name>
    <filter-class>com.github.bedrin.jdbc.sniffer.servlet.SnifferFilter</filter-class>
    <init-param>
        <param-name>inject-html</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>sniffer</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
```

Restart your server and you will see the number of queries in bottom right corner of your app:
<commit_msg>Added screenshot of injected html to README.md<commit_after>JDBC Sniffer
============

[![Join the chat at https://gitter.im/bedrin/jdbc-sniffer](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bedrin/jdbc-sniffer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![CI Status](https://travis-ci.org/bedrin/jdbc-sniffer.svg?branch=master)](https://travis-ci.org/bedrin/jdbc-sniffer)
[![Coverage Status](https://coveralls.io/repos/bedrin/jdbc-sniffer/badge.png?branch=master)](https://coveralls.io/r/bedrin/jdbc-sniffer?branch=master)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.bedrin/jdbc-sniffer/badge.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/com.github.bedrin/jdbc-sniffer)
[![Download](https://api.bintray.com/packages/bedrin/github/jdbc-sniffer/images/download.svg) ](https://bintray.com/bedrin/github/jdbc-sniffer/_latestVersion)

JDBC Sniffer counts the number of executed SQL queries and provides an API for validating them
It is designed for unit tests and allows you to test if particular method doesn't make more than N SQL queries
Especially it's useful to catch the ORM [N+1 problem](http://stackoverflow.com/questions/97197/what-is-the-n1-selects-issue) at early stages 

```java
try (Spy s = Sniffer.expectAtMostOnce(Query.SELECT).expectNever(Threads.OTHERS);
     Statement statement = connection.createStatement()) {
    statement.execute("SELECT 1 FROM DUAL");
    // JDBC Sniffer will throw an Exception if you execute query other than SELECT or uncomment line below
    //statement.execute("SELECT 1 FROM DUAL");
}
```

You can also use JDBC Sniffer in your test environments to see the number of SQL queries executed by each HTTP request.
Just add it to your `web.xml` file:
```xml
<filter>
    <filter-name>sniffer</filter-name>
    <filter-class>com.github.bedrin.jdbc.sniffer.servlet.SnifferFilter</filter-class>
    <init-param>
        <param-name>inject-html</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>sniffer</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
```

Restart your server and you will see the number of queries in bottom right corner of your app:

![SnifferFilterInjectHtml](https://bedrin.github.io/jdbc-sniffer/SnifferFilterInjectHtml.png)

{'fraction_non_alphanumeric': 0.14437689969604864, 'fraction_numerical': 0.004776378636561007, 'mean_word_length': 4.906410256410257, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 23, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1906141', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1480, 'n_words': 321}