Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sql.Null[T] #45

Merged
merged 11 commits into from
Jul 11, 2024
Merged

Support sql.Null[T] #45

merged 11 commits into from
Jul 11, 2024

Conversation

mackee
Copy link
Owner

@mackee mackee commented Jul 10, 2024

Description

This Pull Request introduces support for sql.Null[T] in the go-sqlla library, enhancing type handling capabilities. The key updates include:

  • Support for sql.Null[T]: Enables the use of sql.Null[T] in column definitions.
    • Convenient Methods for Null Handling: Generates helpful methods for handling nullable columns using sql.Null[T], distinct from other sql.Null types.
  • Internal Refactoring: Includes refactoring of internal static analysis for improved code maintainability and readability.

Additionally, comprehensive tests have been added to ensure the new functionality works correctly.

Example

To define a column using sql.Null[T], you can create the following schema:

type ProductID uint64

//sqlla:table product
type Product struct {
    ID           ProductID          `db:"id,primarykey"`
    SubProductID sql.Null[ProductID] `db:"sub_product_id"`
}

In this schema definition, sub_product_id is recognized as a nullable column, generating corresponding methods. sql.Null[T] allows for both basic and user-defined types.

Unlike other sql.Null* types, you can directly specify ProductID in the Select() WHERE clause without wrapping it in sql.Null[T]:

id := ProductID(42)
// SELECT `id`, `sub_product_id` FROM `product` WHERE sub_product_id = 42;
NewProductSQL().Select().SubProductID(id)

You can also explicitly specify null:

// SELECT `id`, `sub_product_id` FROM `product` WHERE `sub_product_id` IS NULL
NewProductSQL().Select().SubProductIDIsNull()
// SELECT `id`, `sub_product_id` FROM `product` WHERE `sub_product_id` IS NOT NULL
NewProductSQL().Select().SubProductIDIsNotNull()

The IN operator can be specified with multiple ProductID values:

// SELECT `id`, `sub_product_id` FROM `product` WHERE `sub_product_id` IN (1, 2, 3)
NewProductSQL().Select().SubProductIDIn(1, 2, 3)

For Update, you can directly specify ProductID or set null explicitly:

id := ProductID(42)
// UPDATE `product` SET `sub_product_id` = 42
NewProductSQL().Update().SetSubProductID(id)
// UPDATE `product` SET `sub_product_id` = NULL
NewProductSQL().Update().SetSubProductIDToNull()

For Insert(), direct specification or explicit null setting is possible:

id := ProductID(42)
// INSERT INTO `product` (`sub_product_id`) VALUES (42)
NewProductSQL().Insert().ValueSubProductID(id)
// INSERT INTO `product` (`sub_product_id`) VALUES(NULL)
NewProductSQL().Insert().ValueSubProductIDIsNull()

The Insert().OnDuplicateKeyUpdate() method for handling key conflicts also supports these methods:

id := ProductID(42)
// INSERT INTO `product` (`sub_product_id`) VALUES (42) ON DUPLICATE KEY UPDATE SET `sub_product_id` = 42
NewProductSQL().Insert().ValueSubProductID(id).
    OnDuplicateKeyUpdate().
    ValueOnUpdateSubProductID(id)
// INSERT INTO `product` (`sub_product_id`) VALUES (42) ON DUPLICATE KEY UPDATE SET `sub_product_id` = NULL
NewProductSQL().Insert().ValueSubProductID(id).
    OnDuplicateKeyUpdate().
    ValueOnUpdateSubProductIDToNull() 

The Delete() method also allows queries similar to Select():

id := ProductID(42)
// DELETE FROM `product` WHERE sub_product_id = 42
NewProductSQL().Delete().SubProductID(id)
// DELETE FROM `product` WHERE `sub_product_id` IS NULL
NewProductSQL().Delete().SubProductIDIsNull()
// DELETE FROM `product` WHERE `sub_product_id` IS NOT NULL
NewProductSQL().Delete().SubProductIDIsNotNull()
// DELETE FROM `product` WHERE `sub_product_id` IN (1, 2, 3)
NewProductSQL().Delete().SubProductIDIn(1, 2, 3)

These functionalities become available when using sql.Null[T] for column definitions.

@mackee mackee force-pushed the feature/supports-sql-null-t branch from 54c8113 to 43a2c60 Compare July 10, 2024 05:14
@mackee mackee merged commit 99dda07 into master Jul 11, 2024
1 check passed
@mackee mackee deleted the feature/supports-sql-null-t branch July 11, 2024 01:24
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.

1 participant