Advertisement
Guest User

Generate Codes Stored Procedure

a guest
Feb 10th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.28 KB | None | 0 0
  1. DELIMITER ;;
  2. DROP PROCEDURE `generate_codes`;;
  3. CREATE DEFINER=`root`@`localhost` PROCEDURE `generate_codes`(
  4.     IN bf_allocation_num INT
  5. )
  6. BEGIN
  7.  
  8.     SET @Allocation = bf_allocation_num;
  9.     SET @Tmptable = CONCAT( 'tmp_code_generation_jobs_', @Allocation );
  10.     SET @Canceled = 0;
  11.    
  12.     SET @qry_vars = "SELECT
  13.         `cj`.`code_count`,
  14.         `s`.`prefix`,
  15.         `c`.`set`,
  16.         LENGTH(`c`.`set`)
  17.     FROM
  18.         `code_generation_jobs` AS `cj`
  19.    
  20.     JOIN `skus` AS `s` ON `s`.`id` = `cj`.`sku_id`
  21.     JOIN `charsets` AS `c` ON `c`.`id` = `cj`.`charset_id`
  22.    
  23.     WHERE `cj`.`id` = @Allocation
  24.    
  25.     INTO
  26.         @Codes,
  27.         @Prefix,
  28.         @Chars,
  29.         @CChars";
  30.        
  31.     PREPARE qry_vars_stmt FROM @qry_vars;
  32.     EXECUTE qry_vars_stmt;
  33.     DEALLOCATE PREPARE qry_vars_stmt;
  34.    
  35.     SET @qry_status1 = CONCAT('CREATE TABLE `', @Tmptable, '` (count int) ENGINE = MEMORY');
  36.     PREPARE qry_status_stmt1 FROM @qry_status1;
  37.     EXECUTE qry_status_stmt1;
  38.     DEALLOCATE PREPARE qry_status_stmt1;
  39.    
  40.     SET @qry_status2 = CONCAT('TRUNCATE TABLE `', @Tmptable, '`');
  41.     SET @qry_status3 = CONCAT('INSERT INTO `', @Tmptable, '` (`count`) VALUES (@Codes)');
  42.     SET @qry_status4 = CONCAT('DROP TABLE `', @Tmptable, '`');
  43.     PREPARE qry_status_stmt2 FROM @qry_status2;
  44.     PREPARE qry_status_stmt3 FROM @qry_status3;
  45.     PREPARE qry_status_stmt4 FROM @qry_status4;
  46.  
  47.  
  48.     SET @qry_rand = "SELECT CONCAT(
  49.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  50.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  51.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  52.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  53.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  54.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  55.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  56.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  57.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  58.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  59.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1),
  60.         SUBSTRING(@Chars, 0.5 + RAND() * @CChars, 1)
  61.     ) INTO @Random";
  62.     PREPARE qry_rand_stmt FROM @qry_rand;
  63.  
  64.     SET @qry_insert = CONCAT('INSERT IGNORE INTO `codes_', @Prefix, '` (`code`,`allocation`) VALUES ( @Random, @Allocation )');
  65.     PREPARE qry_insert_stmt FROM @qry_insert;
  66.  
  67.     SET @qry_complete = "UPDATE `code_generation_jobs` SET `status` = 'complete' WHERE `id` = @Allocation";
  68.     PREPARE qry_complete_stmt FROM @qry_complete;
  69.    
  70.     SET @qry_canceled = "SELECT IF( `status` = 'canceled', 1, 0 ) INTO @Canceled FROM `code_generation_jobs` WHERE `id` = @Allocation";
  71.     PREPARE qry_canceled_stmt FROM @qry_canceled;
  72.  
  73.     START TRANSACTION;
  74.  
  75.     code_gen_loop: WHILE @Codes > 0 DO
  76.  
  77.         IF MOD( @Codes, 10000 ) = 0 THEN
  78.            
  79.             EXECUTE qry_status_stmt2;
  80.             EXECUTE qry_status_stmt3;
  81.             EXECUTE qry_canceled_stmt;
  82.            
  83.             SAVEPOINT foo;
  84.             ROLLBACK TO SAVEPOINT foo;
  85.            
  86.             IF @Canceled = 1 THEN
  87.                 ROLLBACK;
  88.                 LEAVE code_gen_loop;
  89.             END IF;
  90.            
  91.         END IF;
  92.  
  93.         EXECUTE qry_rand_stmt;
  94.         EXECUTE qry_insert_stmt;
  95.  
  96.         SET @Codes = @Codes - ROW_COUNT();
  97.  
  98.     END WHILE;
  99.  
  100.     IF @Canceled = 0 THEN
  101.         COMMIT;
  102.         EXECUTE qry_complete_stmt;
  103.     END IF;
  104.  
  105.     EXECUTE qry_status_stmt4;
  106.  
  107.     DEALLOCATE PREPARE qry_rand_stmt;
  108.     DEALLOCATE PREPARE qry_insert_stmt;
  109.     DEALLOCATE PREPARE qry_status_stmt2;
  110.     DEALLOCATE PREPARE qry_status_stmt3;
  111.     DEALLOCATE PREPARE qry_status_stmt4;
  112.     DEALLOCATE PREPARE qry_complete_stmt;
  113.  
  114. END;;
  115. DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement