feat: add prepared statements in Go

main
sudo pacman -Syu 2023-05-03 00:38:15 +07:00
parent 5ea0aa74b7
commit 427caae9f0
3 changed files with 54 additions and 0 deletions

View File

@ -7,6 +7,7 @@ all:
$(MAKE) format $(MAKE) format
$(MAKE) gen $(MAKE) gen
$(MAKE) format-html $(MAKE) format-html
$(MAKE) srht
test: test:
go test -race -failfast ./... go test -race -failfast ./...

View File

@ -551,6 +551,15 @@ internal
correctly is time consuming. So just stick to plain SQL. It is easier to correctly is time consuming. So just stick to plain SQL. It is easier to
debug when something is wrong. debug when something is wrong.
</p> </p>
<p>
Also please use
<a href="https://go.dev/doc/database/prepared-statements" rel="nofollow"
>prepared statement</a
>
as much as possible. Idealy, we should init all prepared statement when we
init database connection to cached it, not create it every time we need
it.
</p>
<p> <p>
But <code>database/sql</code> has its own limit. For example, it is hard But <code>database/sql</code> has its own limit. For example, it is hard
to get primary key after insert/update. So may be you want to use ORM for to get primary key after insert/update. So may be you want to use ORM for
@ -635,6 +644,30 @@ internal
<li><code>Loc</code> to <code>time.UTC</code>.</li> <li><code>Loc</code> to <code>time.UTC</code>.</li>
<li><code>CheckConnLiveness</code> to true.</li> <li><code>CheckConnLiveness</code> to true.</li>
</ul> </ul>
<h3>
<a
id="user-content-connect-sqlite-with-moderncorgsqlite"
class="anchor"
aria-hidden="true"
href="#connect-sqlite-with-moderncorgsqlite"
><span aria-hidden="true" class="octicon octicon-link"></span></a
>Connect SQLite with
<a href="https://gitlab.com/cznic/sqlite" rel="nofollow"
>modernc.org/sqlite</a
>
</h3>
<p>Remember to config:</p>
<ul>
<li>Write-Ahead Logging: <code>PRAGMA journal_mode=WAL</code></li>
<li>
Disable connections pool with <code>SetMaxOpenConns</code> sets to 1
</li>
</ul>
<p>
Don't use
<a href="https://github.com/mattn/go-sqlite3">mattn/go-sqlite3</a>, it's
required <code>CGO_ENABLED</code>.
</p>
<h3> <h3>
<a <a
id="user-content-connect-kafka-with-shopifysarama" id="user-content-connect-kafka-with-shopifysarama"
@ -859,6 +892,13 @@ fieldalignment -fix ./internal/business/<span class="pl-k">*</span>.go</pre>
>Speed Up GoMock with Conditional Generation</a >Speed Up GoMock with Conditional Generation</a
> >
</li> </li>
<li>
<a
href="https://turriate.com/articles/making-sqlite-faster-in-go"
rel="nofollow"
>Making SQLite faster in Go</a
>
</li>
</ul> </ul>
<div> <div>

View File

@ -275,6 +275,9 @@ To learn and use those libs correctly is time consuming.
So just stick to plain SQL. So just stick to plain SQL.
It is easier to debug when something is wrong. It is easier to debug when something is wrong.
Also please use [prepared statement](https://go.dev/doc/database/prepared-statements) as much as possible.
Idealy, we should init all prepared statement when we init database connection to cached it, not create it every time we need it.
But `database/sql` has its own limit. But `database/sql` has its own limit.
For example, it is hard to get primary key after insert/update. For example, it is hard to get primary key after insert/update.
So may be you want to use ORM for those cases. So may be you want to use ORM for those cases.
@ -333,6 +336,15 @@ Remember to config:
- `Loc` to `time.UTC`. - `Loc` to `time.UTC`.
- `CheckConnLiveness` to true. - `CheckConnLiveness` to true.
### Connect SQLite with [modernc.org/sqlite](https://gitlab.com/cznic/sqlite)
Remember to config:
- Write-Ahead Logging: `PRAGMA journal_mode=WAL`
- Disable connections pool with `SetMaxOpenConns` sets to 1
Don't use [mattn/go-sqlite3](https://github.com/mattn/go-sqlite3), it's required `CGO_ENABLED`.
### Connect Kafka with [Shopify/sarama](https://github.com/Shopify/sarama) ### Connect Kafka with [Shopify/sarama](https://github.com/Shopify/sarama)
Don't use [confluentinc/confluent-kafka-go](https://github.com/confluentinc/confluent-kafka-go), it's required `CGO_ENABLED`. Don't use [confluentinc/confluent-kafka-go](https://github.com/confluentinc/confluent-kafka-go), it's required `CGO_ENABLED`.
@ -428,3 +440,4 @@ gofmt -w -r '"github.com/Sirupsen/logrus" -> "github.com/sirupsen/logrus"' *.go
- [Fixing Memory Exhaustion Bugs in My Golang Web App](https://mtlynch.io/notes/picoshare-perf/) - [Fixing Memory Exhaustion Bugs in My Golang Web App](https://mtlynch.io/notes/picoshare-perf/)
- [Prevent Logging Secrets in Go by Using Custom Types](https://www.commonfate.io/blog/prevent-logging-secrets-in-go-by-using-custom-types) - [Prevent Logging Secrets in Go by Using Custom Types](https://www.commonfate.io/blog/prevent-logging-secrets-in-go-by-using-custom-types)
- [Speed Up GoMock with Conditional Generation](https://jonwillia.ms/2019/12/22/conditional-gomock-mockgen) - [Speed Up GoMock with Conditional Generation](https://jonwillia.ms/2019/12/22/conditional-gomock-mockgen)
- [Making SQLite faster in Go](https://turriate.com/articles/making-sqlite-faster-in-go)