PostgreSQL loadable plugin
Source
func setResult(results map[string]interface{}, values []interface{}, columns []string) {
/*
** 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"
"encoding/json"
"errors"
"strings"
"github.com/jackc/pgx/v4"
"golang.zabbix.com/sdk/zbxerr"
)
// customQueryHandler executes custom user queries from *.sql files.
func customQueryHandler(ctx context.Context, conn PostgresClient,
_ string, params map[string]string, extraParams ...string) (interface{}, error) {
queryName := params["QueryName"]
queryArgs := make([]interface{}, len(extraParams))
for i, v := range extraParams {
queryArgs[i] = v
}
rows, err := conn.QueryByName(ctx, queryName, queryArgs...)
if err != nil {
return nil, zbxerr.ErrorCannotFetchData.Wrap(err)
}
defer rows.Close()
// JSON marshaling
var data []string
columns, err := rows.Columns()
if err != nil {
return nil, zbxerr.ErrorCannotFetchData.Wrap(err)
}
values := make([]interface{}, len(columns))
valuePointers := make([]interface{}, len(values))
for i := range values {
valuePointers[i] = &values[i]
}
results := make(map[string]interface{})
for rows.Next() {