Reply to comment

Drupal Contribution: Generate vancode using mysql alone

I once had to do a migration entirely on mysql and find myself spending a quite fair amount of time digging about the vancode thing. and then I thought why not sharing ? so here it is... two mysql functions do the magic..

 

/**
*
*/
DELIMITER $$

USE `%new_db%`$$

DROP FUNCTION IF EXISTS `int2vancode`$$

CREATE FUNCTION `int2vancode`(_max INT) RETURNS VARCHAR(255) CHARSET latin1
BEGIN
        /* int2vancode as seen on drupal :P */ 
        /* Convert it again to base 36 */
        SET @__num = (SELECT CONV(_max ,10,36));
        RETURN (SELECT CONCAT(CHAR(CHAR_LENGTH( @__num) + ORD(0) -1),@__num));
END$$

USE `%new_db%`$$

DROP FUNCTION IF EXISTS `vancode2int`$$

CREATE FUNCTION `vancode2int`() RETURNS INT(11)
BEGIN
        /* Get the latest Thread ID from comments */
        SET @__max_thread = (SELECT CASE WHEN ISNULL(MAX(thread)) OR  MAX(thread) = '' THEN 0 ELSE MAX(thread) END FROM comments);
        /* Clean Thread ID <?php rtrin(@__max_thread,'/'); ?>*/
        SET @__clean_max_thread = (SELECT SUBSTRING(@__max_thread, 1 ,CASE WHEN CHAR_LENGTH(@__max_thread) = 1 THEN 1 ELSE CHAR_LENGTH(@__max_thread)-1 END));
        /* vancode2int as it was on drupal */  
        RETURN (SELECT CONV(SUBSTRING(@__clean_max_thread,CASE WHEN CHAR_LENGTH(@__clean_max_thread) = 1 THEN 1 ELSE 2 END),36,10));       
END$$

DELIMITER ;

and here's how to use them

 

/* insert comments from system X into drupal */
%new_db% = drupal database
%old_db% your old DB
%[new|old]_p% prefix

  /* Only valid drupal users are allowed to comment */
    INSERT INTO %new_db%.%new_p%comments
        (
            nid,
            uid,
            COMMENT,
            TIMESTAMP,
            STATUS,
            thread
        )
        SELECT
            @_nid,
            d6.uid,
            tc.commen,
            tc.addtime,
            CASE WHEN tc.is_approved = 'Yes' THEN 0 ELSE 1 END,
            CONCAT(int2vancode(vancode2int() + 1),'/')
        FROM
            %old_db%.comments tc,
            %new_db%.%new_p%users d6
        WHERE
            tc.uid = d6.uid AND
            tc.vid = new.tangle_vid;

you can safely add a new index to you drupal comments.thread column to speed things up... this is a single level solution (ie no multi-level comments, but can be easily tweaked)

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <b> <address> <blockquote> <br> <caption> <center> <code> <dd> <del> <div> <dl> <dt> <em> <font> <h2> <h3> <h4> <h5> <h6> <hr> <i> <img> <li> <ol> <p> <pre> <span> <strong> <sub> <sup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <u> <ul> <tr>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
11 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.