<commit_before># datasource-proxy

[![Build Status](https://travis-ci.org/ttddyy/datasource-proxy.svg?branch=master)](https://travis-ci.org/ttddyy/datasource-proxy)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/net.ttddyy/datasource-proxy/badge.svg)](https://maven-badges.herokuapp.com/maven-central/net.ttddyy/datasource-proxy/)
[![Download](https://api.bintray.com/packages/ttddyy/maven/datasource-proxy/images/download.svg) ](https://bintray.com/ttddyy/maven/datasource-proxy/_latestVersion)


## about

Provide proxy classes for JDBC API to intercept executing queries.

## feature

- Query and Parameter Logging  
  You can log executing database queries and parameters with choice of your logging framework(commons, slf4j, sysout).
  Output can be formatted as JSON.

- Query Metrics  
  You can collect statistics of executed query such as total time, number of select, insert, update, delete queries, etc.
  Output can be formatted as JSON.

- Custom Logic Injection for Query Execution  
  You can write own `QueryExecutionListener` and they can get called before/after query execution. 

- Web Application Support  
  `ProxyDataSource` can be configured via JNDI.   
  Query metrics information are available per web request basis(request-response lifecycle).  
  For UI with JSP, custom tag to access metrics(`<dsp:metrics/>`) is available as well.
   
- Query and parameter replacement
  `QueryTransformer` and `ParameterTransformer` allows you to modify executing query and parameters right before 
  calling the database.


## log example


Query execution:

```sql
Name:MyProxy, Time:1, Success:True, Type:Statement, Batch:False, QuerySize:1, BatchSize:0, Query:["CREATE TABLE users(id INT, name VARCHAR(255))"], Params:[]
Name:MyProxy, Time:1, Success:True, Type:Prepared, Batch:True, QuerySize:1, BatchSize:2, Query:["INSERT INTO users (id, name) VALUES (?, ?)"], Params:[(1=1,2=foo),(1=2,2=bar)]
```

```json
// JSON output
{"name":"MyProxy", "time":1, "success":true, "type":"Statement", "batch":false, "querySize":1, "batchSize":0, "query":["CREATE TABLE users(id INT, name VARCHAR(255))"], "params":[]}
{"name":"MyProxy", "time":0, "success":true, "type":"Prepared", "batch":true, "querySize":1, "batchSize":3, "query":["INSERT INTO users (id, name) VALUES (?, ?)"], "params":[{"1":"1","2":"foo"},{"1":"2","2":"bar"},{"1":"3","2":"baz"}]}
```

Query metrics:

```sql
Name:"MyProxy", Time:6, Total:1, Success:1, Failure:0, Select:1, Insert:0, Update:0, Delete:0, Other:0
```

```json
// JSON output
{"name":"MyProxy", "time":10, "total":3, "success":3, "failure":0, "select":1, "insert":2, "update":0, "delete":0, "other":0}
```


## maven

```xml
<dependency>
  <groupId>net.ttddyy</groupId>
  <artifactId>datasource-proxy</artifactId>
  <version>1.3</version>
</dependency>
```

- No dependencies to other libraries, everything is optional.
    - For example, if you want to use slf4j logger with `SLF4JQueryLoggingListener`, then you need slf4j library.

- requires jdk1.6+


### download

Available in [maven central repo](http://search.maven.org/#search|ga|1|datasource-proxy).


## how to use

Create `ProxyDataSource` class and pass it as a `DataSource` to your application.
 
**Java based**  

```java
DataSource dataSource = 
    ProxyDataSourceBuilder
        .create(actualDataSource)
        .logQueryByCommons(INFO)    // logQueryBySlf4j(), logQueryToSysOut()
        .countQuery()
        .build();
```
<commit_msg>update referenced version to 1.3.1<commit_after># datasource-proxy

[![Build Status](https://travis-ci.org/ttddyy/datasource-proxy.svg?branch=master)](https://travis-ci.org/ttddyy/datasource-proxy)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/net.ttddyy/datasource-proxy/badge.svg)](https://maven-badges.herokuapp.com/maven-central/net.ttddyy/datasource-proxy/)
[![Download](https://api.bintray.com/packages/ttddyy/maven/datasource-proxy/images/download.svg) ](https://bintray.com/ttddyy/maven/datasource-proxy/_latestVersion)


## about

Provide proxy classes for JDBC API to intercept executing queries.

## feature

- Query and Parameter Logging  
  You can log executing database queries and parameters with choice of your logging framework(commons, slf4j, sysout).
  Output can be formatted as JSON.

- Query Metrics  
  You can collect statistics of executed query such as total time, number of select, insert, update, delete queries, etc.
  Output can be formatted as JSON.

- Custom Logic Injection for Query Execution  
  You can write own `QueryExecutionListener` and they can get called before/after query execution. 

- Web Application Support  
  `ProxyDataSource` can be configured via JNDI.   
  Query metrics information are available per web request basis(request-response lifecycle).  
  For UI with JSP, custom tag to access metrics(`<dsp:metrics/>`) is available as well.
   
- Query and parameter replacement
  `QueryTransformer` and `ParameterTransformer` allows you to modify executing query and parameters right before 
  calling the database.


## log example


Query execution:

```sql
Name:MyProxy, Time:1, Success:True, Type:Statement, Batch:False, QuerySize:1, BatchSize:0, Query:["CREATE TABLE users(id INT, name VARCHAR(255))"], Params:[]
Name:MyProxy, Time:1, Success:True, Type:Prepared, Batch:True, QuerySize:1, BatchSize:2, Query:["INSERT INTO users (id, name) VALUES (?, ?)"], Params:[(1=1,2=foo),(1=2,2=bar)]
```

```json
// JSON output
{"name":"MyProxy", "time":1, "success":true, "type":"Statement", "batch":false, "querySize":1, "batchSize":0, "query":["CREATE TABLE users(id INT, name VARCHAR(255))"], "params":[]}
{"name":"MyProxy", "time":0, "success":true, "type":"Prepared", "batch":true, "querySize":1, "batchSize":3, "query":["INSERT INTO users (id, name) VALUES (?, ?)"], "params":[{"1":"1","2":"foo"},{"1":"2","2":"bar"},{"1":"3","2":"baz"}]}
```

Query metrics:

```sql
Name:"MyProxy", Time:6, Total:1, Success:1, Failure:0, Select:1, Insert:0, Update:0, Delete:0, Other:0
```

```json
// JSON output
{"name":"MyProxy", "time":10, "total":3, "success":3, "failure":0, "select":1, "insert":2, "update":0, "delete":0, "other":0}
```


## maven

```xml
<dependency>
  <groupId>net.ttddyy</groupId>
  <artifactId>datasource-proxy</artifactId>
  <version>1.3.1</version>
</dependency>
```

- No dependencies to other libraries, everything is optional.
    - For example, if you want to use slf4j logger with `SLF4JQueryLoggingListener`, then you need slf4j library.

- requires jdk1.6+


### download

Available in [maven central repo](http://search.maven.org/#search|ga|1|datasource-proxy).


## how to use

Create `ProxyDataSource` class and pass it as a `DataSource` to your application.
 
**Java based**  

```java
DataSource dataSource = 
    ProxyDataSourceBuilder
        .create(actualDataSource)
        .logQueryByCommons(INFO)    // logQueryBySlf4j(), logQueryToSysOut()
        .countQuery()
        .build();
```

{'fraction_non_alphanumeric': 0.16214275455981617, 'fraction_numerical': 0.018382880942122648, 'mean_word_length': 5.162831858407079, 'pattern_counts': {'":': 68, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1478082', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2200, 'n_words': 604}