<commit_before># pqt [![GoDoc](https://godoc.org/github.com/piotrkowalczuk/pqt?status.svg)](http://godoc.org/github.com/piotrkowalczuk/pqt)&nbsp;[![Build Status](https://travis-ci.org/piotrkowalczuk/pqt.svg)](https://travis-ci.org/piotrkowalczuk/pqt)&nbsp;[![codecov.io](https://codecov.io/github/piotrkowalczuk/pqt/coverage.svg?branch=master)](https://codecov.io/github/piotrkowalczuk/pqt?branch=master)
This package is a toolbox for postgres driven applications.
It provides multiple tools to help to work with postgres efficiently.
In comparison to other currently available libraries instead of pushing struct tags into anti pattern or parsing SQL it allow to define schema programmatically.

It relies to a large degree on packages:

* [ntypes](http://github.com/piotrkowalczuk/ntypes)
* [qtypes](http://github.com/piotrkowalczuk/qtypes)

## Features:

- __sql generation__
- __go generation__ - it includes:
	- `entity` - struct that reflects single row within the database
	- `criteria` - object that can be passed to the `Find` method, it allow to create complex queries
	- `repository` - data access layer that expose API to manipulate entities:
		- `Find` - returns collection of entities that match given criteria
		- `Insert` - saves given entity into the database
		- `FindOneBy<primary-key>` - retrieves single entity
		- `UpdateBy<primary-key>` - modifies single entity
		- `DeleteBy<primary-key>` - modifies single entity
	- `patch` - structure used by `UpdateBy<primary-key>` methods to modify existing entity
- __schema definition__ - allow to programmatically define database schema, that includes:
	- `schemas`
	- `tables`
	- `columns`
	- `constraints`
	- `relationships`


## Example

```
s := pqt.Schema("custom")
u := pqt.NewTable("user", pqt.WithIfNotExists()).
	AddColumn(pqt.NewColumn("password", pqt.TypeBytea(), pqt.WithNotNull())).
	AddColumn(pqt.NewColumn("username", pqt.TypeText(), pqt.WithNotNull(), pqt.WithUnique())).
	AddColumn(pqt.NewColumn("first_name", pqt.TypeText(), pqt.WithNotNull())).
	AddColumn(pqt.NewColumn("last_name", pqt.TypeText(), pqt.WithNotNull())).
	AddColumn(pqt.NewColumn("is_superuser", pqt.TypeBool(), pqt.WithNotNull(), pqt.WithDefault("FALSE"))).
	AddColumn(pqt.NewColumn("is_active", pqt.TypeBool(), pqt.WithNotNull(), pqt.WithDefault("FALSE"))).
	AddColumn(pqt.NewColumn("is_staff", pqt.TypeBool(), pqt.WithNotNull(), pqt.WithDefault("FALSE"))).
	AddColumn(pqt.NewColumn("is_confirmed", pqt.TypeBool(), pqt.WithNotNull(), pqt.WithDefault("FALSE"))).
	AddColumn(pqt.NewColumn("confirmation_token", pqt.TypeBytea())).
	AddColumn(pqt.NewColumn("last_login_at", pqt.TypeTimestampTZ()))
s.AddTable(u)
```

Package itself do not provide any command line application that would generate output out of given input.
Instead it encourage to write local generation application next to the proper package.
```go<commit_msg>README.md features update<commit_after># pqt [![GoDoc](https://godoc.org/github.com/piotrkowalczuk/pqt?status.svg)](http://godoc.org/github.com/piotrkowalczuk/pqt)&nbsp;[![Build Status](https://travis-ci.org/piotrkowalczuk/pqt.svg)](https://travis-ci.org/piotrkowalczuk/pqt)&nbsp;[![codecov.io](https://codecov.io/github/piotrkowalczuk/pqt/coverage.svg?branch=master)](https://codecov.io/github/piotrkowalczuk/pqt?branch=master)
This package is a toolbox for postgres driven applications.
It provides multiple tools to help to work with postgres efficiently.
In comparison to other currently available libraries instead of pushing struct tags into anti pattern or parsing SQL it allow to define schema programmatically.

It relies to a large degree on packages:

* [ntypes](http://github.com/piotrkowalczuk/ntypes)
* [qtypes](http://github.com/piotrkowalczuk/qtypes)

## Features:

- __sql generation__
- __go generation__ - it includes:
	- `entity` - struct that reflects single row within the database
	- `criteria` - object that can be passed to the `Find` method, it allows to create complex queries
	- `patch` - structure used by `UpdateBy<primary-key>` methods to modify existing entity
	- `constants`:
		- `table names`
		- `column names`
		- `constraints` - library generates exact names of each constraint and corresponding constant that allow to easily handle query errors using `pqt.ErrorConstraint` helper function.
	- `repository` - data access layer that expose API to manipulate entities:
		- `Find` - returns collection of entities that match given criteria
		- `Insert` - saves given entity into the database
		- `FindOneBy<primary-key>` - retrieves single entity
		- `UpdateBy<primary-key>` - modifies single entity
		- `DeleteBy<primary-key>` - modifies single entity
- __schema definition__ - allow to programmatically define database schema, that includes:
	- `schemas`
	- `tables`
	- `columns`
	- `constraints`
	- `relationships`


## Example

```go
s := pqt.Schema("custom")
u := pqt.NewTable("user", pqt.WithIfNotExists()).
	AddColumn(pqt.NewColumn("password", pqt.TypeBytea(), pqt.WithNotNull())).
	AddColumn(pqt.NewColumn("username", pqt.TypeText(), pqt.WithNotNull(), pqt.WithUnique())).
	AddColumn(pqt.NewColumn("first_name", pqt.TypeText(), pqt.WithNotNull())).
	AddColumn(pqt.NewColumn("last_name", pqt.TypeText(), pqt.WithNotNull())).
	AddColumn(pqt.NewColumn("is_superuser", pqt.TypeBool(), pqt.WithNotNull(), pqt.WithDefault("FALSE"))).
	AddColumn(pqt.NewColumn("is_active", pqt.TypeBool(), pqt.WithNotNull(), pqt.WithDefault("FALSE"))).
	AddColumn(pqt.NewColumn("is_staff", pqt.TypeBool(), pqt.WithNotNull(), pqt.WithDefault("FALSE"))).
	AddColumn(pqt.NewColumn("is_confirmed", pqt.TypeBool(), pqt.WithNotNull(), pqt.WithDefault("FALSE"))).
	AddColumn(pqt.NewColumn("confirmation_token", pqt.TypeBytea())).
	AddColumn(pqt.NewColumn("last_login_at", pqt.TypeTimestampTZ()))
s.AddTable(u)
```

Package itself do not provide any command line application that would generate output out of given input.
Instead it encourage to write local generation application next to the proper package.
```go
{'fraction_non_alphanumeric': 0.1395465155051684, 'fraction_numerical': 0.0, 'mean_word_length': 5.549126637554585, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6999225', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1885, 'n_words': 477}