Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(contrib/drivers/mssql): update tables SQL query for better compatibility#4170

Merged
hailaz merged 13 commits intogogf:masterfrom
li-zheng-hao:master
Dec 3, 2025
Merged

fix(contrib/drivers/mssql): update tables SQL query for better compatibility#4170
hailaz merged 13 commits intogogf:masterfrom
li-zheng-hao:master

Conversation

@li-zheng-hao
Copy link
Contributor

修复gf gen在sqlserver上的异常问题:

  1. gf gen dao 连接sqlserver 生成dao出错 对象名 'SYSOBJECTS' 无效 #1722
  2. mssql: 对象名 'SYSOBJECTS' 无效 大小写问题 #1761
> gf gen dao
fetching tables failed: SELECT NAME FROM SYSOBJECTS WHERE XTYPE='U' AND STATUS >= 0 ORDER BY NAME: mssql: 对象名 
'SYSOBJECTS' 无效。
1. SELECT NAME FROM SYSOBJECTS WHERE XTYPE='U' AND STATUS >= 0 ORDER BY NAME
2. mssql: 对象名 'SYSOBJECTS' 无效。

在SqlServer 2022已测试通过:

image

@gqcn
Copy link
Member

gqcn commented Feb 27, 2025

@li-zheng-hao 你好,我没有在mssql 2022上复现这个问题。如果该问题确实存在,需要提交对应的gf gen dao的单测来覆盖你的修改,保证结果如预期。单测的编写请参考cmd/gf下已有的单测代码:https://github.com/gogf/gf/blob/master/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@li-zheng-hao Hello, I did not reproduce this problem on mssql 2022. If this problem does exist, you need to submit a corresponding single test of gf gen dao to cover your modifications and ensure that the results are as expected. For writing single tests, please refer to the existing single test code under cmd/gf: https://github.com/gogf/gf/blob/master/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go

@gqcn gqcn added missing unit testing cases Used in PR, the associated unit testing cases should be committed. and removed need more details labels Feb 27, 2025
@li-zheng-hao li-zheng-hao reopened this Mar 22, 2025
@li-zheng-hao
Copy link
Contributor Author

@gqcn 已补充测试用例,另外复现步骤如下:

  1. Docker运行SqlServer 2022版本
    image

  2. 连接数据库并执行查询语句SELECT NAME FROM SYSOBJECTS WHERE XTYPE='U' AND STATUS >= 0 ORDER BY NAME;

image

  1. 执行正确的查询语句SELECT name FROM sys.objects WHERE type='U' AND is_ms_shipped = 0 ORDER BY name:

image

refactor(contrib/drivers/mssql): 优化 SQL 执行逻辑,简化条件判断
- 将 `github.com/emirpasic/gods` 从 v1.18.1 更新至 v2.0.0-alpha
- 更新以下模块的 go.mod 和 go.sum 文件:
  - contrib/drivers/sqlitecgo
  - contrib/metric/otelmetric
  - contrib/nosql/redis
  - contrib/registry/consul
  - contrib/registry/etcd
  - contrib/registry/file
  - contrib/registry/nacos
  - contrib/registry/polaris
  - contrib/registry/zookeeper
  - contrib/rpc/grpcx
  - contrib/sdk/httpclient
  - contrib/trace/otlpgrpc
  - contrib/trace/otlphttp
Copy link
Contributor

Copilot AI left a comment

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 fixes SQL Server compatibility issues with the gf gen dao command by updating from deprecated system tables to modern catalog views, and upgrades the emirpasic/gods dependency to v2.

  • Updated MSSQL driver to use sys.objects and sys.columns instead of deprecated SYSOBJECTS and syscolumns
  • Upgraded github.com/emirpasic/gods from v1.18.1 to v2.0.0-alpha across all modules
  • Enhanced test coverage with proper test implementations and removed blocking test code

Reviewed changes

Copilot reviewed 34 out of 60 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
contrib/drivers/mssql/mssql_tables.go Updated table listing query to use sys.objects for SQL Server 2005+ compatibility
contrib/drivers/mssql/mssql_table_fields.go Modernized field metadata query using sys.columns with better type handling
contrib/drivers/mssql/mssql_do_exec.go Refactored constants for clarity and modified rowsAffected calculation logic
contrib/drivers/mssql/mssql_z_unit_init_test.go Added test database creation logic and updated to use modern SQL Server catalog views
contrib/drivers/mssql/mssql_z_unit_basic_test.go Updated test schema references to use TestSchema constant
contrib/drivers/mssql/mssql_z_unit_model_test.go Removed UID field from test data, commented out debug output, and removed blocking return statement
contrib/drivers/mssql/mssql_do_filter_test.go Replaced empty test skeleton with comprehensive DoFilter test cases
contrib/drivers/mssql/go.mod Updated gods dependency to v2.0.0-alpha
.github/workflows/scripts/ci-main.sh Removed MSSQL test skip logic now that compatibility issues are resolved
[43 other go.mod/go.sum files] Consistently updated gods dependency across all modules
Comments suppressed due to low confidence (1)

contrib/drivers/mssql/mssql_z_unit_init_test.go:140

  • Potential SQL injection vulnerability: Table names and column names are directly interpolated into SQL queries using fmt.Sprintf without proper escaping. While these are test helper functions, this pattern could be copied to production code. Consider using proper identifier escaping for SQL Server (e.g., QUOTENAME() function or bracket notation like [%s]) to prevent potential SQL injection if table names come from untrusted sources. Example: IF NOT EXISTS (SELECT * FROM sys.objects WHERE name='%s' and type='U') CREATE TABLE [%s] (...)
	if _, err := db.Exec(context.Background(), fmt.Sprintf(`
		IF NOT EXISTS (SELECT * FROM sys.objects WHERE name='%s' and type='U')
		CREATE TABLE %s (
		ID numeric(10,0) NOT NULL,
		PASSPORT VARCHAR(45)  NULL,
		PASSWORD VARCHAR(32)  NULL,
		NICKNAME VARCHAR(45)  NULL,
		CREATE_TIME datetime NULL,
		CREATED_AT datetimeoffset NULL,
		UPDATED_AT datetimeoffset NULL,
		PRIMARY KEY (ID))
	`, name, name)); err != nil {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hailaz hailaz merged commit 48845c3 into gogf:master Dec 3, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing unit testing cases Used in PR, the associated unit testing cases should be committed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants