PostgreSQL loadable plugin
Source
xxxxxxxxxx
func (conn *PGConn) QueryRowByName(ctx context.Context, queryName string, args ...interface{}) (row *sql.Row, err error) {
/*
** Zabbix
** Copyright 2001-2024 Zabbix SIA
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**/
package plugin
import (
"context"
"database/sql"
"fmt"
"net"
"net/url"
"path/filepath"
"strings"
"sync"
"time"
"git.zabbix.com/ap/plugin-support/metric"
"git.zabbix.com/ap/plugin-support/tlsconfig"
"git.zabbix.com/ap/plugin-support/uri"
"git.zabbix.com/ap/plugin-support/zbxerr"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v4/stdlib"
"github.com/omeid/go-yarn"
)
const (
// pgx dns field names
password = "password"
sslMode = "sslmode"
rootCA = "sslrootcert"
cert = "sslcert"
key = "sslkey"
cacheMode = "statement_cache_mode"
// connType
disable = "disable"
require = "require"
verifyCa = "verify-ca"
verifyFull = "verify-full"
MinSupportedPGVersion = 100000
)
type PostgresClient interface {
Query(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)
QueryByName(ctx context.Context, queryName string, args ...interface{}) (rows *sql.Rows, err error)
QueryRow(ctx context.Context, query string, args ...interface{}) (row *sql.Row, err error)
QueryRowByName(ctx context.Context, queryName string, args ...interface{}) (row *sql.Row, err error)
PostgresVersion() int
}
// PGConn holds pointer to the Pool of PostgreSQL Instance.
type PGConn struct {
client *sql.DB
callTimeout time.Duration
ctx context.Context
lastTimeAccess time.Time
version int
queryStorage *yarn.Yarn
address string
}
type connID struct {
uri uri.URI
cacheMode string
}
var errorQueryNotFound = "query %q not found"
// Query wraps pgxpool.Query.
func (conn *PGConn) Query(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
rows, err = conn.client.QueryContext(ctx, query, args...)
if ctxErr := ctx.Err(); ctxErr != nil {
err = ctxErr
}