Skip to content

Commit

Permalink
read me
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuel-skrenkovic committed Jul 19, 2023
1 parent e04bdd9 commit c1f2da3
Showing 1 changed file with 10 additions and 38 deletions.
48 changes: 10 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
# tql

A horrible name (blame AI) for a library that uses generics to execute database queries.
Simple convenience functions (with generics) around `database/sql`.
Designed to be used with existing `sql.DB`, `sql.Tx` types.

Marshals rows into structs using the `db` tag. For the struct field to be marshalled, it needs to contain the `db` tag.

### Example struct:
```go
type Foo struct {
ID string `db:"id"` // will be marshalled
Value int // won't be marshalled
}
```

### Example usage:
```go
type Foo struct {
Expand All @@ -30,24 +23,19 @@ if err != nil {
```

### Supports named parameters:
```sql
INSERT INTO foo (id) VALUES (:id);
```

### Example usage:
```go
type Foo struct {
ID string `db:"id"`
Value string `db:"value"`
}

const stmt = "INSERT INTO foo (id, value) VALUES (:id, :value);"

foo := Foo {
ID: "foo",
Value: "bar",
}

const stmt = "INSERT INTO foo (id, value) VALUES (:id, :value);"

result, err := tql.Exec(context.Background(), db, stmt, foo)
if err != nil {
// error handling
Expand All @@ -58,34 +46,18 @@ if err != nil {

## API
```go
QuerySingle[T any](ctx context.Context, q Querier, query string, params ...any) (result T, err error)
```
QuerySingle queries the database and returns the first result returned. If the query does not return any rows, returns an error. If the query produces more than one result, returns an error.
QuerySingle[T any](ctx context.Context, q Querier, query string, params ...any) (T, error)

```go
QuerySingleOrDefault[T any](ctx context.Context, q Querier, def T, query string, params ...any) (result T, err error)
```
QuerySingleOrDefault queries the database and returns the first result returned. If the query does not return any rows, returns the provided default value. If the query produces more than 1 row, returns an error.
QuerySingleOrDefault[T any](ctx context.Context, q Querier, def T, query string, params ...any) (T, error)

```go
QueryFirst[T any](ctx context.Context, q Querier, query string, params ...any) (result T, err error)
```
QueryFirst queries the database and returns the first result.
QueryFirst[T any](ctx context.Context, q Querier, query string, params ...any) (T, error)

```go
QueryFirstOrDefault[T any](ctx context.Context, q Querier, def T, query string, params ...any) (result T, err error)
```
QueryFirst queries the database and returns the first result. If there are no rows returned, returns the provided default value.
QueryFirstOrDefault[T any](ctx context.Context, q Querier, def T, query string, params ...any) (T, error)

```go
Query[T any](ctx context.Context, q Querier, query string, params ...any) (result []T, err error)
```
Query queries the database and returns all the returned rows.
Query[T any](ctx context.Context, q Querier, query string, params ...any) ([]T, error)

```go
Exec(ctx context.Context, e Executor, query string, params ...any) (sql.Result, error)
```
Exec executes a statement and returns an sql.Result or an error.

## Interfaces used
```go
Expand All @@ -97,4 +69,4 @@ type Querier interface {
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
```
```

0 comments on commit c1f2da3

Please sign in to comment.