最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Creating mySQL procedure with $wpdb

programmeradmin3浏览0评论

I'm trying to create a calendar table upon activation of a custom plugin I am building. The procedure script I am using can be seen here:

The first part that creates the time_dimension table works fine but the procedure does not. I read a post about using dbDelta() instead of $wpdb but it hasn't helped. There is an error on line 13 of the script on the github page but I have resolved that.

When I go into phpMyAdmin and run the script manually it works fine but an alert does pop up to confirm that you want to run the procedure so I wonder is that causing an issue.

Thanks for reading!

function activate_wp_ssm()
{

require_once plugin_dir_path(__FILE__) . 'includes/class-wp-ssm-activator.php';
wp_ssm_Activator::activate();
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

global $wpdb;
$sql = "DROP TABLE IF EXISTS time_dimension;
CREATE TABLE time_dimension (
        id                      INTEGER PRIMARY KEY,  -- year*10000+month*100+day
        db_date                 DATE NOT NULL,
        year                    INTEGER NOT NULL,
        month                   INTEGER NOT NULL, -- 1 to 12
        day                     INTEGER NOT NULL, -- 1 to 31
        quarter                 INTEGER NOT NULL, -- 1 to 4
        week                    INTEGER NOT NULL, -- 1 to 52/53
        day_name                VARCHAR(9) NOT NULL, -- 'Monday', 'Tuesday'...
        month_name              VARCHAR(9) NOT NULL, -- 'January', 'February'...
        holiday_flag            CHAR(1) DEFAULT 'f' CHECK (holiday_flag in ('t', 'f')),
        weekend_flag            CHAR(1) DEFAULT 'f' CHECK (weekend_flag in ('t', 'f')),
        event                   VARCHAR(50),
        UNIQUE td_ymd_idx (year,month,day),
        UNIQUE td_dbdate_idx (db_date)

) Engine=MyISAM;";

$create_procedure = "DROP PROCEDURE IF EXISTS fill_date_dimension;
DELIMITER //
CREATE PROCEDURE fill_date_dimension(IN startdate DATE,IN stopdate DATE)
BEGIN
DECLARE currentdate DATE;
SET currentdate = startdate;
WHILE currentdate < stopdate DO
    INSERT INTO time_dimension VALUES (
                YEAR(currentdate)*10000+MONTH(currentdate)*100 + DAY(currentdate),
                currentdate,
                YEAR(currentdate),
                MONTH(currentdate),
                DAY(currentdate),
                QUARTER(currentdate),
                WEEKOFYEAR(currentdate),
                DATE_FORMAT(currentdate,'%W'),
                DATE_FORMAT(currentdate,'%M'),
                'f',
                CASE DAYOFWEEK(currentdate) WHEN 1 THEN 't' WHEN 7 then 't' ELSE 'f' END,
                NULL);
SET currentdate = ADDDATE(currentdate,INTERVAL 1 DAY);
END WHILE;
END
// DELIMITER;

TRUNCATE TABLE time_dimension;";

$call_procedure = "CALL fill_date_dimension('1-01-01','2015-01-01');
OPTIMIZE TABLE time_dimension";

dbDelta($sql);
dbDelta( $create_procedure );
dbDelta( $call_procedure );
}
发布评论

评论列表(0)

  1. 暂无评论