#|

src/models/category-2.lisp

Category v2

Based on DB_NestedSet <https://pear.php.net/manual/en/package.DB_NestedSet.php>

It looks like keepAsArray controls whether or not it outputs a list or
something passed through MAKE-INSTANCE.

|#

(defpackage #:cl-deck-builder2.models.category-2
  (:use #:cl))

(:in-package #:cl-deck-builder2.models.category-2)

;; CLASSES


#|
# The table which holds the structure
CREATE TABLE tb_nodes (
  STRID int(11) NOT NULL auto_increment,
  ROOTID int(11) NOT NULL default '0',
  l int(11) NOT NULL default '0',
  r int(11) NOT NULL default '0',
  PARENT int(11) NOT NULL default '0',
  STREH int(11) NOT NULL default '0',
  LEVEL int(11) NOT NULL default '0',
  STRNA char(128) NOT NULL default '',
  PRIMARY KEY  (STRID),
  KEY ROOTID (ROOTID),
  KEY STREH (STREH),
  KEY l (l),
  KEY r (r),
  KEY LEVEL (LEVEL),
  KEY SRLR (ROOTID,l,r),
  KEY parent (PARENT)
) TYPE=MyISAM COMMENT='NestedSet table';
|#

(defclass db-nested-set-node (registered-table-class)
  ((root-id        :col-type :integer           :default 1)
   (left           :col-type :integer           :default 1)
   (right          :col-type :integer           :default 1)
   (parent         :col-type :integer           :default 1))
  ;; (streh          :col-type :integer           :default 0)
  ;; (level          :col-type :integer           :default 0)
  ;; (strna          :col-type (:varchar 128)     :default "")
  (:documentation "Class for DB_NestedSet Nodes."))

(defmethod closer-mop:validate-superclass ((class db-nested-set-node)
                                           (superclass registered-table-class))
  t)



#|
# A table which is used for a little table locking to avoid conflicts
CREATE TABLE tb_locks (
  lockID char(32) NOT NULL default '',
  lockTable char(32) NOT NULL default '',
  lockStamp int(11) NOT NULL default '0',
  PRIMARY KEY  (lockID,lockTable)
) TYPE=MyISAM COMMENT='Table locks for NestedSet';
|#

(defclass db-nested-set-lock ()
  (;; TODO Being able to switch the table name would be cool. With
   ;; MITO It would be a class. I'm not sure how to code that.
   (lock-table :col-type (:varchar 32))
   (lock-stamp :col-type :timestamp
               :initform (local-time:now)
               :inflate #'local-time:universal-to-timestamp
               :deflate #'local-time:timestamp-to-universal))
  (:metaclass registered-table-class)
  (:documentation "Class for DB_NestedSet Locks Table."))

;; Helper Class
(defclass db-nested-set ()
  ((db-nested-set-nodes :accessor nested-set-nodes
                        :initform '())
   (db-nested-set-locks :accessor nested-set-nodes
                        :initform '())))

;; GENERICS

(defgeneric create-root-node (obj))
(defgeneric create-left-node (obj))
(defgeneric create-right-node (obj))
(defgeneric create-sub-node (obj))

(defgeneric delete-node (obj))

(defgeneric get-all-nodes (obj))
(defgeneric get-root-nodes (obj))

(defgeneric pick-node (obj))

(defgeneric update-node (obj))

(defgeneric get-branch (obj))
(defgeneric get-children (obj))
(defgeneric get-parent (obj))
(defgeneric get-parents (obj))
(defgeneric get-siblings (obj))
(defgeneric get-sub-branch (obj))

(defgeneric is-parent (obj))

(defgeneric move-across (obj))
(defgeneric move-cleanup (obj))
(defgeneric move-root-to-root (obj))
(defgeneric move-tree (obj))

;; METHODS

(defmethod create-root-node ((obj db-nested-set)))
(defmethod create-left-node ((obj db-nested-set)))
(defmethod create-right-node ((obj db-nested-set)))
(defmethod create-sub-node ((obj db-nested-set)))

(defmethod delete-node ((obj db-nested-set)))

(defmethod get-all-nodes ((obj db-nested-set)))
(defmethod get-root-nodes ((obj db-nested-set)))

(defmethod pick-node ((obj db-nested-set)))

(defmethod update-node ((obj db-nested-set)))

(defmethod get-branch ((obj db-nested-set)))
(defmethod get-children ((obj db-nested-set)))
(defmethod get-parent ((obj db-nested-set)))
(defmethod get-parents ((obj db-nested-set)))
(defmethod get-siblings ((obj db-nested-set)))
(defmethod get-sub-branch ((obj db-nested-set)))

(defmethod is-parent ((obj db-nested-set)))

(defmethod move-across ((obj db-nested-set)))
(defmethod move-cleanup ((obj db-nested-set)))
(defmethod move-root-to-root ((obj db-nested-set)))
(defmethod move-tree ((obj db-nested-set)))

#|
@$this->cache->flush('function_cache');
function & factory($driver, $dsn, $params = array()) {
function DB_NestedSet($params) {
function _DB_NestedSet() {
function _addSQL($addSQL, $type, $prefix = false) {
function _debugMessage($msg) {
function _getMessage($code) {
function _getSelectFields($aliasFields) {
function _lockGC() {
function _moveAcross($source, $target, $pos, $first = false) {
function _moveCleanup($copy = false) {
function _moveRoot2Root($source, $target, $pos) {
function _processResultSet($sql, $keepAsArray, $fieldsAreAliased) {
function _raiseError($code, $mode, $option, $epr = array()) {
function _releaseLock($exclusive = false) {
function _secSort($nodeSet) {
function _secSortCollect($segment, $deepArray, $reset = false) {
function _setLock($exclusive = false) {
function _testFatalAbort($errobj, $file, $line) {
function _values2InsertQuery($values, $addval = false) {
function _values2UpdateQuery($values, $addval = false) {
function _verifyUserValues($caller, & $values) {
function addListener($event, & $listener) {
function apiVersion() {
function convertTreeModel(& $orig, & $copy, $_parent = false) {
function createLeftNode($id, $values) {
function createRightNode($id, $values) {
function createRootNode($values, $id = false, $first = false, $pos = NESE_MOVE_AFTER) {
function createSubNode($id, $values) {
function deleteNode($id) {
function getAllNodes($keepAsArray = false, $aliasFields = true, $addSQL = array()) {
function getBranch($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {
function getChildren($id, $keepAsArray = false, $aliasFields = true, $forceNorder = false, $addSQL = array()) {
function getParent($id, $keepAsArray = false, $aliasFields = true, $addSQL = array(), $useDB = true) {
function getParents($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {
function getRootNodes($keepAsArray = false, $aliasFields = true, $addSQL = array()) {
function getSiblings($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {
function getSubBranch($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {
function isParent($parent, $child) {
function moveTree($id, $targetid, $pos, $copy = false) {
function pickNode($id, $keepAsArray = false, $aliasFields = true, $idfield = 'id', $addSQL = array()) {
function removeListener($event, $listenerID) {
function setAttr($attr) {
function setDbOption($option, $val) {
function setsortMode($sortMode = false) {
function testLock() {
function triggerEvent($event, & $node, $eparams = false) {
function updateNode($id, $values, $_internal = false) {
register_shutdown_function(array(& $this, '_DB_NestedSet'));
|#
