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

add Charset() option #1679

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

methane
Copy link
Member

@methane methane commented Feb 23, 2025

Description

Fix #1664.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

Summary by CodeRabbit

  • New Features

    • Introduced an option to configure your connection's charset and collation settings for improved database communication.
  • Refactor

    • Streamlined the management of connection charset information to enhance consistency and reliability.

Copy link

coderabbitai bot commented Feb 23, 2025

Walkthrough

This pull request revises the dsn.go file. The charsets field, previously removed from the public Config struct, is now reintroduced as a private field. In addition, a new Charset function is implemented to set the connection charset and collation by updating the appropriate fields in Config. The documentation details its behavior concerning the SET NAMES SQL command if a collation is provided.

Changes

File Change Summary
dsn.go - Reintroduced the charsets field as a private field in the Config struct.
- Added the Charset method to set charset and collation with proper documentation.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client Code
    participant Config as Config Struct
    Client->>+Config: Charset(charset, collation)
    Note right of Config: Update private `charsets` field\nand `Collation` field based on inputs
    Config-->>-Client: Updated configuration
Loading

Possibly related PRs

Suggested reviewers

  • shogo82148

Poem

I’m a rabbit with a coding knack,
Hopping through the lines to bring things back.
Charsets revived with a clever twist,
And collation settings not to be missed.
Code and carrots, a perfect hack! 🐰💻

Happy hops in every stack!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
dsn.go (1)

83-83: Consider simplifying the charsets field type.

The charsets field is defined as []string but based on the Charset() function implementation and typical MySQL usage, only a single charset is needed for the SET NAMES command. Consider simplifying this to just string.

-	charsets      []string                             // Connection charset. When set, this will be set in SET NAMES <charset> query
+	charset       string                               // Connection charset. When set, this will be set in SET NAMES <charset> query
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 58941dd and 0e828f7.

📒 Files selected for processing (1)
  • dsn.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (15)
  • GitHub Check: test (windows-latest, 1.23, mariadb-10.5)
  • GitHub Check: test (windows-latest, 1.23, mariadb-10.6)
  • GitHub Check: test (windows-latest, 1.23, mariadb-10.11)
  • GitHub Check: test (windows-latest, 1.23, mariadb-11.1)
  • GitHub Check: test (windows-latest, 1.23, mariadb-11.2)
  • GitHub Check: test (windows-latest, 1.23, mariadb-11.4)
  • GitHub Check: test (windows-latest, 1.23, 5.7)
  • GitHub Check: test (windows-latest, 1.23, 8.0)
  • GitHub Check: test (windows-latest, 1.23, 8.4)
  • GitHub Check: test (windows-latest, 1.23, 9.0)
  • GitHub Check: test (macos-latest, 1.23, mariadb-10.5)
  • GitHub Check: test (macos-latest, 1.23, mariadb-10.6)
  • GitHub Check: test (macos-latest, 1.23, mariadb-10.11)
  • GitHub Check: test (macos-latest, 1.23, mariadb-11.1)
  • GitHub Check: test (macos-latest, 1.23, mariadb-11.4)
🔇 Additional comments (1)
dsn.go (1)

138-151: Implementation looks good!

The Charset() function is well-documented and follows the established functional options pattern. The implementation correctly handles both charset-only and charset+collation cases, which aligns with MySQL's SET NAMES command behavior.

Comment on lines +138 to +151
// Charset sets the connection charset and collation.
//
// charset is the connection charset.
// collation is the connection collation. It can be null or empty string.
//
// When collation is not specified, `SET NAMES <charset>` command is sent when the connection is established.
// When collation is specified, `SET NAMES <charset> COLLATE <collation>` command is sent when the connection is established.
func Charset(charset, collation string) Option {
return func(cfg *Config) error {
cfg.charsets = []string{charset}
cfg.Collation = collation
return nil
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation for charset parameter.

The function accepts any string as charset without validation. Consider adding validation to ensure the charset is supported by MySQL to prevent runtime errors.

 func Charset(charset, collation string) Option {
 	return func(cfg *Config) error {
+		if charset == "" {
+			return errors.New("charset cannot be empty")
+		}
 		cfg.charsets = []string{charset}
 		cfg.Collation = collation
 		return nil
 	}
 }

Additionally, you might want to consider maintaining a list of valid MySQL charsets for validation:

var validCharsets = map[string]bool{
    "utf8mb4": true,
    "utf8": true,
    "latin1": true,
    // Add other supported charsets
}

func Charset(charset, collation string) Option {
    return func(cfg *Config) error {
        if charset == "" {
            return errors.New("charset cannot be empty")
        }
        if !validCharsets[charset] {
            return fmt.Errorf("unsupported charset: %s", charset)
        }
        cfg.charsets = []string{charset}
        cfg.Collation = collation
        return nil
    }
}

@coveralls
Copy link

Coverage Status

coverage: 82.946% (-0.1%) from 83.073%
when pulling 0e828f7 on methane:add-charset
into 58941dd on go-sql-driver:master.

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.

Charsets in the DSN Config is lowercase and cannot be used
2 participants