Skip to content

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
masterfrom
feat/cli-gen-dao
Open

feat(cmd/gf): gendao command now can generate dao/do/entity files directly parsing from sql files#4737
gqcn wants to merge 5 commits into
masterfrom
feat/cli-gen-dao

Conversation

@gqcn

@gqcn gqcn commented Mar 14, 2026

Copy link
Copy Markdown
Member

This pull request makes significant improvements to the gen dao command 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

  • Added support for a new "SQL file mode" in the gen dao command, allowing DAO code generation from SQL DDL files without needing a live database connection. The new SqlDir and SqlType options, along with supporting logic, enable this workflow. (CGenDaoInput, doGenDaoFromSQLFiles, and related helpers) [1] [2] [3]

Documentation and Code Clarity

  • Added detailed comments and documentation to all major structs (such as CGenDaoInput, CGenDaoInternalInput, and CustomAttributeType) 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

  • Introduced new helper functions such as 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

  • Added a new up target to the Makefile that stages changes, generates an AI-powered commit message using Claude, and pushes the commit. This streamlines the commit workflow for contributors.

Cleanup Logic Documentation

  • Added comments to the doClear and doClearItem functions, clarifying how stale generated files are identified and removed, which helps prevent orphaned files when database tables are dropped. [1] [2]

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 dao via sqlDir/sqlType.
  • Extract/extend DB field-type parsing and local-type inference in gdb so 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 thread Makefile Outdated
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
Copilot AI review requested due to automatic review settings May 18, 2026 20:36
@gqcn gqcn removed the request for review from Copilot May 18, 2026 20:36
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.

2 participants