find_possible_truncations

in lib/pg_query/truncate.rb [53:87]


    def find_possible_truncations 
      truncations = []

      treewalker! @tree do |node, k, v, location|
        case k
        when :target_list
          next unless node.is_a?(PgQuery::SelectStmt) || node.is_a?(PgQuery::UpdateStmt) || node.is_a?(PgQuery::OnConflictClause)
          length = PgQuery.deparse_stmt(PgQuery::SelectStmt.new(k => v.to_a, op: :SETOP_NONE)).size - 7 
          truncations << PossibleTruncation.new(location, :target_list, length, true)
        when :where_clause
          next unless node.is_a?(PgQuery::SelectStmt) || node.is_a?(PgQuery::UpdateStmt) || node.is_a?(PgQuery::DeleteStmt) ||
                      node.is_a?(PgQuery::CopyStmt) || node.is_a?(PgQuery::IndexStmt) || node.is_a?(PgQuery::RuleStmt) ||
                      node.is_a?(PgQuery::InferClause) || node.is_a?(PgQuery::OnConflictClause)

          length = PgQuery.deparse_expr(v).size
          truncations << PossibleTruncation.new(location, :where_clause, length, false)
        when :ctequery
          next unless node.is_a?(PgQuery::CommonTableExpr)
          length = PgQuery.deparse_stmt(v[v.node.to_s]).size
          truncations << PossibleTruncation.new(location, :ctequery, length, false)
        when :cols
          next unless node.is_a?(PgQuery::InsertStmt)
          length = PgQuery.deparse_stmt(
            PgQuery::InsertStmt.new(
              relation: PgQuery::RangeVar.new(relname: 'x', inh: true),
              cols: v.to_a
            )
          ).size - 31 
          truncations << PossibleTruncation.new(location, :cols, length, true)
        end
      end

      truncations
    end