Skip to content

Conversation

mdwhatcott
Copy link
Contributor

@mdwhatcott mdwhatcott commented Sep 12, 2025

The sqldb module has had a very 'enterprise' feel to it, but it's not really doing that much. This PR proposes an implementation that retains the behavior of previous versions but does so with less than 100 lines of code. It also adds inherent support for prepared statements, as
well as QueryRowContext.

See this commit for the new implementation at a glance: 7295811

Implement the spirit of previous versions in a single file with just a
few simple functions. Adds inherent support for prepared statements, as
well as QueryRowContext.

It is very plausible that this module, which is now less than 100 lines
of code, is a good candidate for copy-paste installation.
@mdwhatcott mdwhatcott changed the title V3 v3 Sep 12, 2025
@kierstenSmarty
Copy link

This is awesome! I like how succinct it is. Are you planning on utilizing prepared statements more moving forward?

sqldb.go Outdated
if err != nil {
return 0, err // already normalized
}
count += rows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it helpful to sum the count of affected rows if multiple statements are being executed? Would it be more helpful to return a slice of counts or count of successful statement executions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point/questions. The big advantages of this v3 proposal are:

  1. it is at least an order of magnitude more succinct,
  2. it doesn't completely bury the standard library API (like v2 does), and
  3. it preserves the spirit of the current (v2) API in order to ease potential migrations, should we decide to go this direction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running multiple statements in a single call is clunky by design, IMO. I'm sure we could come up with a better API for this, but that wasn't my top priority.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I like the succinctness of v3 and I think we should move this direction. I am just a little concerned that this is going to be confusing. You're right that it's inherently an issue and there isn't a perfect solution so I don't have an issue punting this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The need to split multiple statements into multiple execution statements is actually a limitation of the MySQL driver specifically. Other drivers (pgsql) can handle multiple statements. The split with the subsequent aggregation of rows affected is masking the fact that it's multiple, separate statements being executed.

The count as a single integer is important for optimistic concurrency, e.g. 27 rows in total were affected and we expected 30 rows affected therefore something must have changed underneath us.

@mdwhatcott
Copy link
Contributor Author

@kierstenSmarty - I don't really have immediate plans, but perhaps we should be considering this for some of our more active paths.

..lest we pollute the main module's dependency listing.

This suite utilizes an in-memory sqlite database to avoid any need for a
running database server.
This DB leverages prepared statements when configured thresholds are
encountered.

The DB type is the approach I'd like to use for new code.
Existing mappers can be more easily transitioned to the package-level
functions that were implemented in previous commits on this v3 branch.

Perhaps those functions will be moved to a separate package...
No more package-level/static functions.
I'm allowing these contractual interfaces to be exported so they can be
conveniently referenced by calling code. This decision was made after
careful consideration of intended implementation patterns as I actually
coded against the new API, which helped determine that the ability to
reference these type definitions provides significant value.
@mdwhatcott mdwhatcott closed this Sep 23, 2025
@mdwhatcott mdwhatcott deleted the v3 branch September 23, 2025 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants