Module: N::SqlTreeTraversable

Defined in:
lib/n/db/mixins.rb

Overview

SqlTreeTraversable

Implements the common Preorder Tree Traversal SQL pattern. Usefull if you need to combine a Hierarchical view with an SQL LIMIT clause.

The technique is further discussed here: www.ibase.ru/devinfo/DBMSTrees/sqltrees.html

depth is used in the limit scenarios. This pattern works well with stored procedures.

initialy depth = 0, lft = 0, rgt = 1

Optimization: instead of lft,rgt it only stores tree_index, so it cannot generate subtrees.

Instance Method Summary collapse

Instance Method Details

#__db_pre_insert(conn) ⇒ Object

If depth, lft, rgt are not initialized try to autocalculate from the parent.



240
241
242
243
244
245
246
247
# File 'lib/n/db/mixins.rb', line 240

def __db_pre_insert(conn)
	super
	unless @depth
			@depth = 0
			@lft = 0
			@rgt = 1
	end
end

#insert_into_sql_tree(prgt, pdepth) ⇒ Object



249
250
251
252
253
# File 'lib/n/db/mixins.rb', line 249

def insert_into_sql_tree(prgt, pdepth)
	@depth = pdepth.to_i() + 1
	@lft = prgt.to_i()
	@rgt = @lft + 1
end