lib/google/cloud/bigquery/service.rb in google-cloud-bigquery-0.23.0 vs lib/google/cloud/bigquery/service.rb in google-cloud-bigquery-0.24.0
- old
+ new
@@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
require "google/cloud/bigquery/version"
+require "google/cloud/bigquery/convert"
require "google/cloud/errors"
require "google/apis/bigquery_v2"
require "pathname"
require "digest/md5"
require "mime/types"
@@ -169,12 +170,11 @@
end
def insert_tabledata dataset_id, table_id, rows, options = {}
insert_rows = Array(rows).map do |row|
Google::Apis::BigqueryV2::InsertAllTableDataRequest::Row.new(
- insert_id: Digest::MD5.base64digest(row.inspect),
- # Hash[row.map{|(k,v)| [k.to_s,v]}] for Hash<String,Object>
+ insert_id: Digest::MD5.base64digest(row.to_json),
json: row
)
end
insert_req = Google::Apis::BigqueryV2::InsertAllTableDataRequest.new(
rows: insert_rows,
@@ -399,28 +399,29 @@
create_disposition: create_disposition(options[:create]),
write_disposition: write_disposition(options[:write]),
allow_large_results: options[:large_results],
flatten_results: options[:flatten],
default_dataset: default_dataset,
- use_legacy_sql: resolve_legacy_sql(options[:legacy_sql],
- options[:standard_sql])
+ use_legacy_sql: Convert.resolve_legacy_sql(
+ options[:standard_sql], options[:legacy_sql])
+
)
)
)
if options[:params]
if Array === options[:params]
req.configuration.query.use_legacy_sql = false
req.configuration.query.parameter_mode = "POSITIONAL"
req.configuration.query.query_parameters = options[:params].map do |param|
- to_query_param param
+ Convert.to_query_param param
end
elsif Hash === options[:params]
req.configuration.query.use_legacy_sql = false
req.configuration.query.parameter_mode = "NAMED"
req.configuration.query.query_parameters = options[:params].map do |name, param|
- to_query_param(param).tap do |named_param|
+ Convert.to_query_param(param).tap do |named_param|
named_param.name = String name
end
end
else
fail "Query parameters must be an Array or a Hash."
@@ -438,26 +439,26 @@
max_results: options[:max],
default_dataset: dataset_config,
timeout_ms: options[:timeout],
dry_run: options[:dryrun],
use_query_cache: options[:cache],
- use_legacy_sql: resolve_legacy_sql(options[:legacy_sql],
- options[:standard_sql])
+ use_legacy_sql: Convert.resolve_legacy_sql(
+ options[:standard_sql], options[:legacy_sql])
)
if options[:params]
if Array === options[:params]
req.use_legacy_sql = false
req.parameter_mode = "POSITIONAL"
req.query_parameters = options[:params].map do |param|
- to_query_param param
+ Convert.to_query_param param
end
elsif Hash === options[:params]
req.use_legacy_sql = false
req.parameter_mode = "NAMED"
req.query_parameters = options[:params].map do |name, param|
- to_query_param(param).tap do |named_param|
+ Convert.to_query_param(param).tap do |named_param|
named_param.name = String name
end
end
else
fail "Query parameters must be an Array or a Hash."
@@ -465,105 +466,10 @@
end
req
end
- def to_query_param value
- if TrueClass === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "BOOL"),
- parameter_value: API::QueryParameterValue.new(value: true)
- )
- elsif FalseClass === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "BOOL"),
- parameter_value: API::QueryParameterValue.new(value: false)
- )
- elsif Integer === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "INT64"),
- parameter_value: API::QueryParameterValue.new(value: value)
- )
- elsif Float === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "FLOAT64"),
- parameter_value: API::QueryParameterValue.new(value: value)
- )
- elsif String === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "STRING"),
- parameter_value: API::QueryParameterValue.new(value: value)
- )
- elsif DateTime === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "DATETIME"),
- parameter_value: API::QueryParameterValue.new(
- value: value.strftime("%Y-%m-%d %H:%M:%S.%6N"))
- )
- elsif Date === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "DATE"),
- parameter_value: API::QueryParameterValue.new(value: value.to_s)
- )
- elsif ::Time === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "TIMESTAMP"),
- parameter_value: API::QueryParameterValue.new(
- value: value.strftime("%Y-%m-%d %H:%M:%S.%6N%:z"))
- )
- elsif Bigquery::Time === value
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "TIME"),
- parameter_value: API::QueryParameterValue.new(value: value.value)
- )
- elsif value.respond_to?(:read) && value.respond_to?(:rewind)
- value.rewind
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(type: "BYTES"),
- parameter_value: API::QueryParameterValue.new(
- value: value.read.force_encoding("ASCII-8BIT"))
- )
- elsif Array === value
- array_params = value.map { |param| to_query_param param }
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(
- type: "ARRAY",
- array_type: array_params.first.parameter_type
- ),
- parameter_value: API::QueryParameterValue.new(
- array_values: array_params.map(&:parameter_value)
- )
- )
- elsif Hash === value
- struct_pairs = value.map do |name, param|
- struct_param = to_query_param param
- [API::QueryParameterType::StructType.new(
- name: String(name),
- type: struct_param.parameter_type
- ), struct_param.parameter_value]
- end
-
- return API::QueryParameter.new(
- parameter_type: API::QueryParameterType.new(
- type: "STRUCT",
- struct_types: struct_pairs.map(&:first)
- ),
- parameter_value: API::QueryParameterValue.new(
- struct_values: struct_pairs.map(&:last)
- )
- )
- else
- fail "A query parameter of type #{value.class} is not supported."
- end
- v
- end
-
# rubocop:enable all
-
- def resolve_legacy_sql legacy_sql, standard_sql
- return legacy_sql unless legacy_sql.nil?
- return !standard_sql unless standard_sql.nil?
- end
##
# Job description for copy job
def copy_table_config source, target, options = {}
API::Job.new(