All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.zetasql.Parameter Maven / Gradle / Ivy

There is a newer version: 2024.11.1
Show newest version
/*
 * Copyright 2019 Google LLC
 *
 * 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 com.google.zetasql;

import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CheckReturnValue;
import java.util.Map;

/**
 * Class containing utility methods for processing the parameters used in prepared expressions,
 * queries or modify statements
 */
@CheckReturnValue // see (broken link)
final class Parameter {

  private Parameter() {}

  /** Serialize a name:value pair as a proto Parameter */
  static com.google.zetasql.LocalService.Parameter serialize(String name, Value value) {
    return com.google.zetasql.LocalService.Parameter.newBuilder()
        .setName(name)
        .setValue(value.serialize())
        .build();
  }

  /**
   * Normalize the map of parameters by transforming the parameters' names (keys) into lower case
   */
  static ImmutableMap normalize(Map parameters) {
    ImmutableMap.Builder lowerMapBuilder = new ImmutableMap.Builder<>();
    for (Map.Entry entry : parameters.entrySet()) {
      lowerMapBuilder.put(Ascii.toLowerCase(entry.getKey()), entry.getValue());
    }
    return lowerMapBuilder.buildOrThrow();
  }

  /**
   * Normalize a map of parameters by transforming the parameters' names (keys) into lower case and
   * also validate that each parameter is expected (part of the expected map)
   */
  static ImmutableMap normalizeAndValidate(
      Map parameters, Map expected, String kind) {
    ImmutableMap.Builder lowerMapBuilder = ImmutableMap.builder();
    for (Map.Entry entry : parameters.entrySet()) {
      String name = Ascii.toLowerCase(entry.getKey());
      if (!expected.containsKey(name)) {
        throw new SqlException("Unexpected " + kind + " parameter '" + name + "'");
      }

      Value value = entry.getValue();
      Type type = expected.get(name);
      if (!type.equals(value.getType())) {
        throw new SqlException(
            "Expected " + kind + " parameter '" + name + "' to be of type " + type);
      }

      lowerMapBuilder.put(name, value);
    }
    return lowerMapBuilder.buildOrThrow();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy