<commit_before># encoding: utf-8

require_relative '../../../importer/lib/importer/cartodb_id_query_batcher'
require_relative '../../../importer/lib/importer/query_batcher'

module CartoDB
  module InternalGeocoder

    class LatitudeLongitude

      def initialize(db, logger = nil)
        @db = db
        @logger = logger
      end

      def geocode(table_schema, table_name, latitude_column, longitude_column)
        qualified_table_name = "\"#{table_schema}\".\"#{table_name}\""
        query_fragment_update = %Q{
          UPDATE #{qualified_table_name}
          SET
            the_geom = ST_GeomFromText(
              'POINT(' || REPLACE(TRIM(CAST("#{longitude_column}" AS text)), ',', '.') || ' ' ||
                REPLACE(TRIM(CAST("#{latitude_column}" AS text)), ',', '.') || ')', #{CartoDB::SRID}
            )
        }
        # TODO: should we avoid overwriting the_geom?
        query_fragment_where = %Q{
          REPLACE(TRIM(CAST("#{longitude_column}" AS text)), ',', '.') ~
            '^(([-+]?(([0-9]|[1-9][0-9]|1[0-7][0-9])(\.[0-9]+)?))|[-+]?180)$'
          AND REPLACE(TRIM(CAST("#{latitude_column}" AS text)), ',', '.')  ~
            '^(([-+]?(([0-9]|[1-8][0-9])(\.[0-9]+)?))|[-+]?90)$'
        }

        # TODO: next time this decision is needed should be refactored out to a factory
        if(table_has_cartodb_id(table_schema, table_name))
          CartoDB::Importer2::CartodbIdQueryBatcher.new(@db, @logger).execute(
              %Q{#{query_fragment_update} where #{query_fragment_where}},
              table_schema, table_name
          )
        else
        CartoDB::Importer2::QueryBatcher::execute(
          @db,
          %Q{
            #{query_fragment_update} 
            #{CartoDB::Importer2::QueryBatcher::QUERY_WHERE_PLACEHOLDER}
            where #{query_fragment_where} 
            #{CartoDB::Importer2::QueryBatcher::QUERY_LIMIT_SUBQUERY_PLACEHOLDER}<commit_msg>Inline constant<commit_after># encoding: utf-8

require_relative '../../../importer/lib/importer/cartodb_id_query_batcher'
require_relative '../../../importer/lib/importer/query_batcher'

module CartoDB
  module InternalGeocoder

    class LatitudeLongitude

      def initialize(db, logger = nil)
        @db = db
        @logger = logger
      end

      def geocode(table_schema, table_name, latitude_column, longitude_column)
        qualified_table_name = "\"#{table_schema}\".\"#{table_name}\""
        query_fragment_update = %Q{
          UPDATE #{qualified_table_name}
          SET
            the_geom = ST_GeomFromText(
              'POINT(' || REPLACE(TRIM(CAST("#{longitude_column}" AS text)), ',', '.') || ' ' ||
                REPLACE(TRIM(CAST("#{latitude_column}" AS text)), ',', '.') || ')', 4326
            )
        }
        # TODO: should we avoid overwriting the_geom?
        query_fragment_where = %Q{
          REPLACE(TRIM(CAST("#{longitude_column}" AS text)), ',', '.') ~
            '^(([-+]?(([0-9]|[1-9][0-9]|1[0-7][0-9])(\.[0-9]+)?))|[-+]?180)$'
          AND REPLACE(TRIM(CAST("#{latitude_column}" AS text)), ',', '.')  ~
            '^(([-+]?(([0-9]|[1-8][0-9])(\.[0-9]+)?))|[-+]?90)$'
        }

        # TODO: next time this decision is needed should be refactored out to a factory
        if(table_has_cartodb_id(table_schema, table_name))
          CartoDB::Importer2::CartodbIdQueryBatcher.new(@db, @logger).execute(
              %Q{#{query_fragment_update} where #{query_fragment_where}},
              table_schema, table_name
          )
        else
        CartoDB::Importer2::QueryBatcher::execute(
          @db,
          %Q{
            #{query_fragment_update} 
            #{CartoDB::Importer2::QueryBatcher::QUERY_WHERE_PLACEHOLDER}
            where #{query_fragment_where} 
            #{CartoDB::Importer2::QueryBatcher::QUERY_LIMIT_SUBQUERY_PLACEHOLDER}
{'fraction_non_alphanumeric': 0.17535174570088588, 'fraction_numerical': 0.017196456487754037, 'mean_word_length': 2.5546296296296296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2653723', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1293, 'n_words': 186}