Files
gf/contrib/drivers
Jack Ling e0c032d1b1 fix(database/gdb): handle empty string in Fields() gracefully (#4700)
## Summary
Fix bug where `Fields("")` with empty string generates invalid SQL
`SELECT FROM table`.

## Root Cause
`mappingAndFilterToTableFields` method doesn't skip empty strings when
processing fields:
- `gstr.SplitAndTrim("", ",")` returns empty array
- No fields added to query
- Results in invalid SQL: `SELECT FROM table`

## Fix
Skip empty string fields in `mappingAndFilterToTableFields` (line
97-100):
```go
// Skip empty string fields
if fieldStr == "" {
    continue
}
```

## Behavior Changes
- `Fields("")` → SELECT * FROM table (uses default)
- `Fields("", "id")` → SELECT id FROM table (ignores empty string)
- `Fields("id", "", "nickname")` → SELECT id, nickname FROM table

## Tests
Added `Test_Issue4697` with 3 scenarios covering all cases above.

## Related
Fixes #4697
Ref #4703 (discovered during pagination test development)
2026-02-26 16:27:00 +08:00
..
2026-01-26 20:37:48 +08:00
2026-01-26 20:37:48 +08:00
2026-01-26 20:37:48 +08:00
2026-01-26 20:37:48 +08:00
2026-01-26 20:37:48 +08:00
2026-01-26 20:37:48 +08:00

Database drivers

Powerful database drivers for package gdb.

Installation

Let's take mysql for example.

go get github.com/gogf/gf/contrib/drivers/mysql/v2@latest

# Easy for copying:
go get github.com/gogf/gf/contrib/drivers/clickhouse/v2@latest
go get github.com/gogf/gf/contrib/drivers/dm/v2@latest
go get github.com/gogf/gf/contrib/drivers/gaussdb/v2@latest
go get github.com/gogf/gf/contrib/drivers/mariadb/v2@latest
go get github.com/gogf/gf/contrib/drivers/mssql/v2@latest
go get github.com/gogf/gf/contrib/drivers/oceanbase/v2@latest
go get github.com/gogf/gf/contrib/drivers/oracle/v2@latest
go get github.com/gogf/gf/contrib/drivers/pgsql/v2@latest
go get github.com/gogf/gf/contrib/drivers/sqlite/v2@latest
go get github.com/gogf/gf/contrib/drivers/sqlitecgo/v2@latest
go get github.com/gogf/gf/contrib/drivers/tidb/v2@latest

Choose and import the driver to your project:

import _ "github.com/gogf/gf/contrib/drivers/mysql/v2"

Commonly imported at top of main.go:

package main

import (
	_ "github.com/gogf/gf/contrib/drivers/mysql/v2"

	// Other imported packages.
)

func main() {
	// Main logics.
}

Supported Drivers

MySQL

import _ "github.com/gogf/gf/contrib/drivers/mysql/v2"

MariaDB

import _ "github.com/gogf/gf/contrib/drivers/mariadb/v2"

TiDB

import _ "github.com/gogf/gf/contrib/drivers/tidb/v2"

OceanBase

import _ "github.com/gogf/gf/contrib/drivers/oceanbase/v2"

GaussDB

import _ "github.com/gogf/gf/contrib/drivers/gaussdb/v2"

SQLite

import _ "github.com/gogf/gf/contrib/drivers/sqlite/v2"

cgo version

When the target is a 32-bit Windows system, the cgo version needs to be used.

import _ "github.com/gogf/gf/contrib/drivers/sqlitecgo/v2"

PostgreSQL

import _ "github.com/gogf/gf/contrib/drivers/pgsql/v2"

SQL Server

import _ "github.com/gogf/gf/contrib/drivers/mssql/v2"

Note:

  • InsertIgnore returns error if there is no primary key or unique index submitted with record.
  • It supports server version >= SQL Server2005
  • It ONLY supports datetime2 and datetimeoffset types for auto handling created_at/updated_at/deleted_at columns, because datetime type does not support microseconds precision when column value is passed as string.

Oracle

import _ "github.com/gogf/gf/contrib/drivers/oracle/v2"

Note:

  • It does not support LastInsertId.
  • InsertIgnore returns error if there is no primary key or unique index submitted with record.

ClickHouse

import _ "github.com/gogf/gf/contrib/drivers/clickhouse/v2"

Note:

  • It does not support InsertIgnore/InsertAndGetId features.
  • It does not support Save/Replace features.
  • It does not support Transaction feature.
  • It does not support RowsAffected feature.

DM

import _ "github.com/gogf/gf/contrib/drivers/dm/v2"

Note:

  • InsertIgnore returns error if there is no primary key or unique index submitted with record.

Custom Drivers

It's quick and easy, please refer to current driver source. It's quite appreciated if any PR for new drivers support into current repo.