Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'gems/click_house-client/lib/click_house/client/formatter.rb')
-rw-r--r--gems/click_house-client/lib/click_house/client/formatter.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/gems/click_house-client/lib/click_house/client/formatter.rb b/gems/click_house-client/lib/click_house/client/formatter.rb
new file mode 100644
index 00000000000..bb60d8db7f7
--- /dev/null
+++ b/gems/click_house-client/lib/click_house/client/formatter.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module ClickHouse
+ module Client
+ class Formatter
+ DEFAULT = ->(value) { value }
+
+ TYPE_CASTERS = {
+ 'UInt64' => ->(value) { Integer(value) },
+ "DateTime64(6, 'UTC')" => ->(value) { ActiveSupport::TimeZone["UTC"].parse(value) }
+ }.freeze
+
+ def self.format(result)
+ name_type_mapping = result['meta'].each_with_object({}) do |column, hash|
+ hash[column['name']] = column['type']
+ end
+
+ result['data'].map do |row|
+ row.each_with_object({}) do |(column, value), casted_row|
+ caster = TYPE_CASTERS.fetch(name_type_mapping[column], DEFAULT)
+
+ casted_row[column] = caster.call(value)
+ end
+ end
+ end
+ end
+ end
+end