Sha256: c64af08aeab8f3d39605a58fb4a722ad017734b4f34aa01d6a881d42bf25fe28

Contents?: true

Size: 1.56 KB

Versions: 15

Compression:

Stored size: 1.56 KB

Contents

module SafetyPin
  class Query
    def self.execute(query_string)
      query = query_manager.create_query(query_string, "JCR-SQL2")
      node_iter = query.execute.nodes
      nodes = []
      while node_iter.has_next
        nodes << Node.new(node_iter.next_node)
      end
      nodes
    end
    
    def self.query_manager
      JCR.session.workspace.query_manager
    end
    
    def sql
      [select_statement, where_statement].compact.join(" ")
    end
    
    def type(type)
      @type = type
    end
    
    def where(properties)
      self.where_conditions = where_conditions + properties.map {|name, value| WhereCondition.new(name, value) }
    end
    
    def within(path)
      @within ||= []
      if path.is_a? String
        @within << path
      elsif path.is_a? Array
        @within += path
      end
    end
    
    def where_conditions
      @where_conditions ||= []
    end
    
    def where_conditions=(where_conditions)
      @where_conditions = where_conditions
    end
    
    private
    def select_statement
      type = @type || "nt:base"
      "SELECT * FROM [#{type}]"
    end
    
    def where_statement
      "WHERE #{where_sql.join(" AND ")}" unless where_conditions.empty? and within_path_conditions.empty?
    end
    
    def where_sql
      (where_conditions + within_path_conditions).map(&:sql_fragment)
    end
    
    def within_path_conditions
      unless @within.nil?
        @within.map {|path| WhereCondition.new("jcr:path", "#{path}%", "LIKE") }
      else 
        []
      end
    end
  end
  
  class InvalidQuery < Exception; end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
safety-pin-0.1.5 lib/safety_pin/query.rb
safety-pin-0.1.4 lib/safety_pin/query.rb
safety-pin-0.1.3 lib/safety_pin/query.rb
safety-pin-0.1.2 lib/safety_pin/query.rb
safety-pin-0.1.1 lib/safety_pin/query.rb
safety-pin-0.1.0 lib/safety_pin/query.rb
safety-pin-0.0.9 lib/safety_pin/query.rb
safety_pin-0.0.8 lib/safety_pin/query.rb
safety_pin-0.0.7 lib/safety_pin/query.rb
safety_pin-0.0.6 lib/safety_pin/query.rb
safety_pin-0.0.5 lib/safety_pin/query.rb
safety_pin-0.0.4 lib/safety_pin/query.rb
safety_pin-0.0.3 lib/safety_pin/query.rb
safety_pin-0.0.2 lib/safety_pin/query.rb
safety_pin-0.0.1 lib/safety_pin/query.rb