github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_valid.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package json
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  	"github.com/dolthub/go-mysql-server/sql/types"
    22  )
    23  
    24  // JSON_VALID(val)
    25  //
    26  // Returns 0 or 1 to indicate whether a value is valid JSON. Returns NULL if the argument is NULL.
    27  //
    28  // https://dev.mysql.com/doc/refman/8.0/en/json-attribute-functions.html#function_json-valid
    29  type JSONValid struct {
    30  	JSON sql.Expression
    31  }
    32  
    33  var _ sql.FunctionExpression = JSONValid{}
    34  
    35  // NewJSONValid creates a new JSONValid function.
    36  func NewJSONValid(args ...sql.Expression) (sql.Expression, error) {
    37  	if len(args) != 1 {
    38  		return nil, sql.ErrInvalidArgumentNumber.New("JSON_VALID", "1", len(args))
    39  	}
    40  	return &JSONValid{args[0]}, nil
    41  }
    42  
    43  // FunctionName implements sql.FunctionExpression
    44  func (j JSONValid) FunctionName() string {
    45  	return "json_valid"
    46  }
    47  
    48  // Description implements sql.FunctionExpression
    49  func (j JSONValid) Description() string {
    50  	return "returns whether JSON value is valid."
    51  }
    52  
    53  // IsUnsupported implements sql.UnsupportedFunctionStub
    54  func (j JSONValid) IsUnsupported() bool {
    55  	return false
    56  }
    57  
    58  func (j JSONValid) Resolved() bool {
    59  	return j.JSON.Resolved()
    60  }
    61  
    62  func (j JSONValid) String() string {
    63  	return fmt.Sprintf("%s(%s)", j.FunctionName(), j.JSON.String())
    64  }
    65  
    66  func (j JSONValid) Type() sql.Type {
    67  	return types.Boolean
    68  }
    69  
    70  func (j JSONValid) IsNullable() bool {
    71  	return j.JSON.IsNullable()
    72  }
    73  
    74  func (j JSONValid) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    75  	doc, err := getJSONDocumentFromRow(ctx, row, j.JSON)
    76  	if err != nil {
    77  		return false, nil
    78  	}
    79  	if doc == nil {
    80  		return nil, nil
    81  	}
    82  	return true, nil
    83  }
    84  
    85  func (j JSONValid) Children() []sql.Expression {
    86  	return []sql.Expression{j.JSON}
    87  }
    88  
    89  func (j JSONValid) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    90  	if len(j.Children()) != len(children) {
    91  		return nil, fmt.Errorf("json_valid did not receive the correct amount of args")
    92  	}
    93  
    94  	return NewJSONValid(children...)
    95  }