feat(cmd/gf): gendao command now can generate dao/do/entity files directly parsing from sql files#4737
Open
gqcn wants to merge 5 commits into
Open
feat(cmd/gf): gendao command now can generate dao/do/entity files directly parsing from sql files#4737gqcn wants to merge 5 commits into
gqcn wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR enhances the cmd/gf gen dao generator to support an “SQL file mode”, allowing DAO/DO/entity/table generation directly from SQL DDL files without a live database connection, alongside refactors/documentation improvements to make the generator code easier to follow.
Changes:
- Add SQL DDL parsing pipeline + per-dialect parsers (MySQL/PgSQL/MSSQL/Oracle/SQLite) and wire it into
gen daoviasqlDir/sqlType. - Extract/extend DB field-type parsing and local-type inference in
gdbso it can run without a DB connection (used by SQL file mode). - Add extensive parser/unit tests and improve inline documentation across generator components; add a Makefile automation target and update
.gitignore.
Reviewed changes
Copilot reviewed 24 out of 25 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
cmd/gf/internal/cmd/gendao/gendao.go |
Adds SQL file mode (sqlDir/sqlType), offline table-field retrieval, and generation flow integration. |
cmd/gf/internal/cmd/gendao/gendao_sql_parser.go |
Introduces core SQL statement splitting/classification, CREATE/ALTER/DROP/RENAME/comment handling utilities. |
cmd/gf/internal/cmd/gendao/gendao_sql_parser_*.go |
Adds per-dialect DDL parsers for MySQL/PgSQL/MSSQL/Oracle/SQLite. |
cmd/gf/internal/cmd/gendao/gendao_sql_parser*_test.go |
Adds unit tests covering parsing scenarios and type inference. |
cmd/gf/internal/cmd/gendao/gendao_{dao,do,entity,table,structure}.go |
Switches generation to use getTableFields and supports nil-DB mode. |
database/gdb/gdb_core_structure.go |
Extracts FormatDBTypeName and CheckLocalTypeForFieldType for offline type inference. |
database/gdb/gdb.go |
Adds constants for additional dialect-specific type names used in inference. |
Makefile |
Adds up automation target (AI commit message + push). |
.gitignore |
Adds .aiprompt.zh.md ignore entry. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+679
to
+729
| // splitSQLStatements splits SQL content into individual statements by semicolons, | ||
| // handling quoted strings and parentheses properly. | ||
| func splitSQLStatements(sql string) []string { | ||
| var ( | ||
| statements []string | ||
| current strings.Builder | ||
| inSingle bool | ||
| inDouble bool | ||
| inBlock bool // block comment | ||
| depth int | ||
| prev byte | ||
| ) | ||
| for i := 0; i < len(sql); i++ { | ||
| ch := sql[i] | ||
| switch { | ||
| case inBlock: | ||
| current.WriteByte(ch) | ||
| if ch == '/' && prev == '*' { | ||
| inBlock = false | ||
| } | ||
| case ch == '/' && i+1 < len(sql) && sql[i+1] == '*' && !inSingle && !inDouble: | ||
| inBlock = true | ||
| current.WriteByte(ch) | ||
| case ch == '-' && i+1 < len(sql) && sql[i+1] == '-' && !inSingle && !inDouble: | ||
| // Line comment - skip to end of line | ||
| for i < len(sql) && sql[i] != '\n' { | ||
| i++ | ||
| } | ||
| case ch == '\'' && !inDouble: | ||
| inSingle = !inSingle | ||
| current.WriteByte(ch) | ||
| case ch == '"' && !inSingle: | ||
| inDouble = !inDouble | ||
| current.WriteByte(ch) | ||
| case ch == '(' && !inSingle && !inDouble: | ||
| depth++ | ||
| current.WriteByte(ch) | ||
| case ch == ')' && !inSingle && !inDouble: | ||
| depth-- | ||
| current.WriteByte(ch) | ||
| case ch == ';' && !inSingle && !inDouble && depth == 0: | ||
| stmt := strings.TrimSpace(current.String()) | ||
| if stmt != "" { | ||
| statements = append(statements, stmt) | ||
| } | ||
| current.Reset() | ||
| default: | ||
| current.WriteByte(ch) | ||
| } | ||
| prev = ch | ||
| } |
Comment on lines
+322
to
+330
| if restIdx >= len(upperWords) { | ||
| return nil | ||
| } | ||
|
|
||
| // Process the ALTER TABLE actions. Some dialects allow multiple actions separated by commas | ||
| // but we handle one action at a time for simplicity and split multi-action later. | ||
| return processAlterAction(upperWords, words, restIdx, fields, tableName, tables, columnParser) | ||
| } | ||
|
|
Comment on lines
+530
to
+534
| if existing, ok := fields[oldName]; ok { | ||
| field.Index = existing.Index | ||
| } else { | ||
| field.Index = existingIndex | ||
| } |
Comment on lines
+3
to
+23
| # commit changes with AI-generated commit message | ||
| .PHONY: up | ||
| up: | ||
| @if git diff --quiet HEAD && git diff --cached --quiet && [ -z "$$(git ls-files --others --exclude-standard)" ]; then \ | ||
| echo "No changes to commit"; \ | ||
| exit 0; \ | ||
| fi | ||
| @git add -A | ||
| @echo "Analyzing changes and generating commit message via AI..." | ||
| @set -e; \ | ||
| MSG=$$(git diff --cached --stat && echo "---" && git diff --cached | head -2000 | \ | ||
| claude -p "Analyze the git diff above and generate a concise commit message (single line, max 72 chars, lowercase, no quotes). Output only the commit message itself, nothing else." \ | ||
| --model haiku) || { echo "Error: Claude command failed"; exit 1; }; \ | ||
| COMMIT_MSG=$$(echo "$$MSG" | tail -1); \ | ||
| if [ -z "$$COMMIT_MSG" ]; then \ | ||
| echo "Error: Failed to generate commit message"; \ | ||
| exit 1; \ | ||
| fi; \ | ||
| echo "Commit: $$COMMIT_MSG"; \ | ||
| git commit -m "$$COMMIT_MSG" && \ | ||
| git push origin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request makes significant improvements to the
gen daocommand in the codebase, focusing on enhanced documentation, improved support for SQL file-based generation, better code organization, and clearer function responsibilities. The changes introduce a new mode for generating DAO code directly from SQL DDL files (without requiring a database connection) and add comprehensive comments to structures and functions for better maintainability and clarity.The most important changes are:
New Feature: SQL File-Based Generation
gen daocommand, allowing DAO code generation from SQL DDL files without needing a live database connection. The newSqlDirandSqlTypeoptions, along with supporting logic, enable this workflow. (CGenDaoInput,doGenDaoFromSQLFiles, and related helpers) [1] [2] [3]Documentation and Code Clarity
CGenDaoInput,CGenDaoInternalInput, andCustomAttributeType) and functions, explaining their purpose and usage. This greatly improves code readability and onboarding for new contributors. [1] [2] [3] [4] [5] [6] [7] [8] [9]Refactoring and Helper Functions
getTableFields(abstracts table field retrieval from either a DB or SQL file),getTemplateFromPathOrDefault, and improved existing ones with clearer responsibilities and documentation. [1] [2]Makefile Automation
uptarget to theMakefilethat stages changes, generates an AI-powered commit message using Claude, and pushes the commit. This streamlines the commit workflow for contributors.Cleanup Logic Documentation
doClearanddoClearItemfunctions, clarifying how stale generated files are identified and removed, which helps prevent orphaned files when database tables are dropped. [1] [2]