Unlimited classification of mysql stored procedures

Source: Internet
Author: User

Unlimited classification of mysql stored procedures

Drop table if exists 'pcms _ channel ';

Create table if not exists 'pcms _ channel '(

'Cid' tinyint (3) unsigned not null auto_increment,

'Name' char (10) not null comment' channel name ',

'Parentid' tinyint (4) not null comment 'parent id ',

'Lft 'tinyint (4) not null comment' left value ',

'Rgt 'tinyint (4) not null comment 'right value ',

'Lv' tinyint (3) unsigned not null default '0' comment' level Layer ',

'Themeid' tinyint (3) unsigned not null default '1' comment' topic id ',

Primary key ('cid '),

KEY 'parentid' ('parentid', 'lft ', 'rgt ')

) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 7;

--

-- Export the data in the table 'pcms _ channel'

--

Insert into 'pcms _ channel' ('cid', 'name', 'parentid', 'lft ', 'rgt', 'lv', 'themeid') VALUES

(1, 'phocms ', 0, 1, 12, 0, 1 ),

(2, 'test', 1, 2, 7, 1, 1 ),

(3, 'te', 2, 3, 6, 2, 1 ),

(4, 'tes ', 1, 8, 9, 1, 1 ),

(5, 'dd', 3, 4, 5, 3, 1 ),

(6, 'fromphp', 1, 10, 11, 1, 1 );
Copy the code to clear the TABLE record truncate table 'pcms _ channel'
Copy the code to add the node to the specified node drop procedure if exists addChannel //

Create procedure addChannel (in pid int, in name varchar (20 ))

BEGIN

DECLARE pr INT;/* Right Value */

DECLARE lvv INT;/* level */

DECLARE aff INT;/* counter */

DECLARE af int default 0;

SET @ result = null;

SELECT 'rgt ', 'lv' INTO pr, lvv FROM 'pcms _ channel' WHERE 'cid' = pid;

IF pr THEN

Start transaction;

UPDATE 'pcms _ channel' SET 'lft '= 'lft' + 2 WHERE 'lft '> pr;

SELECT ROW_COUNT () INTO aff;

SET af = aff + af;

UPDATE 'pcms _ channel' SET 'rgt '= 'rgt' + 2 WHERE 'rgt '> = pr;

SELECT ROW_COUNT () INTO aff;

SET af = aff + af;

Insert into 'pcms _ channel' ('name', 'parentid', 'lft ', 'rgt', 'lv') VALUES (name, pid, pr, pr + 1, lvv + 1 );

SELECT ROW_COUNT () INTO aff;

SET af = aff + af;

IF af> = 2 THEN

COMMIT;

SET @ result = 1000;

SELECT 1000 AS result;

ELSE

ROLLBACK;

SET @ result = 1002;

SELECT 1002 AS result;

End if;

ELSE

SET @ result = 1001;

SELECT 1001 AS result;

End if;

END //

Call addChannel (5, "aa ")//
Copy the code to delete the specified node drop procedure if exists delChannel //

Create procedure delChannel (in pid int)

BEGIN

DECLARE pl INT;

DECLARE pn INT;

DECLARE aff INT;

DECLARE af int default 0;

SET @ result = null;

SET @ parentid = null;

SET @ name = null;

SELECT. 'lft ', IFNULL (COUNT (B. 'cid'), 0),. 'parentid',. 'name' INTO pl, pn, @ parentid, @ name FROM 'pcms _ channel' AS a left join 'pcms _ channel' AS B ON. 'cid' = B. 'parentid' WHERE. 'cid' = pid group by B. 'parentid ';

IF pl &&! Pn THEN

IF pl! = 1 THEN

Start transaction;

UPDATE 'pcms _ channel' SET 'lft '= 'lft'-2 WHERE 'lft '> pl;

SELECT ROW_COUNT () INTO aff;

SET af = aff + af;

UPDATE 'pcms _ channel' SET 'rgt '= 'rgt'-2 WHERE 'rgt '> pl;

SELECT ROW_COUNT () INTO aff;

SET af = aff + af;

Delete from 'pcms _ channel' WHERE 'cid' = pid;

SELECT ROW_COUNT () INTO aff;

SET af = aff + af;

IF af> = 2 THEN

COMMIT;

SET @ result = 1000;

SELECT 1000 AS result;

ELSE

ROLLBACK;

SET @ result = 1002;

SELECT 1002 AS result;

End if;

ELSE

SET @ result = 1004;

SELECT 1004 AS result;

End if;

ELSEIF pn & pl THEN

SET @ result = 1003;

SELECT 1003 AS result;

ELSE

SET @ result = 1001;

SELECT 1001 AS result;

End if;

END //

Call delChannel (1 )//
Copy the code to move the node drop procedure if exists moveChannel //

Create procedure moveChannel (pid int, tid int)

BEGIN

IF pid = 1 THEN

SELECT 1004 AS result;

ELSE

IF pid! = Tid THEN

Call delChannel (pid );

IF @ result = 1000 THEN

Call addChannel (tid, @ name );

IF @ result THEN

SELECT 1000 AS result;

ELSE

Call addChannel (@ parentid, @ name );

SELECT @ result AS result;

End if;

ELSE

SELECT @ result AS result;

End if;

ELSE

SELECT 1005 AS result;

End if;

End if;

SET @ result = null;

SET @ parentid = null;

SET @ name = null;

END //

Call moveChannel (1, 1 )//
Copy the code to query the Tree Structure select cid, concat (repeat ("", lv) from pcms_channel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.