Sha256: a5443083dfec0c288b2e31522e49a76302d540cbee62054a7a299e791e46bdce

Contents?: true

Size: 1.82 KB

Versions: 13

Compression:

Stored size: 1.82 KB

Contents

module DirectiveRecord
  module Query
    class BigQuery < SQL

    private

      def aggregate_delimiter
        "__"
      end

      def normalize_from!(options)
        period = options[:period]

        options[:where].delete_if do |statement|
          if statement.match(/^#{period} = '(\d{4}-\d{2}-\d{2})'$/)
            begin_date, end_date = $1, $1
          elsif statement.match(/^#{period} >= '(\d{4}-\d{2}-\d{2})' AND #{period} <= '(\d{4}-\d{2}-\d{2})'$/)
            begin_date, end_date = $1, $2
          end
          if begin_date
            dataset = ::BigQuery.connection.instance_variable_get(:@dataset) # TODO: fix this
            options[:from] = "\n  TABLE_DATE_RANGE(#{dataset}.#{base.table_name}_, TIMESTAMP('#{begin_date}'), TIMESTAMP('#{end_date}'))"
          end
        end
      end

      def prepend_base_alias!(options); end

      def finalize_options!(options)
        aliases = options[:aliases] || {}

        options[:select].collect! do |string|
          if string.match(/^(.*) AS (.*)$/)
            select_expression, select_alias = $1, $2
            aliases[select_expression] = select_alias
          end

          select_expression ||= string
          group_by_expression = select_alias || string

          if options[:group_by].include?(group_by_expression) || options[:group_by].include?(select_expression) || !select_expression.match(/^\w+(\.\w+)*$/)
            string
          else
            ["MAX(#{select_expression})", select_alias].compact.join(" AS ")
          end
        end if options[:group_by]

        [:group_by, :having, :order_by].each do |key|
          if value = options[key]
            value = value.join ", "
            aliases.each{|pattern, replacement| value.gsub! pattern, replacement}
            options[key] = value
          end
        end
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
directiverecord-0.1.20 lib/directive_record/query/big_query.rb
directiverecord-0.1.19 lib/directive_record/query/big_query.rb
directiverecord-0.1.18 lib/directive_record/query/big_query.rb
directiverecord-0.1.17 lib/directive_record/query/big_query.rb
directiverecord-0.1.16 lib/directive_record/query/big_query.rb
directiverecord-0.1.15 lib/directive_record/query/big_query.rb
directiverecord-0.1.14 lib/directive_record/query/big_query.rb
directiverecord-0.1.13 lib/directive_record/query/big_query.rb
directiverecord-0.1.12 lib/directive_record/query/big_query.rb
directiverecord-0.1.11 lib/directive_record/query/big_query.rb
directiverecord-0.1.10 lib/directive_record/query/big_query.rb
directiverecord-0.1.9 lib/directive_record/query/big_query.rb
directiverecord-0.1.8 lib/directive_record/query/big_query.rb