WiP: Adding sqlc and pgx database integration

This commit is contained in:
pro100ton 2024-11-07 17:56:57 +03:00
parent 3afd65ee35
commit 0608cbaa09
10 changed files with 185 additions and 26 deletions

15
compose.yml Normal file
View file

@ -0,0 +1,15 @@
version: "3.9"
services:
postgres:
image: postgres:17.0
environment:
POSTGRES_DB: "ruprod"
POSTGRES_USER: "ruprod_user"
POSTGRES_PASSWORD: "ruprod_password"
ports:
- "5433:5432"
volumes:
- ruprod-data:/etc/data
volumes:
ruprod-data:

9
go.mod
View file

@ -2,4 +2,11 @@ module t0xa/pdf_to_txt
go 1.22.5
require github.com/rudolfoborges/pdf2go v0.1.1 // indirect
require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/rudolfoborges/pdf2go v0.1.1 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/text v0.18.0 // indirect
)

17
go.sum
View file

@ -1,2 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rudolfoborges/pdf2go v0.1.1 h1:cAEi53YeticMNnuts30UD11W6AAcB+Xe3odLVrI9XIE=
github.com/rudolfoborges/pdf2go v0.1.1/go.mod h1:l+ur9AjixJEV8ZEapV1ggOLAkSiBj+jSSXcWKRJp3TY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

60
main.go
View file

@ -1,11 +1,13 @@
package main
import (
"fmt"
"regexp"
"strings"
"context"
// "fmt"
// "regexp"
// "strings"
// "t0xa/pdf_to_txt/internal/parser"
"t0xa/pdf_to_txt/internal/parser"
"github.com/jackc/pgx/v5"
)
type CheckEntry struct {
@ -14,28 +16,36 @@ type CheckEntry struct {
}
func main() {
// TODO: Redo format of file passing to function
text := parser.ParseX5Check("./internal/parser/testfiles/test3.pdf")
// TODO: Redo format of file passing to function
// text := parser.ParseX5Check("./internal/parser/testfiles/test3.pdf")
//
// split := strings.Split(text, "\n")
// stringsToFilter := "https://mail|Gmail - Электронный чек|Сумма|НДС"
// re := regexp.MustCompile(stringsToFilter)
// filtered := []string{}
// for _, element := range split {
// if re.MatchString(element) {
// continue
// }
// if len(element) < 1 {
// continue
// }
// filtered = append(filtered, element)
//
// }
// for index, element := range filtered {
// if element == "Цена*Кол" {
// fmt.Println("Товар: ", filtered[index-1])
// fmt.Println("Цена: ", filtered[index+1])
// fmt.Println("------")
// }
// }
split := strings.Split(text, "\n")
stringsToFilter := "https://mail|Gmail - Электронный чек|Сумма|НДС"
re := regexp.MustCompile(stringsToFilter)
filtered := []string{}
for _, element := range split {
if re.MatchString(element) {
continue
}
if len(element) < 1 {
continue
}
filtered = append(filtered, element)
ctx := context.Background()
conn, err := pgx.Connect(ctx, "postgres://ruprod_user:ruprod_password@localhost:5433")
if err != nil {
panic(err)
}
for index, element := range filtered {
if element == "Цена*Кол" {
fmt.Println("Товар: ", filtered[index-1])
fmt.Println("Цена: ", filtered[index+1])
fmt.Println("------")
}
}
defer conn.Close(ctx)
}

7
query.sql Normal file
View file

@ -0,0 +1,7 @@
-- name: CreateProduct :one
INSERT INTO product (
name, price, purchase_date
) VALUES (
$1, $2, $3
)
RETURNING *;

32
rupdrod/db.go Normal file
View file

@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
package ruprod
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}

16
rupdrod/models.go Normal file
View file

@ -0,0 +1,16 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
package ruprod
import (
"github.com/jackc/pgx/v5/pgtype"
)
type Product struct {
ID int64
Name string
Price pgtype.Float8
PurchaseDate pgtype.Timestamp
}

39
rupdrod/query.sql.go Normal file
View file

@ -0,0 +1,39 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: query.sql
package ruprod
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createProduct = `-- name: CreateProduct :one
INSERT INTO product (
name, price, purchase_date
) VALUES (
$1, $2, $3
)
RETURNING id, name, price, purchase_date
`
type CreateProductParams struct {
Name string
Price pgtype.Float8
PurchaseDate pgtype.Timestamp
}
func (q *Queries) CreateProduct(ctx context.Context, arg CreateProductParams) (Product, error) {
row := q.db.QueryRow(ctx, createProduct, arg.Name, arg.Price, arg.PurchaseDate)
var i Product
err := row.Scan(
&i.ID,
&i.Name,
&i.Price,
&i.PurchaseDate,
)
return i, err
}

6
schema.sql Normal file
View file

@ -0,0 +1,6 @@
CREATE TABLE product (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
price float,
purchase_date timestamp
);

10
sqlc.yaml Normal file
View file

@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
queries: "query.sql"
schema: "schema.sql"
gen:
go:
package: "ruprod"
out: "rupdrod"
sql_package: "pgx/v5"