getVersion gets the version of Postgres server.
(ctx context.Context)
| 355 | |
| 356 | // getVersion gets the version of Postgres server. |
| 357 | func (d *Driver) getVersion(ctx context.Context) (string, error) { |
| 358 | // SHOW server_version_num returns an integer such as 100005, which means 10.0.5. |
| 359 | // It is more convenient to use SHOW server_version to get the version string. |
| 360 | // PostgreSQL supports it since 8.2. |
| 361 | // https://www.postgresql.org/docs/current/functions-info.html |
| 362 | query := "SHOW server_version_num" |
| 363 | var version string |
| 364 | if err := d.db.QueryRowContext(ctx, query).Scan(&version); err != nil { |
| 365 | if err == sql.ErrNoRows { |
| 366 | return "", common.FormatDBErrorEmptyRowWithQuery(query) |
| 367 | } |
| 368 | return "", util.FormatErrorWithQuery(err, query) |
| 369 | } |
| 370 | versionNum, err := strconv.Atoi(version) |
| 371 | if err != nil { |
| 372 | return "", err |
| 373 | } |
| 374 | // https://www.postgresql.org/docs/current/libpq-status.html#LIBPQ-PQSERVERVERSION |
| 375 | // Convert to semantic version. |
| 376 | major, minor, patch := versionNum/1_00_00, (versionNum/100)%100, versionNum%100 |
| 377 | return fmt.Sprintf("%d.%d.%d", major, minor, patch), nil |
| 378 | } |
| 379 | |
| 380 | // Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres |
| 381 | // will not use transactions to execute the statement but will still use transactions to execute the rest of statements. |
no test coverage detected