HEX
Server: Microsoft-IIS/10.0
System: Windows NT ITPWINWEBSVR22 10.0 build 20348 (Windows Server 2022) AMD64
User: www.conferencesearch.co.uk (0)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: D:/MariaDB Data/mysql/proc.MAD
�o۶��[P��m�&-I�$Im۶�۶m۶m۶m۶m۶m۶m۶m۶q����������n�=�[�S�X��@����@�i��9i��9S&�
:&sysps_trace_threadps_trace_threadutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_thread_id BIGINT UNSIGNED,
        IN in_outfile VARCHAR(255),
        IN in_max_runtime DECIMAL(20,2),
        IN in_interval DECIMAL(20,2),
        IN in_start_fresh BOOLEAN,
        IN in_auto_setup BOOLEAN,
        IN in_debug BOOLEAN
    �i��3i��36�	�sysextract_table_from_file_nameextract_table_from_file_nameutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostpath VARCHAR(512)
    varchar(64) CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    RETURN LEFT(SUBSTRING_INDEX(REPLACE(SUBSTRING_INDEX(REPLACE(path, '\\', '/'), '/', -1), '@0024', '$'), '.', 1), 64);
END
             Description
             Takes a raw file path, and extracts the table name from it.
             Useful for when interacting with Performance Schema data
             concerning IO statistics, for example.
             Parameters
             path (VARCHAR(512)):
               The full file path to a data file to extract the table name from.
             Returns
             VARCHAR(64)
             Example
             mysql> SELECT sys.extract_table_from_file_name('/var/lib/mysql/employees/employee.ibd');
             +---------------------------------------------------------------------------+
             | sys.extract_table_from_file_name('/var/lib/mysql/employees/employee.ibd') |
             +---------------------------------------------------------------------------+
             | employee                                                                  |
             +---------------------------------------------------------------------------+
             1 row in set (0.02 sec)
            BEGIN
    RETURN LEFT(SUBSTRING_INDEX(REPLACE(SUBSTRING_INDEX(REPLACE(path, '\', '/'), '/', -1), '@0024', '$'), '.', 1), 64);
END�i��3i��3/�sysformat_bytesformat_bytesutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostbytes TEXT
    text CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
  IF bytes IS NULL THEN RETURN NULL;
  ELSEIF bytes >= 1125899906842624 THEN RETURN CONCAT(ROUND(bytes / 1125899906842624, 2), ' PiB');
  ELSEIF bytes >= 1099511627776 THEN RETURN CONCAT(ROUND(bytes / 1099511627776, 2), ' TiB');
  ELSEIF bytes >= 1073741824 THEN RETURN CONCAT(ROUND(bytes / 1073741824, 2), ' GiB');
  ELSEIF bytes >= 1048576 THEN RETURN CONCAT(ROUND(bytes / 1048576, 2), ' MiB');
  ELSEIF bytes >= 1024 THEN RETURN CONCAT(ROUND(bytes / 1024, 2), ' KiB');
  ELSE RETURN CONCAT(ROUND(bytes, 0), ' bytes');
  END IF;
END
             Description
             Takes a raw bytes value, and converts it to a human readable format.
             Parameters
             bytes (TEXT):
               A raw bytes value.
             Returns
             TEXT
             Example
             mysql> SELECT sys.format_bytes(2348723492723746) AS size;
             +----------+
             | size     |
             +----------+
             | 2.09 PiB |
             +----------+
             1 row in set (0.00 sec)
             mysql> SELECT sys.format_bytes(2348723492723) AS size;
             +----------+
             | size     |
             +----------+
             | 2.14 TiB |
             +----------+
             1 row in set (0.00 sec)
             mysql> SELECT sys.format_bytes(23487234) AS size;
             +-----------+
             | size      |
             +-----------+
             | 22.40 MiB |
             +-----------+
             1 row in set (0.00 sec)
            BEGIN
  IF bytes IS NULL THEN RETURN NULL;
  ELSEIF bytes >= 1125899906842624 THEN RETURN CONCAT(ROUND(bytes / 1125899906842624, 2), ' PiB');
  ELSEIF bytes >= 1099511627776 THEN RETURN CONCAT(ROUND(bytes / 1099511627776, 2), ' TiB');
  ELSEIF bytes >= 1073741824 THEN RETURN CONCAT(ROUND(bytes / 1073741824, 2), ' GiB');
  ELSEIF bytes >= 1048576 THEN RETURN CONCAT(ROUND(bytes / 1048576, 2), ' MiB');
  ELSEIF bytes >= 1024 THEN RETURN CONCAT(ROUND(bytes / 1024, 2), ' KiB');
  ELSE RETURN CONCAT(ROUND(bytes, 0), ' bytes');
  END IF;
END�������i��4i��421{Z^sysps_thread_stackps_thread_stackutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostthd_id BIGINT UNSIGNED,
        debug BOOLEAN
    longtext CHARSET latin1 COLLATE latin1_swedish_ci������i��4i��4!30n-sysps_thread_trx_infops_thread_trx_infoutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_thread_id BIGINT UNSIGNED
    longtext CHARSET utf8mb3 COLLATE utf8mb3_general_ci�����i��8i��8J)�	)syscreate_synonym_dbcreate_synonym_dbutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_db_name VARCHAR(64),
        IN in_synonym VARCHAR(64)
    ��@��+��@
�i��8i��8�C�V��sysdiagnosticsdiagnosticsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_max_runtime int unsigned, IN in_interval int unsigned,
        IN in_auto_config enum ('current', 'medium', 'full')
    ��@���@i��8i��8""�9�9sysps_statement_avg_latency_histogramps_statement_avg_latency_histogramutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost����@�i��8i��8���sysps_trace_statement_digestps_trace_statement_digestutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_digest VARCHAR(32),
        IN in_runtime INT,
        IN in_interval DECIMAL(2,2),
        IN in_start_fresh BOOLEAN,
        IN in_auto_enable BOOLEAN
    �����i��9i��9"��sysps_setup_reset_to_defaultps_setup_reset_to_defaultutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
       IN in_verbose BOOLEAN
    �i��9i��9""���sysps_setup_show_disabled_instrumentsps_setup_show_disabled_instrumentsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    SELECT name AS disabled_instruments, timed
      FROM performance_schema.setup_instruments
     WHERE enabled = 'NO'
     ORDER BY disabled_instruments;
END
             Description
             Shows all currently disabled instruments.
             Parameters
             None
             Example
             mysql> CALL sys.ps_setup_show_disabled_instruments();
            BEGIN
    SELECT name AS disabled_instruments, timed
      FROM performance_schema.setup_instruments
     WHERE enabled = 'NO'
     ORDER BY disabled_instruments;
END�i��:i��:!!���sysps_setup_show_enabled_instrumentsps_setup_show_enabled_instrumentsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    SELECT name AS enabled_instruments, timed
      FROM performance_schema.setup_instruments
     WHERE enabled = 'YES'
     ORDER BY enabled_instruments;
END
             Description
             Shows all currently enabled instruments.
             Parameters
             None
             Example
             mysql> CALL sys.ps_setup_show_enabled_instruments();
            BEGIN
    SELECT name AS enabled_instruments, timed
      FROM performance_schema.setup_instruments
     WHERE enabled = 'YES'
     ORDER BY enabled_instruments;
END    IN in_auto_config enum ('current', 'medium', 'full')
    ��@���@i��8i��8""�9�9sysps_statement_avg_latency_histogramps_statement_avg_latency_histogramutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost����@�i��8i��8���sysps_trace_statement_digestps_trace_statement_digestutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_digest VARCHAR(32),
        IN in_runtime INT,
        IN in_interval DECIMAL(2,2),
        IN in_start_fresh BOOLEAN,
        IN in_auto_enable �������e���2���
�������!YCR�P��j

             Description
             Returns a JSON object with info on the given threads current transaction,
             and the statements it has already executed, derived from the
             performance_schema.events_transactions_current and
             performance_schema.events_statements_history tables (so the consumers
             for these also have to be enabled within Performance Schema to get full
             data in the object).
             When the output exceeds the default truncation length (65535), a JSON error
             object is returned, such as:
             { "error": "Trx info truncated: Row 6 was cut by GROUP_CONCAT()" }
             Similar error objects are returned for other warnings/and exceptions raised
             when calling the function.
             The max length of the output of this function can be controlled with the
             ps_thread_trx_info.max_length variable set via sys_config, or the
             @sys.ps_thread_trx_info.max_length user variable, as appropriate.
             Parameters
             in_thread_id (BIGINT UNSIGNED):
               The id of the thread to return the transaction info for.
             Example
             SELECT sys.ps_thread_trx_info(48)\G
             *************************** 1. row ***************************
             sys.ps_thread_trx_info(48): [
               {
                 "time": "790.70 us",
                 "state": "COMMITTED",
                 "mode": "READ WRITE",
                 "autocommitted": "NO",
                 "gtid": "AUTOMATIC",
                 "isolation": "REPEATABLE READ",
                 "statements_executed": [
                   {
                     "sql_text": "INSERT INTO info VALUES (1, 'foo')",
                     "time": "471.02 us",
                     "schema": "trx",
                     "rows_examined": 0,
                     "rows_affected": 1,
                     "rows_sent": 0,
                     "tmp_tables": 0,
                     "tmp_disk_tables": 0,
                     "sort_rows": 0,
                     "sort_merge_passes": 0
                   },
                   {
                     "sql_text": "COMMIT",
                     "time": "254.42 us",
                     "schema": "trx",
                     "rows_examined": 0,
                     "rows_affected": 0,
                     "rows_sent": 0,
                     "tmp_tables": 0,
                     "tmp_disk_tables": 0,
                     "sort_rows": 0,
                     "sort_merge_passes": 0
                   }
                 ]
               },
               {
                 "time": "426.20 us",
                 "state": "COMMITTED",
                 "mode": "READ WRITE",
                 "autocommitted": "NO",
                 "gtid": "AUTOMATIC",
                 "isolation": "REPEATABLE READ",
                 "statements_executed": [
                   {
                     "sql_text": "INSERT INTO info VALUES (2, 'bar')",
                     "time": "107.33 us",
                     "schema": "trx",
                     "rows_examined": 0,
                     "rows_affected": 1,
                     "rows_sent": 0,
                     "tmp_tables": 0,
                     "tmp_disk_tables": 0,
                     "sort_rows": 0,
                     "sort_merge_passes": 0
                   },
                   {
                     "sql_text": "COMMIT",
                     "time": "213.23 us",
                     "schema": "trx",
                     "rows_examined": 0,
                     "rows_affected": 0,
                     "rows_sent": 0,
                     "tmp_tables": 0,
                     "tmp_disk_tables": 0,
                     "sort_rows": 0,
                     "sort_merge_passes": 0
                   }
                 ]
               }
             ]
             1 row in set (0.03 sec)
            
             Description
             Outputs a textual histogram graph of the average latency values
             across all normalized queries tracked within the Performance Schema
             events_statements_summary_by_digest table.
             Can be used to show a very high level picture of what kind of
             latency distribution statements running within this instance have.
             Parameters
             None.
             Example
             mysql> CALL sys.ps_statement_avg_latency_histogram()\G
             *************************** 1. row ***************************
             Performance Schema Statement Digest Average Latency Histogram:
               . = 1 unit
               * = 2 units
               # = 3 units
             (0 - 38ms)     240 | ################################################################################
             (38 - 77ms)    38  | ......................................
             (77 - 115ms)   3   | ...
             (115 - 154ms)  62  | *******************************
             (154 - 192ms)  3   | ...
             (192 - 231ms)  0   |
             (231 - 269ms)  0   |
             (269 - 307ms)  0   |
             (307 - 346ms)  0   |
             (346 - 384ms)  1   | .
             (384 - 423ms)  1   | .
             (423 - 461ms)  0   |
             (461 - 499ms)  0   |
             (499 - 538ms)  0   |
             (538 - 576ms)  0   |
             (576 - 615ms)  1   | .
               Total Statements: 350; Buckets: 16; Bucket Size: 38 ms;
            y distribution statements running within this instance have.
             Parameters
             None.
             Example
             mysql> CALL sys.ps_statement_avg_latency_histogram()\G
             *************************** 1. row ***************************
             Performance Schema Statement Digest Average Latency Histogram:
               . = 1 unit
               * = 2 units
               # = 3 units
             (0 - 38ms)     240 | ################################################################################
             (38 - 77ms)    38  | ......................................
             (77 - 115ms)   3   | ...
             (115 - 154ms)  62  | *******************************
             (154 - 192ms)  3   | ...
             (192 - 231ms)  0   |
             (231 - 269ms)  0   |
             (269 - 307ms)  0   |
             (307 - 346ms)  0   |
             (346 - 384ms)  1   | .
             (384 - 423ms)  1   | .
             (423 - 461ms)  0   |
             (461 - 499ms)  0   |
             (499 - 538ms)  0   |
             (538 - 576ms)  0   |
             (576 - 615ms)  1   | .
               Total Statements: 350; Buckets: 16; Bucket Size: 38 ms;
            zn�"���Q�YBEGIN
    DECLARE v_output LONGTEXT DEFAULT '{}';
    DECLARE v_msg_text TEXT DEFAULT '';
    DECLARE v_signal_msg TEXT DEFAULT '';
    DECLARE v_mysql_errno INT;
    DECLARE v_max_output_len BIGINT;
    DECLARE EXIT HANDLER FOR SQLWARNING, SQLEXCEPTION
    BEGIN
        GET DIAGNOSTICS CONDITION 1
            v_msg_text = MESSAGE_TEXT,
            v_mysql_errno = MYSQL_ERRNO;
        IF v_mysql_errno = 1260 THEN
            SET v_signal_msg = CONCAT('{ "error": "Trx info truncated: ', v_msg_text, '" }');
        ELSE
            SET v_signal_msg = CONCAT('{ "error": "', v_msg_text, '" }');
        END IF;
        RETURN v_signal_msg;
    END;
    IF (@sys.ps_thread_trx_info.max_length IS NULL) THEN
        SET @sys.ps_thread_trx_info.max_length = sys.sys_get_config('ps_thread_trx_info.max_length', 65535);
    END IF;
    IF (@sys.ps_thread_trx_info.max_length != @@session.group_concat_max_len) THEN
        SET @old_group_concat_max_len = @@session.group_concat_max_len;
        SET v_max_output_len = (@sys.ps_thread_trx_info.max_length - 5);
        SET SESSION group_concat_max_len = v_max_output_len;
    END IF;
    SET v_output = (
        SELECT CONCAT('[', IFNULL(GROUP_CONCAT(trx_info ORDER BY event_id), ''), '\n]') AS trx_info
          FROM (SELECT trxi.thread_id,
                       trxi.event_id,
                       GROUP_CONCAT(
                         IFNULL(
                           CONCAT('\n  {\n',
                                  '    "time": "', IFNULL(sys.format_time(trxi.timer_wait), ''), '",\n',
                                  '    "state": "', IFNULL(trxi.state, ''), '",\n',
                                  '    "mode": "', IFNULL(trxi.access_mode, ''), '",\n',
                                  '    "autocommitted": "', IFNULL(trxi.autocommit, ''), '",\n',
                                  '    "gtid": "', IFNULL(trxi.gtid, ''), '",\n',
                                  '    "isolation": "', IFNULL(trxi.isolation_level, ''), '",\n',
                                  '    "statements_executed": [', IFNULL(s.stmts, ''), IF(s.stmts IS NULL, ' ]\n', '\n    ]\n'),
                                  '  }'
                           ),
                           '')
                         ORDER BY event_id) AS trx_info
                  FROM (
                        (SELECT thread_id, event_id, timer_wait, state,access_mode, autocommit, gtid, isolation_level
                           FROM performance_schema.events_transactions_current
                          WHERE thread_id = in_thread_id
                            AND end_event_id IS NULL)
                        UNION
                        (SELECT thread_id, event_id, timer_wait, state,access_mode, autocommit, gtid, isolation_level
                           FROM performance_schema.events_transactions_history
                          WHERE thread_id = in_thread_id)
                       ) AS trxi
                  LEFT JOIN (SELECT thread_id,
                                    nesting_event_id,
                                    GROUP_CONCAT(
                                      IFNULL(
                                        CONCAT('\n      {\n',
                                               '        "sql_text": "', IFNULL(sys.format_statement(REPLACE(sql_text, '\\', '\\\\')), ''), '",\n',
                                               '        "time": "', IFNULL(sys.format_time(timer_wait), ''), '",\n',
                                               '        "schema": "', IFNULL(current_schema, ''), '",\n',
                                               '        "rows_examined": ', IFNULL(rows_examined, ''), ',\n',
                                               '        "rows_affected": ', IFNULL(rows_affected, ''), ',\n',
                                               '        "rows_sent": ', IFNULL(rows_sent, ''), ',\n',
                                               '        "tmp_tables": ', IFNULL(created_tmp_tables, ''), ',\n',
                                               '        "tmp_disk_tables": ', IFNULL(created_tmp_disk_tables, ''), ',\n',
                                               '        "sort_rows": ', IFNULL(sort_rows, ''), ',\n',
                                               '        "sort_merge_passes": ', IFNULL(sort_merge_passes, ''), '\n',
                                               '      }'), '') ORDER BY event_id) AS stmts
                               FROM performance_schema.events_statements_history
                              WHERE sql_text IS NOT NULL
                                AND thread_id = in_thread_id
                              GROUP BY thread_id, nesting_event_id
                            ) AS s
                    ON trxi.thread_id = s.thread_id
                   AND trxi.event_id = s.nesting_event_id
                 WHERE trxi.thread_id = in_thread_id
                 GROUP BY trxi.thread_id, trxi.event_id
                ) trxs
          GROUP BY thread_id
    );
    IF (@old_group_concat_max_len IS NOT NULL) THEN
        SET SESSION group_concat_max_len = @old_group_concat_max_len;
    END IF;
    RETURN v_output;
ENDIMESTAMP(),
        in_interval    = IFNULL(in_interval, 1.00),
        in_max_runtime = IFNULL(in_max_runtime, 60.00);
    WHILE (v_runtime < in_max_runtime
           AND (SELECT INSTRUMENTED FROM performance_schema.threads WHERE THREAD_ID = in_thread_id) = 'YES') DO
        SET v_done = FALSE;
        OPEN c_stack;
        c_stack_loop: LOOP
            FETCH c_stack INTO v_event, v_min_event_id;
            IF v_done THEN
                LEAVE c_stack_loop;
            END IF;
            IF (LENGTH(v_event) > 0) THEN
                INSERT INTO tmp_events VALUES (v_min_event_id, v_event);
            END IF;
        END LOOP;
        CLOSE c_stack;
        SELECT SLEEP(in_interval) INTO @sleep;
        SET v_runtime = (UNIX_TIMESTAMP() - v_start);
    END WHILE;
    INSERT INTO tmp_events VALUES (v_min_event_id+1, '}');
    SET @query = CONCAT('SELECT event FROM tmp_events ORDER BY event_id INTO OUTFILE ''', in_outfile, ''' FIELDS ESCAPED BY '''' LINES TERMINATED BY ''''');
    PREPARE stmt_output FROM @query;
    EXECUTE stmt_output;
    DEALLOCATE PREPARE stmt_output;
    SELECT CONCAT('Stack trace written to ', in_outfile) AS 'Info';
    SELECT CONCAT('dot -Tpdf -o /tmp/stack_', in_thread_id, '.pdf ', in_outfile) AS 'Convert to PDF';
    SELECT CONCAT('dot -Tpng -o /tmp/stack_', in_thread_id, '.png ', in_outfile) AS 'Convert to PNG';
    DROP TEMPORARY TABLE tmp_events;
    IF (in_auto_setup) THEN
        CALL sys.ps_setup_reload_saved();
    END IF;
    IF (v_this_thread_enabed = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    SET sql_log_bin = @log_bin;
END<_0]"�V!�R���BEGIN
    DECLARE v_done bool DEFAULT FALSE;
    DECLARE v_db_name_check VARCHAR(64);
    DECLARE v_db_err_msg TEXT;
    DECLARE v_table VARCHAR(64);
    DECLARE v_views_created INT DEFAULT 0;
    DECLARE v_table_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY') DEFAULT '';
    DECLARE v_temp_table TEXT;
    DECLARE c_table_names CURSOR FOR
        SELECT TABLE_NAME
          FROM INFORMATION_SCHEMA.TABLES
         WHERE TABLE_SCHEMA = in_db_name;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    SELECT SCHEMA_NAME INTO v_db_name_check
      FROM INFORMATION_SCHEMA.SCHEMATA
     WHERE SCHEMA_NAME = in_db_name;
    IF v_db_name_check IS NULL THEN
        SET v_db_err_msg = CONCAT('Unknown database ', in_db_name);
        SIGNAL SQLSTATE 'HY000'
            SET MESSAGE_TEXT = v_db_err_msg;
    END IF;
    SELECT SCHEMA_NAME INTO v_db_name_check
      FROM INFORMATION_SCHEMA.SCHEMATA
     WHERE SCHEMA_NAME = in_synonym;
    IF v_db_name_check = in_synonym THEN
        SET v_db_err_msg = CONCAT('Can\'t create database ', in_synonym, '; database exists');
        SIGNAL SQLSTATE 'HY000'
            SET MESSAGE_TEXT = v_db_err_msg;
    END IF;
    SET @create_db_stmt := CONCAT('CREATE DATABASE ', sys.quote_identifier(in_synonym));
    PREPARE create_db_stmt FROM @create_db_stmt;
    EXECUTE create_db_stmt;
    DEALLOCATE PREPARE create_db_stmt;
    SET v_done = FALSE;
    OPEN c_table_names;
    c_table_names: LOOP
        FETCH c_table_names INTO v_table;
        IF v_done THEN
            LEAVE c_table_names;
        END IF;
        CALL sys.table_exists(in_db_name, v_table, v_table_exists);
        IF (v_table_exists = 'TEMPORARY') THEN
            SET v_temp_table =
            CONCAT(
                'Table',
                 sys.quote_identifier(in_db_name),
                 '.',
                 sys.quote_identifier(v_table),
                 'shadows base table. View cannot be created! Terminating!');
             SIGNAL SQLSTATE 'HY000'
                 SET MESSAGE_TEXT = v_temp_table;
             LEAVE c_table_names;
        END IF;
        SET @create_view_stmt = CONCAT(
            'CREATE SQL SECURITY INVOKER VIEW ',
            sys.quote_identifier(in_synonym),
            '.',
            sys.quote_identifier(v_table),
            ' AS SELECT * FROM ',
            sys.quote_identifier(in_db_name),
            '.',
            sys.quote_identifier(v_table)
        );
        PREPARE create_view_stmt FROM @create_view_stmt;
        EXECUTE create_view_stmt;
        DEALLOCATE PREPARE create_view_stmt;
        SET v_views_created = v_views_created + 1;
    END LOOP;
    CLOSE c_table_names;
    SELECT CONCAT(
        'Created ', v_views_created, ' view',
        IF(v_views_created != 1, 's', ''), ' in the ',
        sys.quote_identifier(in_synonym), ' database'
    ) AS summary;
END
             Description
             Dumps all data within Performance Schema for an instrumented thread,
             to create a DOT formatted graph file.
             Each resultset returned from the procedure should be used for a complete graph
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Parameters
             in_thread_id (BIGINT UNSIGNED):
               The thread that you would like a stack trace for
             in_outfile  (VARCHAR(255)):
               The filename the dot file will be written to
             in_max_runtime (DECIMAL(20,2)):
               The maximum time to keep collecting data.
               Use NULL to get the default which is 60 seconds.
             in_interval (DECIMAL(20,2)):
               How long to sleep between data collections.
               Use NULL to get the default which is 1 second.
             in_start_fresh (BOOLEAN):
               Whether to reset all Performance Schema data before tracing.
             in_auto_setup (BOOLEAN):
               Whether to disable all other threads and enable all consumers/instruments.
               This will also reset the settings at the end of the run.
             in_debug (BOOLEAN):
               Whether you would like to include file:lineno in the graph
             Example
             mysql> CALL sys.ps_trace_thread(25, CONCAT('/tmp/stack-', REPLACE(NOW(), ' ', '-'), '.dot'), NULL, NULL, TRUE, TRUE, TRUE);
             +-------------------+
             | summary           |
             +-------------------+
             | Disabled 1 thread |
             +-------------------+
             1 row in set (0.00 sec)
             +---------------------------------------------+
             | Info                                        |
             +---------------------------------------------+
             | Data collection starting for THREAD_ID = 25 |
             +---------------------------------------------+
             1 row in set (0.03 sec)
             +-----------------------------------------------------------+
             | Info                                                      |
             +-----------------------------------------------------------+
             | Stack trace written to /tmp/stack-2014-02-16-21:18:41.dot |
             +-----------------------------------------------------------+
             1 row in set (60.07 sec)
             +-------------------------------------------------------------------+
             | Convert to PDF                                                    |
             +-------------------------------------------------------------------+
             | dot -Tpdf -o /tmp/stack_25.pdf /tmp/stack-2014-02-16-21:18:41.dot |
             +-------------------------------------------------------------------+
             1 row in set (60.07 sec)
             +-------------------------------------------------------------------+
             | Convert to PNG                                                    |
             +-------------------------------------------------------------------+
             | dot -Tpng -o /tmp/stack_25.png /tmp/stack-2014-02-16-21:18:41.dot |
             +-------------------------------------------------------------------+
             1 row in set (60.07 sec)
             +------------------+
             | summary          |
             +------------------+
             | Enabled 1 thread |
             +------------------+
             1 row in set (60.32 sec)
            
             Description
             Resets the Performance Schema setup to the default settings.
             Parameters
             in_verbose (BOOLEAN):
               Whether to print each setup stage (including the SQL) whilst running.
             Example
             mysql> CALL sys.ps_setup_reset_to_default(true)\G
             *************************** 1. row ***************************
             status: Resetting: setup_actors
             DELETE
             FROM performance_schema.setup_actors
              WHERE NOT (HOST = '%' AND USER = '%' AND ROLE = '%')
             1 row in set (0.00 sec)
             *************************** 1. row ***************************
             status: Resetting: setup_actors
             INSERT IGNORE INTO performance_schema.setup_actors
             VALUES ('%', '%', '%')
             1 row in set (0.00 sec)
             ...
             mysql> CALL sys.ps_setup_reset_to_default(false)\G
             Query OK, 0 rows affected (0.00 sec)
            5�
)�N�ޏU���      ...
             To create an overall report of the 95th percentile queries and the top 10 queries with full table scans:
             mysql> CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
             Query OK, 0 rows affected (0.01 sec)
             mysql> SET @sys.statement_performance_analyzer.limit = 10;
             Query OK, 0 rows affected (0.00 sec)
             mysql> CALL sys.statement_performance_analyzer('overall', NULL, 'with_runtimes_in_95th_percentile,with_full_table_scans');
             +-----------------------------------------+
             | Next Output                             |
             +-----------------------------------------+
             | Queries with Runtime in 95th Percentile |
             +-----------------------------------------+
             1 row in set (0.01 sec)
             ...
             +-------------------------------------+
             | Next Output                         |
             +-------------------------------------+
             | Top 10 Queries with Full Table Scan |
             +-------------------------------------+
             1 row in set (0.09 sec)
             ...
             Use a custom view showing the top 10 query sorted by total execution time refreshing the view every minute using
             the watch command in Linux.
             mysql> CREATE OR REPLACE VIEW mydb.my_statements AS
                 -> SELECT sys.format_statement(DIGEST_TEXT) AS query,
                 ->        SCHEMA_NAME AS db,
                 ->        COUNT_STAR AS exec_count,
                 ->        sys.format_time(SUM_TIMER_WAIT) AS total_latency,
                 ->        sys.format_time(AVG_TIMER_WAIT) AS avg_latency,
                 ->        ROUND(IFNULL(SUM_ROWS_SENT / NULLIF(COUNT_STAR, 0), 0)) AS rows_sent_avg,
                 ->        ROUND(IFNULL(SUM_ROWS_EXAMINED / NULLIF(COUNT_STAR, 0), 0)) AS rows_examined_avg,
                 ->        ROUND(IFNULL(SUM_ROWS_AFFECTED / NULLIF(COUNT_STAR, 0), 0)) AS rows_affected_avg,
                 ->        DIGEST AS digest
                 ->   FROM performance_schema.events_statements_summary_by_digest
                 -> ORDER BY SUM_TIMER_WAIT DESC;
             Query OK, 0 rows affected (0.01 sec)
             mysql> CALL sys.statement_performance_analyzer('create_table', 'mydb.digests_prev', NULL);
             Query OK, 0 rows affected (0.10 sec)
             shell$ watch -n 60 "mysql sys --table -e "
             > SET @sys.statement_performance_analyzer.view = 'mydb.my_statements';
             > SET @sys.statement_performance_analyzer.limit = 10;
             > CALL statement_performance_analyzer('snapshot', NULL, NULL);
             > CALL statement_performance_analyzer('delta', 'mydb.digests_prev', 'custom');
             > CALL statement_performance_analyzer('save', 'mydb.digests_prev', NULL);
             > ""
             Every 60.0s: mysql sys --table -e "                                                                                                   ...  Mon Dec 22 10:58:51 2014
             +----------------------------------+
             | Next Output                      |
             +----------------------------------+
             | Top 10 Queries Using Custom View |
             +----------------------------------+
             +-------------------+-------+------------+---------------+-------------+---------------+-------------------+-------------------+----------------------------------+
             | query             | db    | exec_count | total_latency | avg_latency | rows_sent_avg | rows_examined_avg | rows_affected_avg | digest                           |
             +-------------------+-------+------------+---------------+-------------+---------------+-------------------+-------------------+----------------------------------+
             ...
            mitted": "NO",
                 "gtid": "AUTOMATIC",
                 "isolation": "REPEATABLE READ",
                 "statements_executed": [
                   {
                     "sql_text": "INSERT INTO info VALUES (2, 'bar')",
                     "time": "107.33 us",
                     "schema": "trx",
                     "rows_examined": 0,
                     "rows_affected": 1,
                     "rows_sent": 0,
                     "tmp_tables": 0,
                     "tmp_disk_tables": 0,
                     "sort_rows": 0,
                     "sort_merge_passes": 0
                   },
                   {
                     "sql_text": "COMMIT",
                     "time": "213.23 us",
                     "schema": "trx",
                     "rows_examined": 0,
                     "rows_affected": 0,
                     "rows_sent": 0,
                     "tmp_tables": 0,
                     "tmp_disk_tables": 0,
                     "sort_rows": 0,
                     "sort_merge_passes": 0
                   }
                 ]
               }
             ]
             1 row in set (0.03 sec)
            terval    = IFNULL(in_interval, 1.00),
        in_max_runtime = IFNULL(in_max_runtime, 60.00);
    WHILE (v_runtime < in_max_runtime
           AND (SELECT INSTRUMENTED FROM performance_schema.threads WHERE THREAD_ID = in_thread_id) = 'YES') DO
        SET v_done = FALSE;
        OPEN c_stack;
        c_stack_loop: LOOP
            FETCH c_stack INTO v_event, v_min_event_id;
            IF v_done THEN
                LEAVE c_stack_loop;
            END IF;
            IF (LENGTH(v_event) > 0) THEN
                INSERT INTO tmp_events VALUES (v_min_event_id, v_event
             Description
             Takes a source database name and synonym name, and then creates the
             synonym database with views that point to all of the tables within
             the source database.
             Useful for creating a "ps" synonym for "performance_schema",
             or "is" instead of "information_schema", for example.
             Parameters
             in_db_name (VARCHAR(64)):
               The database name that you would like to create a synonym for.
             in_synonym (VARCHAR(64)):
               The database synonym name.
             Example
             mysql> SHOW DATABASES;
             +--------------------+
             | Database           |
             +--------------------+
             | information_schema |
             | mysql              |
             | performance_schema |
             | sys                |
             | test               |
             +--------------------+
             5 rows in set (0.00 sec)
             mysql> CALL sys.create_synonym_db('performance_schema', 'ps');
             +---------------------------------------+
             | summary                               |
             +---------------------------------------+
             | Created 74 views in the `ps` database |
             +---------------------------------------+
             1 row in set (8.57 sec)
             Query OK, 0 rows affected (8.57 sec)
             mysql> SHOW DATABASES;
             +--------------------+
             | Database           |
             +--------------------+
             | information_schema |
             | mysql              |
             | performance_schema |
             | ps                 |
             | sys                |
             | test               |
             +--------------------+
             6 rows in set (0.00 sec)
             mysql> SHOW FULL TABLES FROM ps;
             +------------------------------------------------------+------------+
             | Tables_in_ps                                         | Table_type |
             +------------------------------------------------------+------------+
             | accounts                                             | VIEW       |
             | cond_instances                                       | VIEW       |
             | events_stages_current                                | VIEW       |
             | events_stages_history                                | VIEW       |
             ...
            �	;g4C���R�uBEGIN
    DECLARE v_done bool DEFAULT FALSE;
    DECLARE v_db_name_check VARCHAR(64);
    DECLARE v_db_err_msg TEXT;
    DECLARE v_table VARCHAR(64);
    DECLARE v_views_created INT DEFAULT 0;
    DECLARE v_table_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY') DEFAULT '';
    DECLARE v_temp_table TEXT;
    DECLARE c_table_names CURSOR FOR
        SELECT TABLE_NAME
          FROM INFORMATION_SCHEMA.TABLES
         WHERE TABLE_SCHEMA = in_db_name;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    SELECT SCHEMA_NAME INTO v_db_name_check
      FROM INFORMATION_SCHEMA.SCHEMATA
     WHERE SCHEMA_NAME = in_db_name;
    IF v_db_name_check IS NULL THEN
        SET v_db_err_msg = CONCAT('Unknown database ', in_db_name);
        SIGNAL SQLSTATE 'HY000'
            SET MESSAGE_TEXT = v_db_err_msg;
    END IF;
    SELECT SCHEMA_NAME INTO v_db_name_check
      FROM INFORMATION_SCHEMA.SCHEMATA
     WHERE SCHEMA_NAME = in_synonym;
    IF v_db_name_check = in_synonym THEN
        SET v_db_err_msg = CONCAT('Can''t create database ', in_synonym, '; database exists');
        SIGNAL SQLSTATE 'HY000'
            SET MESSAGE_TEXT = v_db_err_msg;
    END IF;
    SET @create_db_stmt := CONCAT('CREATE DATABASE ', sys.quote_identifier(in_synonym));
    PREPARE create_db_stmt FROM @create_db_stmt;
    EXECUTE create_db_stmt;
    DEALLOCATE PREPARE create_db_stmt;
    SET v_done = FALSE;
    OPEN c_table_names;
    c_table_names: LOOP
        FETCH c_table_names INTO v_table;
        IF v_done THEN
            LEAVE c_table_names;
        END IF;
        CALL sys.table_exists(in_db_name, v_table, v_table_exists);
        IF (v_table_exists = 'TEMPORARY') THEN
            SET v_temp_table =
            CONCAT(
                'Table',
                 sys.quote_identifier(in_db_name),
                 '.',
                 sys.quote_identifier(v_table),
                 'shadows base table. View cannot be created! Terminating!');
             SIGNAL SQLSTATE 'HY000'
                 SET MESSAGE_TEXT = v_temp_table;
             LEAVE c_table_names;
        END IF;
        SET @create_view_stmt = CONCAT(
            'CREATE SQL SECURITY INVOKER VIEW ',
            sys.quote_identifier(in_synonym),
            '.',
            sys.quote_identifier(v_table),
            ' AS SELECT * FROM ',
            sys.quote_identifier(in_db_name),
            '.',
            sys.quote_identifier(v_table)
        );
        PREPARE create_view_stmt FROM @create_view_stmt;
        EXECUTE create_view_stmt;
        DEALLOCATE PREPARE create_view_stmt;
        SET v_views_created = v_views_created + 1;
    END LOOP;
    CLOSE c_table_names;
    SELECT CONCAT(
        'Created ', v_views_created, ' view',
        IF(v_views_created != 1, 's', ''), ' in the ',
        sys.quote_identifier(in_synonym), ' database'
    ) AS summary;
END
             Description
             Resets the Performance Schema setup to the default settings.
             Parameters
             in_verbose (BOOLEAN):
               Whether to print each setup stage (including the SQL) whilst running.
             Example
             mysql> CALL sys.ps_setup_reset_to_default(true)\G
             *************************** 1. row ***************************
             status: Resetting: setup_actors
             DELETE
             FROM performance_schema.setup_actors
              WHERE NOT (HOST = '%' AND USER = '%' AND ROLE = '%')
             1 row in set (0.00 sec)
             *************************** 1. row ***************************
             status: Resetting: setup_actors
             INSERT IGNORE INTO performance_schema.setup_actors
             VALUES ('%', '%', '%')
             1 row in set (0.00 sec)
             ...
             mysql> CALL sys.ps_setup_reset_to_default(false)\G
             Query OK, 0 rows affected (0.00 sec)
            terval    = IFNULL(in_interval, 1.00),
        in_max_runtime = IFNULL(in_max_runtime, 60.00);
    WHILE (v_runtime < in_max_runtime
           AND (SELECT INSTRUMENTED FROM performance_schema.threads WHERE THREAD_ID = in_thread_id) = 'YES') DO
        SET v_done = FALSE;
        OPEN c_stack;
        c_stack_loop: LOOP
            FETCH c_stack INTO v_event, v_min_event_id;
            IF v_done THEN
                LEAVE c_stack_loop;
            END IF;
            IF (LENGTH(v_event) > 0) THEN
                INSERT INTO tmp_events VALUES (v_min_event_id, v_event);
            END IF;
        END LOOP;
        CLOSE c_stack;
        SELECT SLEEP(in_interval) INTO @sleep;
        SET v_runtime = (UNIX_TIMESTAMP() - v_start);
    END WHILE;
    INSERT INTO tmp_events VALUES (v_min_event_id+1, '}');
    SET @query = CONCAT('SELECT event FROM tmp_events ORDER BY event_id INTO OUTFILE ''', in_outfile, ''' FIELDS ESCAPED BY '''' LINES TERMINATED BY ''''');
    PREPARE stmt_output FROM @query;
    EXECUTE stmt_output;
    DEALLOCATE PREPARE stmt_output;
    SELECT CONCAT('Stack trace written to ', in_outfile) AS 'Info';
    SELECT CONCAT('dot -Tpdf -o /tmp/stack_', in_thread_id, '.pdf ', in_outfile) AS 'Convert to PDF';
    SELECT CONCAT('dot -Tpng -o /tmp/stack_', in_thread_id, '.png ', in_outfile) AS 'Convert to PNG';
    DROP TEMPORARY TABLE tmp_events;
    IF (in_auto_setup) THEN
        CALL sys.ps_setup_reload_saved();
    END IF;
    IF (v_this_thread_enabed = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    SET sql_log_bin = @log_bin;
ENDon": "', VERSION(), '"'),
                            CONCAT('"mysql_user": "', CURRENT_USER(), '"'),
                            CONCAT('"events": [', IFNULL(json_objects,''), ']')
                           ),
                  '}');
END
             Description
             Resets the Performance Schema setup to the default settings.
             Parameters
             in_verbose (BOOLEAN):
               Whether to print each setup stage (including the SQL) whilst running.
             Example
             mysql> CALL sys.ps_setup_reset_to_default(true)\G
             *************************** 1. row ***************************
             status: Resetting: setup_actors
             DELETE
             FROM performance_schema.setup_actors
              WHERE NOT (HOST = '%' AND USER = '%' AND ROLE = '%')
             1 row in set (0.00 sec)
             *************************** 1. row ***************************
             status: Resetting: setup_actors
             INSERT IGNORE INTO performance_schema.setup_actors
             VALUES ('%', '%', '%')
             1 row in set (0.00 sec)
             ...
             mysql> CALL sys.ps_setup_reset_to_default(false)\G
             Query OK, 0 rows affected (0.00 sec)
            5F��)������O�m
EN
            SELECT 'The following query will be used to generate the query for each sys view' AS 'Debug';
            SELECT @sys.diagnostics.sql AS 'Debug';
        END IF;
        PREPARE stmt_gen_query FROM @sys.diagnostics.sql;
        SET v_done = FALSE;
        OPEN c_sysviews_w_delta;
        c_sysviews_w_delta_loop: LOOP
            FETCH c_sysviews_w_delta INTO v_table_name;
            IF v_done THEN
                LEAVE c_sysviews_w_delta_loop;
            END IF;
            IF (@sys.debug = 'ON') THEN
                SELECT CONCAT('The following queries are for storing the final content of ', v_table_name) AS 'Debug';
            END IF;
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS `tmp_', v_table_name, '_end`'));
            CALL sys.execute_prepared_stmt(CONCAT('CREATE TEMPORARY TABLE `tmp_', v_table_name, '_end` SELECT * FROM `sys`.`x$', v_table_name, '`'));
            IF (@sys.diagnostics.include_raw = 'ON') THEN
                SET @sys.diagnostics.table_name = CONCAT('x$', v_table_name);
                EXECUTE stmt_gen_query USING @sys.diagnostics.table_name;
                SELECT CONCAT(@sys.diagnostics.sql_select,
                                IF(order_by IS NOT NULL, CONCAT('\n ORDER BY ', REPLACE(order_by, '%{TABLE}', CONCAT('tmp_', v_table_name, '_end'))), ''),
                                IF(limit_rows IS NOT NULL, CONCAT('\n LIMIT ', limit_rows), '')
                        )
                    INTO @sys.diagnostics.sql_select
                    FROM tmp_sys_views_delta
                    WHERE TABLE_NAME = v_table_name;
                SELECT CONCAT('Overall ', v_table_name) AS 'The following output is:';
                CALL sys.execute_prepared_stmt(@sys.diagnostics.sql_select);
            END IF;
        END LOOP;
        CLOSE c_sysviews_w_delta;
        DEALLOCATE PREPARE stmt_gen_query;
        SELECT '
======================
     Delta Status
======================
' AS '';
        CALL sys.statement_performance_analyzer('delta', 'tmp_digests_start', 'with_runtimes_in_95th_percentile');
        CALL sys.statement_performance_analyzer('cleanup', NULL, NULL);
        DROP TEMPORARY TABLE tmp_digests_start;
        IF (@sys.debug = 'ON') THEN
            SELECT 'The following query will be used to generate the query for each sys view delta' AS 'Debug';
            SELECT @sys.diagnostics.sql_gen_query_delta AS 'Debug';
        END IF;
        PREPARE stmt_gen_query_delta FROM @sys.diagnostics.sql_gen_query_delta;
        SET v_old_group_concat_max_len = @@session.group_concat_max_len;
        SET @@session.group_concat_max_len = 2048;
        SET v_done = FALSE;
        OPEN c_sysviews_w_delta;
        c_sysviews_w_delta_loop: LOOP
            FETCH c_sysviews_w_delta INTO v_table_name;
            IF v_done THEN
                LEAVE c_sysviews_w_delta_loop;
            END IF;
            SET @sys.diagnostics.table_name = v_table_name;
            EXECUTE stmt_gen_query_delta USING @sys.diagnostics.table_name;
            SELECT CONCAT(@sys.diagnostics.sql_select,
                            IF(where_delta IS NOT NULL, CONCAT('\n WHERE ', where_delta), ''),
                            IF(order_by_delta IS NOT NULL, CONCAT('\n ORDER BY ', order_by_delta), ''),
                            IF(limit_rows IS NOT NULL, CONCAT('\n LIMIT ', limit_rows), '')
                    )
                INTO @sys.diagnostics.sql_select
                FROM tmp_sys_views_delta
                WHERE TABLE_NAME = v_table_name;
            SELECT CONCAT('Delta ', v_table_name) AS 'The following output is:';
            CALL sys.execute_prepared_stmt(@sys.diagnostics.sql_select);
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE `tmp_', v_table_name, '_end`'));
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE `tmp_', v_table_name, '_start`'));
        END LOOP;
        CLOSE c_sysviews_w_delta;
        SET @@session.group_concat_max_len = v_old_group_concat_max_len;
        DEALLOCATE PREPARE stmt_gen_query_delta;
        DROP TEMPORARY TABLE tmp_sys_views_delta;
    END IF;
    IF (v_has_metrics) THEN
        SELECT 'SELECT * FROM sys.metrics' AS 'The following output is:';
    ELSE
        SELECT 'sys.metrics equivalent' AS 'The following output is:';
    END IF;
    CALL sys.execute_prepared_stmt(
        CONCAT(v_sql_status_summary_select, v_sql_status_summary_delta, ', Type, s1.Enabled', v_sql_status_summary_from,
               '
 ORDER BY Type, Variable_name'
        )
    );
    SET v_count = 0;
    WHILE (v_count < v_output_count) DO
        SET v_count = v_count + 1;
        SET v_table_name = CONCAT('tmp_metrics_', v_count);
        CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS ', v_table_name));
    END WHILE;
    IF (in_auto_config <> 'current') THEN
        CALL sys.ps_setup_reload_saved();
        SET sql_log_bin = @log_bin;
    END IF;
    SET @sys.diagnostics.output_time            = NULL,
        @sys.diagnostics.sql                    = NULL,
        @sys.diagnostics.sql_gen_query_delta    = NULL,
        @sys.diagnostics.sql_gen_query_template = NULL,
        @sys.diagnostics.sql_select             = NULL,
        @sys.diagnostics.table_name             = NULL;
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = @log_bin;
    END IF;
END��X2�U�<BEGIN
    DECLARE v_error BOOLEAN DEFAULT FALSE;
    DECLARE db_quoted VARCHAR(64);
    DECLARE table_quoted VARCHAR(64);
    DECLARE v_table_type VARCHAR(16) DEFAULT '';
    DECLARE v_system_db BOOLEAN
        DEFAULT LOWER(in_db) IN ('information_schema', 'performance_schema');
    DECLARE CONTINUE HANDLER FOR 1050 SET v_error = TRUE;
    DECLARE CONTINUE HANDLER FOR 1146 SET v_error = TRUE;
    SET out_exists = '';
    SET db_quoted = sys.quote_identifier(in_db);
    SET table_quoted = sys.quote_identifier(in_table);
    IF (EXISTS(SELECT 1 FROM information_schema.TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME = in_table)) THEN
        IF v_system_db = FALSE THEN
            SET @sys.tmp.table_exists.SQL = CONCAT('CREATE TEMPORARY TABLE ',
                                                    db_quoted,
                                                    '.',
                                                    table_quoted,
                                                    '(id INT PRIMARY KEY)');
            PREPARE stmt_create_table FROM @sys.tmp.table_exists.SQL;
            EXECUTE stmt_create_table;
            DEALLOCATE PREPARE stmt_create_table;
            SET @sys.tmp.table_exists.SQL = CONCAT('DROP TEMPORARY TABLE ',
                                                                db_quoted,
                                                                '.',
                                                                table_quoted);
            PREPARE stmt_drop_table FROM @sys.tmp.table_exists.SQL;
            EXECUTE stmt_drop_table;
            DEALLOCATE PREPARE stmt_drop_table;
        END IF;
        IF (v_error) THEN
            SET out_exists = 'TEMPORARY';
        ELSE
            SET v_table_type = (SELECT TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME = in_table);
            IF v_table_type = 'SYSTEM VERSIONED' THEN
                SET out_exists = 'BASE TABLE';
            ELSE
                SET out_exists = v_table_type;
            END IF;
        END IF;
    ELSE
        IF v_system_db = FALSE THEN
            SET @sys.tmp.table_exists.SQL = CONCAT('SELECT COUNT(*) FROM ',
                                                            db_quoted,
                                                            '.',
                                                            table_quoted);
            PREPARE stmt_select FROM @sys.tmp.table_exists.SQL;
            IF (NOT v_error) THEN
                DEALLOCATE PREPARE stmt_select;
                SET out_exists = 'TEMPORARY';
            END IF;
        END IF;
    END IF;
END
             to create a DOT formatted graph file.
             Each resultset returned from the procedure should be used for a complete graph
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Parameters
             in_thread_id (BIGINT UNSIGNED):
               The thread that you would like a stack trace for
             in_outfile  (VARCHAR(255)):
               The filename the dot file will be written to
             in_max_runtime (DECIMAL(20,2)):
               The maximum time to keep collecting data.
               Use NULL to get the default which is 60 seconds.
             in_interval (DECIMAL(20,2)):
               How long to sleep between data collections.
               Use NULL to get the default which is 1 second.
             in_start_fresh (BOOLEAN):
               Whether to reset all Performance Schema data before tracing.
             in_auto_setup (BOOLEAN):
               Whether to disable all other threads and enable all consumers/instruments.
               This will also reset the settings at the end of the run.
             in_debug (BOOLEAN):
               Whether you would like to include file:lineno in the graph
             Example
             mysql> CALL sys.ps_trace_thread(25, CONCAT('/tmp/stack-', REPLACE(NOW(), ' ', '-'), '.dot'), NULL, NULL, TRUE, TRUE, TRUE);
             +-------------------+
             | summary           |
             +-------------------+
             | Disabled 1 thread |
             +-------------------+
             1 row in set (0.00 sec)
             +---------------------------------------------+
             | Info                                        |
             +---------------------------------------------+
             | Data collection starting for THREAD_ID = 25 |
             +---------------------------------------------+
             1 row in set (0.03 sec)
             +-----------------------------------------------------------+
             | Info                                                      |
             +-----------------------------------------------------------+
             | Stack trace written to /tmp/stack-2014-02-16-21:18:41.dot |
             +-----------------------------------------------------------+
             1 row in set (60.07 sec)
             +-------------------------------------------------------------------+
             | Convert to PDF                                                    |
             +-------------------------------------------------------------------+
             | dot -Tpdf -o /tmp/stack_25.pdf /tmp/stack-2014-02-16-21:18:41.dot |
             +-------------------------------------------------------------------+
             1 row in set (60.07 sec)
             +-------------------------------------------------------------------+
             | Convert to PNG                                                    |
             +-------------------------------------------------------------------+
             | dot -Tpng -o /tmp/stack_25.png /tmp/stack-2014-02-16-21:18:41.dot |
             +-------------------------------------------------------------------+
             1 row in set (60.07 sec)
             +------------------+
             | summary          |
             +------------------+
             | Enabled 1 thread |
             +------------------+
             1 row in set (60.32 sec)
            
             Description
             Outputs a JSON formatted stack of all statements, stages and events
             within Performance Schema for the specified thread.
             Parameters
             thd_id (BIGINT UNSIGNED):
               The id of the thread to trace. This should match the thread_id
               column from the performance_schema.threads table.
             in_verbose (BOOLEAN):
               Include file:lineno information in the events.
             Example
             (line separation added for output)
             mysql> SELECT sys.ps_thread_stack(37, FALSE) AS thread_stack\G
             *************************** 1. row ***************************
             thread_stack: {"rankdir": "LR","nodesep": "0.10","stack_created": "2014-02-19 13:39:03",
             "mysql_version": "5.7.3-m13","mysql_user": "root@localhost","events":
             [{"nesting_event_id": "0", "event_id": "10", "timer_wait": 256.35, "event_info":
             "sql/select", "wait_info": "select @@version_comment limit 1\nerrors: 0\nwarnings: 0\nlock time:
             ...
            �ZN
��F>=D2�U�N
             Description
             Tests whether the table specified in in_db and in_table exists either as a regular
             table, or as a temporary table. The returned value corresponds to the table that
             will be used, so if there's both a temporary and a permanent table with the given
             name, then 'TEMPORARY' will be returned.
             Parameters
             in_db (VARCHAR(64)):
               The database name to check for the existence of the table in.
             in_table (VARCHAR(64)):
               The name of the table to check the existence of.
             out_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY'):
               The return value: whether the table exists. The value is one of:
                 * ''             - the table does not exist neither as a base table, view, sequence nor temporary table.
                 * 'BASE TABLE'   - the table name exists as a permanent base table table.
                 * 'VIEW'         - the table name exists as a view.
                 * 'TEMPORARY'    - the table name exists as a temporary table.
                 * 'SEQUENCE'     - the table name exists as a sequence.
                 * 'SYSTEM VIEW'  - the table name exists as a system view.
             Example
             mysql> CREATE DATABASE db1;
             Query OK, 1 row affected (0.07 sec)
             mysql> use db1;
             Database changed
             mysql> CREATE TABLE t1 (id INT PRIMARY KEY);
             Query OK, 0 rows affected (0.08 sec)
             mysql> CREATE TABLE t2 (id INT PRIMARY KEY);
             Query OK, 0 rows affected (0.08 sec)
             mysql> CREATE view v_t1 AS SELECT * FROM t1;
             Query OK, 0 rows affected (0.00 sec)
             mysql> CREATE TEMPORARY TABLE t1 (id INT PRIMARY KEY);
             Query OK, 0 rows affected (0.00 sec)
             mysql> CALL sys.table_exists('db1', 't1', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.00 sec)
             +------------+
             | @exists    |
             +------------+
             | TEMPORARY  |
             +------------+
             1 row in set (0.00 sec)
             mysql> CALL sys.table_exists('db1', 't2', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.00 sec)
             +------------+
             | @exists    |
             +------------+
             | BASE TABLE |
             +------------+
             1 row in set (0.01 sec)
             mysql> CALL sys.table_exists('db1', 'v_t1', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.00 sec)
             +---------+
             | @exists |
             +---------+
             | VIEW    |
             +---------+
             1 row in set (0.00 sec)
             MariaDB [sys]> CALL sys.table_exists('db1', 's', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.006 sec)
             +----------+
             | @exists  |
             +----------+
             | SEQUENCE |
             +----------+
             1 row in set (0.000 sec)
             MariaDB [sys]> CALL table_exists('information_schema', 'user_variables', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.003 sec)
             +-------------+
             | @exists     |
             +-------------+
             | SYSTEM VIEW |
             +-------------+
             1 row in set (0.001 sec)
             mysql> CALL sys.table_exists('db1', 't3', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.01 sec)
             +---------+
             | @exists |
             +---------+
             |         |
             +---------+
             1 row in set (0.00 sec)
            �&����O��	ql = REPLACE(@sys.diagnostics.sql_gen_query_template, '%{OUTPUT}', 'end');
        IF (@sys.debug = 'ON') THEN
            SELECT 'The following query will be used to generate the query for each sys view' AS 'Debug';
            SELECT @sys.diagnostics.sql AS 'Debug';
        END IF;
        PREPARE stmt_gen_query FROM @sys.diagnostics.sql;
        SET v_done = FALSE;
        OPEN c_sysviews_w_delta;
        c_sysviews_w_delta_loop: LOOP
            FETCH c_sysviews_w_delta INTO v_table_name;
            IF v_done THEN
                LEAVE c_sysviews_w_delta_loop;
            END IF;
            IF (@sys.debug = 'ON') THEN
                SELECT CONCAT('The following queries are for storing the final content of ', v_table_name) AS 'Debug';
            END IF;
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS `tmp_', v_table_name, '_end`'));
            CALL sys.execute_prepared_stmt(CONCAT('CREATE TEMPORARY TABLE `tmp_', v_table_name, '_end` SELECT * FROM `sys`.`x$', v_table_name, '`'));
            IF (@sys.diagnostics.include_raw = 'ON') THEN
                SET @sys.diagnostics.table_name = CONCAT('x$', v_table_name);
                EXECUTE stmt_gen_query USING @sys.diagnostics.table_name;
                SELECT CONCAT(@sys.diagnostics.sql_select,
                                IF(order_by IS NOT NULL, CONCAT('\n ORDER BY ', REPLACE(order_by, '%{TABLE}', CONCAT('tmp_', v_table_name, '_end'))), ''),
                                IF(limit_rows IS NOT NULL, CONCAT('\n LIMIT ', limit_rows), '')
                        )
                    INTO @sys.diagnostics.sql_select
                    FROM tmp_sys_views_delta
                    WHERE TABLE_NAME = v_table_name;
                SELECT CONCAT('Overall ', v_table_name) AS 'The following output is:';
                CALL sys.execute_prepared_stmt(@sys.diagnostics.sql_select);
            END IF;
        END LOOP;
        CLOSE c_sysviews_w_delta;
        DEALLOCATE PREPARE stmt_gen_query;
        SELECT '\n======================\n     Delta Status\n======================\n' AS '';
        CALL sys.statement_performance_analyzer('delta', 'tmp_digests_start', 'with_runtimes_in_95th_percentile');
        CALL sys.statement_performance_analyzer('cleanup', NULL, NULL);
        DROP TEMPORARY TABLE tmp_digests_start;
        IF (@sys.debug = 'ON') THEN
            SELECT 'The following query will be used to generate the query for each sys view delta' AS 'Debug';
            SELECT @sys.diagnostics.sql_gen_query_delta AS 'Debug';
        END IF;
        PREPARE stmt_gen_query_delta FROM @sys.diagnostics.sql_gen_query_delta;
        SET v_old_group_concat_max_len = @@session.group_concat_max_len;
        SET @@session.group_concat_max_len = 2048;
        SET v_done = FALSE;
        OPEN c_sysviews_w_delta;
        c_sysviews_w_delta_loop: LOOP
            FETCH c_sysviews_w_delta INTO v_table_name;
            IF v_done THEN
                LEAVE c_sysviews_w_delta_loop;
            END IF;
            SET @sys.diagnostics.table_name = v_table_name;
            EXECUTE stmt_gen_query_delta USING @sys.diagnostics.table_name;
            SELECT CONCAT(@sys.diagnostics.sql_select,
                            IF(where_delta IS NOT NULL, CONCAT('\n WHERE ', where_delta), ''),
                            IF(order_by_delta IS NOT NULL, CONCAT('\n ORDER BY ', order_by_delta), ''),
                            IF(limit_rows IS NOT NULL, CONCAT('\n LIMIT ', limit_rows), '')
                    )
                INTO @sys.diagnostics.sql_select
                FROM tmp_sys_views_delta
                WHERE TABLE_NAME = v_table_name;
            SELECT CONCAT('Delta ', v_table_name) AS 'The following output is:';
            CALL sys.execute_prepared_stmt(@sys.diagnostics.sql_select);
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE `tmp_', v_table_name, '_end`'));
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE `tmp_', v_table_name, '_start`'));
        END LOOP;
        CLOSE c_sysviews_w_delta;
        SET @@session.group_concat_max_len = v_old_group_concat_max_len;
        DEALLOCATE PREPARE stmt_gen_query_delta;
        DROP TEMPORARY TABLE tmp_sys_views_delta;
    END IF;
    IF (v_has_metrics) THEN
        SELECT 'SELECT * FROM sys.metrics' AS 'The following output is:';
    ELSE
        SELECT 'sys.metrics equivalent' AS 'The following output is:';
    END IF;
    CALL sys.execute_prepared_stmt(
        CONCAT(v_sql_status_summary_select, v_sql_status_summary_delta, ', Type, s1.Enabled', v_sql_status_summary_from,
               '\n ORDER BY Type, Variable_name'
        )
    );
    SET v_count = 0;
    WHILE (v_count < v_output_count) DO
        SET v_count = v_count + 1;
        SET v_table_name = CONCAT('tmp_metrics_', v_count);
        CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS ', v_table_name));
    END WHILE;
    IF (in_auto_config <> 'current') THEN
        CALL sys.ps_setup_reload_saved();
        SET sql_log_bin = @log_bin;
    END IF;
    SET @sys.diagnostics.output_time            = NULL,
        @sys.diagnostics.sql                    = NULL,
        @sys.diagnostics.sql_gen_query_delta    = NULL,
        @sys.diagnostics.sql_gen_query_template = NULL,
        @sys.diagnostics.sql_select             = NULL,
        @sys.diagnostics.table_name             = NULL;
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = @log_bin;
    END IF;
END��c��3Q�:BEGIN
    DECLARE v_start_fresh BOOLEAN DEFAULT false;
    DECLARE v_auto_enable BOOLEAN DEFAULT false;
    DECLARE v_explain     BOOLEAN DEFAULT true;
    DECLARE v_this_thread_enabed ENUM('YES', 'NO');
    DECLARE v_runtime INT DEFAULT 0;
    DECLARE v_start INT DEFAULT 0;
    DECLARE v_found_stmts INT;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    DROP TEMPORARY TABLE IF EXISTS stmt_trace;
    CREATE TEMPORARY TABLE stmt_trace (
        thread_id BIGINT UNSIGNED,
        timer_start BIGINT UNSIGNED,
        event_id BIGINT UNSIGNED,
        sql_text longtext,
        timer_wait BIGINT UNSIGNED,
        lock_time BIGINT UNSIGNED,
        errors BIGINT UNSIGNED,
        mysql_errno INT,
        rows_sent BIGINT UNSIGNED,
        rows_affected BIGINT UNSIGNED,
        rows_examined BIGINT UNSIGNED,
        created_tmp_tables BIGINT UNSIGNED,
        created_tmp_disk_tables BIGINT UNSIGNED,
        no_index_used BIGINT UNSIGNED,
        PRIMARY KEY (thread_id, timer_start)
    );
    DROP TEMPORARY TABLE IF EXISTS stmt_stages;
    CREATE TEMPORARY TABLE stmt_stages (
       event_id BIGINT UNSIGNED,
       stmt_id BIGINT UNSIGNED,
       event_name VARCHAR(128),
       timer_wait BIGINT UNSIGNED,
       PRIMARY KEY (event_id)
    );
    SET v_start_fresh = in_start_fresh;
    IF v_start_fresh THEN
        TRUNCATE TABLE performance_schema.events_statements_history_long;
        TRUNCATE TABLE performance_schema.events_stages_history_long;
    END IF;
    SET v_auto_enable = in_auto_enable;
    IF v_auto_enable THEN
        CALL sys.ps_setup_save(0);
        UPDATE performance_schema.threads
           SET INSTRUMENTED = IF(PROCESSLIST_ID IS NOT NULL, 'YES', 'NO');
        UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
         WHERE NAME NOT LIKE '%\_history'
               AND NAME NOT LIKE 'events_wait%'
               AND NAME NOT LIKE 'events_transactions%'
               AND NAME <> 'statements_digest';
        UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES',
               TIMED   = 'YES'
         WHERE NAME LIKE 'statement/%' OR NAME LIKE 'stage/%';
    END IF;
    WHILE v_runtime < in_runtime DO
        SELECT UNIX_TIMESTAMP() INTO v_start;
        INSERT IGNORE INTO stmt_trace
        SELECT thread_id, timer_start, event_id, sql_text, timer_wait, lock_time, errors, mysql_errno,
               rows_sent, rows_affected, rows_examined, created_tmp_tables, created_tmp_disk_tables, no_index_used
          FROM performance_schema.events_statements_history_long
        WHERE digest = in_digest;
        INSERT IGNORE INTO stmt_stages
        SELECT stages.event_id, stmt_trace.event_id,
               stages.event_name, stages.timer_wait
          FROM performance_schema.events_stages_history_long AS stages
          JOIN stmt_trace ON stages.nesting_event_id = stmt_trace.event_id;
        SELECT SLEEP(in_interval) INTO @sleep;
        SET v_runtime = v_runtime + (UNIX_TIMESTAMP() - v_start);
    END WHILE;
    SELECT "SUMMARY STATISTICS";
    SELECT COUNT(*) executions,
           sys.format_time(SUM(timer_wait)) AS exec_time,
           sys.format_time(SUM(lock_time)) AS lock_time,
           SUM(rows_sent) AS rows_sent,
           SUM(rows_affected) AS rows_affected,
           SUM(rows_examined) AS rows_examined,
           SUM(created_tmp_tables) AS tmp_tables,
           SUM(no_index_used) AS full_scans
      FROM stmt_trace;
    SELECT event_name,
           COUNT(*) as count,
           sys.format_time(SUM(timer_wait)) as latency
      FROM stmt_stages
     GROUP BY event_name
     ORDER BY SUM(timer_wait) DESC;
    SELECT "LONGEST RUNNING STATEMENT";
    SELECT thread_id,
           sys.format_time(timer_wait) AS exec_time,
           sys.format_time(lock_time) AS lock_time,
           rows_sent,
           rows_affected,
           rows_examined,
           created_tmp_tables AS tmp_tables,
           no_index_used AS full_scan
      FROM stmt_trace
     ORDER BY timer_wait DESC LIMIT 1;
    SELECT sql_text
      FROM stmt_trace
     ORDER BY timer_wait DESC LIMIT 1;
    SELECT sql_text, event_id INTO @sql, @sql_id
      FROM stmt_trace
    ORDER BY timer_wait DESC LIMIT 1;
    IF (@sql_id IS NOT NULL) THEN
        SELECT event_name,
               sys.format_time(timer_wait) as latency
          FROM stmt_stages
         WHERE stmt_id = @sql_id
         ORDER BY event_id;
    END IF;
    DROP TEMPORARY TABLE stmt_trace;
    DROP TEMPORARY TABLE stmt_stages;
    IF (@sql IS NOT NULL) THEN
        SET @stmt := CONCAT("EXPLAIN FORMAT=JSON ", @sql);
        BEGIN
            DECLARE CONTINUE HANDLER FOR 1064, 1146 SET v_explain = false;
            PREPARE explain_stmt FROM @stmt;
        END;
        IF (v_explain) THEN
            EXECUTE explain_stmt;
            DEALLOCATE PREPARE explain_stmt;
        END IF;
    END IF;
    IF v_auto_enable THEN
        CALL sys.ps_setup_reload_saved();
    END IF;
    IF (v_this_thread_enabed = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    SET sql_log_bin = @log_bin;
END��+?�3Q�:BEGIN
    DECLARE v_start_fresh BOOLEAN DEFAULT false;
    DECLARE v_auto_enable BOOLEAN DEFAULT false;
    DECLARE v_explain     BOOLEAN DEFAULT true;
    DECLARE v_this_thread_enabed ENUM('YES', 'NO');
    DECLARE v_runtime INT DEFAULT 0;
    DECLARE v_start INT DEFAULT 0;
    DECLARE v_found_stmts INT;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    DROP TEMPORARY TABLE IF EXISTS stmt_trace;
    CREATE TEMPORARY TABLE stmt_trace (
        thread_id BIGINT UNSIGNED,
        timer_start BIGINT UNSIGNED,
        event_id BIGINT UNSIGNED,
        sql_text longtext,
        timer_wait BIGINT UNSIGNED,
        lock_time BIGINT UNSIGNED,
        errors BIGINT UNSIGNED,
        mysql_errno INT,
        rows_sent BIGINT UNSIGNED,
        rows_affected BIGINT UNSIGNED,
        rows_examined BIGINT UNSIGNED,
        created_tmp_tables BIGINT UNSIGNED,
        created_tmp_disk_tables BIGINT UNSIGNED,
        no_index_used BIGINT UNSIGNED,
        PRIMARY KEY (thread_id, timer_start)
    );
    DROP TEMPORARY TABLE IF EXISTS stmt_stages;
    CREATE TEMPORARY TABLE stmt_stages (
       event_id BIGINT UNSIGNED,
       stmt_id BIGINT UNSIGNED,
       event_name VARCHAR(128),
       timer_wait BIGINT UNSIGNED,
       PRIMARY KEY (event_id)
    );
    SET v_start_fresh = in_start_fresh;
    IF v_start_fresh THEN
        TRUNCATE TABLE performance_schema.events_statements_history_long;
        TRUNCATE TABLE performance_schema.events_stages_history_long;
    END IF;
    SET v_auto_enable = in_auto_enable;
    IF v_auto_enable THEN
        CALL sys.ps_setup_save(0);
        UPDATE performance_schema.threads
           SET INSTRUMENTED = IF(PROCESSLIST_ID IS NOT NULL, 'YES', 'NO');
        UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
         WHERE NAME NOT LIKE '%\_history'
               AND NAME NOT LIKE 'events_wait%'
               AND NAME NOT LIKE 'events_transactions%'
               AND NAME <> 'statements_digest';
        UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES',
               TIMED   = 'YES'
         WHERE NAME LIKE 'statement/%' OR NAME LIKE 'stage/%';
    END IF;
    WHILE v_runtime < in_runtime DO
        SELECT UNIX_TIMESTAMP() INTO v_start;
        INSERT IGNORE INTO stmt_trace
        SELECT thread_id, timer_start, event_id, sql_text, timer_wait, lock_time, errors, mysql_errno,
               rows_sent, rows_affected, rows_examined, created_tmp_tables, created_tmp_disk_tables, no_index_used
          FROM performance_schema.events_statements_history_long
        WHERE digest = in_digest;
        INSERT IGNORE INTO stmt_stages
        SELECT stages.event_id, stmt_trace.event_id,
               stages.event_name, stages.timer_wait
          FROM performance_schema.events_stages_history_long AS stages
          JOIN stmt_trace ON stages.nesting_event_id = stmt_trace.event_id;
        SELECT SLEEP(in_interval) INTO @sleep;
        SET v_runtime = v_runtime + (UNIX_TIMESTAMP() - v_start);
    END WHILE;
    SELECT "SUMMARY STATISTICS";
    SELECT COUNT(*) executions,
           sys.format_time(SUM(timer_wait)) AS exec_time,
           sys.format_time(SUM(lock_time)) AS lock_time,
           SUM(rows_sent) AS rows_sent,
           SUM(rows_affected) AS rows_affected,
           SUM(rows_examined) AS rows_examined,
           SUM(created_tmp_tables) AS tmp_tables,
           SUM(no_index_used) AS full_scans
      FROM stmt_trace;
    SELECT event_name,
           COUNT(*) as count,
           sys.format_time(SUM(timer_wait)) as latency
      FROM stmt_stages
     GROUP BY event_name
     ORDER BY SUM(timer_wait) DESC;
    SELECT "LONGEST RUNNING STATEMENT";
    SELECT thread_id,
           sys.format_time(timer_wait) AS exec_time,
           sys.format_time(lock_time) AS lock_time,
           rows_sent,
           rows_affected,
           rows_examined,
           created_tmp_tables AS tmp_tables,
           no_index_used AS full_scan
      FROM stmt_trace
     ORDER BY timer_wait DESC LIMIT 1;
    SELECT sql_text
      FROM stmt_trace
     ORDER BY timer_wait DESC LIMIT 1;
    SELECT sql_text, event_id INTO @sql, @sql_id
      FROM stmt_trace
    ORDER BY timer_wait DESC LIMIT 1;
    IF (@sql_id IS NOT NULL) THEN
        SELECT event_name,
               sys.format_time(timer_wait) as latency
          FROM stmt_stages
         WHERE stmt_id = @sql_id
         ORDER BY event_id;
    END IF;
    DROP TEMPORARY TABLE stmt_trace;
    DROP TEMPORARY TABLE stmt_stages;
    IF (@sql IS NOT NULL) THEN
        SET @stmt := CONCAT("EXPLAIN FORMAT=JSON ", @sql);
        BEGIN
            DECLARE CONTINUE HANDLER FOR 1064, 1146 SET v_explain = false;
            PREPARE explain_stmt FROM @stmt;
        END;
        IF (v_explain) THEN
            EXECUTE explain_stmt;
            DEALLOCATE PREPARE explain_stmt;
        END IF;
    END IF;
    IF v_auto_enable THEN
        CALL sys.ps_setup_reload_saved();
    END IF;
    IF (v_this_thread_enabed = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    SET sql_log_bin = @log_bin;
END����c2�U���i��9i��9&���sysps_setup_disable_consumerps_setup_disable_consumerutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN consumer VARCHAR(128)
    BEGIN
    UPDATE performance_schema.setup_consumers
       SET enabled = 'NO'
     WHERE name LIKE CONCAT('%', consumer, '%');
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Disables consumers within Performance Schema
             matching the input pattern.
             Parameters
             consumer (VARCHAR(128)):
               A LIKE pattern match (using "%consumer%") of consumers to disable
             Example
             To disable all consumers:
             mysql> CALL sys.ps_setup_disable_consumer('');
             +--------------------------+
             | summary                  |
             +--------------------------+
             | Disabled 15 consumers    |
             +--------------------------+
             1 row in set (0.02 sec)
             To disable just the event_stage consumers:
             mysql> CALL sys.ps_setup_disable_comsumers('stage');
             +------------------------+
             | summary                |
             +------------------------+
             | Disabled 3 consumers   |
             +------------------------+
             1 row in set (0.00 sec)
            BEGIN
    UPDATE performance_schema.setup_consumers
       SET enabled = 'NO'
     WHERE name LIKE CONCAT('%', consumer, '%');
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END�i��9i��9(���sysps_setup_disable_threadps_setup_disable_threadutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_connection_id BIGINT
    BEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'NO'
     WHERE processlist_id = in_connection_id;
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' thread', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Disable the given connection/thread in Performance Schema.
             Parameters
             in_connection_id (BIGINT):
               The connection ID (PROCESSLIST_ID from performance_schema.threads
               or the ID shown within SHOW PROCESSLIST)
             Example
             mysql> CALL sys.ps_setup_disable_thread(3);
             +-------------------+
             | summary           |
             +-------------------+
             | Disabled 1 thread |
             +-------------------+
             1 row in set (0.01 sec)
             To disable the current connection:
             mysql> CALL sys.ps_setup_disable_thread(CONNECTION_ID());
             +-------------------+
             | summary           |
             +-------------------+
             | Disabled 1 thread |
             +-------------------+
             1 row in set (0.00 sec)
            BEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'NO'
     WHERE processlist_id = in_connection_id;
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' thread', IF(@rows != 1, 's', '')) AS summary;
END�i��9i��9""��sysps_setup_enable_background_threadsps_setup_enable_background_threadsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'YES'
     WHERE type = 'BACKGROUND';
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' background thread', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Enable all background thread instrumentation within Performance Schema.
             Parameters
             None.
             Example
             mysql> CALL sys.ps_setup_enable_background_threads();
             +-------------------------------+
             | summary                       |
             +-------------------------------+
             | Enabled 18 background threads |
             +-------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'YES'
     WHERE type = 'BACKGROUND';
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' background thread', IF(@rows != 1, 's', '')) AS summary;
END�i��9i��9&�:�sysps_setup_enable_consumerps_setup_enable_consumerutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN consumer VARCHAR(128)
    BEGIN
    UPDATE performance_schema.setup_consumers
       SET enabled = 'YES'
     WHERE name LIKE CONCAT('%', consumer, '%');
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Enables consumers within Performance Schema
             matching the input pattern.
             Parameters
             consumer (VARCHAR(128)):
               A LIKE pattern match (using "%consumer%") of consumers to enable
             Example
             To enable all consumers:
             mysql> CALL sys.ps_setup_enable_consumer('');
             +-------------------------+
             | summary                 |
             +-------------------------+
             | Enabled 10 consumers    |
             +-------------------------+
             1 row in set (0.02 sec)
             Query OK, 0 rows affected (0.02 sec)
             To enable just "waits" consumers:
             mysql> CALL sys.ps_setup_enable_consumer('waits');
             +-----------------------+
             | summary               |
             +-----------------------+
             | Enabled 3 consumers   |
             +-----------------------+
             1 row in set (0.00 sec)
             Query OK, 0 rows affected (0.00 sec)
             BEGIN
    UPDATE performance_schema.setup_consumers
       SET enabled = 'YES'
     WHERE name LIKE CONCAT('%', consumer, '%');
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END��@��@��@i��:i��:>Q\//�\sysstatement_performance_analyzerstatement_performance_analyzerutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_action ENUM('snapshot', 'overall', 'delta', 'create_table', 'create_tmp', 'save', 'cleanup'),
        IN in_table VARCHAR(129),
        IN in_views SET ('with_runtimes_in_95th_percentile', 'analysis', 'with_errors_or_warnings', 'with_full_table_scans', 'with_sorting', 'with_temp_tables', 'custom')
    ���	��i��:i��:�N
�N
systable_existstable_existsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_db VARCHAR(64), IN in_table VARCHAR(64),
        OUT out_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY', 'SEQUENCE', 'SYSTEM VIEW')
    �9�*��v�&�K�R��R��i��9i��9���sysps_setup_reload_savedps_setup_reload_savedutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    DECLARE v_done bool DEFAULT FALSE;
    DECLARE v_lock_result INT;
    DECLARE v_lock_used_by BIGINT;
    DECLARE v_signal_message TEXT;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        SIGNAL SQLSTATE VALUE '90001'
           SET MESSAGE_TEXT = 'An error occurred, was sys.ps_setup_save() run before this procedure?';
    END;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT IS_USED_LOCK('sys.ps_setup_save') INTO v_lock_used_by;
    IF (v_lock_used_by != CONNECTION_ID()) THEN
        SET v_signal_message = CONCAT('The sys.ps_setup_save lock is currently owned by ', v_lock_used_by);
        SIGNAL SQLSTATE VALUE '90002'
           SET MESSAGE_TEXT = v_signal_message;
    END IF;
    DELETE FROM performance_schema.setup_actors;
    INSERT INTO performance_schema.setup_actors SELECT * FROM tmp_setup_actors;
    BEGIN
        DECLARE v_name varchar(64);
        DECLARE v_enabled enum('YES', 'NO');
        DECLARE c_consumers CURSOR FOR SELECT NAME, ENABLED FROM tmp_setup_consumers;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
        SET v_done = FALSE;
        OPEN c_consumers;
        c_consumers_loop: LOOP
            FETCH c_consumers INTO v_name, v_enabled;
            IF v_done THEN
               LEAVE c_consumers_loop;
            END IF;
            UPDATE performance_schema.setup_consumers
               SET ENABLED = v_enabled
             WHERE NAME = v_name;
         END LOOP;
         CLOSE c_consumers;
    END;
    UPDATE performance_schema.setup_instruments
     INNER JOIN tmp_setup_instruments USING (NAME)
       SET performance_schema.setup_instruments.ENABLED = tmp_setup_instruments.ENABLED,
           performance_schema.setup_instruments.TIMED   = tmp_setup_instruments.TIMED;
    BEGIN
        DECLARE v_thread_id bigint unsigned;
        DECLARE v_instrumented enum('YES', 'NO');
        DECLARE c_threads CURSOR FOR SELECT THREAD_ID, INSTRUMENTED FROM tmp_threads;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
        SET v_done = FALSE;
        OPEN c_threads;
        c_threads_loop: LOOP
            FETCH c_threads INTO v_thread_id, v_instrumented;
            IF v_done THEN
               LEAVE c_threads_loop;
            END IF;
            UPDATE performance_schema.threads
               SET INSTRUMENTED = v_instrumented
             WHERE THREAD_ID = v_thread_id;
        END LOOP;
        CLOSE c_threads;
    END;
    UPDATE performance_schema.threads
       SET INSTRUMENTED = IF(PROCESSLIST_USER IS NOT NULL,
                             sys.ps_is_account_enabled(PROCESSLIST_HOST, PROCESSLIST_USER),
                             'YES')
     WHERE THREAD_ID NOT IN (SELECT THREAD_ID FROM tmp_threads);
    DROP TEMPORARY TABLE tmp_setup_actors;
    DROP TEMPORARY TABLE tmp_setup_consumers;
    DROP TEMPORARY TABLE tmp_setup_instruments;
    DROP TEMPORARY TABLE tmp_threads;
    SELECT RELEASE_LOCK('sys.ps_setup_save') INTO v_lock_result;
    SET sql_log_bin = @log_bin;
END
             Description
             Reloads a saved Performance Schema configuration,
             so that you can alter the setup for debugging purposes,
             but restore it to a previous state.
             Use the companion procedure - ps_setup_save(), to
             save a configuration.
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Parameters
             None.
             Example
             mysql> CALL sys.ps_setup_save();
             Query OK, 0 rows affected (0.08 sec)
             mysql> UPDATE performance_schema.setup_instruments SET enabled = 'YES', timed = 'YES';
             Query OK, 547 rows affected (0.40 sec)
             Rows matched: 784  Changed: 547  Warnings: 0
             /* Run some tests that need more detailed instrumentation here */
             mysql> CALL sys.ps_setup_reload_saved();
             Query OK, 0 rows affected (0.32 sec)
            BEGIN
    DECLARE v_done bool DEFAULT FALSE;
    DECLARE v_lock_result INT;
    DECLARE v_lock_used_by BIGINT;
    DECLARE v_signal_message TEXT;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        SIGNAL SQLSTATE VALUE '90001'
           SET MESSAGE_TEXT = 'An error occurred, was sys.ps_setup_save() run before this procedure?';
    END;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT IS_USED_LOCK('sys.ps_setup_save') INTO v_lock_used_by;
    IF (v_lock_used_by != CONNECTION_ID()) THEN
        SET v_signal_message = CONCAT('The sys.ps_setup_save lock is currently owned by ', v_lock_used_by);
        SIGNAL SQLSTATE VALUE '90002'
           SET MESSAGE_TEXT = v_signal_message;
    END IF;
    DELETE FROM performance_schema.setup_actors;
    INSERT INTO performance_schema.setup_actors SELECT * FROM tmp_setup_actors;
    BEGIN
        DECLARE v_name varchar(64);
        DECLARE v_enabled enum('YES', 'NO');
        DECLARE c_consumers CURSOR FOR SELECT NAME, ENABLED FROM tmp_setup_consumers;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
        SET v_done = FALSE;
        OPEN c_consumers;
        c_consumers_loop: LOOP
            FETCH c_consumers INTO v_name, v_enabled;
            IF v_done THEN
               LEAVE c_consumers_loop;
            END IF;
            UPDATE performance_schema.setup_consumers
               SET ENABLED = v_enabled
             WHERE NAME = v_name;
         END LOOP;
         CLOSE c_consumers;
    END;
    UPDATE performance_schema.setup_instruments
     INNER JOIN tmp_setup_instruments USING (NAME)
       SET performance_schema.setup_instruments.ENABLED = tmp_setup_instruments.ENABLED,
           performance_schema.setup_instruments.TIMED   = tmp_setup_instruments.TIMED;
    BEGIN
        DECLARE v_thread_id bigint unsigned;
        DECLARE v_instrumented enum('YES', 'NO');
        DECLARE c_threads CURSOR FOR SELECT THREAD_ID, INSTRUMENTED FROM tmp_threads;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
        SET v_done = FALSE;
        OPEN c_threads;
        c_threads_loop: LOOP
            FETCH c_threads INTO v_thread_id, v_instrumented;
            IF v_done THEN
               LEAVE c_threads_loop;
            END IF;
            UPDATE performance_schema.threads
               SET INSTRUMENTED = v_instrumented
             WHERE THREAD_ID = v_thread_id;
        END LOOP;
        CLOSE c_threads;
    END;
    UPDATE performance_schema.threads
       SET INSTRUMENTED = IF(PROCESSLIST_USER IS NOT NULL,
                             sys.ps_is_account_enabled(PROCESSLIST_HOST, PROCESSLIST_USER),
                             'YES')
     WHERE THREAD_ID NOT IN (SELECT THREAD_ID FROM tmp_threads);
    DROP TEMPORARY TABLE tmp_setup_actors;
    DROP TEMPORARY TABLE tmp_setup_consumers;
    DROP TEMPORARY TABLE tmp_setup_instruments;
    DROP TEMPORARY TABLE tmp_threads;
    SELECT RELEASE_LOCK('sys.ps_setup_save') INTO v_lock_result;
    SET sql_log_bin = @log_bin;
END��#�t!�R�kBEGIN
    SET @query = 'DELETE
                    FROM performance_schema.setup_actors
                   WHERE NOT (HOST = ''%'' AND USER = ''%'' AND ROLE = ''%'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_actors\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'INSERT IGNORE INTO performance_schema.setup_actors
                  VALUES (''%'', ''%'', ''%'', ''YES'', ''YES'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_actors\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.setup_instruments
                     SET ENABLED = sys.ps_is_instrument_default_enabled(NAME),
                         TIMED   = sys.ps_is_instrument_default_timed(NAME)';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_instruments\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.setup_consumers
                     SET ENABLED = IF(NAME IN (''events_statements_current'', ''events_transactions_current'', ''global_instrumentation'', ''thread_instrumentation'', ''statements_digest''), ''YES'', ''NO'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_consumers\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'DELETE
                    FROM performance_schema.setup_objects
                   WHERE NOT (OBJECT_TYPE IN (''EVENT'', ''FUNCTION'', ''PROCEDURE'', ''TABLE'', ''TRIGGER'') AND OBJECT_NAME = ''%''
                     AND (OBJECT_SCHEMA = ''mysql''              AND ENABLED = ''NO''  AND TIMED = ''NO'' )
                      OR (OBJECT_SCHEMA = ''performance_schema'' AND ENABLED = ''NO''  AND TIMED = ''NO'' )
                      OR (OBJECT_SCHEMA = ''information_schema'' AND ENABLED = ''NO''  AND TIMED = ''NO'' )
                      OR (OBJECT_SCHEMA = ''%''                  AND ENABLED = ''YES'' AND TIMED = ''YES''))';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_objects\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'INSERT IGNORE INTO performance_schema.setup_objects
                  VALUES (''EVENT''    , ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''EVENT''    , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''EVENT''    , ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''EVENT''    , ''%''                 , ''%'', ''YES'', ''YES''),
                         (''FUNCTION'' , ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''FUNCTION'' , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''FUNCTION'' , ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''FUNCTION'' , ''%''                 , ''%'', ''YES'', ''YES''),
                         (''PROCEDURE'', ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''PROCEDURE'', ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''PROCEDURE'', ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''PROCEDURE'', ''%''                 , ''%'', ''YES'', ''YES''),
                         (''TABLE''    , ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''TABLE''    , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''TABLE''    , ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''TABLE''    , ''%''                 , ''%'', ''YES'', ''YES''),
                         (''TRIGGER''  , ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''TRIGGER''  , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''TRIGGER''  , ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''TRIGGER''  , ''%''                 , ''%'', ''YES'', ''YES'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_objects\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.threads
                     SET INSTRUMENTED = ''YES''';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: threads\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
END�*�&!�R�JBEGIN
    SET @query = 'DELETE\n                    FROM performance_schema.setup_actors\n                   WHERE NOT (HOST = ''%'' AND USER = ''%'' AND ROLE = ''%'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_actors\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'INSERT IGNORE INTO performance_schema.setup_actors\n                  VALUES (''%'', ''%'', ''%'', ''YES'', ''YES'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_actors\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.setup_instruments\n                     SET ENABLED = sys.ps_is_instrument_default_enabled(NAME),\n                         TIMED   = sys.ps_is_instrument_default_timed(NAME)';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_instruments\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.setup_consumers\n                     SET ENABLED = IF(NAME IN (''events_statements_current'', ''events_transactions_current'', ''global_instrumentation'', ''thread_instrumentation'', ''statements_digest''), ''YES'', ''NO'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_consumers\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'DELETE\n                    FROM performance_schema.setup_objects\n                   WHERE NOT (OBJECT_TYPE IN (''EVENT'', ''FUNCTION'', ''PROCEDURE'', ''TABLE'', ''TRIGGER'') AND OBJECT_NAME = ''%''\n                     AND (OBJECT_SCHEMA = ''mysql''              AND ENABLED = ''NO''  AND TIMED = ''NO'' )\n                      OR (OBJECT_SCHEMA = ''performance_schema'' AND ENABLED = ''NO''  AND TIMED = ''NO'' )\n                      OR (OBJECT_SCHEMA = ''information_schema'' AND ENABLED = ''NO''  AND TIMED = ''NO'' )\n                      OR (OBJECT_SCHEMA = ''%''                  AND ENABLED = ''YES'' AND TIMED = ''YES''))';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_objects\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'INSERT IGNORE INTO performance_schema.setup_objects\n                  VALUES (''EVENT''    , ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''EVENT''    , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''EVENT''    , ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''EVENT''    , ''%''                 , ''%'', ''YES'', ''YES''),\n                         (''FUNCTION'' , ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''FUNCTION'' , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''FUNCTION'' , ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''FUNCTION'' , ''%''                 , ''%'', ''YES'', ''YES''),\n                         (''PROCEDURE'', ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''PROCEDURE'', ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''PROCEDURE'', ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''PROCEDURE'', ''%''                 , ''%'', ''YES'', ''YES''),\n                         (''TABLE''    , ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''TABLE''    , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''TABLE''    , ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''TABLE''    , ''%''                 , ''%'', ''YES'', ''YES''),\n                         (''TRIGGER''  , ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''TRIGGER''  , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''TRIGGER''  , ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''TRIGGER''  , ''%''                 , ''%'', ''YES'', ''YES'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_objects\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.threads\n                     SET INSTRUMENTED = ''YES''';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: threads\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
END���!��IS���i��9i��9P"9"sysps_setup_show_disabledps_setup_show_disabledutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_show_instruments BOOLEAN,
        IN in_show_threads BOOLEAN
    BEGIN
    SELECT @@performance_schema AS performance_schema_enabled;
    
    SELECT object_type,
           CONCAT(object_schema, '.', object_name) AS objects,
           enabled,
           timed
      FROM performance_schema.setup_objects
     WHERE enabled = 'NO'
     ORDER BY object_type, objects;
    SELECT name AS disabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'NO'
     ORDER BY disabled_consumers;
    IF (in_show_threads) THEN
        SELECT IF(name = 'thread/sql/one_connection',
                  CONCAT(processlist_user, '@', processlist_host),
                  REPLACE(name, 'thread/', '')) AS disabled_threads,
        TYPE AS thread_type
          FROM performance_schema.threads
         WHERE INSTRUMENTED = 'NO'
         ORDER BY disabled_threads;
    END IF;
    IF (in_show_instruments) THEN
        SELECT name AS disabled_instruments,
               timed
          FROM performance_schema.setup_instruments
         WHERE enabled = 'NO'
         ORDER BY disabled_instruments;
    END IF;
END
             Description
             Shows all currently disable Performance Schema configuration.
             Disabled users is only available for MySQL 5.7.6 and later.
             In earlier versions it was only possible to enable users.
             Parameters
             in_show_instruments (BOOLEAN):
               Whether to print disabled instruments (can print many items)
             in_show_threads (BOOLEAN):
               Whether to print disabled threads
             Example
             mysql> CALL sys.ps_setup_show_disabled(TRUE, TRUE);
             +----------------------------+
             | performance_schema_enabled |
             +----------------------------+
             |                          1 |
             +----------------------------+
             1 row in set (0.00 sec)
             +--------------------+
             | disabled_users     |
             +--------------------+
             | 'mark'@'localhost' |
             +--------------------+
             1 row in set (0.00 sec)
             +-------------+----------------------+---------+-------+
             | object_type | objects              | enabled | timed |
             +-------------+----------------------+---------+-------+
             | EVENT       | mysql.%              | NO      | NO    |
             | EVENT       | performance_schema.% | NO      | NO    |
             | EVENT       | information_schema.% | NO      | NO    |
             | FUNCTION    | mysql.%              | NO      | NO    |
             | FUNCTION    | performance_schema.% | NO      | NO    |
             | FUNCTION    | information_schema.% | NO      | NO    |
             | PROCEDURE   | mysql.%              | NO      | NO    |
             | PROCEDURE   | performance_schema.% | NO      | NO    |
             | PROCEDURE   | information_schema.% | NO      | NO    |
             | TABLE       | mysql.%              | NO      | NO    |
             | TABLE       | performance_schema.% | NO      | NO    |
             | TABLE       | information_schema.% | NO      | NO    |
             | TRIGGER     | mysql.%              | NO      | NO    |
             | TRIGGER     | performance_schema.% | NO      | NO    |
             | TRIGGER     | information_schema.% | NO      | NO    |
             +-------------+----------------------+---------+-------+
             15 rows in set (0.00 sec)
             +----------------------------------+
             | disabled_consumers               |
             +----------------------------------+
             | events_stages_current            |
             | events_stages_history            |
             | events_stages_history_long       |
             | events_statements_history        |
             | events_statements_history_long   |
             | events_transactions_history      |
             | events_transactions_history_long |
             | events_waits_current             |
             | events_waits_history             |
             | events_waits_history_long        |
             +----------------------------------+
             10 rows in set (0.00 sec)
             Empty set (0.00 sec)
             +---------------------------------------------------------------------------------------+-------+
             | disabled_instruments                                                                  | timed |
             +---------------------------------------------------------------------------------------+-------+
             | wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_tc                                             | NO    |
             | wait/synch/mutex/sql/LOCK_des_key_file                                                | NO    |
             | wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit                                       | NO    |
             ...
             | memory/sql/servers_cache                                                              | NO    |
             | memory/sql/udf_mem                                                                    | NO    |
             | wait/lock/metadata/sql/mdl                                                            | NO    |
             +---------------------------------------------------------------------------------------+-------+
             547 rows in set (0.00 sec)
             Query OK, 0 rows affected (0.01 sec)
            BEGIN
    SELECT @@performance_schema AS performance_schema_enabled;
    
    SELECT object_type,
           CONCAT(object_schema, '.', object_name) AS objects,
           enabled,
           timed
      FROM performance_schema.setup_objects
     WHERE enabled = 'NO'
     ORDER BY object_type, objects;
    SELECT name AS disabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'NO'
     ORDER BY disabled_consumers;
    IF (in_show_threads) THEN
        SELECT IF(name = 'thread/sql/one_connection',
                  CONCAT(processlist_user, '@', processlist_host),
                  REPLACE(name, 'thread/', '')) AS disabled_threads,
        TYPE AS thread_type
          FROM performance_schema.threads
         WHERE INSTRUMENTED = 'NO'
         ORDER BY disabled_threads;
    END IF;
    IF (in_show_instruments) THEN
        SELECT name AS disabled_instruments,
               timed
          FROM performance_schema.setup_instruments
         WHERE enabled = 'NO'
         ORDER BY disabled_instruments;
    END IF;
ENDc��e��S���i��9i��9P�0�sysps_setup_show_enabledps_setup_show_enabledutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_show_instruments BOOLEAN,
        IN in_show_threads BOOLEAN
    BEGIN
    SELECT @@performance_schema AS performance_schema_enabled;
    SELECT CONCAT('\'', user, '\'@\'', host, '\'') AS enabled_users
      FROM performance_schema.setup_actors
      WHERE enabled = 'YES'
     ORDER BY enabled_users;
    SELECT object_type,
           CONCAT(object_schema, '.', object_name) AS objects,
           enabled,
           timed
      FROM performance_schema.setup_objects
     WHERE enabled = 'YES'
     ORDER BY object_type, objects;
    SELECT name AS enabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'YES'
     ORDER BY enabled_consumers;
    IF (in_show_threads) THEN
        SELECT IF(name = 'thread/sql/one_connection',
                  CONCAT(processlist_user, '@', processlist_host),
                  REPLACE(name, 'thread/', '')) AS enabled_threads,
        TYPE AS thread_type
          FROM performance_schema.threads
         WHERE INSTRUMENTED = 'YES' AND name <> 'thread/innodb/thread_pool_thread'
         ORDER BY enabled_threads;
    END IF;
    IF (in_show_instruments) THEN
        SELECT name AS enabled_instruments,
               timed
          FROM performance_schema.setup_instruments
         WHERE enabled = 'YES'
         ORDER BY enabled_instruments;
    END IF;
END
             Description
             Shows all currently enabled Performance Schema configuration.
             Parameters
             in_show_instruments (BOOLEAN):
               Whether to print enabled instruments (can print many items)
             in_show_threads (BOOLEAN):
               Whether to print enabled threads
             Example
             mysql> CALL sys.ps_setup_show_enabled(TRUE, TRUE);
             +----------------------------+
             | performance_schema_enabled |
             +----------------------------+
             |                          1 |
             +----------------------------+
             1 row in set (0.00 sec)
             +---------------+
             | enabled_users |
             +---------------+
             | '%'@'%'       |
             +---------------+
             1 row in set (0.01 sec)
             +-------------+---------+---------+-------+
             | object_type | objects | enabled | timed |
             +-------------+---------+---------+-------+
             | EVENT       | %.%     | YES     | YES   |
             | FUNCTION    | %.%     | YES     | YES   |
             | PROCEDURE   | %.%     | YES     | YES   |
             | TABLE       | %.%     | YES     | YES   |
             | TRIGGER     | %.%     | YES     | YES   |
             +-------------+---------+---------+-------+
             5 rows in set (0.01 sec)
             +---------------------------+
             | enabled_consumers         |
             +---------------------------+
             | events_statements_current |
             | global_instrumentation    |
             | thread_instrumentation    |
             | statements_digest         |
             +---------------------------+
             4 rows in set (0.05 sec)
             +---------------------------------+-------------+
             | enabled_threads                 | thread_type |
             +---------------------------------+-------------+
             | sql/main                        | BACKGROUND  |
             | sql/thread_timer_notifier       | BACKGROUND  |
             | innodb/io_ibuf_thread           | BACKGROUND  |
             | innodb/io_log_thread            | BACKGROUND  |
             | innodb/io_read_thread           | BACKGROUND  |
             | innodb/io_read_thread           | BACKGROUND  |
             | innodb/io_write_thread          | BACKGROUND  |
             | innodb/io_write_thread          | BACKGROUND  |
             | innodb/page_cleaner_thread      | BACKGROUND  |
             | innodb/srv_lock_timeout_thread  | BACKGROUND  |
             | innodb/srv_error_monitor_thread | BACKGROUND  |
             | innodb/srv_monitor_thread       | BACKGROUND  |
             | innodb/srv_master_thread        | BACKGROUND  |
             | innodb/srv_purge_thread         | BACKGROUND  |
             | innodb/srv_worker_thread        | BACKGROUND  |
             | innodb/srv_worker_thread        | BACKGROUND  |
             | innodb/srv_worker_thread        | BACKGROUND  |
             | innodb/buf_dump_thread          | BACKGROUND  |
             | innodb/dict_stats_thread        | BACKGROUND  |
             | sql/signal_handler              | BACKGROUND  |
             | sql/compress_gtid_table         | FOREGROUND  |
             | root@localhost                  | FOREGROUND  |
             +---------------------------------+-------------+
             22 rows in set (0.01 sec)
             +-------------------------------------+-------+
             | enabled_instruments                 | timed |
             +-------------------------------------+-------+
             | wait/io/file/sql/map                | YES   |
             | wait/io/file/sql/binlog             | YES   |
             ...
             | statement/com/Error                 | YES   |
             | statement/com/                      | YES   |
             | idle                                | YES   |
             +-------------------------------------+-------+
             210 rows in set (0.08 sec)
             Query OK, 0 rows affected (0.89 sec)
            BEGIN
    SELECT @@performance_schema AS performance_schema_enabled;
    SELECT CONCAT('''', user, '''@''', host, '''') AS enabled_users
      FROM performance_schema.setup_actors
      WHERE enabled = 'YES'
     ORDER BY enabled_users;
    SELECT object_type,
           CONCAT(object_schema, '.', object_name) AS objects,
           enabled,
           timed
      FROM performance_schema.setup_objects
     WHERE enabled = 'YES'
     ORDER BY object_type, objects;
    SELECT name AS enabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'YES'
     ORDER BY enabled_consumers;
    IF (in_show_threads) THEN
        SELECT IF(name = 'thread/sql/one_connection',
                  CONCAT(processlist_user, '@', processlist_host),
                  REPLACE(name, 'thread/', '')) AS enabled_threads,
        TYPE AS thread_type
          FROM performance_schema.threads
         WHERE INSTRUMENTED = 'YES' AND name <> 'thread/innodb/thread_pool_thread'
         ORDER BY enabled_threads;
    END IF;
    IF (in_show_instruments) THEN
        SELECT name AS enabled_instruments,
               timed
          FROM performance_schema.setup_instruments
         WHERE enabled = 'YES'
         ORDER BY enabled_instruments;
    END IF;
END�ss�N2�U��BEGIN
    DECLARE v_error BOOLEAN DEFAULT FALSE;
    DECLARE db_quoted VARCHAR(64);
    DECLARE table_quoted VARCHAR(64);
    DECLARE v_table_type VARCHAR(16) DEFAULT '';
    DECLARE v_system_db BOOLEAN
        DEFAULT LOWER(in_db) IN ('information_schema', 'performance_schema');
    DECLARE CONTINUE HANDLER FOR 1050 SET v_error = TRUE;
    DECLARE CONTINUE HANDLER FOR 1146 SET v_error = TRUE;
    SET out_exists = '';
    SET db_quoted = sys.quote_identifier(in_db);
    SET table_quoted = sys.quote_identifier(in_table);
    IF (EXISTS(SELECT 1 FROM information_schema.TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME = in_table)) THEN
        IF v_system_db = FALSE THEN
            SET @sys.tmp.table_exists.SQL = CONCAT('CREATE TEMPORARY TABLE ',
                                                    db_quoted,
                                                    '.',
                                                    table_quoted,
                                                    '(id INT PRIMARY KEY)');
            PREPARE stmt_create_table FROM @sys.tmp.table_exists.SQL;
            EXECUTE stmt_create_table;
            DEALLOCATE PREPARE stmt_create_table;
            SET @sys.tmp.table_exists.SQL = CONCAT('DROP TEMPORARY TABLE ',
                                                                db_quoted,
                                                                '.',
                                                                table_quoted);
            PREPARE stmt_drop_table FROM @sys.tmp.table_exists.SQL;
            EXECUTE stmt_drop_table;
            DEALLOCATE PREPARE stmt_drop_table;
        END IF;
        IF (v_error) THEN
            SET out_exists = 'TEMPORARY';
        ELSE
            SET v_table_type = (SELECT TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME = in_table);
            IF v_table_type = 'SYSTEM VERSIONED' THEN
                SET out_exists = 'BASE TABLE';
            ELSE
                SET out_exists = v_table_type;
            END IF;
        END IF;
    ELSE
        IF v_system_db = FALSE THEN
            SET @sys.tmp.table_exists.SQL = CONCAT('SELECT COUNT(*) FROM ',
                                                            db_quoted,
                                                            '.',
                                                            table_quoted);
            PREPARE stmt_select FROM @sys.tmp.table_exists.SQL;
            IF (NOT v_error) THEN
                DEALLOCATE PREPARE stmt_select;
                SET out_exists = 'TEMPORARY';
            END IF;
        END IF;
    END IF;
ENDN
2�V_TIMESTAMP()''')
        );
        SET v_output_time = @sys.diagnostics.output_time;
        SET v_sql_status_summary_select = CONCAT(v_sql_status_summary_select, ',\n       CONCAT(\n           LEFT(s', v_output_count, '.Variable_value, ', v_status_summary_width, '),\n           IF(', REPLACE(v_no_delta_names, '%{COUNT}', v_output_count), ' AND s', v_output_count, '.Variable_value REGEXP ''^[0-9]+(\\.[0-9]+)?$'', CONCAT('' ('', ROUND(s', v_output_count, '.Variable_value/', v_output_time, ', 2), ''/sec)''), '''')\n       ) AS ''Output ', v_output_count, ''''),
            v_sql_status_summary_from   = CONCAT(v_sql_status_summary_from, '\n',
                                                    IF(v_output_count = 1, '  FROM ', '       INNER JOIN '),
                                                    v_table_name, ' s', v_output_count,
                                                    IF (v_output_count = 1, '', ' USING (Type, Variable_name)'));
        IF (v_output_count > 1) THEN
            SET v_sql_status_summary_delta  = CONCAT(v_sql_status_summary_delta, ',\n       IF(', REPLACE(v_no_delta_names, '%{COUNT}', v_output_count), ' AND s', (v_output_count-1), '.Variable_value REGEXP ''^[0-9]+(\\.[0-9]+)?$'' AND s', v_output_count, '.Variable_value REGEXP ''^[0-9]+(\\.[0-9]+)?$'',\n          CONCAT(IF(s', (v_output_count-1), '.Variable_value REGEXP ''^[0-9]+\\.[0-9]+$'' OR s', v_output_count, '.Variable_value REGEXP ''^[0-9]+\\.[0-9]+$'',\n                    ROUND((s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value), 2),\n                    (s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value)\n                   ),\n                 '' ('', ROUND((s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value)/(', v_output_time, '-', v_output_time_prev, '), 2), ''/sec)''\n          ),\n          ''''\n       ) AS ''Delta (', (v_output_count-1), ' -> ', v_output_count, ')''');
        END IF;
        SET v_output_time_prev = v_output_time;
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            IF (v_has_metrics) THEN
                SELECT 'SELECT * FROM sys.metrics' AS 'The following output is:';
            ELSE
                SELECT 'sys.metrics equivalent' AS 'The following output is:';
            END IF;
            CALL sys.execute_prepared_stmt(CONCAT('SELECT Type, Variable_name, Enabled, Variable_value FROM ', v_table_name, ' ORDER BY Type, Variable_name'));
        END IF;
        IF (v_has_innodb IN ('DEFAULT', 'YES')) THEN
            SELECT 'SHOW ENGINE INNODB STATUS' AS 'The following output is:';
            EXECUTE stmt_innodb_status;
            SELECT 'InnoDB - Transactions' AS 'The following output is:';
            SELECT * FROM information_schema.INNODB_TRX;
        END IF;
        IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
            SELECT 'SHOW ENGINE NDBCLUSTER STATUS' AS 'The following output is:';
            EXECUTE stmt_ndbcluster_status;
            SELECT 'ndbinfo.memoryusage' AS 'The following output is:';
            SELECT node_id, memory_type, sys.format_bytes(used) AS used, used_pages, sys.format_bytes(total) AS total, total_pages,
                   ROUND(100*(used/total), 2) AS 'Used %'
            FROM ndbinfo.memoryusage;
            SET v_done = FALSE;
            OPEN c_ndbinfo;
            c_ndbinfo_loop: LOOP
                FETCH c_ndbinfo INTO v_table_name;
                IF v_done THEN
                LEAVE c_ndbinfo_loop;
                END IF;
                SELECT CONCAT('SELECT * FROM ndbinfo.', v_table_name) AS 'The following output is:';
                CALL sys.execute_prepared_stmt(CONCAT('SELECT * FROM `ndbinfo`.`', v_table_name, '`'));
            END LOOP;
            CLOSE c_ndbinfo;
            SELECT * FROM information_schema.FILES;
        END IF;
        SELECT 'SELECT * FROM sys.processlist' AS 'The following output is:';
        SELECT processlist.* FROM sys.processlist;
        IF (v_has_ps = 'YES') THEN
            IF (sys.ps_is_consumer_enabled('events_waits_history_long') = 'YES') THEN
                SELECT 'SELECT * FROM sys.latest_file_io' AS 'The following output is:';
                SELECT * FROM sys.latest_file_io;
            END IF;
            IF (EXISTS(SELECT 1 FROM performance_schema.setup_instruments WHERE NAME LIKE 'memory/%' AND ENABLED = 'YES')) THEN
                SELECT 'SELECT * FROM sys.memory_by_host_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_host_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_by_thread_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_thread_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_by_user_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_user_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_global_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_global_by_current_bytes;
            END IF;
        END IF;
        SET v_runtime = (UNIX_TIMESTAMP(NOW(2)) - v_start);
    UNTIL (v_runtime + in_interval >= in_max_runtime) END REPEAT;
    IF (v_has_ps = 'YES') THEN
        SELECT 'SHOW ENGINE PERFORMANCE_SCHEMA STATUS' AS 'The following output is:';
        EXECUTE stmt_ps_status;
    END IF;
    IF (v_has_innodb IN ('DEFAULT', 'YES')) THEN
        DEALLOCATE PREPARE stmt_innodb_status;
    END IF;
    IF (v_has_ps = 'YES') THEN
        DEALLOCATE PREPARE stmt_ps_status;
    END IF;
    IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
        DEALLOCATE PREPARE stmt_ndbcluster_status;
    END IF;
    SELECT '\n============================\n     Schema Information\n============================\n' AS '';
    SELECT COUNT(*) AS 'Total Number of Tables' FROM information_schema.TABLES;
    IF (@sys.diagnostics.allow_i_s_tables = 'ON') THEN
        SELECT 'Storage Engine Usage' AS 'The following output is:';
        SELECT ENGINE, COUNT(*) AS NUM_TABLES,
                sys.format_bytes(SUM(DATA_LENGTH)) AS DATA_LENGTH,
                sys.format_bytes(SUM(INDEX_LENGTH)) AS INDEX_LENGTH,
                sys.format_bytes(SUM(DATA_LENGTH+INDEX_LENGTH)) AS TOTAL
            FROM information_schema.TABLES
            GROUP BY ENGINE;
        SELECT 'Schema Object Overview' AS 'The following output is:';
        SELECT * FROM sys.schema_object_overview;
        SELECT 'Tables without a PRIMARY KEY' AS 'The following output is:';
        SELECT TABLES.TABLE_SCHEMA, ENGINE, COUNT(*) AS NumTables
          FROM information_schema.TABLES
               LEFT OUTER JOIN information_schema.STATISTICS ON STATISTICS.TABLE_SCHEMA = TABLES.TABLE_SCHEMA
                                                                AND STATISTICS.TABLE_NAME = TABLES.TABLE_NAME
                                                                AND STATISTICS.INDEX_NAME = 'PRIMARY'
         WHERE STATISTICS.TABLE_NAME IS NULL
               AND TABLES.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
               AND TABLES.TABLE_TYPE = 'BASE TABLE'
         GROUP BY TABLES.TABLE_SCHEMA, ENGINE;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SELECT 'Unused Indexes' AS 'The following output is:';
        SELECT object_schema, COUNT(*) AS NumUnusedIndexes
          FROM performance_schema.table_io_waits_summary_by_index_usage
         WHERE index_name IS NOT NULL
               AND count_star = 0
               AND object_schema NOT IN ('mysql', 'sys')
               AND index_name != 'PRIMARY'
         GROUP BY object_schema;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SELECT '\n=========================\n     Overall Status\n=========================\n' AS '';
        SELECT 'CALL sys.ps_statement_avg_latency_histogram()' AS 'The following output is:';
        CALL sys.ps_statement_avg_latency_histogram();
        CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
        CALL sys.statement_performance_analyzer('overall', NULL, 'with_runtimes_in_95th_percentile');
        SET @sys.diagnostics.si+�U�L��	ql = REPLACE(@sys.diagnostics.sql_gen_query_template, '%{OUTPUT}', 'end');
        IF (@sys.debug = 'ON') THEN
            SELECT 'The following query will be used to generate the query for each sys view' AS 'Debug';
            SELECT @sys.diagnostics.sql AS 'Debug';
        END IF;
        PREPARE stmt_gen_query FROM @sys.diagnostics.sql;
        SET v_done = FALSE;
        OPEN c_sysviews_w_delta;
        c_sysviews_w_delta_loop: LOOP
            FETCH c_sysviews_w_delta INTO v_table_name;
            IF v_done THEN
                LEAVE c_sysviews_w_delta_loop;
            END IF;
            IF (@sys.debug = 'ON') THEN
                SELECT CONCAT('The following queries are for storing the final content of ', v_table_name) AS 'Debug';
            END IF;
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS `tmp_', v_table_name, '_end`'));
            CALL sys.execute_prepared_stmt(CONCAT('CREATE TEMPORARY TABLE `tmp_', v_table_name, '_end` SELECT * FROM `sys`.`x$', v_table_name, '`'));
            IF (@sys.diagnostics.include_raw = 'ON') THEN
                SET @sys.diagnostics.table_name = CONCAT('x$', v_table_name);
                EXECUTE stmt_gen_query USING @sys.diagnostics.table_name;
                SELECT CONCAT(@sys.diagnostics.sql_select,
                                IF(order_by IS NOT NULL, CONCAT('\n ORDER BY ', REPLACE(order_by, '%{TABLE}', CONCAT('tmp_', v_table_name, '_end'))), ''),
                                IF(limit_rows IS NOT NULL, CONCAT('\n LIMIT ', limit_rows), '')
                        )
                    INTO @sys.diagnostics.sql_select
                    FROM tmp_sys_views_delta
                    WHERE TABLE_NAME = v_table_name;
                SELECT CONCAT('Overall ', v_table_name) AS 'The following output is:';
                CALL sys.execute_prepared_stmt(@sys.diagnostics.sql_select);
            END IF;
        END LOOP;
        CLOSE c_sysviews_w_delta;
        DEALLOCATE PREPARE stmt_gen_query;
        SELECT '\n======================\n     Delta Status\n======================\n' AS '';
        CALL sys.statement_performance_analyzer('delta', 'tmp_digests_start', 'with_runtimes_in_95th_percentile');
        CALL sys.statement_performance_analyzer('cleanup', NULL, NULL);
        DROP TEMPORARY TABLE tmp_digests_start;
        IF (@sys.debug = 'ON') THEN
            SELECT 'The following query will be used to generate the query for each sys view delta' AS 'Debug';
            SELECT @sys.diagnostics.sql_gen_query_delta AS 'Debug';
        END IF;
        PREPARE stmt_gen_query_delta FROM @sys.diagnostics.sql_gen_query_delta;
        SET v_old_group_concat_max_len = @@session.group_concat_max_len;
        SET @@session.group_concat_max_len = 2048;
        SET v_done = FALSE;
        OPEN c_sysviews_w_delta;
        c_sysviews_w_delta_loop: LOOP
            FETCH c_sysviews_w_delta INTO v_table_name;
            IF v_done THEN
                LEAVE c_sysviews_w_delta_loop;
            END IF;
            SET @sys.diagnostics.table_name = v_table_name;
            EXECUTE stmt_gen_query_delta USING @sys.diagnostics.table_name;
            SELECT CONCAT(@sys.diagnostics.sql_select,
                            IF(where_delta IS NOT NULL, CONCAT('\n WHERE ', where_delta), ''),
                            IF(order_by_delta IS NOT NULL, CONCAT('\n ORDER BY ', order_by_delta), ''),
                            IF(limit_rows IS NOT NULL, CONCAT('\n LIMIT ', limit_rows), '')
                    )
                INTO @sys.diagnostics.sql_select
                FROM tmp_sys_views_delta
                WHERE TABLE_NAME = v_table_name;
            SELECT CONCAT('Delta ', v_table_name) AS 'The following output is:';
            CALL sys.execute_prepared_stmt(@sys.diagnostics.sql_select);
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE `tmp_', v_table_name, '_end`'));
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE `tmp_', v_table_name, '_start`'));
        END LOOP;
        CLOSE c_sysviews_w_delta;
        SET @@session.group_concat_max_len = v_old_group_concat_max_len;
        DEALLOCATE PREPARE stmt_gen_query_delta;
        DROP TEMPORARY TABLE tmp_sys_views_delta;
    END IF;
    IF (v_has_metrics) THEN
        SELECT 'SELECT * FROM sys.metrics' AS 'The following output is:';
    ELSE
        SELECT 'sys.metrics equivalent' AS 'The following output is:';
    END IF;
    CALL sys.execute_prepared_stmt(
        CONCAT(v_sql_status_summary_select, v_sql_status_summary_delta, ', Type, s1.Enabled', v_sql_status_summary_from,
               '\n ORDER BY Type, Variable_name'
        )
    );
    SET v_count = 0;
    WHILE (v_count < v_output_count) DO
        SET v_count = v_count + 1;
        SET v_table_name = CONCAT('tmp_metrics_', v_count);
        CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS ', v_table_name));
    END WHILE;
    IF (in_auto_config <> 'current') THEN
        CALL sys.ps_setup_reload_saved();
        SET sql_log_bin = @log_bin;
    END IF;
    SET @sys.diagnostics.output_time            = NULL,
        @sys.diagnostics.sql                    = NULL,
        @sys.diagnostics.sql_gen_query_delta    = NULL,
        @sys.diagnostics.sql_gen_query_template = NULL,
        @sys.diagnostics.sql_select             = NULL,
        @sys.diagnostics.table_name             = NULL;
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = @log_bin;
    END IF;
END���OBEGIN
SELECT CONCAT('\n',
       '\n  . = 1 unit',
       '\n  * = 2 units',
       '\n  # = 3 units\n',
       @label := CONCAT(@label_inner := CONCAT('\n(0 - ',
                                               ROUND((@bucket_size := (SELECT ROUND((MAX(avg_us) - MIN(avg_us)) / (@buckets := 16)) AS size
                                                                         FROM sys.x$ps_digest_avg_latency_distribution)) / (@unit_div := 1000)),
                                                (@unit := 'ms'), ')'),
                        REPEAT(' ', (@max_label_size := ((1 + LENGTH(ROUND((@bucket_size * 15) / @unit_div)) + 3 + LENGTH(ROUND(@bucket_size * 16) / @unit_div)) + 1)) - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us <= @bucket_size), 0)),
       REPEAT(' ', (@max_label_len := (@max_label_size + LENGTH((@total_queries := (SELECT SUM(cnt) FROM sys.x$ps_digest_avg_latency_distribution)))) + 1) - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < (@one_unit := 40), '.', IF(@count_in_bucket < (@two_unit := 80), '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND(@bucket_size / @unit_div), ' - ', ROUND((@bucket_size * 2) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size AND b1.avg_us <= @bucket_size * 2), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 2) / @unit_div), ' - ', ROUND((@bucket_size * 3) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 2 AND b1.avg_us <= @bucket_size * 3), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 3) / @unit_div), ' - ', ROUND((@bucket_size * 4) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 3 AND b1.avg_us <= @bucket_size * 4), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 4) / @unit_div), ' - ', ROUND((@bucket_size * 5) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 4 AND b1.avg_us <= @bucket_size * 5), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 5) / @unit_div), ' - ', ROUND((@bucket_size * 6) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 5 AND b1.avg_us <= @bucket_size * 6), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 6) / @unit_div), ' - ', ROUND((@bucket_size * 7) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 6 AND b1.avg_us <= @bucket_size * 7), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 7) / @unit_div), ' - ', ROUND((@bucket_size * 8) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 7 AND b1.avg_us <= @bucket_size * 8), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 8) / @unit_div), ' - ', ROUND((@bucket_size * 9) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 8 AND b1.avg_us <= @bucket_size * 9), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@�m�count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 9) / @unit_div), ' - ', ROUND((@bucket_size * 10) / @unit_div), @unit, ')'),
                         REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                         @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                       FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                      WHERE b1.avg_us > @bucket_size * 9 AND b1.avg_us <= @bucket_size * 10), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 10) / @unit_div), ' - ', ROUND((@bucket_size * 11) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 10 AND b1.avg_us <= @bucket_size * 11), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 11) / @unit_div), ' - ', ROUND((@bucket_size * 12) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 11 AND b1.avg_us <= @bucket_size * 12), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 12) / @unit_div), ' - ', ROUND((@bucket_size * 13) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 12 AND b1.avg_us <= @bucket_size * 13), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 13) / @unit_div), ' - ', ROUND((@bucket_size * 14) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 13 AND b1.avg_us <= @bucket_size * 14), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 14) / @unit_div), ' - ', ROUND((@bucket_size * 15) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 14 AND b1.avg_us <= @bucket_size * 15), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 15) / @unit_div), ' - ', ROUND((@bucket_size * 16) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 15 AND b1.avg_us <= @bucket_size * 16), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       '\n\n  Total Statements: ', @total_queries, '; Buckets: ', @buckets , '; Bucket Size: ', ROUND(@bucket_size / @unit_div) , ' ', @unit, ';\n'
      ) AS `Performance Schema Statement Digest Average Latency Histogram`;
END�qBEGIN
SELECT CONCAT('\n',
       '\n  . = 1 unit',
       '\n  * = 2 units',
       '\n  # = 3 units\n',
       @label := CONCAT(@label_inner := CONCAT('\n(0 - ',
                                               ROUND((@bucket_size := (SELECT ROUND((MAX(avg_us) - MIN(avg_us)) / (@buckets := 16)) AS size
                                                                         FROM sys.x$ps_digest_avg_latency_distribution)) / (@unit_div := 1000)),
                                                (@unit := 'ms'), ')'),
                        REPEAT(' ', (@max_label_size := ((1 + LENGTH(ROUND((@bucket_size * 15) / @unit_div)) + 3 + LENGTH(ROUND(@bucket_size * 16) / @unit_div)) + 1)) - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us <= @bucket_size), 0)),
       REPEAT(' ', (@max_label_len := (@max_label_size + LENGTH((@total_queries := (SELECT SUM(cnt) FROM sys.x$ps_digest_avg_latency_distribution)))) + 1) - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < (@one_unit := 40), '.', IF(@count_in_bucket < (@two_unit := 80), '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND(@bucket_size / @unit_div), ' - ', ROUND((@bucket_size * 2) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size AND b1.avg_us <= @bucket_size * 2), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 2) / @unit_div), ' - ', ROUND((@bucket_size * 3) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 2 AND b1.avg_us <= @bucket_size * 3), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 3) / @unit_div), ' - ', ROUND((@bucket_size * 4) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 3 AND b1.avg_us <= @bucket_size * 4), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 4) / @unit_div), ' - ', ROUND((@bucket_size * 5) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 4 AND b1.avg_us <= @bucket_size * 5), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 5) / @unit_div), ' - ', ROUND((@bucket_size * 6) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 5 AND b1.avg_us <= @bucket_size * 6), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 6) / @unit_div), ' - ', ROUND((@bucket_size * 7) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 6 AND b1.avg_us <= @bucket_size * 7), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 7) / @unit_div), ' - ', ROUND((@bucket_size * 8) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 7 AND b1.avg_us <= @bucket_size * 8), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 8) / @unit_div), ' - ', ROUND((@bucket_size * 9) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 8 AND b1.avg_us <= @bucket_size * 9), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@���count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 9) / @unit_div), ' - ', ROUND((@bucket_size * 10) / @unit_div), @unit, ')'),
                         REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                         @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                       FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                      WHERE b1.avg_us > @bucket_size * 9 AND b1.avg_us <= @bucket_size * 10), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 10) / @unit_div), ' - ', ROUND((@bucket_size * 11) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 10 AND b1.avg_us <= @bucket_size * 11), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 11) / @unit_div), ' - ', ROUND((@bucket_size * 12) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 11 AND b1.avg_us <= @bucket_size * 12), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 12) / @unit_div), ' - ', ROUND((@bucket_size * 13) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 12 AND b1.avg_us <= @bucket_size * 13), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 13) / @unit_div), ' - ', ROUND((@bucket_size * 14) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 13 AND b1.avg_us <= @bucket_size * 14), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 14) / @unit_div), ' - ', ROUND((@bucket_size * 15) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 14 AND b1.avg_us <= @bucket_size * 15), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 15) / @unit_div), ' - ', ROUND((@bucket_size * 16) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 15 AND b1.avg_us <= @bucket_size * 16), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       '\n\n  Total Statements: ', @total_queries, '; Buckets: ', @buckets , '; Bucket Size: ', ROUND(@bucket_size / @unit_div) , ' ', @unit, ';\n'
      ) AS `Performance Schema Statement Digest Average Latency Histogram`;
END+O�c�P�:BEGIN
    DECLARE v_start_fresh BOOLEAN DEFAULT false;
    DECLARE v_auto_enable BOOLEAN DEFAULT false;
    DECLARE v_explain     BOOLEAN DEFAULT true;
    DECLARE v_this_thread_enabed ENUM('YES', 'NO');
    DECLARE v_runtime INT DEFAULT 0;
    DECLARE v_start INT DEFAULT 0;
    DECLARE v_found_stmts INT;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    DROP TEMPORARY TABLE IF EXISTS stmt_trace;
    CREATE TEMPORARY TABLE stmt_trace (
        thread_id BIGINT UNSIGNED,
        timer_start BIGINT UNSIGNED,
        event_id BIGINT UNSIGNED,
        sql_text longtext,
        timer_wait BIGINT UNSIGNED,
        lock_time BIGINT UNSIGNED,
        errors BIGINT UNSIGNED,
        mysql_errno INT,
        rows_sent BIGINT UNSIGNED,
        rows_affected BIGINT UNSIGNED,
        rows_examined BIGINT UNSIGNED,
        created_tmp_tables BIGINT UNSIGNED,
        created_tmp_disk_tables BIGINT UNSIGNED,
        no_index_used BIGINT UNSIGNED,
        PRIMARY KEY (thread_id, timer_start)
    );
    DROP TEMPORARY TABLE IF EXISTS stmt_stages;
    CREATE TEMPORARY TABLE stmt_stages (
       event_id BIGINT UNSIGNED,
       stmt_id BIGINT UNSIGNED,
       event_name VARCHAR(128),
       timer_wait BIGINT UNSIGNED,
       PRIMARY KEY (event_id)
    );
    SET v_start_fresh = in_start_fresh;
    IF v_start_fresh THEN
        TRUNCATE TABLE performance_schema.events_statements_history_long;
        TRUNCATE TABLE performance_schema.events_stages_history_long;
    END IF;
    SET v_auto_enable = in_auto_enable;
    IF v_auto_enable THEN
        CALL sys.ps_setup_save(0);
        UPDATE performance_schema.threads
           SET INSTRUMENTED = IF(PROCESSLIST_ID IS NOT NULL, 'YES', 'NO');
        UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
         WHERE NAME NOT LIKE '%\_history'
               AND NAME NOT LIKE 'events_wait%'
               AND NAME NOT LIKE 'events_transactions%'
               AND NAME <> 'statements_digest';
        UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES',
               TIMED   = 'YES'
         WHERE NAME LIKE 'statement/%' OR NAME LIKE 'stage/%';
    END IF;
    WHILE v_runtime < in_runtime DO
        SELECT UNIX_TIMESTAMP() INTO v_start;
        INSERT IGNORE INTO stmt_trace
        SELECT thread_id, timer_start, event_id, sql_text, timer_wait, lock_time, errors, mysql_errno,
               rows_sent, rows_affected, rows_examined, created_tmp_tables, created_tmp_disk_tables, no_index_used
          FROM performance_schema.events_statements_history_long
        WHERE digest = in_digest;
        INSERT IGNORE INTO stmt_stages
        SELECT stages.event_id, stmt_trace.event_id,
               stages.event_name, stages.timer_wait
          FROM performance_schema.events_stages_history_long AS stages
          JOIN stmt_trace ON stages.nesting_event_id = stmt_trace.event_id;
        SELECT SLEEP(in_interval) INTO @sleep;
        SET v_runtime = v_runtime + (UNIX_TIMESTAMP() - v_start);
    END WHILE;
    SELECT "SUMMARY STATISTICS";
    SELECT COUNT(*) executions,
           sys.format_time(SUM(timer_wait)) AS exec_time,
           sys.format_time(SUM(lock_time)) AS lock_time,
           SUM(rows_sent) AS rows_sent,
           SUM(rows_affected) AS rows_affected,
           SUM(rows_examined) AS rows_examined,
           SUM(created_tmp_tables) AS tmp_tables,
           SUM(no_index_used) AS full_scans
      FROM stmt_trace;
    SELECT event_name,
           COUNT(*) as count,
           sys.format_time(SUM(timer_wait)) as latency
      FROM stmt_stages
     GROUP BY event_name
     ORDER BY SUM(timer_wait) DESC;
    SELECT "LONGEST RUNNING STATEMENT";
    SELECT thread_id,
           sys.format_time(timer_wait) AS exec_time,
           sys.format_time(lock_time) AS lock_time,
           rows_sent,
           rows_affected,
           rows_examined,
           created_tmp_tables AS tmp_tables,
           no_index_used AS full_scan
      FROM stmt_trace
     ORDER BY timer_wait DESC LIMIT 1;
    SELECT sql_text
      FROM stmt_trace
     ORDER BY timer_wait DESC LIMIT 1;
    SELECT sql_text, event_id INTO @sql, @sql_id
      FROM stmt_trace
    ORDER BY timer_wait DESC LIMIT 1;
    IF (@sql_id IS NOT NULL) THEN
        SELECT event_name,
               sys.format_time(timer_wait) as latency
          FROM stmt_stages
         WHERE stmt_id = @sql_id
         ORDER BY event_id;
    END IF;
    DROP TEMPORARY TABLE stmt_trace;
    DROP TEMPORARY TABLE stmt_stages;
    IF (@sql IS NOT NULL) THEN
        SET @stmt := CONCAT("EXPLAIN FORMAT=JSON ", @sql);
        BEGIN
            DECLARE CONTINUE HANDLER FOR 1064, 1146 SET v_explain = false;
            PREPARE explain_stmt FROM @stmt;
        END;
        IF (v_explain) THEN
            EXECUTE explain_stmt;
            DEALLOCATE PREPARE explain_stmt;
        END IF;
    END IF;
    IF v_auto_enable THEN
        CALL sys.ps_setup_reload_saved();
    END IF;
    IF (v_this_thread_enabed = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    SET sql_log_bin = @log_bin;
END����J
             Description
             Traces all instrumentation within Performance Schema for a specific
             Statement Digest.
             When finding a statement of interest within the
             performance_schema.events_statements_summary_by_digest table, feed
             the DIGEST MD5 value in to this procedure, set how long to poll for,
             and at what interval to poll, and it will generate a report of all
             statistics tracked within Performance Schema for that digest for the
             interval.
             It will also attempt to generate an EXPLAIN for the longest running
             example of the digest during the interval. Note this may fail, as:
                * Performance Schema truncates long SQL_TEXT values (and hence the
                  EXPLAIN will fail due to parse errors)
                * the default schema is sys (so tables that are not fully qualified
                  in the query may not be found)
                * some queries such as SHOW are not supported in EXPLAIN.
             When the EXPLAIN fails, the error will be ignored and no EXPLAIN
             output generated.
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Parameters
             in_digest (VARCHAR(32)):
               The statement digest identifier you would like to analyze
             in_runtime (INT):
               The number of seconds to run analysis for
             in_interval (DECIMAL(2,2)):
               The interval (in seconds, may be fractional) at which to try
               and take snapshots
             in_start_fresh (BOOLEAN):
               Whether to TRUNCATE the events_statements_history_long and
               events_stages_history_long tables before starting
             in_auto_enable (BOOLEAN):
               Whether to automatically turn on required consumers
             Example
             mysql> call ps_trace_statement_digest('891ec6860f98ba46d89dd20b0c03652c', 10, 0.1, true, true);
             +--------------------+
             | SUMMARY STATISTICS |
             +--------------------+
             | SUMMARY STATISTICS |
             +--------------------+
             1 row in set (9.11 sec)
             +------------+-----------+-----------+-----------+---------------+------------+------------+
             | executions | exec_time | lock_time | rows_sent | rows_examined | tmp_tables | full_scans |
             +------------+-----------+-----------+-----------+---------------+------------+------------+
             |         21 | 4.11 ms   | 2.00 ms   |         0 |            21 |          0 |          0 |
             +------------+-----------+-----------+-----------+---------------+------------+------------+
             1 row in set (9.11 sec)
             +------------------------------------------+-------+-----------+
             | event_name                               | count | latency   |
             +------------------------------------------+-------+-----------+
             | stage/sql/checking query cache for query |    16 | 724.37 us |
             | stage/sql/statistics                     |    16 | 546.92 us |
             | stage/sql/freeing items                  |    18 | 520.11 us |
             | stage/sql/init                           |    51 | 466.80 us |
             ...
             | stage/sql/cleaning up                    |    18 | 11.92 us  |
             | stage/sql/executing                      |    16 | 6.95 us   |
             +------------------------------------------+-------+-----------+
             17 rows in set (9.12 sec)
             +---------------------------+
             | LONGEST RUNNING STATEMENT |
             +---------------------------+
             | LONGEST RUNNING STATEMENT |
             +---------------------------+
             1 row in set (9.16 sec)
             +-----------+-----------+-----------+-----------+---------------+------------+-----------+
             | thread_id | exec_time | lock_time | rows_sent | rows_examined | tmp_tables | full_scan |
             +-----------+-----------+-----------+-----------+---------------+------------+-----------+
             |    166646 | 618.43 us | 1.00 ms   |         0 |             1 |          0 |         0 |
             +-----------+-----------+-----------+-----------+---------------+------------+-----------+
             1 row in set (9.16 sec)
             // Truncated for clarity...
             +-----------------------------------------------------------------+
             | sql_text                                                        |
             +-----------------------------------------------------------------+
             | select hibeventhe0_.id as id1382_, hibeventhe0_.createdTime ... |
             +-----------------------------------------------------------------+
             1 row in set (9.17 sec)
             +------------------------------------------+-----------+
             | event_name                               | latency   |
             +------------------------------------------+-----------+
             | stage/sql/init                           | 8.61 us   |
             | stage/sql/Waiting for query cache lock   | 453.23 us |
             | stage/sql/init                           | 331.07 ns |
             | stage/sql/checking query cache for query | 43.04 us  |
             ...
             | stage/sql/freeing items                  | 30.46 us  |
             | stage/sql/cleaning up                    | 662.13 ns |
             +------------------------------------------+-----------+
             18 rows in set (9.23 sec)
             +----+-------------+--------------+-------+---------------+-----------+---------+-------------+------+-------+
             | id | select_type | table        | type  | possible_keys | key       | key_len | ref         | rows | Extra |
             +----+-------------+--------------+-------+---------------+-----------+---------+-------------+------+-------+
             |  1 | SIMPLE      | hibeventhe0_ | const | fixedTime     | fixedTime | 775     | const,const |    1 | NULL  |
             +----+-------------+--------------+-------+---------------+-----------+---------+-------------+------+-------+
             1 row in set (9.27 sec)
             Query OK, 0 rows affected (9.28 sec)
            �c?dc�P�:BEGIN
    DECLARE v_start_fresh BOOLEAN DEFAULT false;
    DECLARE v_auto_enable BOOLEAN DEFAULT false;
    DECLARE v_explain     BOOLEAN DEFAULT true;
    DECLARE v_this_thread_enabed ENUM('YES', 'NO');
    DECLARE v_runtime INT DEFAULT 0;
    DECLARE v_start INT DEFAULT 0;
    DECLARE v_found_stmts INT;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    DROP TEMPORARY TABLE IF EXISTS stmt_trace;
    CREATE TEMPORARY TABLE stmt_trace (
        thread_id BIGINT UNSIGNED,
        timer_start BIGINT UNSIGNED,
        event_id BIGINT UNSIGNED,
        sql_text longtext,
        timer_wait BIGINT UNSIGNED,
        lock_time BIGINT UNSIGNED,
        errors BIGINT UNSIGNED,
        mysql_errno INT,
        rows_sent BIGINT UNSIGNED,
        rows_affected BIGINT UNSIGNED,
        rows_examined BIGINT UNSIGNED,
        created_tmp_tables BIGINT UNSIGNED,
        created_tmp_disk_tables BIGINT UNSIGNED,
        no_index_used BIGINT UNSIGNED,
        PRIMARY KEY (thread_id, timer_start)
    );
    DROP TEMPORARY TABLE IF EXISTS stmt_stages;
    CREATE TEMPORARY TABLE stmt_stages (
       event_id BIGINT UNSIGNED,
       stmt_id BIGINT UNSIGNED,
       event_name VARCHAR(128),
       timer_wait BIGINT UNSIGNED,
       PRIMARY KEY (event_id)
    );
    SET v_start_fresh = in_start_fresh;
    IF v_start_fresh THEN
        TRUNCATE TABLE performance_schema.events_statements_history_long;
        TRUNCATE TABLE performance_schema.events_stages_history_long;
    END IF;
    SET v_auto_enable = in_auto_enable;
    IF v_auto_enable THEN
        CALL sys.ps_setup_save(0);
        UPDATE performance_schema.threads
           SET INSTRUMENTED = IF(PROCESSLIST_ID IS NOT NULL, 'YES', 'NO');
        UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
         WHERE NAME NOT LIKE '%\_history'
               AND NAME NOT LIKE 'events_wait%'
               AND NAME NOT LIKE 'events_transactions%'
               AND NAME <> 'statements_digest';
        UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES',
               TIMED   = 'YES'
         WHERE NAME LIKE 'statement/%' OR NAME LIKE 'stage/%';
    END IF;
    WHILE v_runtime < in_runtime DO
        SELECT UNIX_TIMESTAMP() INTO v_start;
        INSERT IGNORE INTO stmt_trace
        SELECT thread_id, timer_start, event_id, sql_text, timer_wait, lock_time, errors, mysql_errno,
               rows_sent, rows_affected, rows_examined, created_tmp_tables, created_tmp_disk_tables, no_index_used
          FROM performance_schema.events_statements_history_long
        WHERE digest = in_digest;
        INSERT IGNORE INTO stmt_stages
        SELECT stages.event_id, stmt_trace.event_id,
               stages.event_name, stages.timer_wait
          FROM performance_schema.events_stages_history_long AS stages
          JOIN stmt_trace ON stages.nesting_event_id = stmt_trace.event_id;
        SELECT SLEEP(in_interval) INTO @sleep;
        SET v_runtime = v_runtime + (UNIX_TIMESTAMP() - v_start);
    END WHILE;
    SELECT "SUMMARY STATISTICS";
    SELECT COUNT(*) executions,
           sys.format_time(SUM(timer_wait)) AS exec_time,
           sys.format_time(SUM(lock_time)) AS lock_time,
           SUM(rows_sent) AS rows_sent,
           SUM(rows_affected) AS rows_affected,
           SUM(rows_examined) AS rows_examined,
           SUM(created_tmp_tables) AS tmp_tables,
           SUM(no_index_used) AS full_scans
      FROM stmt_trace;
    SELECT event_name,
           COUNT(*) as count,
           sys.format_time(SUM(timer_wait)) as latency
      FROM stmt_stages
     GROUP BY event_name
     ORDER BY SUM(timer_wait) DESC;
    SELECT "LONGEST RUNNING STATEMENT";
    SELECT thread_id,
           sys.format_time(timer_wait) AS exec_time,
           sys.format_time(lock_time) AS lock_time,
           rows_sent,
           rows_affected,
           rows_examined,
           created_tmp_tables AS tmp_tables,
           no_index_used AS full_scan
      FROM stmt_trace
     ORDER BY timer_wait DESC LIMIT 1;
    SELECT sql_text
      FROM stmt_trace
     ORDER BY timer_wait DESC LIMIT 1;
    SELECT sql_text, event_id INTO @sql, @sql_id
      FROM stmt_trace
    ORDER BY timer_wait DESC LIMIT 1;
    IF (@sql_id IS NOT NULL) THEN
        SELECT event_name,
               sys.format_time(timer_wait) as latency
          FROM stmt_stages
         WHERE stmt_id = @sql_id
         ORDER BY event_id;
    END IF;
    DROP TEMPORARY TABLE stmt_trace;
    DROP TEMPORARY TABLE stmt_stages;
    IF (@sql IS NOT NULL) THEN
        SET @stmt := CONCAT("EXPLAIN FORMAT=JSON ", @sql);
        BEGIN
            DECLARE CONTINUE HANDLER FOR 1064, 1146 SET v_explain = false;
            PREPARE explain_stmt FROM @stmt;
        END;
        IF (v_explain) THEN
            EXECUTE explain_stmt;
            DEALLOCATE PREPARE explain_stmt;
        END IF;
    END IF;
    IF v_auto_enable THEN
        CALL sys.ps_setup_reload_saved();
    END IF;
    IF (v_this_thread_enabed = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    SET sql_log_bin = @log_bin;
END��-P�BEGIN
    DECLARE v_done bool DEFAULT FALSE;
    DECLARE v_start, v_runtime DECIMAL(20,2) DEFAULT 0.0;
    DECLARE v_min_event_id bigint unsigned DEFAULT 0;
    DECLARE v_this_thread_enabed ENUM('YES', 'NO');
    DECLARE v_event longtext;
    DECLARE c_stack CURSOR FOR
        SELECT CONCAT(IF(nesting_event_id IS NOT NULL, CONCAT(nesting_event_id, ' -> '), ''),
                    event_id, '; ', event_id, ' [label="',
                    '(', sys.format_time(timer_wait), ') ',
                    IF (event_name NOT LIKE 'wait/io%',
                        SUBSTRING_INDEX(event_name, '/', -2),
                        IF (event_name NOT LIKE 'wait/io/file%' OR event_name NOT LIKE 'wait/io/socket%',
                            SUBSTRING_INDEX(event_name, '/', -4),
                            event_name)
                        ),
                    IF (event_name LIKE 'statement/%', IFNULL(CONCAT('\\n', wait_info), ''), ''),
                    IF (in_debug AND event_name LIKE 'wait%', wait_info, ''),
                    '", ',
                    CASE WHEN event_name LIKE 'wait/io/file%' THEN
                           'shape=box, style=filled, color=red'
                         WHEN event_name LIKE 'wait/io/table%' THEN
                           'shape=box, style=filled, color=green'
                         WHEN event_name LIKE 'wait/io/socket%' THEN
                           'shape=box, style=filled, color=yellow'
                         WHEN event_name LIKE 'wait/synch/mutex%' THEN
                           'style=filled, color=lightskyblue'
                         WHEN event_name LIKE 'wait/synch/cond%' THEN
                           'style=filled, color=darkseagreen3'
                         WHEN event_name LIKE 'wait/synch/rwlock%' THEN
                           'style=filled, color=orchid'
                         WHEN event_name LIKE 'wait/lock%' THEN
                           'shape=box, style=filled, color=tan'
                         WHEN event_name LIKE 'statement/%' THEN
                           CONCAT('shape=box, style=bold',
                                  CASE WHEN event_name LIKE 'statement/com/%' THEN
                                         ' style=filled, color=darkseagreen'
                                       ELSE
                                         IF((timer_wait/1000000000000) > @@long_query_time,
                                            ' style=filled, color=red',
                                            ' style=filled, color=lightblue')
                                  END
                           )
                         WHEN event_name LIKE 'stage/%' THEN
                           'style=filled, color=slategray3'
                         WHEN event_name LIKE '%idle%' THEN
                           'shape=box, style=filled, color=firebrick3'
                         ELSE '' END,
                     '];\n'
                   ) event, event_id
        FROM (
             (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id,
                     CONCAT(sql_text, '\\n',
                            'errors: ', errors, '\\n',
                            'warnings: ', warnings, '\\n',
                            'lock time: ', sys.format_time(lock_time),'\\n',
                            'rows affected: ', rows_affected, '\\n',
                            'rows sent: ', rows_sent, '\\n',
                            'rows examined: ', rows_examined, '\\n',
                            'tmp tables: ', created_tmp_tables, '\\n',
                            'tmp disk tables: ', created_tmp_disk_tables, '\\n'
                            'select scan: ', select_scan, '\\n',
                            'select full join: ', select_full_join, '\\n',
                            'select full range join: ', select_full_range_join, '\\n',
                            'select range: ', select_range, '\\n',
                            'select range check: ', select_range_check, '\\n',
                            'sort merge passes: ', sort_merge_passes, '\\n',
                            'sort rows: ', sort_rows, '\\n',
                            'sort range: ', sort_range, '\\n',
                            'sort scan: ', sort_scan, '\\n',
                            'no index used: ', IF(no_index_used, 'TRUE', 'FALSE'), '\\n',
                            'no good index used: ', IF(no_good_index_used, 'TRUE', 'FALSE'), '\\n'
                     ) AS wait_info
                FROM performance_schema.events_statements_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
             UNION
             (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id, null AS wait_info
                FROM performance_schema.events_stages_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
             UNION
             (SELECT thread_id, event_id,
                     CONCAT(event_name,
                            IF(event_name NOT LIKE 'wait/synch/mutex%', IFNULL(CONCAT(' - ', operation), ''), ''),
                            IF(number_of_bytes IS NOT NULL, CONCAT(' ', number_of_bytes, ' bytes'), ''),
                            IF(event_name LIKE 'wait/io/file%', '\\n', ''),
                            IF(object_schema IS NOT NULL, CONCAT('\\nObject: ', object_schema, '.'), ''),
                            IF(object_name IS NOT NULL,
                               IF (event_name LIKE 'wait/io/socket%',
                                   CONCAT('\\n', IF (object_name LIKE ':0%', @@socket, object_name)),
                                   object_name),
                               ''
                            ),
                            IF(index_name IS NOT NULL, CONCAT(' Index: ', index_name), ''), '\\n'
                     ) AS event_name,
                     timer_wait, timer_start, nesting_event_id, source AS wait_info
                FROM performance_schema.events_waits_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
           ) events
       ORDER BY event_id;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    IF (in_auto_setup) THEN
        CALL sys.ps_setup_save(0);
        DELETE FROM performance_schema.setup_actors;
        UPDATE performance_schema.threads
           SET INSTRUMENTED = IF(THREAD_ID = in_thread_id, 'YES', 'NO');
        UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
         WHERE NAME NOT LIKE '%\_history';
        UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES',
               TIMED   = 'YES';
    END IF;
    IF (in_start_fresh) THEN
        TRUNCATE performance_schema.events_statements_history_long;
        TRUNCATE performance_schema.events_stages_history_long;
        TRUNCATE performance_schema.events_waits_history_long;
    END IF;
    DROP TEMPORARY TABLE IF EXISTS tmp_events;
    CREATE TEMPORARY TABLE tmp_events (
      event_id bigint unsigned NOT NULL,
      event longblob,
      PRIMARY KEY (event_id)
    );
    INSERT INTO tmp_events VALUES (0, CONCAT('digraph events { rankdir=LR; nodesep=0.10;\n',
                                             '// Stack created .....: ', NOW(), '\n',
                                             '// MySQL version .....: ', VERSION(), '\n',
                                             '// MySQL hostname ....: ', @@hostname, '\n',
                                             '// MySQL port ........: ', @@port, '\n',
                                             '// MySQL socket ......: ', @@socket, '\n',
                                             '// MySQL user ........: ', CURRENT_USER(), '\n'));
    SELECT CONCAT('Data collection starting for THREAD_ID = ', in_thread_id) AS 'Info';
    SET v_min_event_id = 0,
        v_start        = UNIX_T=ֺ�BEGIN
    DECLARE v_done bool DEFAULT FALSE;
    DECLARE v_start, v_runtime DECIMAL(20,2) DEFAULT 0.0;
    DECLARE v_min_event_id bigint unsigned DEFAULT 0;
    DECLARE v_this_thread_enabed ENUM('YES', 'NO');
    DECLARE v_event longtext;
    DECLARE c_stack CURSOR FOR
        SELECT CONCAT(IF(nesting_event_id IS NOT NULL, CONCAT(nesting_event_id, ' -> '), ''),
                    event_id, '; ', event_id, ' [label="',
                    '(', sys.format_time(timer_wait), ') ',
                    IF (event_name NOT LIKE 'wait/io%',
                        SUBSTRING_INDEX(event_name, '/', -2),
                        IF (event_name NOT LIKE 'wait/io/file%' OR event_name NOT LIKE 'wait/io/socket%',
                            SUBSTRING_INDEX(event_name, '/', -4),
                            event_name)
                        ),
                    IF (event_name LIKE 'statement/%', IFNULL(CONCAT('\n', wait_info), ''), ''),
                    IF (in_debug AND event_name LIKE 'wait%', wait_info, ''),
                    '", ',
                    CASE WHEN event_name LIKE 'wait/io/file%' THEN
                           'shape=box, style=filled, color=red'
                         WHEN event_name LIKE 'wait/io/table%' THEN
                           'shape=box, style=filled, color=green'
                         WHEN event_name LIKE 'wait/io/socket%' THEN
                           'shape=box, style=filled, color=yellow'
                         WHEN event_name LIKE 'wait/synch/mutex%' THEN
                           'style=filled, color=lightskyblue'
                         WHEN event_name LIKE 'wait/synch/cond%' THEN
                           'style=filled, color=darkseagreen3'
                         WHEN event_name LIKE 'wait/synch/rwlock%' THEN
                           'style=filled, color=orchid'
                         WHEN event_name LIKE 'wait/lock%' THEN
                           'shape=box, style=filled, color=tan'
                         WHEN event_name LIKE 'statement/%' THEN
                           CONCAT('shape=box, style=bold',
                                  CASE WHEN event_name LIKE 'statement/com/%' THEN
                                         ' style=filled, color=darkseagreen'
                                       ELSE
                                         IF((timer_wait/1000000000000) > @@long_query_time,
                                            ' style=filled, color=red',
                                            ' style=filled, color=lightblue')
                                  END
                           )
                         WHEN event_name LIKE 'stage/%' THEN
                           'style=filled, color=slategray3'
                         WHEN event_name LIKE '%idle%' THEN
                           'shape=box, style=filled, color=firebrick3'
                         ELSE '' END,
                     '];\n'
                   ) event, event_id
        FROM (
             (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id,
                     CONCAT(sql_text, '\n',
                            'errors: ', errors, '\n',
                            'warnings: ', warnings, '\n',
                            'lock time: ', sys.format_time(lock_time),'\n',
                            'rows affected: ', rows_affected, '\n',
                            'rows sent: ', rows_sent, '\n',
                            'rows examined: ', rows_examined, '\n',
                            'tmp tables: ', created_tmp_tables, '\n',
                            'tmp disk tables: ', created_tmp_disk_tables, '\n'
                            'select scan: ', select_scan, '\n',
                            'select full join: ', select_full_join, '\n',
                            'select full range join: ', select_full_range_join, '\n',
                            'select range: ', select_range, '\n',
                            'select range check: ', select_range_check, '\n',
                            'sort merge passes: ', sort_merge_passes, '\n',
                            'sort rows: ', sort_rows, '\n',
                            'sort range: ', sort_range, '\n',
                            'sort scan: ', sort_scan, '\n',
                            'no index used: ', IF(no_index_used, 'TRUE', 'FALSE'), '\n',
                            'no good index used: ', IF(no_good_index_used, 'TRUE', 'FALSE'), '\n'
                     ) AS wait_info
                FROM performance_schema.events_statements_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
             UNION
             (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id, null AS wait_info
                FROM performance_schema.events_stages_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
             UNION
             (SELECT thread_id, event_id,
                     CONCAT(event_name,
                            IF(event_name NOT LIKE 'wait/synch/mutex%', IFNULL(CONCAT(' - ', operation), ''), ''),
                            IF(number_of_bytes IS NOT NULL, CONCAT(' ', number_of_bytes, ' bytes'), ''),
                            IF(event_name LIKE 'wait/io/file%', '\n', ''),
                            IF(object_schema IS NOT NULL, CONCAT('\nObject: ', object_schema, '.'), ''),
                            IF(object_name IS NOT NULL,
                               IF (event_name LIKE 'wait/io/socket%',
                                   CONCAT('\n', IF (object_name LIKE ':0%', @@socket, object_name)),
                                   object_name),
                               ''
                            ),
                            IF(index_name IS NOT NULL, CONCAT(' Index: ', index_name), ''), '\n'
                     ) AS event_name,
                     timer_wait, timer_start, nesting_event_id, source AS wait_info
                FROM performance_schema.events_waits_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
           ) events
       ORDER BY event_id;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    IF (in_auto_setup) THEN
        CALL sys.ps_setup_save(0);
        DELETE FROM performance_schema.setup_actors;
        UPDATE performance_schema.threads
           SET INSTRUMENTED = IF(THREAD_ID = in_thread_id, 'YES', 'NO');
        UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
         WHERE NAME NOT LIKE '%\_history';
        UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES',
               TIMED   = 'YES';
    END IF;
    IF (in_start_fresh) THEN
        TRUNCATE performance_schema.events_statements_history_long;
        TRUNCATE performance_schema.events_stages_history_long;
        TRUNCATE performance_schema.events_waits_history_long;
    END IF;
    DROP TEMPORARY TABLE IF EXISTS tmp_events;
    CREATE TEMPORARY TABLE tmp_events (
      event_id bigint unsigned NOT NULL,
      event longblob,
      PRIMARY KEY (event_id)
    );
    INSERT INTO tmp_events VALUES (0, CONCAT('digraph events { rankdir=LR; nodesep=0.10;\n',
                                             '// Stack created .....: ', NOW(), '\n',
                                             '// MySQL version .....: ', VERSION(), '\n',
                                             '// MySQL hostname ....: ', @@hostname, '\n',
                                             '// MySQL port ........: ', @@port, '\n',
                                             '// MySQL socket ......: ', @@socket, '\n',
                                             '// MySQL user ........: ', CURRENT_USER(), '\n'));
    SELECT CONCAT('Data collection starting for THREAD_ID = ', in_thread_id) AS 'Info';
    SET v_min_event_id = 0,
        v_start        = UNIX_TIMESTAMP(),
        in_in>���_J��i��3i��37���sysformat_pathformat_pathutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_path VARCHAR(512)
    varchar(512) CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
  DECLARE v_dir VARCHAR(1024);
  DECLARE v_path VARCHAR(512);
  DECLARE path_separator CHAR(1) DEFAULT '/';
  IF @@global.version_compile_os LIKE 'win%' THEN
    SET path_separator = '\\';
  END IF;
  IF in_path LIKE '/private/%' THEN
    SET v_path = REPLACE(in_path, '/private', '');
  ELSE
    SET v_path = in_path;
  END IF;
  SET v_dir= IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'innodb_data_home_dir'), '');
  IF v_path IS NULL THEN
    RETURN NULL;
  END IF;
  IF v_path LIKE CONCAT(@@global.datadir, IF(SUBSTRING(@@global.datadir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, @@global.datadir, CONCAT('@@datadir', IF(SUBSTRING(@@global.datadir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  IF v_path LIKE CONCAT(@@global.tmpdir, IF(SUBSTRING(@@global.tmpdir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, @@global.tmpdir, CONCAT('@@tmpdir', IF(SUBSTRING(@@global.tmpdir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  SET v_dir= IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'innodb_data_home_dir'), '');
  IF v_path LIKE CONCAT(v_dir, IF(SUBSTRING(v_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, v_dir, CONCAT('@@innodb_data_home_dir', IF(SUBSTRING(v_dir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  SET v_dir= IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'innodb_log_group_home_dir'), '');
  IF v_path LIKE CONCAT(v_dir, IF(SUBSTRING(v_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, v_dir, CONCAT('@@innodb_log_group_home_dir', IF(SUBSTRING(v_dir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  SET v_dir= IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'slave_load_tmpdir'), '');
  IF v_path LIKE CONCAT(v_dir, IF(SUBSTRING(v_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, v_dir, CONCAT('@@slave_load_tmpdir', IF(SUBSTRING(v_dir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  SET v_dir = IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'innodb_undo_directory'), '');
  IF v_path LIKE CONCAT(v_dir, IF(SUBSTRING(v_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, v_dir, CONCAT('@@innodb_undo_directory', IF(SUBSTRING(v_dir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  IF v_path LIKE CONCAT(@@global.basedir, IF(SUBSTRING(@@global.basedir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, @@global.basedir, CONCAT('@@basedir', IF(SUBSTRING(@@global.basedir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  RETURN v_path;
END
             Description
             Takes a raw path value, and strips out the datadir or tmpdir
             replacing with @@datadir and @@tmpdir respectively.
             Also normalizes the paths across operating systems, so backslashes
             on Windows are converted to forward slashes
             Parameters
             path (VARCHAR(512)):
               The raw file path value to format.
             Returns
             VARCHAR(512) CHARSET UTF8
             Example
             mysql> select @@datadir;
             +-----------------------------------------------+
             | @@datadir                                     |
             +-----------------------------------------------+
             | /Users/mark/sandboxes/SmallTree/AMaster/data/ |
             +-----------------------------------------------+
             1 row in set (0.06 sec)
             mysql> select format_path('/Users/mark/sandboxes/SmallTree/AMaster/data/mysql/proc.MYD') AS path;
             +--------------------------+
             | path                     |
             +--------------------------+
             | @@datadir/mysql/proc.MYD |
             +--------------------------+
             1 row in set (0.03 sec)
            BEGIN
  DECLARE v_dir VARCHAR(1024);
  DECLARE v_path VARCHAR(512);
  DECLARE path_separator CHAR(1) DEFAULT '/';
  IF @@global.version_compile_os LIKE 'win%' THEN
    SET path_separator = '\';
  END IF;
  IF in_path LIKE '/private/%' THEN
    SET v_path = REPLACE(in_path, '/private', '');
  ELSE
    SET v_path = in_path;
  END IF;
  SET v_dir= IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'innodb_data_home_dir'), '');
  IF v_path IS NULL THEN
    RETURN NULL;
  END IF;
  IF v_path LIKE CONCAT(@@global.datadir, IF(SUBSTRING(@@global.datadir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, @@global.datadir, CONCAT('@@datadir', IF(SUBSTRING(@@global.datadir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  IF v_path LIKE CONCAT(@@global.tmpdir, IF(SUBSTRING(@@global.tmpdir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, @@global.tmpdir, CONCAT('@@tmpdir', IF(SUBSTRING(@@global.tmpdir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  SET v_dir= IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'innodb_data_home_dir'), '');
  IF v_path LIKE CONCAT(v_dir, IF(SUBSTRING(v_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, v_dir, CONCAT('@@innodb_data_home_dir', IF(SUBSTRING(v_dir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  SET v_dir= IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'innodb_log_group_home_dir'), '');
  IF v_path LIKE CONCAT(v_dir, IF(SUBSTRING(v_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, v_dir, CONCAT('@@innodb_log_group_home_dir', IF(SUBSTRING(v_dir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  SET v_dir= IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'slave_load_tmpdir'), '');
  IF v_path LIKE CONCAT(v_dir, IF(SUBSTRING(v_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, v_dir, CONCAT('@@slave_load_tmpdir', IF(SUBSTRING(v_dir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  SET v_dir = IFNULL((SELECT VARIABLE_VALUE FROM information_schema.global_variables WHERE VARIABLE_NAME = 'innodb_undo_directory'), '');
  IF v_path LIKE CONCAT(v_dir, IF(SUBSTRING(v_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, v_dir, CONCAT('@@innodb_undo_directory', IF(SUBSTRING(v_dir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  IF v_path LIKE CONCAT(@@global.basedir, IF(SUBSTRING(@@global.basedir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
    SET v_path = REPLACE(v_path, @@global.basedir, CONCAT('@@basedir', IF(SUBSTRING(@@global.basedir, -1) = path_separator, path_separator, '')));
    RETURN v_path;
  END IF;
  RETURN v_path;
END�S2r/��S�=�i��3i��3;�B�sysps_is_consumer_enabledps_is_consumer_enabledutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_consumer varchar(64)
   enum('YES','NO') CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    RETURN (
        SELECT (CASE
                   WHEN c.NAME = 'global_instrumentation' THEN c.ENABLED
                   WHEN c.NAME = 'thread_instrumentation' THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   WHEN c.NAME LIKE '%\_digest'           THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   WHEN c.NAME LIKE '%\_current'          THEN IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   ELSE IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES'
                           AND ( SELECT cc.ENABLED FROM performance_schema.setup_consumers cc WHERE NAME = CONCAT(SUBSTRING_INDEX(c.NAME, '_', 2), '_current')
                               ) = 'YES', 'YES', 'NO')
                END) AS IsEnabled
          FROM performance_schema.setup_consumers c
               INNER JOIN performance_schema.setup_consumers cg
               INNER JOIN performance_schema.setup_consumers ct
         WHERE cg.NAME       = 'global_instrumentation'
               AND ct.NAME   = 'thread_instrumentation'
               AND c.NAME    = in_consumer
       );
END
             Description
             Determines whether a consumer is enabled (taking the consumer hierarchy into consideration)
             within the Performance Schema.
             Parameters
             in_consumer VARCHAR(64):
               The name of the consumer to check.
             Returns
             ENUM('YES', 'NO')
             Example
             mysql> SELECT sys.ps_is_consumer_enabled('events_stages_history');
             +-----------------------------------------------------+
             | sys.ps_is_consumer_enabled('events_stages_history') |
             +-----------------------------------------------------+
             | NO                                                  |
             +-----------------------------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN (
        SELECT (CASE
                   WHEN c.NAME = 'global_instrumentation' THEN c.ENABLED
                   WHEN c.NAME = 'thread_instrumentation' THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   WHEN c.NAME LIKE '%\_digest'           THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   WHEN c.NAME LIKE '%\_current'          THEN IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   ELSE IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES'
                           AND ( SELECT cc.ENABLED FROM performance_schema.setup_consumers cc WHERE NAME = CONCAT(SUBSTRING_INDEX(c.NAME, '_', 2), '_current')
                               ) = 'YES', 'YES', 'NO')
                END) AS IsEnabled
          FROM performance_schema.setup_consumers c
               INNER JOIN performance_schema.setup_consumers cg
               INNER JOIN performance_schema.setup_consumers ct
         WHERE cg.NAME       = 'global_instrumentation'
               AND ct.NAME   = 'thread_instrumentation'
               AND c.NAME    = in_consumer
       );
END�i��:i��:�W�sysps_setup_show_enabled_consumersps_setup_show_enabled_consumersutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    SELECT name AS enabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'YES'
     ORDER BY enabled_consumers;
END
             Description
             Shows all currently enabled consumers.
             Parameters
             None
             Example
             mysql> CALL sys.ps_setup_show_enabled_consumers();
             +---------------------------+
             | enabled_consumers         |
             +---------------------------+
             | events_statements_current |
             | global_instrumentation    |
             | thread_instrumentation    |
             | statements_digest         |
             +---------------------------+
             4 rows in set (0.05 sec)
            BEGIN
    SELECT name AS enabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'YES'
     ORDER BY enabled_consumers;
END, 0 rows affected (0.06 sec)
             mysql> select @@sql_mode;
             +-----------------------------------------------------------------------------------------------+
             | @@sql_mode                                                                                    |
             +-----------------------------------------------------------------------------------------------+
             | ANSI_QUOTES,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
             +-----------------------------------------------------------------------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    IF (in_add_value IS NULL) THEN
        SIGNAL SQLSTATE '02200'
           SET MESSAGE_TEXT = 'Function sys.list_add: in_add_value input variable should not be NULL',
               MYSQL_ERRNO = 1138;
    END IF;
    IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
        RETURN in_add_value;
    END IF;
    RETURN (SELECT CONCAT(TRIM(BOTH ',' FROM TRIM(in_list)), ',', in_add_value));
END�!���"�b�ܣb�ܣ"��sysps_setup_reset_to_defaultps_setup_reset_to_defaultutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
       IN in_verbose BOOLEAN
    �b�ܣb�ܣ""���sysps_setup_show_disabled_instrumentsps_setup_show_disabled_instrumentsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    SELECT name AS disabled_instruments, timed
      FROM performance_schema.setup_instruments
     WHERE enabled = 'NO'
     ORDER BY disabled_instruments;
END
             Description
             Shows all currently disabled instruments.
             Parameters
             None
             Example
             mysql> CALL sys.ps_setup_show_disabled_instruments();
            BEGIN
    SELECT name AS disabled_instruments, timed
      FROM performance_schema.setup_instruments
     WHERE enabled = 'NO'
     ORDER BY disabled_instruments;
END�b�ܣb�ܣ!!���sysps_setup_show_enabled_instrumentsps_setup_show_enabled_instrumentsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    SELECT name AS enabled_instruments, timed
      FROM performance_schema.setup_instruments
     WHERE enabled = 'YES'
     ORDER BY enabled_instruments;
END
             Description
             Shows all currently enabled instruments.
             Parameters
             None
             Example
             mysql> CALL sys.ps_setup_show_enabled_instruments();
            BEGIN
    SELECT name AS enabled_instruments, timed
      FROM performance_schema.setup_instruments
     WHERE enabled = 'YES'
     ORDER BY enabled_instruments;
END�#@�&@	�'@b�ܣb�ܣ>Q\//�\sysstatement_performance_analyzerstatement_performance_analyzerutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_action ENUM('snapshot', 'overall', 'delta', 'create_table', 'create_tmp', 'save', 'cleanup'),
        IN in_table VARCHAR(129),
        IN in_views SET ('with_runtimes_in_95th_percentile', 'analysis', 'with_errors_or_warnings', 'with_full_table_scans', 'with_sorting', 'with_temp_tables', 'custom')
    d��
-�v
���W���R�kBEGIN
    SET @query = 'DELETE
                    FROM performance_schema.setup_actors
                   WHERE NOT (HOST = ''%'' AND USER = ''%'' AND ROLE = ''%'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_actors\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'INSERT IGNORE INTO performance_schema.setup_actors
                  VALUES (''%'', ''%'', ''%'', ''YES'', ''YES'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_actors\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.setup_instruments
                     SET ENABLED = sys.ps_is_instrument_default_enabled(NAME),
                         TIMED   = sys.ps_is_instrument_default_timed(NAME)';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_instruments\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.setup_consumers
                     SET ENABLED = IF(NAME IN (''events_statements_current'', ''events_transactions_current'', ''global_instrumentation'', ''thread_instrumentation'', ''statements_digest''), ''YES'', ''NO'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_consumers\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'DELETE
                    FROM performance_schema.setup_objects
                   WHERE NOT (OBJECT_TYPE IN (''EVENT'', ''FUNCTION'', ''PROCEDURE'', ''TABLE'', ''TRIGGER'') AND OBJECT_NAME = ''%''
                     AND (OBJECT_SCHEMA = ''mysql''              AND ENABLED = ''NO''  AND TIMED = ''NO'' )
                      OR (OBJECT_SCHEMA = ''performance_schema'' AND ENABLED = ''NO''  AND TIMED = ''NO'' )
                      OR (OBJECT_SCHEMA = ''information_schema'' AND ENABLED = ''NO''  AND TIMED = ''NO'' )
                      OR (OBJECT_SCHEMA = ''%''                  AND ENABLED = ''YES'' AND TIMED = ''YES''))';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_objects\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'INSERT IGNORE INTO performance_schema.setup_objects
                  VALUES (''EVENT''    , ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''EVENT''    , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''EVENT''    , ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''EVENT''    , ''%''                 , ''%'', ''YES'', ''YES''),
                         (''FUNCTION'' , ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''FUNCTION'' , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''FUNCTION'' , ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''FUNCTION'' , ''%''                 , ''%'', ''YES'', ''YES''),
                         (''PROCEDURE'', ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''PROCEDURE'', ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''PROCEDURE'', ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''PROCEDURE'', ''%''                 , ''%'', ''YES'', ''YES''),
                         (''TABLE''    , ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''TABLE''    , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''TABLE''    , ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''TABLE''    , ''%''                 , ''%'', ''YES'', ''YES''),
                         (''TRIGGER''  , ''mysql''             , ''%'', ''NO'' , ''NO'' ),
                         (''TRIGGER''  , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''TRIGGER''  , ''information_schema'', ''%'', ''NO'' , ''NO'' ),
                         (''TRIGGER''  , ''%''                 , ''%'', ''YES'', ''YES'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_objects\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.threads
                     SET INSTRUMENTED = ''YES''';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: threads\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
END��N��R�JBEGIN
    SET @query = 'DELETE\n                    FROM performance_schema.setup_actors\n                   WHERE NOT (HOST = ''%'' AND USER = ''%'' AND ROLE = ''%'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_actors\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'INSERT IGNORE INTO performance_schema.setup_actors\n                  VALUES (''%'', ''%'', ''%'', ''YES'', ''YES'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_actors\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.setup_instruments\n                     SET ENABLED = sys.ps_is_instrument_default_enabled(NAME),\n                         TIMED   = sys.ps_is_instrument_default_timed(NAME)';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_instruments\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.setup_consumers\n                     SET ENABLED = IF(NAME IN (''events_statements_current'', ''events_transactions_current'', ''global_instrumentation'', ''thread_instrumentation'', ''statements_digest''), ''YES'', ''NO'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_consumers\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'DELETE\n                    FROM performance_schema.setup_objects\n                   WHERE NOT (OBJECT_TYPE IN (''EVENT'', ''FUNCTION'', ''PROCEDURE'', ''TABLE'', ''TRIGGER'') AND OBJECT_NAME = ''%''\n                     AND (OBJECT_SCHEMA = ''mysql''              AND ENABLED = ''NO''  AND TIMED = ''NO'' )\n                      OR (OBJECT_SCHEMA = ''performance_schema'' AND ENABLED = ''NO''  AND TIMED = ''NO'' )\n                      OR (OBJECT_SCHEMA = ''information_schema'' AND ENABLED = ''NO''  AND TIMED = ''NO'' )\n                      OR (OBJECT_SCHEMA = ''%''                  AND ENABLED = ''YES'' AND TIMED = ''YES''))';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_objects\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'INSERT IGNORE INTO performance_schema.setup_objects\n                  VALUES (''EVENT''    , ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''EVENT''    , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''EVENT''    , ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''EVENT''    , ''%''                 , ''%'', ''YES'', ''YES''),\n                         (''FUNCTION'' , ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''FUNCTION'' , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''FUNCTION'' , ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''FUNCTION'' , ''%''                 , ''%'', ''YES'', ''YES''),\n                         (''PROCEDURE'', ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''PROCEDURE'', ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''PROCEDURE'', ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''PROCEDURE'', ''%''                 , ''%'', ''YES'', ''YES''),\n                         (''TABLE''    , ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''TABLE''    , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''TABLE''    , ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''TABLE''    , ''%''                 , ''%'', ''YES'', ''YES''),\n                         (''TRIGGER''  , ''mysql''             , ''%'', ''NO'' , ''NO'' ),\n                         (''TRIGGER''  , ''performance_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''TRIGGER''  , ''information_schema'', ''%'', ''NO'' , ''NO'' ),\n                         (''TRIGGER''  , ''%''                 , ''%'', ''YES'', ''YES'')';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: setup_objects\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
    SET @query = 'UPDATE performance_schema.threads\n                     SET INSTRUMENTED = ''YES''';
    IF (in_verbose) THEN
        SELECT CONCAT('Resetting: threads\n', REPLACE(@query, '  ', '')) AS status;
    END IF;
    PREPARE reset_stmt FROM @query;
    EXECUTE reset_stmt;
    DEALLOCATE PREPARE reset_stmt;
END�=���BEGIN
    DECLARE v_table_exists, v_tmp_digests_table_exists, v_custom_view_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY') DEFAULT '';
    DECLARE v_this_thread_enabled ENUM('YES', 'NO');
    DECLARE v_force_new_snapshot BOOLEAN DEFAULT FALSE;
    DECLARE v_digests_table VARCHAR(133);
    DECLARE v_quoted_table, v_quoted_custom_view VARCHAR(133) DEFAULT '';
    DECLARE v_table_db, v_table_name, v_custom_db, v_custom_name VARCHAR(64);
    DECLARE v_digest_table_template, v_checksum_ref, v_checksum_table text;
    DECLARE v_sql longtext;
    DECLARE v_error_msg VARCHAR(128);
    SELECT INSTRUMENTED INTO v_this_thread_enabled FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    END IF;
    SET @log_bin := @@sql_log_bin;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = 0;
    END IF;
    IF (@sys.statement_performance_analyzer.limit IS NULL) THEN
        SET @sys.statement_performance_analyzer.limit = sys.sys_get_config('statement_performance_analyzer.limit', '100');
    END IF;
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug                                = sys.sys_get_config('debug'                               , 'OFF');
    END IF;
    IF (in_table = 'NOW()') THEN
        SET v_force_new_snapshot = TRUE,
            in_table             = NULL;
    ELSEIF (in_table IS NOT NULL) THEN
        IF (NOT INSTR(in_table, '.')) THEN
            SET v_table_db   = DATABASE(),
                v_table_name = in_table;
        ELSE
            SET v_table_db   = SUBSTRING_INDEX(in_table, '.', 1);
            SET v_table_name = SUBSTRING(in_table, CHAR_LENGTH(v_table_db)+2);
        END IF;
        SET v_quoted_table = CONCAT('`', v_table_db, '`.`', v_table_name, '`');
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('in_table is: db = ''', v_table_db, ''', table = ''', v_table_name, '''') AS 'Debug';
        END IF;
        IF (v_table_db = DATABASE() AND (v_table_name = 'tmp_digests' OR v_table_name = 'tmp_digests_delta')) THEN
            SET v_error_msg = CONCAT('Invalid value for in_table: ', v_quoted_table, ' is reserved table name.');
            SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = v_error_msg;
        END IF;
        CALL sys.table_exists(v_table_db, v_table_name, v_table_exists);
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('v_table_exists = ', v_table_exists) AS 'Debug';
        END IF;
        IF (v_table_exists = 'BASE TABLE') THEN
            SET v_checksum_ref = (
                 SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, COLUMN_TYPE) ORDER BY ORDINAL_POSITION) AS Checksum
                   FROM information_schema.COLUMNS
                  WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'events_statements_summary_by_digest'
                ),
                v_checksum_table = (
                 SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, COLUMN_TYPE) ORDER BY ORDINAL_POSITION) AS Checksum
                   FROM information_schema.COLUMNS
                  WHERE TABLE_SCHEMA = v_table_db AND TABLE_NAME = v_table_name
                );
            IF (v_checksum_ref <> v_checksum_table) THEN
                SET v_error_msg = CONCAT('The table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 93, CONCAT('...', SUBSTRING(v_quoted_table, -90)), v_quoted_table),
                                         ' has the wrong definition.');
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        END IF;
    END IF;
    IF (in_views IS NULL OR in_views = '') THEN
        SET in_views = 'with_runtimes_in_95th_percentile,analysis,with_errors_or_warnings,with_full_table_scans,with_sorting,with_temp_tables';
    END IF;
    CALL sys.table_exists(DATABASE(), 'tmp_digests', v_tmp_digests_table_exists);
    IF (@sys.debug = 'ON') THEN
        SELECT CONCAT('v_tmp_digests_table_exists = ', v_tmp_digests_table_exists) AS 'Debug';
    END IF;
    CASE
        WHEN in_action IN ('snapshot', 'overall') THEN
            IF (in_table IS NOT NULL) THEN
                IF (NOT v_table_exists IN ('TEMPORARY', 'BASE TABLE')) THEN
                    SET v_error_msg = CONCAT('The ', in_action, ' action requires in_table to be NULL, NOW() or specify an existing table.',
                                             ' The table ',
                                             IF(CHAR_LENGTH(v_quoted_table) > 16, CONCAT('...', SUBSTRING(v_quoted_table, -13)), v_quoted_table),
                                             ' does not exist.');
                    SIGNAL SQLSTATE '45000'
                       SET MESSAGE_TEXT = v_error_msg;
                END IF;
            END IF;
        WHEN in_action IN ('delta', 'save') THEN
            IF (v_table_exists NOT IN ('TEMPORARY', 'BASE TABLE')) THEN
                SET v_error_msg = CONCAT('The ', in_action, ' action requires in_table to be an existing table.',
                                         IF(in_table IS NOT NULL, CONCAT(' The table ',
                                             IF(CHAR_LENGTH(v_quoted_table) > 39, CONCAT('...', SUBSTRING(v_quoted_table, -36)), v_quoted_table),
                                             ' does not exist.'), ''));
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
            IF (in_action = 'delta' AND v_tmp_digests_table_exists <> 'TEMPORARY') THEN
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = 'An existing snapshot generated with the statement_performance_analyzer() must exist.';
            END IF;
        WHEN in_action = 'create_tmp' THEN
            IF (v_table_exists = 'TEMPORARY') THEN
                SET v_error_msg = CONCAT('Cannot create the table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 72, CONCAT('...', SUBSTRING(v_quoted_table, -69)), v_quoted_table),
                                         ' as it already exists.');
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        WHEN in_action = 'create_table' THEN
            IF (v_table_exists <> '') THEN
                SET v_error_msg = CONCAT('Cannot create the table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 52, CONCAT('...', SUBSTRING(v_quoted_table, -49)), v_quoted_table),
                                         ' as it already exists',
                                         IF(v_table_exists = 'TEMPORARY', ' as a temporary table.', '.'));
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        WHEN in_action = 'cleanup' THEN
            DO (SELECT 1);
        ELSE
            SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = 'Unknown action. Supported actions are: cleanup, create_table, create_tmp, delta, overall, save, snapshot';
    END CASE;
    SET v_digest_table_template = 'CREATE %{TEMPORARY}TABLE %{TABLE_NAME} (
  `SCHEMA_NAME` varchar(64) DEFAULT NULL,
  `DIGEST` varchar(32) DEFAULT NULL,
  `DIGEST_TEXT` longtext,
  `COUNT_STAR` bigint(20) unsigned NOT NULL,
  `SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,
  `MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL,
  `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL,
  `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL,
  `SUM_LOCK_TIME` bigint(20) unsigned NOT NULL,
  `SUM_ERRORS` bigint(20) unsigned NOT NULL,
  `SUM_WARNINGS` bigint(20) unsigned NOT NULL,
  `SUM_ROWS_AFFECTED` bigint(20) unsigned NOT NULL,
  `SUM_ROWS_SENT` bigint(20) unsigned NOT NULL,
  `SUM_ROWS_EXAMINED` bigint(20) unsigned NOT NULL,
  `SUM_CREATED_TMP_DISK_TABLES` bigint(20) unsigned NOT NULL,
  `SUM_CREATED_TMP_TABLES` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_FULL_JOIN` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_FULL_RANGE_JOIN` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_RANGE` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_RANGE_CHECK` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_SCAN` bigint(20) unsign���ed NOT NULL,
  `SUM_SORT_MERGE_PASSES` bigint(20) unsigned NOT NULL,
  `SUM_SORT_RANGE` bigint(20) unsigned NOT NULL,
  `SUM_SORT_ROWS` bigint(20) unsigned NOT NULL,
  `SUM_SORT_SCAN` bigint(20) unsigned NOT NULL,
  `SUM_NO_INDEX_USED` bigint(20) unsigned NOT NULL,
  `SUM_NO_GOOD_INDEX_USED` bigint(20) unsigned NOT NULL,
  `FIRST_SEEN` timestamp NULL DEFAULT NULL,
  `LAST_SEEN` timestamp NULL DEFAULT NULL,
  INDEX (SCHEMA_NAME, DIGEST)
) DEFAULT CHARSET=utf8';
    IF (v_force_new_snapshot
           OR in_action = 'snapshot'
           OR (in_action = 'overall' AND in_table IS NULL)
           OR (in_action = 'save' AND v_tmp_digests_table_exists <> 'TEMPORARY')
       ) THEN
        IF (v_tmp_digests_table_exists = 'TEMPORARY') THEN
            IF (@sys.debug = 'ON') THEN
                SELECT 'DROP TEMPORARY TABLE IF EXISTS tmp_digests' AS 'Debug';
            END IF;
            DROP TEMPORARY TABLE IF EXISTS tmp_digests;
        END IF;
        CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', 'TEMPORARY '), '%{TABLE_NAME}', 'tmp_digests'));
        SET v_sql = CONCAT('INSERT INTO tmp_digests SELECT * FROM ',
                           IF(in_table IS NULL OR in_action = 'save', 'performance_schema.events_statements_summary_by_digest', v_quoted_table));
        CALL sys.execute_prepared_stmt(v_sql);
    END IF;
    IF (in_action IN ('create_table', 'create_tmp')) THEN
        IF (in_action = 'create_table') THEN
            CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', ''), '%{TABLE_NAME}', v_quoted_table));
        ELSE
            CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', 'TEMPORARY '), '%{TABLE_NAME}', v_quoted_table));
        END IF;
    ELSEIF (in_action = 'save') THEN
        CALL sys.execute_prepared_stmt(CONCAT('DELETE FROM ', v_quoted_table));
        CALL sys.execute_prepared_stmt(CONCAT('INSERT INTO ', v_quoted_table, ' SELECT * FROM tmp_digests'));
    ELSEIF (in_action = 'cleanup') THEN
        DROP TEMPORARY TABLE IF EXISTS sys.tmp_digests;
        DROP TEMPORARY TABLE IF EXISTS sys.tmp_digests_delta;
    ELSEIF (in_action IN ('overall', 'delta')) THEN
        IF (in_action = 'overall') THEN
            IF (in_table IS NULL) THEN
                SET v_digests_table = 'tmp_digests';
            ELSE
                SET v_digests_table = v_quoted_table;
            END IF;
        ELSE
            SET v_digests_table = 'tmp_digests_delta';
            DROP TEMPORARY TABLE IF EXISTS tmp_digests_delta;
            CREATE TEMPORARY TABLE tmp_digests_delta LIKE tmp_digests;
            SET v_sql = CONCAT('INSERT INTO tmp_digests_delta
SELECT `d_end`.`SCHEMA_NAME`,
       `d_end`.`DIGEST`,
       `d_end`.`DIGEST_TEXT`,
       `d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0) AS ''COUNT_STAR'',
       `d_end`.`SUM_TIMER_WAIT`-IFNULL(`d_start`.`SUM_TIMER_WAIT`, 0) AS ''SUM_TIMER_WAIT'',
       `d_end`.`MIN_TIMER_WAIT` AS ''MIN_TIMER_WAIT'',
       IFNULL((`d_end`.`SUM_TIMER_WAIT`-IFNULL(`d_start`.`SUM_TIMER_WAIT`, 0))/NULLIF(`d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0), 0), 0) AS ''AVG_TIMER_WAIT'',
       `d_end`.`MAX_TIMER_WAIT` AS ''MAX_TIMER_WAIT'',
       `d_end`.`SUM_LOCK_TIME`-IFNULL(`d_start`.`SUM_LOCK_TIME`, 0) AS ''SUM_LOCK_TIME'',
       `d_end`.`SUM_ERRORS`-IFNULL(`d_start`.`SUM_ERRORS`, 0) AS ''SUM_ERRORS'',
       `d_end`.`SUM_WARNINGS`-IFNULL(`d_start`.`SUM_WARNINGS`, 0) AS ''SUM_WARNINGS'',
       `d_end`.`SUM_ROWS_AFFECTED`-IFNULL(`d_start`.`SUM_ROWS_AFFECTED`, 0) AS ''SUM_ROWS_AFFECTED'',
       `d_end`.`SUM_ROWS_SENT`-IFNULL(`d_start`.`SUM_ROWS_SENT`, 0) AS ''SUM_ROWS_SENT'',
       `d_end`.`SUM_ROWS_EXAMINED`-IFNULL(`d_start`.`SUM_ROWS_EXAMINED`, 0) AS ''SUM_ROWS_EXAMINED'',
       `d_end`.`SUM_CREATED_TMP_DISK_TABLES`-IFNULL(`d_start`.`SUM_CREATED_TMP_DISK_TABLES`, 0) AS ''SUM_CREATED_TMP_DISK_TABLES'',
       `d_end`.`SUM_CREATED_TMP_TABLES`-IFNULL(`d_start`.`SUM_CREATED_TMP_TABLES`, 0) AS ''SUM_CREATED_TMP_TABLES'',
       `d_end`.`SUM_SELECT_FULL_JOIN`-IFNULL(`d_start`.`SUM_SELECT_FULL_JOIN`, 0) AS ''SUM_SELECT_FULL_JOIN'',
       `d_end`.`SUM_SELECT_FULL_RANGE_JOIN`-IFNULL(`d_start`.`SUM_SELECT_FULL_RANGE_JOIN`, 0) AS ''SUM_SELECT_FULL_RANGE_JOIN'',
       `d_end`.`SUM_SELECT_RANGE`-IFNULL(`d_start`.`SUM_SELECT_RANGE`, 0) AS ''SUM_SELECT_RANGE'',
       `d_end`.`SUM_SELECT_RANGE_CHECK`-IFNULL(`d_start`.`SUM_SELECT_RANGE_CHECK`, 0) AS ''SUM_SELECT_RANGE_CHECK'',
       `d_end`.`SUM_SELECT_SCAN`-IFNULL(`d_start`.`SUM_SELECT_SCAN`, 0) AS ''SUM_SELECT_SCAN'',
       `d_end`.`SUM_SORT_MERGE_PASSES`-IFNULL(`d_start`.`SUM_SORT_MERGE_PASSES`, 0) AS ''SUM_SORT_MERGE_PASSES'',
       `d_end`.`SUM_SORT_RANGE`-IFNULL(`d_start`.`SUM_SORT_RANGE`, 0) AS ''SUM_SORT_RANGE'',
       `d_end`.`SUM_SORT_ROWS`-IFNULL(`d_start`.`SUM_SORT_ROWS`, 0) AS ''SUM_SORT_ROWS'',
       `d_end`.`SUM_SORT_SCAN`-IFNULL(`d_start`.`SUM_SORT_SCAN`, 0) AS ''SUM_SORT_SCAN'',
       `d_end`.`SUM_NO_INDEX_USED`-IFNULL(`d_start`.`SUM_NO_INDEX_USED`, 0) AS ''SUM_NO_INDEX_USED'',
       `d_end`.`SUM_NO_GOOD_INDEX_USED`-IFNULL(`d_start`.`SUM_NO_GOOD_INDEX_USED`, 0) AS ''SUM_NO_GOOD_INDEX_USED'',
       `d_end`.`FIRST_SEEN`,
       `d_end`.`LAST_SEEN`
  FROM tmp_digests d_end
       LEFT OUTER JOIN ', v_quoted_table, ' d_start ON `d_start`.`DIGEST` = `d_end`.`DIGEST`
                                                    AND (`d_start`.`SCHEMA_NAME` = `d_end`.`SCHEMA_NAME`
                                                          OR (`d_start`.`SCHEMA_NAME` IS NULL AND `d_end`.`SCHEMA_NAME` IS NULL)
                                                        )
 WHERE `d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0) > 0');
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_runtimes_in_95th_percentile', in_views)) THEN
            SELECT 'Queries with Runtime in 95th Percentile' AS 'Next Output';
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_avg_latency_distribution1;
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_avg_latency_distribution2;
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_95th_percentile_by_avg_us;
            CREATE TEMPORARY TABLE tmp_digest_avg_latency_distribution1 (
              cnt bigint unsigned NOT NULL,
              avg_us decimal(21,0) NOT NULL,
              PRIMARY KEY (avg_us)
            ) ENGINE=InnoDB;
            SET v_sql = CONCAT('INSERT INTO tmp_digest_avg_latency_distribution1
SELECT COUNT(*) cnt,
       ROUND(avg_timer_wait/1000000) AS avg_us
  FROM ', v_digests_table, '
 GROUP BY avg_us');
            CALL sys.execute_prepared_stmt(v_sql);
            CREATE TEMPORARY TABLE tmp_digest_avg_latency_distribution2 LIKE tmp_digest_avg_latency_distribution1;
            INSERT INTO tmp_digest_avg_latency_distribution2 SELECT * FROM tmp_digest_avg_latency_distribution1;
            CREATE TEMPORARY TABLE tmp_digest_95th_percentile_by_avg_us (
              avg_us decimal(21,0) NOT NULL,
              percentile decimal(46,4) NOT NULL,
              PRIMARY KEY (avg_us)
            ) ENGINE=InnoDB;
            SET v_sql = CONCAT('INSERT INTO tmp_digest_95th_percentile_by_avg_us
SELECT s2.avg_us avg_us,
       IFNULL(SUM(s1.cnt)/NULLIF((SELECT COUNT(*) FROM ', v_digests_table, '), 0), 0) percentile
  FROM tmp_digest_avg_latency_distribution1 AS s1
       JOIN tmp_digest_avg_latency_distribution2 AS s2 ON s1.avg_us <= s2.avg_us
 GROUP BY s2.avg_us
HAVING percentile > 0.95
 ORDER BY percentile
 LIMIT 1');
            CALL sys.execute_prepared_stmt(v_sql);
            SET v_sql =
                REPLACE(
                    REPLACE(
                        (SELECT VIEW_DEFINITION
                           FROM information_schema.VIEWS
                          WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_runtimes_in_95th_percentile'
                        ),
                        '`performance_schema`.`events_statements_summary_by_digest`',
                        v_digests_table
                    ),
                    'sys.x$ps_digest_95th_percentile_by_avg_us',
                ��T�    '`sys`.`x$ps_digest_95th_percentile_by_avg_us`'
              );
            CALL sys.execute_prepared_stmt(v_sql);
            DROP TEMPORARY TABLE tmp_digest_avg_latency_distribution1;
            DROP TEMPORARY TABLE tmp_digest_avg_latency_distribution2;
            DROP TEMPORARY TABLE tmp_digest_95th_percentile_by_avg_us;
        END IF;
        IF (FIND_IN_SET('analysis', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries Ordered by Total Latency') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statement_analysis'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_errors_or_warnings', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Errors') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_errors_or_warnings'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_full_table_scans', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Full Table Scan') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_full_table_scans'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_sorting', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Sorting') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_sorting'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_temp_tables', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Internal Temporary Tables') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_temp_tables'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('custom', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries Using Custom View') AS 'Next Output';
            IF (@sys.statement_performance_analyzer.view IS NULL) THEN
                SET @sys.statement_performance_analyzer.view = sys.sys_get_config('statement_performance_analyzer.view', NULL);
            END IF;
            IF (@sys.statement_performance_analyzer.view IS NULL) THEN
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = 'The @sys.statement_performance_analyzer.view user variable must be set with the view or query to use.';
            END IF;
            IF (NOT INSTR(@sys.statement_performance_analyzer.view, ' ')) THEN
                IF (NOT INSTR(@sys.statement_performance_analyzer.view, '.')) THEN
                    SET v_custom_db   = DATABASE(),
                        v_custom_name = @sys.statement_performance_analyzer.view;
                ELSE
                    SET v_custom_db   = SUBSTRING_INDEX(@sys.statement_performance_analyzer.view, '.', 1);
                    SET v_custom_name = SUBSTRING(@sys.statement_performance_analyzer.view, CHAR_LENGTH(v_custom_db)+2);
                END IF;
                CALL sys.table_exists(v_custom_db, v_custom_name, v_custom_view_exists);
                IF (v_custom_view_exists <> 'VIEW') THEN
                    SIGNAL SQLSTATE '45000'
                       SET MESSAGE_TEXT = 'The @sys.statement_performance_analyzer.view user variable is set but specified neither an existing view nor a query.';
                END IF;
                SET v_sql =
                    REPLACE(
                        (SELECT VIEW_DEFINITION
                           FROM information_schema.VIEWS
                          WHERE TABLE_SCHEMA = v_custom_db AND TABLE_NAME = v_custom_name
                        ),
                        '`performance_schema`.`events_statements_summary_by_digest`',
                        v_digests_table
                    );
            ELSE
                SET v_sql = REPLACE(@sys.statement_performance_analyzer.view, '`performance_schema`.`events_statements_summary_by_digest`', v_digests_table);
            END IF;
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
    END IF;
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = @log_bin;
    END IF;
END��
             Description
             Create a report of the statements running on the server.
             The views are calculated based on the overall and/or delta activity.
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Parameters
             in_action (ENUM('snapshot', 'overall', 'delta', 'create_tmp', 'create_table', 'save', 'cleanup')):
               The action to take. Supported actions are:
                 * snapshot      Store a snapshot. The default is to make a snapshot of the current content of
                                 performance_schema.events_statements_summary_by_digest, but by setting in_table
                                 this can be overwritten to copy the content of the specified table.
                                 The snapshot is stored in the sys.tmp_digests temporary table.
                 * overall       Generate analyzis based on the content specified by in_table. For the overall analyzis,
                                 in_table can be NOW() to use a fresh snapshot. This will overwrite an existing snapshot.
                                 Use NULL for in_table to use the existing snapshot. If in_table IS NULL and no snapshot
                                 exists, a new will be created.
                                 See also in_views and @sys.statement_performance_analyzer.limit.
                 * delta         Generate a delta analysis. The delta will be calculated between the reference table in
                                 in_table and the snapshot. An existing snapshot must exist.
                                 The action uses the sys.tmp_digests_delta temporary table.
                                 See also in_views and @sys.statement_performance_analyzer.limit.
                 * create_table  Create a regular table suitable for storing the snapshot for later use, e.g. for
                                 calculating deltas.
                 * create_tmp    Create a temporary table suitable for storing the snapshot for later use, e.g. for
                                 calculating deltas.
                 * save          Save the snapshot in the table specified by in_table. The table must exists and have
                                 the correct structure.
                                 If no snapshot exists, a new is created.
                 * cleanup       Remove the temporary tables used for the snapshot and delta.
             in_table (VARCHAR(129)):
               The table argument used for some actions. Use the format 'db1.t1' or 't1' without using any backticks (`)
               for quoting. Periods (.) are not supported in the database and table names.
               The meaning of the table for each action supporting the argument is:
                 * snapshot      The snapshot is created based on the specified table. Set to NULL or NOW() to use
                                 the current content of performance_schema.events_statements_summary_by_digest.
                 * overall       The table with the content to create the overall analyzis for. The following values
                                 can be used:
                                   - A table name - use the content of that table.
                                   - NOW()        - create a fresh snapshot and overwrite the existing snapshot.
                                   - NULL         - use the last stored snapshot.
                 * delta         The table name is mandatory and specified the reference view to compare the currently
                                 stored snapshot against. If no snapshot exists, a new will be created.
                 * create_table  The name of the regular table to create.
                 * create_tmp    The name of the temporary table to create.
                 * save          The name of the table to save the currently stored snapshot into.
             in_views (SET ('with_runtimes_in_95th_percentile', 'analysis', 'with_errors_or_warnings',
                            'with_full_table_scans', 'with_sorting', 'with_temp_tables', 'custom'))
               Which views to include:
                 * with_runtimes_in_95th_percentile  Based on the sys.statements_with_runtimes_in_95th_percentile view
                 * analysis                          Based on the sys.statement_analysis view
                 * with_errors_or_warnings           Based on the sys.statements_with_errors_or_warnings view
                 * with_full_table_scans             Based on the sys.statements_with_full_table_scans view
                 * with_sorting                      Based on the sys.statements_with_sorting view
                 * with_temp_tables                  Based on the sys.statements_with_temp_tables view
                 * custom                            Use a custom view. This view must be specified in @sys.statement_performance_analyzer.view to an existing view or a query
             Default is to include all except 'custom'.
             Configuration Options
             sys.statement_performance_analyzer.limit
               The maximum number of rows to include for the views that does not have a built-in limit (e.g. the 95th percentile view).
               If not set the limit is 100.
             sys.statement_performance_analyzer.view
               Used together with the 'custom' view. If the value contains a space, it is considered a query, otherwise it must be
               an existing view querying the performance_schema.events_statements_summary_by_digest table. There cannot be any limit
               clause including in the query or view definition if @sys.statement_performance_analyzer.limit > 0.
               If specifying a view, use the same format as for in_table.
             sys.debug
               Whether to provide debugging output.
               Default is 'OFF'. Set to 'ON' to include.
             Example
             To create a report with the queries in the 95th percentile since last truncate of performance_schema.events_statements_summary_by_digest
             and the delta for a 1 minute period:
                1. Create a temporary table to store the initial snapshot.
                2. Create the initial snapshot.
                3. Save the initial snapshot in the temporary table.
                4. Wait one minute.
                5. Create a new snapshot.
                6. Perform analyzis based on the new snapshot.
                7. Perform analyzis based on the delta between the initial and new snapshots.
             mysql> CALL sys.statement_performance_analyzer('create_tmp', 'mydb.tmp_digests_ini', NULL);
             Query OK, 0 rows affected (0.08 sec)
             mysql> CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
             Query OK, 0 rows affected (0.02 sec)
             mysql> CALL sys.statement_performance_analyzer('save', 'mydb.tmp_digests_ini', NULL);
             Query OK, 0 rows affected (0.00 sec)
             mysql> DO SLEEP(60);
             Query OK, 0 rows affected (1 min 0.00 sec)
             mysql> CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
             Query OK, 0 rows affected (0.02 sec)
             mysql> CALL sys.statement_performance_analyzer('overall', NULL, 'with_runtimes_in_95th_percentile');
             +-----------------------------------------+
             | Next Output                             |
             +-----------------------------------------+
             | Queries with Runtime in 95th Percentile |
             +-----------------------------------------+
             1 row in set (0.05 sec)
             ...
             mysql> CALL sys.statement_performance_analyzer('delta', 'mydb.tmp_digests_ini', 'with_runtimes_in_95th_percentile');
             +-----------------------------------------+
             | Next Output                             |
             +-----------------------------------------+
             | Queries with Runtime in 95th Percentile |
             +-----------------------------------------+
             1 row in set (0.03 sec)
       >��WBEGIN
    DECLARE v_table_exists, v_tmp_digests_table_exists, v_custom_view_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY') DEFAULT '';
    DECLARE v_this_thread_enabled ENUM('YES', 'NO');
    DECLARE v_force_new_snapshot BOOLEAN DEFAULT FALSE;
    DECLARE v_digests_table VARCHAR(133);
    DECLARE v_quoted_table, v_quoted_custom_view VARCHAR(133) DEFAULT '';
    DECLARE v_table_db, v_table_name, v_custom_db, v_custom_name VARCHAR(64);
    DECLARE v_digest_table_template, v_checksum_ref, v_checksum_table text;
    DECLARE v_sql longtext;
    DECLARE v_error_msg VARCHAR(128);
    SELECT INSTRUMENTED INTO v_this_thread_enabled FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    END IF;
    SET @log_bin := @@sql_log_bin;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = 0;
    END IF;
    IF (@sys.statement_performance_analyzer.limit IS NULL) THEN
        SET @sys.statement_performance_analyzer.limit = sys.sys_get_config('statement_performance_analyzer.limit', '100');
    END IF;
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug                                = sys.sys_get_config('debug'                               , 'OFF');
    END IF;
    IF (in_table = 'NOW()') THEN
        SET v_force_new_snapshot = TRUE,
            in_table             = NULL;
    ELSEIF (in_table IS NOT NULL) THEN
        IF (NOT INSTR(in_table, '.')) THEN
            SET v_table_db   = DATABASE(),
                v_table_name = in_table;
        ELSE
            SET v_table_db   = SUBSTRING_INDEX(in_table, '.', 1);
            SET v_table_name = SUBSTRING(in_table, CHAR_LENGTH(v_table_db)+2);
        END IF;
        SET v_quoted_table = CONCAT('`', v_table_db, '`.`', v_table_name, '`');
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('in_table is: db = ''', v_table_db, ''', table = ''', v_table_name, '''') AS 'Debug';
        END IF;
        IF (v_table_db = DATABASE() AND (v_table_name = 'tmp_digests' OR v_table_name = 'tmp_digests_delta')) THEN
            SET v_error_msg = CONCAT('Invalid value for in_table: ', v_quoted_table, ' is reserved table name.');
            SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = v_error_msg;
        END IF;
        CALL sys.table_exists(v_table_db, v_table_name, v_table_exists);
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('v_table_exists = ', v_table_exists) AS 'Debug';
        END IF;
        IF (v_table_exists = 'BASE TABLE') THEN
            SET v_checksum_ref = (
                 SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, COLUMN_TYPE) ORDER BY ORDINAL_POSITION) AS Checksum
                   FROM information_schema.COLUMNS
                  WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'events_statements_summary_by_digest'
                ),
                v_checksum_table = (
                 SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, COLUMN_TYPE) ORDER BY ORDINAL_POSITION) AS Checksum
                   FROM information_schema.COLUMNS
                  WHERE TABLE_SCHEMA = v_table_db AND TABLE_NAME = v_table_name
                );
            IF (v_checksum_ref <> v_checksum_table) THEN
                SET v_error_msg = CONCAT('The table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 93, CONCAT('...', SUBSTRING(v_quoted_table, -90)), v_quoted_table),
                                         ' has the wrong definition.');
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        END IF;
    END IF;
    IF (in_views IS NULL OR in_views = '') THEN
        SET in_views = 'with_runtimes_in_95th_percentile,analysis,with_errors_or_warnings,with_full_table_scans,with_sorting,with_temp_tables';
    END IF;
    CALL sys.table_exists(DATABASE(), 'tmp_digests', v_tmp_digests_table_exists);
    IF (@sys.debug = 'ON') THEN
        SELECT CONCAT('v_tmp_digests_table_exists = ', v_tmp_digests_table_exists) AS 'Debug';
    END IF;
    CASE
        WHEN in_action IN ('snapshot', 'overall') THEN
            IF (in_table IS NOT NULL) THEN
                IF (NOT v_table_exists IN ('TEMPORARY', 'BASE TABLE')) THEN
                    SET v_error_msg = CONCAT('The ', in_action, ' action requires in_table to be NULL, NOW() or specify an existing table.',
                                             ' The table ',
                                             IF(CHAR_LENGTH(v_quoted_table) > 16, CONCAT('...', SUBSTRING(v_quoted_table, -13)), v_quoted_table),
                                             ' does not exist.');
                    SIGNAL SQLSTATE '45000'
                       SET MESSAGE_TEXT = v_error_msg;
                END IF;
            END IF;
        WHEN in_action IN ('delta', 'save') THEN
            IF (v_table_exists NOT IN ('TEMPORARY', 'BASE TABLE')) THEN
                SET v_error_msg = CONCAT('The ', in_action, ' action requires in_table to be an existing table.',
                                         IF(in_table IS NOT NULL, CONCAT(' The table ',
                                             IF(CHAR_LENGTH(v_quoted_table) > 39, CONCAT('...', SUBSTRING(v_quoted_table, -36)), v_quoted_table),
                                             ' does not exist.'), ''));
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
            IF (in_action = 'delta' AND v_tmp_digests_table_exists <> 'TEMPORARY') THEN
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = 'An existing snapshot generated with the statement_performance_analyzer() must exist.';
            END IF;
        WHEN in_action = 'create_tmp' THEN
            IF (v_table_exists = 'TEMPORARY') THEN
                SET v_error_msg = CONCAT('Cannot create the table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 72, CONCAT('...', SUBSTRING(v_quoted_table, -69)), v_quoted_table),
                                         ' as it already exists.');
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        WHEN in_action = 'create_table' THEN
            IF (v_table_exists <> '') THEN
                SET v_error_msg = CONCAT('Cannot create the table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 52, CONCAT('...', SUBSTRING(v_quoted_table, -49)), v_quoted_table),
                                         ' as it already exists',
                                         IF(v_table_exists = 'TEMPORARY', ' as a temporary table.', '.'));
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        WHEN in_action = 'cleanup' THEN
            DO (SELECT 1);
        ELSE
            SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = 'Unknown action. Supported actions are: cleanup, create_table, create_tmp, delta, overall, save, snapshot';
    END CASE;
    SET v_digest_table_template = 'CREATE %{TEMPORARY}TABLE %{TABLE_NAME} (\n  `SCHEMA_NAME` varchar(64) DEFAULT NULL,\n  `DIGEST` varchar(32) DEFAULT NULL,\n  `DIGEST_TEXT` longtext,\n  `COUNT_STAR` bigint(20) unsigned NOT NULL,\n  `SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,\n  `MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL,\n  `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL,\n  `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL,\n  `SUM_LOCK_TIME` bigint(20) unsigned NOT NULL,\n  `SUM_ERRORS` bigint(20) unsigned NOT NULL,\n  `SUM_WARNINGS` bigint(20) unsigned NOT NULL,\n  `SUM_ROWS_AFFECTED` bigint(20) unsigned NOT NULL,\n  `SUM_ROWS_SENT` bigint(20) unsigned NOT NULL,\n  `SUM_ROWS_EXAMINED` bigint(20) unsigned NOT NULL,\n  `SUM_CREATED_TMP_DISK_TABLES` bigint(20) unsigned NOT NULL,\n  `SUM_CREATED_TMP_TABLES` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_FULL_JOIN` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_FULL_RANGE_JOIN` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_RANGE` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_RANGE_CHECK` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_SC��VAN` bigint(20) unsigned NOT NULL,\n  `SUM_SORT_MERGE_PASSES` bigint(20) unsigned NOT NULL,\n  `SUM_SORT_RANGE` bigint(20) unsigned NOT NULL,\n  `SUM_SORT_ROWS` bigint(20) unsigned NOT NULL,\n  `SUM_SORT_SCAN` bigint(20) unsigned NOT NULL,\n  `SUM_NO_INDEX_USED` bigint(20) unsigned NOT NULL,\n  `SUM_NO_GOOD_INDEX_USED` bigint(20) unsigned NOT NULL,\n  `FIRST_SEEN` timestamp NULL DEFAULT NULL,\n  `LAST_SEEN` timestamp NULL DEFAULT NULL,\n  INDEX (SCHEMA_NAME, DIGEST)\n) DEFAULT CHARSET=utf8';
    IF (v_force_new_snapshot
           OR in_action = 'snapshot'
           OR (in_action = 'overall' AND in_table IS NULL)
           OR (in_action = 'save' AND v_tmp_digests_table_exists <> 'TEMPORARY')
       ) THEN
        IF (v_tmp_digests_table_exists = 'TEMPORARY') THEN
            IF (@sys.debug = 'ON') THEN
                SELECT 'DROP TEMPORARY TABLE IF EXISTS tmp_digests' AS 'Debug';
            END IF;
            DROP TEMPORARY TABLE IF EXISTS tmp_digests;
        END IF;
        CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', 'TEMPORARY '), '%{TABLE_NAME}', 'tmp_digests'));
        SET v_sql = CONCAT('INSERT INTO tmp_digests SELECT * FROM ',
                           IF(in_table IS NULL OR in_action = 'save', 'performance_schema.events_statements_summary_by_digest', v_quoted_table));
        CALL sys.execute_prepared_stmt(v_sql);
    END IF;
    IF (in_action IN ('create_table', 'create_tmp')) THEN
        IF (in_action = 'create_table') THEN
            CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', ''), '%{TABLE_NAME}', v_quoted_table));
        ELSE
            CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', 'TEMPORARY '), '%{TABLE_NAME}', v_quoted_table));
        END IF;
    ELSEIF (in_action = 'save') THEN
        CALL sys.execute_prepared_stmt(CONCAT('DELETE FROM ', v_quoted_table));
        CALL sys.execute_prepared_stmt(CONCAT('INSERT INTO ', v_quoted_table, ' SELECT * FROM tmp_digests'));
    ELSEIF (in_action = 'cleanup') THEN
        DROP TEMPORARY TABLE IF EXISTS sys.tmp_digests;
        DROP TEMPORARY TABLE IF EXISTS sys.tmp_digests_delta;
    ELSEIF (in_action IN ('overall', 'delta')) THEN
        IF (in_action = 'overall') THEN
            IF (in_table IS NULL) THEN
                SET v_digests_table = 'tmp_digests';
            ELSE
                SET v_digests_table = v_quoted_table;
            END IF;
        ELSE
            SET v_digests_table = 'tmp_digests_delta';
            DROP TEMPORARY TABLE IF EXISTS tmp_digests_delta;
            CREATE TEMPORARY TABLE tmp_digests_delta LIKE tmp_digests;
            SET v_sql = CONCAT('INSERT INTO tmp_digests_delta\nSELECT `d_end`.`SCHEMA_NAME`,\n       `d_end`.`DIGEST`,\n       `d_end`.`DIGEST_TEXT`,\n       `d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0) AS ''COUNT_STAR'',\n       `d_end`.`SUM_TIMER_WAIT`-IFNULL(`d_start`.`SUM_TIMER_WAIT`, 0) AS ''SUM_TIMER_WAIT'',\n       `d_end`.`MIN_TIMER_WAIT` AS ''MIN_TIMER_WAIT'',\n       IFNULL((`d_end`.`SUM_TIMER_WAIT`-IFNULL(`d_start`.`SUM_TIMER_WAIT`, 0))/NULLIF(`d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0), 0), 0) AS ''AVG_TIMER_WAIT'',\n       `d_end`.`MAX_TIMER_WAIT` AS ''MAX_TIMER_WAIT'',\n       `d_end`.`SUM_LOCK_TIME`-IFNULL(`d_start`.`SUM_LOCK_TIME`, 0) AS ''SUM_LOCK_TIME'',\n       `d_end`.`SUM_ERRORS`-IFNULL(`d_start`.`SUM_ERRORS`, 0) AS ''SUM_ERRORS'',\n       `d_end`.`SUM_WARNINGS`-IFNULL(`d_start`.`SUM_WARNINGS`, 0) AS ''SUM_WARNINGS'',\n       `d_end`.`SUM_ROWS_AFFECTED`-IFNULL(`d_start`.`SUM_ROWS_AFFECTED`, 0) AS ''SUM_ROWS_AFFECTED'',\n       `d_end`.`SUM_ROWS_SENT`-IFNULL(`d_start`.`SUM_ROWS_SENT`, 0) AS ''SUM_ROWS_SENT'',\n       `d_end`.`SUM_ROWS_EXAMINED`-IFNULL(`d_start`.`SUM_ROWS_EXAMINED`, 0) AS ''SUM_ROWS_EXAMINED'',\n       `d_end`.`SUM_CREATED_TMP_DISK_TABLES`-IFNULL(`d_start`.`SUM_CREATED_TMP_DISK_TABLES`, 0) AS ''SUM_CREATED_TMP_DISK_TABLES'',\n       `d_end`.`SUM_CREATED_TMP_TABLES`-IFNULL(`d_start`.`SUM_CREATED_TMP_TABLES`, 0) AS ''SUM_CREATED_TMP_TABLES'',\n       `d_end`.`SUM_SELECT_FULL_JOIN`-IFNULL(`d_start`.`SUM_SELECT_FULL_JOIN`, 0) AS ''SUM_SELECT_FULL_JOIN'',\n       `d_end`.`SUM_SELECT_FULL_RANGE_JOIN`-IFNULL(`d_start`.`SUM_SELECT_FULL_RANGE_JOIN`, 0) AS ''SUM_SELECT_FULL_RANGE_JOIN'',\n       `d_end`.`SUM_SELECT_RANGE`-IFNULL(`d_start`.`SUM_SELECT_RANGE`, 0) AS ''SUM_SELECT_RANGE'',\n       `d_end`.`SUM_SELECT_RANGE_CHECK`-IFNULL(`d_start`.`SUM_SELECT_RANGE_CHECK`, 0) AS ''SUM_SELECT_RANGE_CHECK'',\n       `d_end`.`SUM_SELECT_SCAN`-IFNULL(`d_start`.`SUM_SELECT_SCAN`, 0) AS ''SUM_SELECT_SCAN'',\n       `d_end`.`SUM_SORT_MERGE_PASSES`-IFNULL(`d_start`.`SUM_SORT_MERGE_PASSES`, 0) AS ''SUM_SORT_MERGE_PASSES'',\n       `d_end`.`SUM_SORT_RANGE`-IFNULL(`d_start`.`SUM_SORT_RANGE`, 0) AS ''SUM_SORT_RANGE'',\n       `d_end`.`SUM_SORT_ROWS`-IFNULL(`d_start`.`SUM_SORT_ROWS`, 0) AS ''SUM_SORT_ROWS'',\n       `d_end`.`SUM_SORT_SCAN`-IFNULL(`d_start`.`SUM_SORT_SCAN`, 0) AS ''SUM_SORT_SCAN'',\n       `d_end`.`SUM_NO_INDEX_USED`-IFNULL(`d_start`.`SUM_NO_INDEX_USED`, 0) AS ''SUM_NO_INDEX_USED'',\n       `d_end`.`SUM_NO_GOOD_INDEX_USED`-IFNULL(`d_start`.`SUM_NO_GOOD_INDEX_USED`, 0) AS ''SUM_NO_GOOD_INDEX_USED'',\n       `d_end`.`FIRST_SEEN`,\n       `d_end`.`LAST_SEEN`\n  FROM tmp_digests d_end\n       LEFT OUTER JOIN ', v_quoted_table, ' d_start ON `d_start`.`DIGEST` = `d_end`.`DIGEST`\n                                                    AND (`d_start`.`SCHEMA_NAME` = `d_end`.`SCHEMA_NAME`\n                                                          OR (`d_start`.`SCHEMA_NAME` IS NULL AND `d_end`.`SCHEMA_NAME` IS NULL)\n                                                        )\n WHERE `d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0) > 0');
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_runtimes_in_95th_percentile', in_views)) THEN
            SELECT 'Queries with Runtime in 95th Percentile' AS 'Next Output';
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_avg_latency_distribution1;
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_avg_latency_distribution2;
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_95th_percentile_by_avg_us;
            CREATE TEMPORARY TABLE tmp_digest_avg_latency_distribution1 (
              cnt bigint unsigned NOT NULL,
              avg_us decimal(21,0) NOT NULL,
              PRIMARY KEY (avg_us)
            ) ENGINE=InnoDB;
            SET v_sql = CONCAT('INSERT INTO tmp_digest_avg_latency_distribution1\nSELECT COUNT(*) cnt,\n       ROUND(avg_timer_wait/1000000) AS avg_us\n  FROM ', v_digests_table, '\n GROUP BY avg_us');
            CALL sys.execute_prepared_stmt(v_sql);
            CREATE TEMPORARY TABLE tmp_digest_avg_latency_distribution2 LIKE tmp_digest_avg_latency_distribution1;
            INSERT INTO tmp_digest_avg_latency_distribution2 SELECT * FROM tmp_digest_avg_latency_distribution1;
            CREATE TEMPORARY TABLE tmp_digest_95th_percentile_by_avg_us (
              avg_us decimal(21,0) NOT NULL,
              percentile decimal(46,4) NOT NULL,
              PRIMARY KEY (avg_us)
            ) ENGINE=InnoDB;
            SET v_sql = CONCAT('INSERT INTO tmp_digest_95th_percentile_by_avg_us\nSELECT s2.avg_us avg_us,\n       IFNULL(SUM(s1.cnt)/NULLIF((SELECT COUNT(*) FROM ', v_digests_table, '), 0), 0) percentile\n  FROM tmp_digest_avg_latency_distribution1 AS s1\n       JOIN tmp_digest_avg_latency_distribution2 AS s2 ON s1.avg_us <= s2.avg_us\n GROUP BY s2.avg_us\nHAVING percentile > 0.95\n ORDER BY percentile\n LIMIT 1');
            CALL sys.execute_prepared_stmt(v_sql);
            SET v_sql =
                REPLACE(
                    REPLACE(
                        (SELECT VIEW_DEFINITION
                           FROM information_schema.VIEWS
                          WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_runtimes_in_95th_percentile'
                        ),
                        '`performance_schema`.`events_statements_summary_by_digest`',
                        v_digests_table
                    ),
   3T�V                 'sys.x$ps_digest_95th_percentile_by_avg_us',
                    '`sys`.`x$ps_digest_95th_percentile_by_avg_us`'
              );
            CALL sys.execute_prepared_stmt(v_sql);
            DROP TEMPORARY TABLE tmp_digest_avg_latency_distribution1;
            DROP TEMPORARY TABLE tmp_digest_avg_latency_distribution2;
            DROP TEMPORARY TABLE tmp_digest_95th_percentile_by_avg_us;
        END IF;
        IF (FIND_IN_SET('analysis', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries Ordered by Total Latency') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statement_analysis'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_errors_or_warnings', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Errors') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_errors_or_warnings'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_full_table_scans', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Full Table Scan') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_full_table_scans'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_sorting', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Sorting') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_sorting'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_temp_tables', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Internal Temporary Tables') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_temp_tables'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('custom', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries Using Custom View') AS 'Next Output';
            IF (@sys.statement_performance_analyzer.view IS NULL) THEN
                SET @sys.statement_performance_analyzer.view = sys.sys_get_config('statement_performance_analyzer.view', NULL);
            END IF;
            IF (@sys.statement_performance_analyzer.view IS NULL) THEN
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = 'The @sys.statement_performance_analyzer.view user variable must be set with the view or query to use.';
            END IF;
            IF (NOT INSTR(@sys.statement_performance_analyzer.view, ' ')) THEN
                IF (NOT INSTR(@sys.statement_performance_analyzer.view, '.')) THEN
                    SET v_custom_db   = DATABASE(),
                        v_custom_name = @sys.statement_performance_analyzer.view;
                ELSE
                    SET v_custom_db   = SUBSTRING_INDEX(@sys.statement_performance_analyzer.view, '.', 1);
                    SET v_custom_name = SUBSTRING(@sys.statement_performance_analyzer.view, CHAR_LENGTH(v_custom_db)+2);
                END IF;
                CALL sys.table_exists(v_custom_db, v_custom_name, v_custom_view_exists);
                IF (v_custom_view_exists <> 'VIEW') THEN
                    SIGNAL SQLSTATE '45000'
                       SET MESSAGE_TEXT = 'The @sys.statement_performance_analyzer.view user variable is set but specified neither an existing view nor a query.';
                END IF;
                SET v_sql =
                    REPLACE(
                        (SELECT VIEW_DEFINITION
                           FROM information_schema.VIEWS
                          WHERE TABLE_SCHEMA = v_custom_db AND TABLE_NAME = v_custom_name
                        ),
                        '`performance_schema`.`events_statements_summary_by_digest`',
                        v_digests_table
                    );
            ELSE
                SET v_sql = REPLACE(@sys.statement_performance_analyzer.view, '`performance_schema`.`events_statements_summary_by_digest`', v_digests_table);
            END IF;
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
    END IF;
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = @log_bin;
    END IF;
END^4�ԐU���i��3i��3,/��syslist_addlist_addutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_list TEXT,
        in_add_value TEXT
    text CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    IF (in_add_value IS NULL) THEN
        SIGNAL SQLSTATE '02200'
           SET MESSAGE_TEXT = 'Function sys.list_add: in_add_value input variable should not be NULL',
               MYSQL_ERRNO = 1138;
    END IF;
    IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
        RETURN in_add_value;
    END IF;
    RETURN (SELECT CONCAT(TRIM(BOTH ',' FROM TRIM(in_list)), ',', in_add_value));
END
             Description
             Takes a list, and a value to add to the list, and returns the resulting list.
             Useful for altering certain session variables, like sql_mode or optimizer_switch for instance.
             Parameters
             in_list (TEXT):
               The comma separated list to add a value to
             in_add_value (TEXT):
               The value to add to the input list
             Returns
             TEXT
             Example
             mysql> select @@sql_mode;
             +-----------------------------------------------------------------------------------+
             | @@sql_mode                                                                        |
             +-----------------------------------------------------------------------------------+
             | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
             +-----------------------------------------------------------------------------------+
             1 row in set (0.00 sec)
             mysql> set sql_mode = sys.list_add(@@sql_mode, 'ANSI_QUOTES');
             Query OK, 0 rows affected (0.06 sec)
             mysql> select @@sql_mode;
             +-----------------------------------------------------------------------------------------------+
             | @@sql_mode                                                                                    |
             +-----------------------------------------------------------------------------------------------+
             | ANSI_QUOTES,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
             +-----------------------------------------------------------------------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    IF (in_add_value IS NULL) THEN
        SIGNAL SQLSTATE '02200'
           SET MESSAGE_TEXT = 'Function sys.list_add: in_add_value input variable should not be NULL',
               MYSQL_ERRNO = 1138;
    END IF;
    IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
        RETURN in_add_value;
    END IF;
    RETURN (SELECT CONCAT(TRIM(BOTH ',' FROM TRIM(in_list)), ',', in_add_value));
END�i��3i��3		-/��syslist_droplist_droputf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_list TEXT,
        in_drop_value TEXT
    text CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    IF (in_drop_value IS NULL) THEN
        SIGNAL SQLSTATE '02200'
           SET MESSAGE_TEXT = 'Function sys.list_drop: in_drop_value input variable should not be NULL',
               MYSQL_ERRNO = 1138;
    END IF;
    IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
        RETURN in_list;
    END IF;
    RETURN (SELECT TRIM(BOTH ',' FROM REPLACE(REPLACE(CONCAT(',', in_list), CONCAT(',', in_drop_value), ''), CONCAT(', ', in_drop_value), '')));
END
             Description
             Takes a list, and a value to attempt to remove from the list, and returns the resulting list.
             Useful for altering certain session variables, like sql_mode or optimizer_switch for instance.
             Parameters
             in_list (TEXT):
               The comma separated list to drop a value from
             in_drop_value (TEXT):
               The value to drop from the input list
             Returns
             TEXT
             Example
             mysql> select @@sql_mode;
             +-----------------------------------------------------------------------------------------------+
             | @@sql_mode                                                                                    |
             +-----------------------------------------------------------------------------------------------+
             | ANSI_QUOTES,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
             +-----------------------------------------------------------------------------------------------+
             1 row in set (0.00 sec)
             mysql> set sql_mode = sys.list_drop(@@sql_mode, 'ONLY_FULL_GROUP_BY');
             Query OK, 0 rows affected (0.03 sec)
             mysql> select @@sql_mode;
             +----------------------------------------------------------------------------+
             | @@sql_mode                                                                 |
             +----------------------------------------------------------------------------+
             | ANSI_QUOTES,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
             +----------------------------------------------------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    IF (in_drop_value IS NULL) THEN
        SIGNAL SQLSTATE '02200'
           SET MESSAGE_TEXT = 'Function sys.list_drop: in_drop_value input variable should not be NULL',
               MYSQL_ERRNO = 1138;
    END IF;
    IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
        RETURN in_list;
    END IF;
    RETURN (SELECT TRIM(BOTH ',' FROM REPLACE(REPLACE(CONCAT(',', in_list), CONCAT(',', in_drop_value), ''), CONCAT(', ', in_drop_value), '')));
END--------------------+
             1 row in set (0.00 sec)
            BEGIN
    IF (in_drop_value IS NULL) THEN
        SIGNAL SQLSTATE '02200'
           SET MESSAGE_TEXT = 'Function sys.list_drop: in_drop_value input variable should not be NULL',
               MYSQL_ERRNO = 1138;
    END IF;
    IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
        RETURN in_list;
    END IF;
    RETURN (SELECT TRIM(BOTH ',' FROM REPLACE(REPLACE(CONCAT(',', in_list), CONCAT(',', in_drop_value), ''), CONCAT(', ', in_drop_value), '')));
END�+��,�-�b�ܣb�ܣ�N
�N
systable_existstable_existsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_db VARCHAR(64), IN in_table VARCHAR(64),
        OUT out_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY', 'SEQUENCE', 'SYSTEM VIEW')
    �b���b���
�� PcommonIsWorkingDayIsWorkingDayutf8mb3utf8mb3_general_cilatin1_swedish_cicommon@%`ADate` DATE
    tinyint(4)BEGIN

   /* (SS,2/7/2016) */
   DECLARE LCount INT;
   
   SELECT COUNT(*) INTO LCount FROM calendar WHERE CalendarDate = ADate AND IsWorkingDay;

	RETURN LCount = 1;

ENDBEGIN

   /* (SS,2/7/2016) */
   DECLARE LCount INT;
   
   SELECT COUNT(*) INTO LCount FROM calendar WHERE CalendarDate = ADate AND IsWorkingDay;

	RETURN LCount = 1;

END�b��b���� jms_demofuncDaysToCompletefuncDaysToCompletelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nDaysToComplete INT(12) DEFAULT 0;
    SELECT (TO_DAYS(DateTimeRepairComplete)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDaysToComplete FROM jobs WHERE JobID=sJobID;
    RETURN nDaysToComplete;
ENDBEGIN
    DECLARE nDaysToComplete INT(12) DEFAULT 0;
    SELECT (TO_DAYS(DateTimeRepairComplete)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDaysToComplete FROM jobs WHERE JobID=sJobID;
    RETURN nDaysToComplete;
END
R��%�]W\@ԐU��BEGIN
    DECLARE v_error BOOLEAN DEFAULT FALSE;
    DECLARE db_quoted VARCHAR(64);
    DECLARE table_quoted VARCHAR(64);
    DECLARE v_table_type VARCHAR(16) DEFAULT '';
    DECLARE v_system_db BOOLEAN
        DEFAULT LOWER(in_db) IN ('information_schema', 'performance_schema');
    DECLARE CONTINUE HANDLER FOR 1050 SET v_error = TRUE;
    DECLARE CONTINUE HANDLER FOR 1146 SET v_error = TRUE;
    SET out_exists = '';
    SET db_quoted = sys.quote_identifier(in_db);
    SET table_quoted = sys.quote_identifier(in_table);
    IF (EXISTS(SELECT 1 FROM information_schema.TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME = in_table)) THEN
        IF v_system_db = FALSE THEN
            SET @sys.tmp.table_exists.SQL = CONCAT('CREATE TEMPORARY TABLE ',
                                                    db_quoted,
                                                    '.',
                                                    table_quoted,
                                                    '(id INT PRIMARY KEY)');
            PREPARE stmt_create_table FROM @sys.tmp.table_exists.SQL;
            EXECUTE stmt_create_table;
            DEALLOCATE PREPARE stmt_create_table;
            SET @sys.tmp.table_exists.SQL = CONCAT('DROP TEMPORARY TABLE ',
                                                                db_quoted,
                                                                '.',
                                                                table_quoted);
            PREPARE stmt_drop_table FROM @sys.tmp.table_exists.SQL;
            EXECUTE stmt_drop_table;
            DEALLOCATE PREPARE stmt_drop_table;
        END IF;
        IF (v_error) THEN
            SET out_exists = 'TEMPORARY';
        ELSE
            SET v_table_type = (SELECT TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME = in_table);
            IF v_table_type = 'SYSTEM VERSIONED' THEN
                SET out_exists = 'BASE TABLE';
            ELSE
                SET out_exists = v_table_type;
            END IF;
        END IF;
    ELSE
        IF v_system_db = FALSE THEN
            SET @sys.tmp.table_exists.SQL = CONCAT('SELECT COUNT(*) FROM ',
                                                            db_quoted,
                                                            '.',
                                                            table_quoted);
            PREPARE stmt_select FROM @sys.tmp.table_exists.SQL;
            IF (NOT v_error) THEN
                DEALLOCATE PREPARE stmt_select;
                SET out_exists = 'TEMPORARY';
            END IF;
        END IF;
    END IF;
END
             Description
             Create a report of the current status of the server for diagnostics purposes. Data collected includes (some items depends on versions and settings):
                * The GLOBAL VARIABLES
                * Several sys schema views including metrics or equivalent (depending on version and settings)
                * Queries in the 95th percentile
                * Several ndbinfo views for MySQL Cluster
                * Replication (both master and slave) information.
             Some of the sys schema views are calculated as initial (optional), overall, delta:
                * The initial view is the content of the view at the start of this procedure.
                  This output will be the same as the the start values used for the delta view.
                  The initial view is included if @sys.diagnostics.include_raw = 'ON'.
                * The overall view is the content of the view at the end of this procedure.
                  This output is the same as the end values used for the delta view.
                  The overall view is always included.
                * The delta view is the difference from the beginning to the end. Note that for min and max values
                  they are simply the min or max value from the end view respectively, so does not necessarily reflect
                  the minimum/maximum value in the monitored period.
                  Note: except for the metrics views the delta is only calculation between the first and last outputs.
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Versions supported:
                * MySQL 5.6: 5.6.10 and later
                * MySQL 5.7: 5.7.9 and later
             Parameters
             in_max_runtime (INT UNSIGNED):
               The maximum time to keep collecting data.
               Use NULL to get the default which is 60 seconds, otherwise enter a value greater than 0.
             in_interval (INT UNSIGNED):
               How long to sleep between data collections.
               Use NULL to get the default which is 30 seconds, otherwise enter a value greater than 0.
             in_auto_config (ENUM('current', 'medium', 'full'))
               Automatically enable Performance Schema instruments and consumers.
               NOTE: The more that are enabled, the more impact on the performance.
               Supported values are:
                  * current - use the current settings.
                  * medium - enable some settings.
                  * full - enables all settings. This will have a big impact on the
                           performance - be careful using this option.
               If another setting the 'current' is chosen, the current settings
               are restored at the end of the procedure.
             Configuration Options
             sys.diagnostics.allow_i_s_tables
               Specifies whether it is allowed to do table scan queries on information_schema.TABLES. This can be expensive if there
               are many tables. Set to 'ON' to allow, 'OFF' to not allow.
               Default is 'OFF'.
             sys.diagnostics.include_raw
               Set to 'ON' to include the raw data (e.g. the original output of "SELECT * FROM sys.metrics").
               Use this to get the initial values of the various views.
               Default is 'OFF'.
             sys.statement_truncate_len
               How much of queries in the process list output to include.
               Default is 64.
             sys.debug
               Whether to provide debugging output.
               Default is 'OFF'. Set to 'ON' to include.
             Example
             To create a report and append it to the file diag.out:
             mysql> TEE diag.out;
             mysql> CALL sys.diagnostics(120, 30, 'current');
             ...
             mysql> NOTEE;
            Z
V��õԐU�N
             Description
             Tests whether the table specified in in_db and in_table exists either as a regular
             table, or as a temporary table. The returned value corresponds to the table that
             will be used, so if there's both a temporary and a permanent table with the given
             name, then 'TEMPORARY' will be returned.
             Parameters
             in_db (VARCHAR(64)):
               The database name to check for the existence of the table in.
             in_table (VARCHAR(64)):
               The name of the table to check the existence of.
             out_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY'):
               The return value: whether the table exists. The value is one of:
                 * ''             - the table does not exist neither as a base table, view, sequence nor temporary table.
                 * 'BASE TABLE'   - the table name exists as a permanent base table table.
                 * 'VIEW'         - the table name exists as a view.
                 * 'TEMPORARY'    - the table name exists as a temporary table.
                 * 'SEQUENCE'     - the table name exists as a sequence.
                 * 'SYSTEM VIEW'  - the table name exists as a system view.
             Example
             mysql> CREATE DATABASE db1;
             Query OK, 1 row affected (0.07 sec)
             mysql> use db1;
             Database changed
             mysql> CREATE TABLE t1 (id INT PRIMARY KEY);
             Query OK, 0 rows affected (0.08 sec)
             mysql> CREATE TABLE t2 (id INT PRIMARY KEY);
             Query OK, 0 rows affected (0.08 sec)
             mysql> CREATE view v_t1 AS SELECT * FROM t1;
             Query OK, 0 rows affected (0.00 sec)
             mysql> CREATE TEMPORARY TABLE t1 (id INT PRIMARY KEY);
             Query OK, 0 rows affected (0.00 sec)
             mysql> CALL sys.table_exists('db1', 't1', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.00 sec)
             +------------+
             | @exists    |
             +------------+
             | TEMPORARY  |
             +------------+
             1 row in set (0.00 sec)
             mysql> CALL sys.table_exists('db1', 't2', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.00 sec)
             +------------+
             | @exists    |
             +------------+
             | BASE TABLE |
             +------------+
             1 row in set (0.01 sec)
             mysql> CALL sys.table_exists('db1', 'v_t1', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.00 sec)
             +---------+
             | @exists |
             +---------+
             | VIEW    |
             +---------+
             1 row in set (0.00 sec)
             MariaDB [sys]> CALL sys.table_exists('db1', 's', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.006 sec)
             +----------+
             | @exists  |
             +----------+
             | SEQUENCE |
             +----------+
             1 row in set (0.000 sec)
             MariaDB [sys]> CALL table_exists('information_schema', 'user_variables', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.003 sec)
             +-------------+
             | @exists     |
             +-------------+
             | SYSTEM VIEW |
             +-------------+
             1 row in set (0.001 sec)
             mysql> CALL sys.table_exists('db1', 't3', @exists); SELECT @exists;
             Query OK, 0 rows affected (0.01 sec)
             +---------+
             | @exists |
             +---------+
             |         |
             +---------+
             1 row in set (0.00 sec)
            �7��XԐU��BEGIN
    DECLARE v_error BOOLEAN DEFAULT FALSE;
    DECLARE db_quoted VARCHAR(64);
    DECLARE table_quoted VARCHAR(64);
    DECLARE v_table_type VARCHAR(16) DEFAULT '';
    DECLARE v_system_db BOOLEAN
        DEFAULT LOWER(in_db) IN ('information_schema', 'performance_schema');
    DECLARE CONTINUE HANDLER FOR 1050 SET v_error = TRUE;
    DECLARE CONTINUE HANDLER FOR 1146 SET v_error = TRUE;
    SET out_exists = '';
    SET db_quoted = sys.quote_identifier(in_db);
    SET table_quoted = sys.quote_identifier(in_table);
    IF (EXISTS(SELECT 1 FROM information_schema.TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME = in_table)) THEN
        IF v_system_db = FALSE THEN
            SET @sys.tmp.table_exists.SQL = CONCAT('CREATE TEMPORARY TABLE ',
                                                    db_quoted,
                                                    '.',
                                                    table_quoted,
                                                    '(id INT PRIMARY KEY)');
            PREPARE stmt_create_table FROM @sys.tmp.table_exists.SQL;
            EXECUTE stmt_create_table;
            DEALLOCATE PREPARE stmt_create_table;
            SET @sys.tmp.table_exists.SQL = CONCAT('DROP TEMPORARY TABLE ',
                                                                db_quoted,
                                                                '.',
                                                                table_quoted);
            PREPARE stmt_drop_table FROM @sys.tmp.table_exists.SQL;
            EXECUTE stmt_drop_table;
            DEALLOCATE PREPARE stmt_drop_table;
        END IF;
        IF (v_error) THEN
            SET out_exists = 'TEMPORARY';
        ELSE
            SET v_table_type = (SELECT TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME = in_table);
            IF v_table_type = 'SYSTEM VERSIONED' THEN
                SET out_exists = 'BASE TABLE';
            ELSE
                SET out_exists = v_table_type;
            END IF;
        END IF;
    ELSE
        IF v_system_db = FALSE THEN
            SET @sys.tmp.table_exists.SQL = CONCAT('SELECT COUNT(*) FROM ',
                                                            db_quoted,
                                                            '.',
                                                            table_quoted);
            PREPARE stmt_select FROM @sys.tmp.table_exists.SQL;
            IF (NOT v_error) THEN
                DEALLOCATE PREPARE stmt_select;
                SET out_exists = 'TEMPORARY';
            END IF;
        END IF;
    END IF;
ENDN
D�4���J��i��3i��3  ;7sysps_is_instrument_default_enabledps_is_instrument_default_enabledutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_instrument VARCHAR(128)
    enum('YES','NO') CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    DECLARE v_enabled ENUM('YES', 'NO');
    SET v_enabled = IF(in_instrument LIKE 'wait/io/file/%'
                        OR in_instrument LIKE 'wait/io/table/%'
                        OR in_instrument LIKE 'statement/%'
                        OR in_instrument LIKE 'memory/performance_schema/%'
                        OR in_instrument IN ('wait/lock/table/sql/handler', 'idle')
               
                      ,
                       'YES',
                       'NO'
                    );
    RETURN v_enabled;
END
             Description
             Returns whether an instrument is enabled by default in this version of MySQL.
             Parameters
             in_instrument VARCHAR(128):
               The instrument to check.
             Returns
             ENUM('YES', 'NO')
             Example
             mysql> SELECT sys.ps_is_instrument_default_enabled('statement/sql/select');
             +--------------------------------------------------------------+
             | sys.ps_is_instrument_default_enabled('statement/sql/select') |
             +--------------------------------------------------------------+
             | YES                                                          |
             +--------------------------------------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    DECLARE v_enabled ENUM('YES', 'NO');
    SET v_enabled = IF(in_instrument LIKE 'wait/io/file/%'
                        OR in_instrument LIKE 'wait/io/table/%'
                        OR in_instrument LIKE 'statement/%'
                        OR in_instrument LIKE 'memory/performance_schema/%'
                        OR in_instrument IN ('wait/lock/table/sql/handler', 'idle')
               
                      ,
                       'YES',
                       'NO'
                    );
    RETURN v_enabled;
ENDNAME LIKE '%\_digest'           THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   WHEN c.NAME LIKE '%\_current'          THEN IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   ELSE IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES'
                           AND ( SELECT cc.ENABLED FROM performance_schema.setup_consumers cc WHERE NAME = CONCAT(SUBSTRING_INDEX(c.NAME, '_', 2), '_current')
                               ) = 'YES', 'YES', 'NO')
                END) AS IsEnabled
          FROM performance_schema.setup_consumers c
               INNER JOIN performance_schema.setup_consumers cg
               INNER JOIN performance_schema.setup_consumers ct
         WHERE cg.NAME       = 'global_instrumentation'
               AND ct.NAME   = 'thread_instrumentation'
               AND c.NAME    = in_consumer
       );
END
             Description
             Determines whether a consumer is enabled (taking the consumer hierarchy into consideration)
             within the Performance Schema.
             Parameters
             in_consumer VARCHAR(64):
               The name of the consumer to check.
             Returns
             ENUM('YES', 'NO')
             Example
             mysql> SELECT sys.ps_is_consumer_enabled('events_stages_history');
             +-----------------------------------------------------+
             | sys.ps_is_consumer_enabled('events_stages_history') |
             +-----------------------------------------------------+
             | NO                                                  |
             +-----------------------------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN (
        SELECT (CASE
                   WHEN c.NAME = 'global_instrumentation' THEN c.ENABLED
                   WHEN c.NAME = 'thread_instrumentation' THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   WHEN c.NAME LIKE '%\_digest'           THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   WHEN c.NAME LIKE '%\_current'          THEN IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
                   ELSE IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES'
                           AND ( SELECT cc.ENABLED FROM performance_schema.setup_consumers cc WHERE NAME = CONCAT(SUBSTRING_INDEX(c.NAME, '_', 2), '_current')
                               ) = 'YES', 'YES', 'NO')
                END) AS IsEnabled
          FROM performance_schema.setup_consumers c
               INNER JOIN performance_schema.setup_consumers cg
               INNER JOIN performance_schema.setup_consumers ct
         WHERE cg.NAME       = 'global_instrumentation'
               AND ct.NAME   = 'thread_instrumentation'
               AND c.NAME    = in_consumer
       );
END�b��b���� jms_demofuncGetBranchNameByBranchIDfuncGetBranchNameByBranchIDlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1nBranchID INT(12)
    varchar(200) CHARSET latin1BEGIN
DECLARE sBranchName VARCHAR(200) DEFAULT "UNKNOWN";
SELECT BranchName INTO sBranchName FROM branches WHERE BranchID=nBranchID;
RETURN sBranchName;
ENDBEGIN
DECLARE sBranchName VARCHAR(200) DEFAULT "UNKNOWN";
SELECT BranchName INTO sBranchName FROM branches WHERE BranchID=nBranchID;
RETURN sBranchName;
END�b��b���� jms_demofuncGetContractValuefuncGetContractValuelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    doubleBEGIN
DECLARE nResult DOUBLE DEFAULT 0.00;
SELECT SellPrice INTO nResult FROM jobs WHERE InvoiceNet=0 AND JobID=sJobID AND CustomerID IN (1709,2150,1270,2135,678,336,1897);
RETURN nResult;
ENDBEGIN
DECLARE nResult DOUBLE DEFAULT 0.00;
SELECT SellPrice INTO nResult FROM jobs WHERE InvoiceNet=0 AND JobID=sJobID AND CustomerID IN (1709,2150,1270,2135,678,336,1897);
RETURN nResult;
END�b��b��
�� jms_demofuncGetCustomerNamefuncGetCustomerNamelatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sJobID CHAR(12)
    varchar(200) CHARSET latin1BEGIN
	DECLARE sCustomerName VARCHAR(200) DEFAULT "UNKNOWN";
	SELECT customers.CustomerName INTO sCustomerName FROM customers JOIN jobs ON customers.CustomerID=jobs.CustomerID WHERE jobs.JobID=sJobID;
	RETURN sCustomerName;
ENDBEGIN
	DECLARE sCustomerName VARCHAR(200) DEFAULT "UNKNOWN";
	SELECT customers.CustomerName INTO sCustomerName FROM customers JOIN jobs ON customers.CustomerID=jobs.CustomerID WHERE jobs.JobID=sJobID;
	RETURN sCustomerName;
END�b��b���� jms_demofuncGetInvoiceValuefuncGetInvoiceValuelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    doubleBEGIN
DECLARE nInvoiceValue DOUBLE DEFAULT 0.00;
SELECT (NumberOff*QuotedPrice) INTO nInvoiceValue FROM jobs WHERE JobID=sJobID;
RETURN nInvoiceValue;
ENDBEGIN
DECLARE nInvoiceValue DOUBLE DEFAULT 0.00;
SELECT (NumberOff*QuotedPrice) INTO nInvoiceValue FROM jobs WHERE JobID=sJobID;
RETURN nInvoiceValue;
END�b��b�� jms_demofuncGetJobTypefuncGetJobTypelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    varchar(50) CHARSET latin1BEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE sJobType VARCHAR(50) DEFAULT "Job Type not set!";
SELECT JobTypeID INTO nJobTypeID FROM jobs WHERE JobID=sJobID;
SELECT JobType INTO sJobType FROM job_types WHERE JobTypeID=nJobTypeID;
RETURN sJobType;
ENDBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE sJobType VARCHAR(50) DEFAULT "Job Type not set!";
SELECT JobTypeID INTO nJobTypeID FROM jobs WHERE JobID=sJobID;
SELECT JobType INTO sJobType FROM job_types WHERE JobTypeID=nJobTypeID;
RETURN sJobType;
END����<�1�o�����K�g	�i��4i��4;�)�sysps_is_instrument_default_timedps_is_instrument_default_timedutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_instrument VARCHAR(128)
    enum('YES','NO') CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    DECLARE v_timed ENUM('YES', 'NO');
    SET v_timed = IF(in_instrument LIKE 'wait/io/file/%'
                        OR in_instrument LIKE 'wait/io/table/%'
                        OR in_instrument LIKE 'statement/%'
                        OR in_instrument IN ('wait/lock/table/sql/handler', 'idle')
               
                      ,
                       'YES',
                       'NO'
                    );
    RETURN v_timed;
END
             Description
             Returns whether an instrument is timed by default in this version of MySQL.
             Parameters
             in_instrument VARCHAR(128):
               The instrument to check.
             Returns
             ENUM('YES', 'NO')
             Example
             mysql> SELECT sys.ps_is_instrument_default_timed('statement/sql/select');
             +------------------------------------------------------------+
             | sys.ps_is_instrument_default_timed('statement/sql/select') |
             +------------------------------------------------------------+
             | YES                                                        |
             +------------------------------------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    DECLARE v_timed ENUM('YES', 'NO');
    SET v_timed = IF(in_instrument LIKE 'wait/io/file/%'
                        OR in_instrument LIKE 'wait/io/table/%'
                        OR in_instrument LIKE 'statement/%'
                        OR in_instrument IN ('wait/lock/table/sql/handler', 'idle')
               
                      ,
                       'YES',
                       'NO'
                    );
    RETURN v_timed;
END�i��4i��4%Ei�isysps_is_thread_instrumentedps_is_thread_instrumentedutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_connection_id BIGINT UNSIGNED
    enum('YES','NO','UNKNOWN') CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    DECLARE v_enabled ENUM('YES', 'NO', 'UNKNOWN');
    IF (in_connection_id IS NULL) THEN
        RETURN NULL;
    END IF;
    SELECT INSTRUMENTED INTO v_enabled
      FROM performance_schema.threads
     WHERE PROCESSLIST_ID = in_connection_id;
    IF (v_enabled IS NULL) THEN
        RETURN 'UNKNOWN';
    ELSE
        RETURN v_enabled;
    END IF;
END
             Description
             Checks whether the provided connection id is instrumented within Performance Schema.
             Parameters
             in_connection_id (BIGINT UNSIGNED):
               The id of the connection to check.
             Returns
             ENUM('YES', 'NO', 'UNKNOWN')
             Example
             mysql> SELECT sys.ps_is_thread_instrumented(CONNECTION_ID());
             +------------------------------------------------+
             | sys.ps_is_thread_instrumented(CONNECTION_ID()) |
             +------------------------------------------------+
             | YES                                            |
             +------------------------------------------------+
            BEGIN
    DECLARE v_enabled ENUM('YES', 'NO', 'UNKNOWN');
    IF (in_connection_id IS NULL) THEN
        RETURN NULL;
    END IF;
    SELECT INSTRUMENTED INTO v_enabled
      FROM performance_schema.threads
     WHERE PROCESSLIST_ID = in_connection_id;
    IF (v_enabled IS NULL) THEN
        RETURN 'UNKNOWN';
    ELSE
        RETURN v_enabled;
    END IF;
ENDment LIKE 'wait/io/file/%'
                        OR in_instrument LIKE 'wait/io/table/%'
                        OR in_instrument LIKE 'statement/%'
                        OR in_instrument IN ('wait/lock/table/sql/handler', 'idle')
               
                      ,
                       'YES',
                       'NO'
                    );
    RETURN v_timed;
END�b�ܟb�ܟ%*i�isysps_is_thread_instrumentedps_is_thread_instrumentedutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_connection_id BIGINT UNSIGNED
    enum('YES','NO','UNKNOWN') CHARSET utf8mb3BEGIN
    DECLARE v_enabled ENUM('YES', 'NO', 'UNKNOWN');
    IF (in_connection_id IS NULL) THEN
        RETURN NULL;
    END IF;
    SELECT INSTRUMENTED INTO v_enabled
      FROM performance_schema.threads
     WHERE PROCESSLIST_ID = in_connection_id;
    IF (v_enabled IS NULL) THEN
        RETURN 'UNKNOWN';
    ELSE
        RETURN v_enabled;
    END IF;
END
             Description
             Checks whether the provided connection id is instrumented within Performance Schema.
             Parameters
             in_connection_id (BIGINT UNSIGNED):
               The id of the connection to check.
             Returns
             ENUM('YES', 'NO', 'UNKNOWN')
             Example
             mysql> SELECT sys.ps_is_thread_instrumented(CONNECTION_ID());
             +------------------------------------------------+
             | sys.ps_is_thread_instrumented(CONNECTION_ID()) |
             +------------------------------------------------+
             | YES                                            |
             +------------------------------------------------+
            BEGIN
    DECLARE v_enabled ENUM('YES', 'NO', 'UNKNOWN');
    IF (in_connection_id IS NULL) THEN
        RETURN NULL;
    END IF;
    SELECT INSTRUMENTED INTO v_enabled
      FROM performance_schema.threads
     WHERE PROCESSLIST_ID = in_connection_id;
    IF (v_enabled IS NULL) THEN
        RETURN 'UNKNOWN';
    ELSE
        RETURN v_enabled;
    END IF;
END�b��b��
�� jms_demofuncGetMaterialCostfuncGetMaterialCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�b��b��


 jms_demofuncGetMaterialCostNewfuncGetMaterialCostNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  # Serial: 2008-12-19-01

  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID;  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  # Serial: 2008-12-19-01

  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID;  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�b��b���� jms_demofuncGetRepairEngineerfuncGetRepairEngineerlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    varchar(100) CHARSET latin1BEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
END��2�������������_L��i��4i��4

Q�Qsysversion_majorversion_majorutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhosttinyint(3) unsignedBEGIN
    RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', 1);
END
             Description
             Returns the major version of MySQL Server.
             Returns
             TINYINT UNSIGNED
             Example
             mysql> SELECT VERSION(), sys.version_major();
             +--------------------------------------+---------------------+
             | VERSION()                            | sys.version_major() |
             +--------------------------------------+---------------------+
             | 5.7.9-enterprise-commercial-advanced | 5                   |
             +--------------------------------------+---------------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', 1);
END�i��4i��4!//	/sysps_thread_accountps_thread_accountutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_thread_id BIGINT UNSIGNED
    text CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    RETURN (SELECT IF(
                      type = 'FOREGROUND',
                      CONCAT(processlist_user, '@', processlist_host),
                      type
                     ) AS account
              FROM `performance_schema`.`threads`
             WHERE thread_id = in_thread_id);
END
             Description
             Return the user@host account for the given Performance Schema thread id.
             Parameters
             in_thread_id (BIGINT UNSIGNED):
               The id of the thread to return the account for.
             Example
             mysql> select thread_id, processlist_user, processlist_host from performance_schema.threads where type = 'foreground';
              +-----------+------------------+------------------+
              | thread_id | processlist_user | processlist_host |
              +-----------+------------------+------------------+
              |        23 | NULL             | NULL             |
              |        30 | root             | localhost        |
              |        31 | msandbox         | localhost        |
              |        32 | msandbox         | localhost        |
              +-----------+------------------+------------------+
              4 rows in set (0.00 sec)
              mysql> select sys.ps_thread_account(31);
              +---------------------------+
              | sys.ps_thread_account(31) |
              +---------------------------+
              | msandbox@localhost        |
              +---------------------------+
              1 row in set (0.00 sec)
            BEGIN
    RETURN (SELECT IF(
                      type = 'FOREGROUND',
                      CONCAT(processlist_user, '@', processlist_host),
                      type
                     ) AS account
              FROM `performance_schema`.`threads`
             WHERE thread_id = in_thread_id);
END�i��4i��4

k�ksysversion_minorversion_minorutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhosttinyint(3) unsignedBEGIN
    RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', 2), '.', -1);
END
             Description
             Returns the minor (release series) version of MySQL Server.
             Returns
             TINYINT UNSIGNED
             Example
             mysql> SELECT VERSION(), sys.server_minor();
             +--------------------------------------+---------------------+
             | VERSION()                            | sys.version_minor() |
             +--------------------------------------+---------------------+
             | 5.7.9-enterprise-commercial-advanced | 7                   |
             +--------------------------------------+---------------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', 2), '.', -1);
END�i��4i��4

R�Rsysversion_patchversion_patchutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhosttinyint(3) unsignedBEGIN
    RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', -1);
END
             Description
             Returns the patch release version of MySQL Server.
             Returns
             TINYINT UNSIGNED
             Example
             mysql> SELECT VERSION(), sys.version_patch();
             +--------------------------------------+---------------------+
             | VERSION()                            | sys.version_patch() |
             +--------------------------------------+---------------------+
             | 5.7.9-enterprise-commercial-advanced | 9                   |
             +--------------------------------------+---------------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', -1);
END) CHARSET utf8mb3BEGIN
    DECLARE v_value VARCHAR(128) DEFAULT NULL;
    DECLARE old_val INTEGER DEFAULT NULL;
    SET v_value = (SELECT value FROM sys.sys_config WHERE variable = in_variable_name);
    IF (v_value IS NULL) THEN
        SET v_value = in_default_value;
    END IF;
    RETURN v_value;
END
             Description
             Returns the value for the requested variable using the following logic:
                1. If the option exists in sys.sys_config return the value from there.
                2. Else fall back on the provided default value.
             Notes for using sys_get_config():
                * If the default value argument to sys_get_config() is NULL and case 2. is reached, NULL is returned.
                  It is then expected that the caller is able to handle NULL for the given configuration option.
                * The convention is to name the user variables @sys.<name of variable>. It is <name of variable> that
                 �i��4i��4!//	/sysps_thread_accountps_thread_accountutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_thread_id BIGINT UNSIGNED
    text CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    RETURN (SELECT IF(
                      type = 'FOREGROUND',
                      CONCAT(processlist_user, '@', processlist_host),
                      type
                     ) AS account
              FROM `performance_schema`.`threads`
             WHERE thread_id = in_thread_id);
END
             Description
             Return the user@host account for the given Performance Schema thread id.
             Parameters
             in_thread_id (BIGINT UNSIGNED):
               The id of the thread to return the account for.
             Example
             mysql> select thread_id, processlist_user, processlist_host from performance_schema.threads where type = 'foreground';
              +-----------+------------------+------------------+
              | thread_id | processlist_user | processlist_host |
              +-----------+------------------+------------------+
              |        23 | NULL             | NULL             |
              |        30 | root             | localhost        |
              |        31 | msandbox         | localhost        |
              |        32 | msandbox         | localhost        |
              +-----------+------------------+------------------+
              4 rows in set (0.00 sec)
              mysql> select sys.ps_thread_account(31);
              +---------------------------+
              | sys.ps_thread_account(31) |
              +---------------------------+
              | msandbox@localhost        |
              +---------------------------+
              1 row in set (0.00 sec)
            BEGIN
    RETURN (SELECT IF(
                      type = 'FOREGROUND',
                      CONCAT(processlist_user, '@', processlist_host),
                      type
                     ) AS account
              FROM `performance_schema`.`threads`
             WHERE thread_id = in_thread_��G��#��Q�\	�i��9i��9##��sysps_setup_disable_background_threadsps_setup_disable_background_threadsutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'NO'
     WHERE type = 'BACKGROUND';
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' background thread', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Disable all background thread instrumentation within Performance Schema.
             Parameters
             None.
             Example
             mysql> CALL sys.ps_setup_disable_background_threads();
             +--------------------------------+
             | summary                        |
             +--------------------------------+
             | Disabled 18 background threads |
             +--------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'NO'
     WHERE type = 'BACKGROUND';
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' background thread', IF(@rows != 1, 's', '')) AS summary;
ENDared_stmt prepared statement name is used for the query and is required not to exist.
             Parameters
             in_query (longtext CHARACTER SET UTF8):
               The query to execute.
             Configuration Options
             sys.debug
               Whether to provide debugging output.
               Default is 'OFF'. Set to 'ON' to include.
             Example
             mysql> CALL sys.execute_prepared_stmt('SELECT * FROM sys.sys_config');
             +------------------------+-------+---------------------+--------+
             | variable               | value | set_time            | set_by |
             +------------------------+-------+---------------------+--------+
             | statement_truncate_len | 64    | 2015-06-30 13:06:00 | NULL   |
             +------------------------+-------+---------------------+--------+
             1 row in set (0.00 sec)
             Query OK, 0 rows affected (0.00 sec)
            BEGIN
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug = sys.sys_get_config('debug', 'OFF');
    END IF;
    IF (in_query IS NULL OR LENGTH(in_query) < 4) THEN
       SIGNAL SQLSTATE '45000'
          SET MESSAGE_TEXT = "The @sys.execute_prepared_stmt.sql must contain a query";
    END IF;
    SET @sys.execute_prepared_stmt.sql = in_query;
    IF (@sys.debug = 'ON') THEN
        SELECT @sys.execute_prepared_stmt.sql AS 'Debug';
    END IF;
    PREPARE sys_execute_prepared_stmt FROM @sys.execute_prepared_stmt.sql;
    EXECUTE sys_execute_prepared_stmt;
    DEALLOCATE PREPARE sys_execute_prepared_stmt;
    SET @sys.execute_prepared_stmt.sql = NULL;
END------------+---------------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', -1);
END�b�ܢb�ܢ5���sysexecute_prepared_stmtexecute_prepared_stmtutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_query longtext CHARACTER SET UTF8
    BEGIN
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug = sys.sys_get_config('debug', 'OFF');
    END IF;
    IF (in_query IS NULL OR LENGTH(in_query) < 4) THEN
       SIGNAL SQLSTATE '45000'
          SET MESSAGE_TEXT = "The @sys.execute_prepared_stmt.sql must contain a query";
    END IF;
    SET @sys.execute_prepared_stmt.sql = in_query;
    IF (@sys.debug = 'ON') THEN
        SELECT @�i��8i��85���sysexecute_prepared_stmtexecute_prepared_stmtutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_query longtext CHARACTER SET UTF8
    BEGIN
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug = sys.sys_get_config('debug', 'OFF');
    END IF;
    IF (in_query IS NULL OR LENGTH(in_query) < 4) THEN
       SIGNAL SQLSTATE '45000'
          SET MESSAGE_TEXT = "The @sys.execute_prepared_stmt.sql must contain a query";
    END IF;
    SET @sys.execute_prepared_stmt.sql = in_query;
    IF (@sys.debug = 'ON') THEN
        SELECT @sys.execute_prepared_stmt.sql AS 'Debug';
    END IF;
    PREPARE sys_execute_prepared_stmt FROM @sys.execute_prepared_stmt.sql;
    EXECUTE sys_execute_prepared_stmt;
    DEALLOCATE PREPARE sys_execute_prepared_stmt;
    SET @sys.execute_prepared_stmt.sql = NULL;
END
             Description
             Takes the query in the argument and executes it using a prepared statement. The prepared statement is deallocated,
             so the procedure is mainly useful for executing one off dynamically created queries.
             The sys_execute_prepared_stmt prepared statement name is used for the query and is required not to exist.
             Parameters
             in_query (longtext CHARACTER SET UTF8):
               The query to execute.
             Configuration Options
             sys.debug
               Whether to provide debugging output.
               Default is 'OFF'. Set to 'ON' to include.
             Example
             mysql> CALL sys.execute_prepared_stmt('SELECT * FROM sys.sys_config');
             +------------------------+-------+---------------------+--------+
             | variable               | value | set_time            | set_by |
             +------------------------+-------+---------------------+--------+
             | statement_truncate_len | 64    | 2015-06-30 13:06:00 | NULL   |
             +------------------------+-------+---------------------+--------+
             1 row in set (0.00 sec)
             Query OK, 0 rows affected (0.00 sec)
            BEGIN
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug = sys.sys_get_config('debug', 'OFF');
    END IF;
    IF (in_query IS NULL OR LENGTH(in_query) < 4) THEN
       SIGNAL SQLSTATE '45000'
          SET MESSAGE_TEXT = "The @sys.execute_prepared_stmt.sql must contain a query";
    END IF;
    SET @sys.execute_prepared_stmt.sql = in_query;
    IF (@sys.debug = 'ON') THEN
        SELECT @sys.execute_prepared_stmt.sql AS 'Debug';
    END IF;
    PREPARE sys_execute_prepared_stmt FROM @sys.execute_prepared_stmt.sql;
    EXECUTE sys_execute_prepared_stmt;
    DEALLOCATE PREPARE sys_execute_prepared_stmt;
    SET @sys.execute_prepared_stmt.sql = NULL;
END�b��b���� jms_demofuncIsJobQuotationfuncIsJobQuotationlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID INT(12)
    int(12)BEGIN
DECLARE nResult INT(12) DEFAULT 0;
SELECT NumberOff INTO nResult FROM jobs JOIN job_types ON jobs.JobTypeID=job_types.JobTypeID WHERE JobID=sJobID AND job_types.JobType="Quotation";
RETURN nResult;
ENDBEGIN
DECLARE nResult INT(12) DEFAULT 0;
SELECT NumberOff INTO nResult FROM jobs JOIN job_types ON jobs.JobTypeID=job_types.JobTypeID WHERE JobID=sJobID AND job_types.JobType="Quotation";
RETURN nResult;
END�b��b���� jms_demofuncJobBecomesWarrantyItemCountfuncJobBecomesWarrantyItemCountlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nResult INT(12);
    SELECT SUM(NumberOff) INTO nResult FROM jobs WHERE CWJobID=sJobID;
    IF nResult IS NULL THEN
        SET nResult=0;
    END IF;
    RETURN nResult;
ENDBEGIN
    DECLARE nResult INT(12);
    SELECT SUM(NumberOff) INTO nResult FROM jobs WHERE CWJobID=sJobID;
    IF nResult IS NULL THEN
        SET nResult=0;
    END IF;
    RETURN nResult;
END�b��b��
�� jms_demoGetNextInvoiceNoGetNextInvoiceNolatin1latin1_swedish_cilatin1_swedish_cijms_demo@%nBranchID INTEGER(11)
    int(11)BEGIN
  DECLARE nNextInvoiceNo INTEGER;
  SELECT NextInvoiceNo INTO nNextInvoiceNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextInvoiceNo = NextInvoiceNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextInvoiceNo ;
ENDBEGIN
  DECLARE nNextInvoiceNo INTEGER;
  SELECT NextInvoiceNo INTO nNextInvoiceNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextInvoiceNo = NextInvoiceNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextInvoiceNo ;
ENDX�H�K�
�
��V���R���i��9i��9(���sysps_setup_disable_instrumentps_setup_disable_instrumentutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_pattern VARCHAR(128)
    BEGIN
    UPDATE performance_schema.setup_instruments
       SET enabled = 'NO', timed = 'NO'
     WHERE name LIKE CONCAT('%', in_pattern, '%');
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Disables instruments within Performance Schema
             matching the input pattern.
             Parameters
             in_pattern (VARCHAR(128)):
               A LIKE pattern match (using "%in_pattern%") of events to disable
             Example
             To disable all mutex instruments:
             mysql> CALL sys.ps_setup_disable_instrument('wait/synch/mutex');
             +--------------------------+
             | summary                  |
             +--------------------------+
             | Disabled 155 instruments |
             +--------------------------+
             1 row in set (0.02 sec)
             To disable just a specific TCP/IP based network IO instrument:
             mysql> CALL sys.ps_setup_disable_instrument('wait/io/socket/sql/server_tcpip_socket');
             +------------------------+
             | summary                |
             +------------------------+
             | Disabled 1 instruments |
             +------------------------+
             1 row in set (0.00 sec)
             To disable all instruments:
             mysql> CALL sys.ps_setup_disable_instrument('');
             +--------------------------+
             | summary                  |
             +--------------------------+
             | Disabled 547 instruments |
             +--------------------------+
             1 row in set (0.01 sec)
            BEGIN
    UPDATE performance_schema.setup_instruments
       SET enabled = 'NO', timed = 'NO'
     WHERE name LIKE CONCAT('%', in_pattern, '%');
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
END  +--------------------------+
             1 row in set (0.02 sec)
             To disable just the event_stage consumers:
             mysql> CALL sys.ps_setup_disable_comsumers('stage');
             +------------------------+
             | summary                |
             +------------------------+
             | Disabled 3 consumers   |
             +------------------------+
             1 row in set (0.00 sec)
            BEGIN
    UPDATE performance_schema.setup_consumers
       SET enabled = 'NO'
     WHERE name LIKE CONCAT('%', consumer, '%');
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END�b�ܣb�ܣ(���sysps_setup_disable_instrumentps_setup_disable_instrumentutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_pattern VARCHAR(128)
    BEGIN
    UPDATE performance_schema.setup_instruments
       SET enabled = 'NO', timed = 'NO'
     WHERE name LIKE CONCAT('%', in_pattern, '%');
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Disables instruments within Performance Schema
             matching the input pattern.
             Parameters
             in_pattern (VARCHAR(128)):
               A LIKE pattern match (using "%in_pattern%") of events to disable
             Example
             To disable all mutex instruments:
             mysql> CALL sys.ps_setup_disable_instrument('wait/synch/mutex');
             +--------------------------+
             | summary                  |
             +--------------------------+
             | Disabled 155 instruments |
             +--------------------------+
             1 row in set (0.02 sec)
             To disable just a specific TCP/IP based network IO instrument:
             mysql> CALL sys.ps_setup_disable_instrument('wait/io/socket/sql/server_tcpip_socket');
             +------------------------+
             | summary                |
             +------------------------+
             | Disabled 1 instruments |
             +------------------------+
             1 row in set (0.00 sec)
             To disable all instruments:
             mysql> CALL sys.ps_setup_disable_instrument('');
             +--------------------------+
             | summary                  |
             +--------------------------+
             | Disabled 547 instruments |
             +--------------------------+
             1 row in set (0.01 sec)
            BEGIN
    UPDATE performance_schema.setup_instruments
       SET enabled = 'NO', timed = 'NO'
     WHERE name LIKE CONCAT('%', in_pattern, '%');
    SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
END�b��b��
�� jms_demoGetNextOrderNoGetNextOrderNolatin1latin1_swedish_cilatin1_swedish_cijms_demo@%nBranchID INTEGER(11)
    int(11)BEGIN
  DECLARE nNextOrderNo INTEGER;
  SELECT NextOrderNo INTO nNextOrderNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextOrderNo = NextOrderNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextOrderNo;
ENDBEGIN
  DECLARE nNextOrderNo INTEGER;
  SELECT NextOrderNo INTO nNextOrderNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextOrderNo = NextOrderNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextOrderNo;
END�b��b���� jms_demo_masterfuncDaysToCompletefuncDaysToCompletelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nDaysToComplete INT(12) DEFAULT 0;
    SELECT (TO_DAYS(DateTimeRepairComplete)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDaysToComplete FROM jobs WHERE JobID=sJobID;
    RETURN nDaysToComplete;
ENDBEGIN
    DECLARE nDaysToComplete INT(12) DEFAULT 0;
    SELECT (TO_DAYS(DateTimeRepairComplete)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDaysToComplete FROM jobs WHERE JobID=sJobID;
    RETURN nDaysToComplete;
END�b��b���� jms_demo_masterfuncGetBranchNameByBranchIDfuncGetBranchNameByBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%nBranchID INT(12)
    varchar(200) CHARSET latin1BEGIN
DECLARE sBranchName VARCHAR(200) DEFAULT "UNKNOWN";
SELECT BranchName INTO sBranchName FROM branches WHERE BranchID=nBranchID;
RETURN sBranchName;
ENDBEGIN
DECLARE sBranchName VARCHAR(200) DEFAULT "UNKNOWN";
SELECT BranchName INTO sBranchName FROM branches WHERE BranchID=nBranchID;
RETURN sBranchName;
END�b��b���� jms_demo_masterfuncGetContractValuefuncGetContractValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    doubleBEGIN
DECLARE nResult DOUBLE DEFAULT 0.00;
SELECT SellPrice INTO nResult FROM jobs WHERE InvoiceNet=0 AND JobID=sJobID AND CustomerID IN (1709,2150,1270,2135,678,336,1897);
RETURN nResult;
ENDBEGIN
DECLARE nResult DOUBLE DEFAULT 0.00;
SELECT SellPrice INTO nResult FROM jobs WHERE InvoiceNet=0 AND JobID=sJobID AND CustomerID IN (1709,2150,1270,2135,678,336,1897);
RETURN nResult;
END�b��b���� jms_demo_masterfuncGetCustomerNamefuncGetCustomerNamelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    varchar(200) CHARSET latin1BEGIN
	DECLARE sCustomerName VARCHAR(200) DEFAULT "UNKNOWN";
	SELECT customers.CustomerName INTO sCustomerName FROM customers JOIN jobs ON customers.CustomerID=jobs.CustomerID WHERE jobs.JobID=sJobID;
	RETURN sCustomerName;
ENDBEGIN
	DECLARE sCustomerName VARCHAR(200) DEFAULT "UNKNOWN";
	SELECT customers.CustomerName INTO sCustomerName FROM customers JOIN jobs ON customers.CustomerID=jobs.CustomerID WHERE jobs.JobID=sJobID;
	RETURN sCustomerName;
END��L;4�\nja���*���]R�0�i��9i��9(���sysps_setup_enable_threadps_setup_enable_threadutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_connection_id BIGINT
    BEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'YES'
     WHERE processlist_id = in_connection_id;
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' thread', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Enable the given connection/thread in Performance Schema.
             Parameters
             in_connection_id (BIGINT):
               The connection ID (PROCESSLIST_ID from performance_schema.threads
               or the ID shown within SHOW PROCESSLIST)
             Example
             mysql> CALL sys.ps_setup_enable_thread(3);
             +------------------+
             | summary          |
             +------------------+
             | Enabled 1 thread |
             +------------------+
             1 row in set (0.01 sec)
             To enable the current connection:
             mysql> CALL sys.ps_setup_enable_thread(CONNECTION_ID());
             +------------------+
             | summary          |
             +------------------+
             | Enabled 1 thread |
             +------------------+
             1 row in set (0.00 sec)
            BEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'YES'
     WHERE processlist_id = in_connection_id;
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' thread', IF(@rows != 1, 's', '')) AS summary;
ENDsec)
             Query OK, 0 rows affected (0.00 sec)
             To enable all instruments:
             mysql> CALL sys.ps_setup_enable_instrument('');
             +-------------------------+
             | summary                 |
             +-------------------------+
             | Enabled 547 instruments |
             +-------------------------+
             1 row in set (0.01 sec)
             Query OK, 0 rows affected (0.01 sec)
            BEGIN
    UPDATE performance_schema.setup_instruments
       SET enabled = 'YES', timed = 'YES'
     WHERE name LIKE CONCAT('%', in_pattern, '%');
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
ENDsummary                       |
             +-------------------------------+
             | Enabled 18 background threads |
             +-------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    UPDATE performance_schema.threads
       SET instrumented = 'YES'
     WHERE type = 'BACKGROUND';
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' background thread', IF(@rows != 1, 's', '')) AS summary;
END�b�ܣb�ܣ&�:�sysps_setup_enable_consumerps_setup_enable_consumerutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN consumer VARCHAR(128)
    BEGIN
    UPDATE performance_schema.setup_consumers
       SET enabled = 'YES'
     WHERE name LIKE CONCAT('%', consumer, '%');
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Enables consumers within Performance Schema
             matching the input pattern.
             Parameters
             consumer (VARCHAR(128)):
               A LIKE pattern match (using "%consumer%") of consumers to enable
             Example
             To enable all consumers:
             mysql> CALL sys.ps_setup_enable_consumer('');
             +-------------------------+
             | summary                 |
             +-------------------------+
             | Enabled 10 consumers    |
             +-------------------------+
             1 row in set (0.02 sec)
             Query OK, 0 rows affected (0.02 sec)
             To enable just "waits" consumers:
             mysql> CALL sys.ps_setup_enable_consumer('waits');
             +-----------------------+
             | summary               |
             +-----------------------+
             | Enabled 3 consumers   |
             +-----------------------+
             1 row in set (0.00 sec)
             Query OK, 0 rows affected (0.00 sec)
             BEGIN
    UPDATE performance_schema.setup_consumers
       SET enabled = 'YES'
     WHERE name LIKE CONCAT('%', consumer, '%');
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END�b�ܣb�ܣ(��sysps_setup_enable_instrumentps_setup_enable_instrumentutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_pattern VARCHAR(128)
    BEGIN
    UPDATE perform�i��9i��9(��sysps_setup_enable_instrumentps_setup_enable_instrumentutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_pattern VARCHAR(128)
    BEGIN
    UPDATE performance_schema.setup_instruments
       SET enabled = 'YES', timed = 'YES'
     WHERE name LIKE CONCAT('%', in_pattern, '%');
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
END
             Description
             Enables instruments within Performance Schema
             matching the input pattern.
             Parameters
             in_pattern (VARCHAR(128)):
               A LIKE pattern match (using "%in_pattern%") of events to enable
             Example
             To enable all mutex instruments:
             mysql> CALL sys.ps_setup_enable_instrument('wait/synch/mutex');
             +-------------------------+
             | summary                 |
             +-------------------------+
             | Enabled 155 instruments |
             +-------------------------+
             1 row in set (0.02 sec)
             Query OK, 0 rows affected (0.02 sec)
             To enable just a specific TCP/IP based network IO instrument:
             mysql> CALL sys.ps_setup_enable_instrument('wait/io/socket/sql/server_tcpip_socket');
             +-----------------------+
             | summary               |
             +-----------------------+
             | Enabled 1 instruments |
             +-----------------------+
             1 row in set (0.00 sec)
             Query OK, 0 rows affected (0.00 sec)
             To enable all instruments:
             mysql> CALL sys.ps_setup_enable_instrument('');
             +-------------------------+
             | summary                 |
             +-------------------------+
             | Enabled 547 instruments |
             +-------------------------+
             1 row in set (0.01 sec)
             Query OK, 0 rows affected (0.01 sec)
            BEGIN
    UPDATE performance_schema.setup_instruments
       SET enabled = 'YES', timed = 'YES'
     WHERE name LIKE CONCAT('%', in_pattern, '%');
    SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
END�b��b���� jms_demo_masterfuncGetInvoiceValuefuncGetInvoiceValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    doubleBEGIN
DECLARE nInvoiceValue DOUBLE DEFAULT 0.00;
SELECT (NumberOff*QuotedPrice) INTO nInvoiceValue FROM jobs WHERE JobID=sJobID;
RETURN nInvoiceValue;
ENDBEGIN
DECLARE nInvoiceValue DOUBLE DEFAULT 0.00;
SELECT (NumberOff*QuotedPrice) INTO nInvoiceValue FROM jobs WHERE JobID=sJobID;
RETURN nInvoiceValue;
END�b��b�� jms_demo_masterfuncGetJobTypefuncGetJobTypelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    varchar(50) CHARSET latin1BEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE sJobType VARCHAR(50) DEFAULT "Job Type not set!";
SELECT JobTypeID INTO nJobTypeID FROM jobs WHERE JobID=sJobID;
SELECT JobType INTO sJobType FROM job_types WHERE JobTypeID=nJobTypeID;
RETURN sJobType;
ENDBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE sJobType VARCHAR(50) DEFAULT "Job Type not set!";
SELECT JobTypeID INTO nJobTypeID FROM jobs WHERE JobID=sJobID;
SELECT JobType INTO sJobType FROM job_types WHERE JobTypeID=nJobTypeID;
RETURN sJobType;
END�2�R�������S�	�i��9i��9

���sysps_setup_saveps_setup_saveutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_timeout INT
    BEGIN
    DECLARE v_lock_result INT;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT GET_LOCK('sys.ps_setup_save', in_timeout) INTO v_lock_result;
    IF v_lock_result THEN
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_actors;
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_consumers;
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_instruments;
        DROP TEMPORARY TABLE IF EXISTS tmp_threads;
        CREATE TEMPORARY TABLE tmp_setup_actors LIKE performance_schema.setup_actors;
        CREATE TEMPORARY TABLE tmp_setup_consumers LIKE performance_schema.setup_consumers;
        CREATE TEMPORARY TABLE tmp_setup_instruments LIKE performance_schema.setup_instruments;
        CREATE TEMPORARY TABLE tmp_threads (THREAD_ID bigint unsigned NOT NULL PRIMARY KEY, INSTRUMENTED enum('YES','NO') NOT NULL);
        INSERT INTO tmp_setup_actors SELECT * FROM performance_schema.setup_actors;
        INSERT INTO tmp_setup_consumers SELECT * FROM performance_schema.setup_consumers;
        INSERT INTO tmp_setup_instruments SELECT * FROM performance_schema.setup_instruments;
        INSERT INTO tmp_threads SELECT THREAD_ID, INSTRUMENTED FROM performance_schema.threads;
    ELSE
        SIGNAL SQLSTATE VALUE '90000'
           SET MESSAGE_TEXT = 'Could not lock the sys.ps_setup_save user lock, another thread has a saved configuration';
    END IF;
    SET sql_log_bin = @log_bin;
END
             Description
             Saves the current configuration of Performance Schema,
             so that you can alter the setup for debugging purposes,
             but restore it to a previous state.
             Use the companion procedure - ps_setup_reload_saved(), to
             restore the saved config.
             The named lock "sys.ps_setup_save" is taken before the
             current configuration is saved. If the attempt to get the named
             lock times out, an error occurs.
             The lock is released after the settings have been restored by
             calling ps_setup_reload_saved().
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Parameters
             in_timeout INT
               The timeout in seconds used when trying to obtain the lock.
               A negative timeout means infinite timeout.
             Example
             mysql> CALL sys.ps_setup_save(-1);
             Query OK, 0 rows affected (0.08 sec)
             mysql> UPDATE performance_schema.setup_instruments
                 ->    SET enabled = 'YES', timed = 'YES';
             Query OK, 547 rows affected (0.40 sec)
             Rows matched: 784  Changed: 547  Warnings: 0
             /* Run some tests that need more detailed instrumentation here */
             mysql> CALL sys.ps_setup_reload_saved();
             Query OK, 0 rows affected (0.32 sec)
            BEGIN
    DECLARE v_lock_result INT;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT GET_LOCK('sys.ps_setup_save', in_timeout) INTO v_lock_result;
    IF v_lock_result THEN
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_actors;
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_consumers;
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_instruments;
        DROP TEMPORARY TABLE IF EXISTS tmp_threads;
        CREATE TEMPORARY TABLE tmp_setup_actors LIKE performance_schema.setup_actors;
        CREATE TEMPORARY TABLE tmp_setup_consumers LIKE performance_schema.setup_consumers;
        CREATE TEMPORARY TABLE tmp_setup_instruments LIKE performance_schema.setup_instruments;
        CREATE TEMPORARY TABLE tmp_threads (THREAD_ID bigint unsigned NOT NULL PRIMARY KEY, INSTRUMENTED enum('YES','NO') NOT NULL);
        INSERT INTO tmp_setup_actors SELECT * FROM performance_schema.setup_actors;
        INSERT INTO tmp_setup_consumers SELECT * FROM performance_schema.setup_consumers;
        INSERT INTO tmp_setup_instruments SELECT * FROM performance_schema.setup_instruments;
        INSERT INTO tmp_threads SELECT THREAD_ID, INSTRUMENTED FROM performance_schema.threads;
    ELSE
        SIGNAL SQLSTATE VALUE '90000'
           SET MESSAGE_TEXT = 'Could not lock the sys.ps_setup_save user lock, another thread has a saved configuration';
    END IF;
    SET sql_log_bin = @log_bin;
ENDled instrumentation here */
             mysql> CALL sys.ps_setup_reload_saved();
             Query OK, 0 rows affected (0.32 sec)
            BEGIN
    DECLARE v_lock_result INT;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT GET_LOCK('sys.ps_setup_save', in_timeout) INTO v_lock_result;
    IF v_lock_result THEN
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_actors;
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_consumers;
        DROP TEMPORARY TABLE IF EXISTS tmp_setup_instruments;
        DROP TEMPORARY TABLE IF EXISTS tmp_threads;
        CREATE TEMPORARY TABLE tmp_setup_actors LIKE performance_schema.setup_actors;
        CREATE TEMPORARY TABLE tmp_setup_consumers LIKE performance_schema.setup_consumers;
        CREATE TEMPORARY TABLE tmp_setup_instruments LIKE performance_schema.setup_instruments;
        CREATE TEMPORARY TABLE tmp_threads (THREAD_ID bigint unsigned NOT NULL PRIMARY KEY, INSTRUMENTED enum('YES','NO') NOT NULL);
        INSERT INTO tmp_setup_actors SELECT * FROM performance_schema.setup_actors;
        INSERT INTO tmp_setup_consumers SELECT * FROM performance_schema.setup_consumers;
        INSERT INTO tmp_setup_instruments SELECT * FROM performance_schema.setup_instruments;
        INSERT INTO tmp_threads SELECT THREAD_ID, INSTRUMENTED FROM performance_schema.threads;
    ELSE
        SIGNAL SQLSTATE VALUE '90000'
           SET MESSAGE_TEXT = 'Could not lock the sys.ps_setup_save user lock, another thread has a saved configuration';
    END IF;
    SET sql_log_bin = @log_bin;
END�b��b���� jms_demo_masterfuncGetMaterialCostfuncGetMaterialCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�b��b��

 jms_demo_masterfuncGetMaterialCostNewfuncGetMaterialCostNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  # Serial: 2008-12-19-01

  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID;  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  # Serial: 2008-12-19-01

  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID;  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�m�K��-h����S�u
�i��9i��9  �Y�sysps_setup_show_disabled_consumersps_setup_show_disabled_consumersutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostBEGIN
    SELECT name AS disabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'NO'
     ORDER BY disabled_consumers;
END
             Description
             Shows all currently disabled consumers.
             Parameters
             None
             Example
             mysql> CALL sys.ps_setup_show_disabled_consumers();
             +---------------------------+
             | disabled_consumers        |
             +---------------------------+
             | events_statements_current |
             | global_instrumentation    |
             | thread_instrumentation    |
             | statements_digest         |
             +---------------------------+
             4 rows in set (0.05 sec)
            BEGIN
    SELECT name AS disabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'NO'
     ORDER BY disabled_consumers;
END�b��b���� jms_demo_masterfuncGetRepairEngineerfuncGetRepairEngineerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    varchar(100) CHARSET latin1BEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
END�i��:i��:#$�$sysps_truncate_all_tablesps_truncate_all_tablesutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_verbose BOOLEAN
    BEGIN
    DECLARE v_done INT DEFAULT FALSE;
    DECLARE v_total_tables INT DEFAULT 0;
    DECLARE v_ps_table VARCHAR(64);
    DECLARE ps_tables CURSOR FOR
        SELECT table_name
          FROM INFORMATION_SCHEMA.TABLES
         WHERE table_schema = 'performance_schema'
           AND (table_name LIKE '%summary%'
            OR table_name LIKE '%history%');
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    OPEN ps_tables;
    ps_tables_loop: LOOP
        FETCH ps_tables INTO v_ps_table;
        IF v_done THEN
          LEAVE ps_tables_loop;
        END IF;
        SET @truncate_stmt := CONCAT('TRUNCATE TABLE performance_schema.', v_ps_table);
        IF in_verbose THEN
            SELECT CONCAT('Running: ', @truncate_stmt) AS status;
        END IF;
        PREPARE truncate_stmt FROM @truncate_stmt;
        EXECUTE truncate_stmt;
        DEALLOCATE PREPARE truncate_stmt;
        SET v_total_tables = v_total_tables + 1;
    END LOOP;
    CLOSE ps_tables;
    SELECT CONCAT('Truncated ', v_total_tables, ' tables') AS summary;
END
             Description
             Truncates all summary tables within Performance Schema,
             resetting all aggregated instrumentation as a snapshot.
             Parameters
             in_verbose (BOOLEAN):
               Whether to print each TRUNCATE statement before running
             Example
             mysql> CALL sys.ps_truncate_all_tables(false);
             +---------------------+
             | summary             |
             +---------------------+
             | Truncated 44 tables |
             +---------------------+
             1 row in set (0.10 sec)
             Query OK, 0 rows affected (0.10 sec)
            BEGIN
    DECLARE v_done INT DEFAULT FALSE;
    DECLARE v_total_tables INT DEFAULT 0;
    DECLARE v_ps_table VARCHAR(64);
    DECLARE ps_tables CURSOR FOR
        SELECT table_name
          FROM INFORMATION_SCHEMA.TABLES
         WHERE table_schema = 'performance_schema'
           AND (table_name LIKE '%summary%'
            OR table_name LIKE '%history%');
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    OPEN ps_tables;
    ps_tables_loop: LOOP
        FETCH ps_tables INTO v_ps_table;
        IF v_done THEN
          LEAVE ps_tables_loop;
        END IF;
        SET @truncate_stmt := CONCAT('TRUNCATE TABLE performance_schema.', v_ps_table);
        IF in_verbose THEN
            SELECT CONCAT('Running: ', @truncate_stmt) AS status;
        END IF;
        PREPARE truncate_stmt FROM @truncate_stmt;
        EXECUTE truncate_stmt;
        DEALLOCATE PREPARE truncate_stmt;
        SET v_total_tables = v_total_tables + 1;
    END LOOP;
    CLOSE ps_tables;
    SELECT CONCAT('Truncated ', v_total_tables, ' tables') AS summary;
END', v_lock_used_by);
        SIGNAL SQLSTATE VALUE '90002'
           SET MESSAGE_TEXT = v_signal_message;
    END IF;
    DELETE FROM performance_schema.setup_actors;
    INSERT INTO performance_schema.setup_actors SELECT * FROM tmp_setup_actors;
    BEGIN
        DECLARE v_name varchar(64);
        DECLARE v_enabled enum('YES', 'NO');
        DECLARE c_consumers CURSOR FOR SELECT NAME, ENABLED FROM tmp_setup_consumers;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
        SET v_done = FALSE;
        OPEN c_consumers;
        c_consumers_loop: LOOP
            FETCH c_consumers INTO v_name, v_enabled;
            IF v_done THEN
               LEAVE c_consumers_loop;
            END IF;
            UPDATE performance_schema.setup_consumers
               SET ENABLED = v_enabled
             WHERE NAME = v_name;
         END LOOP;
         CLOSE c_consumers;
    END;
    UPDATE performance_schema.setup_instruments
     INNER JOIN tmp_setup_instruments USING (NAME)
       SET performance_schema.setup_instruments.ENABLED = tmp_setup_instruments.ENABLED,
           performance_schema.setup_instruments.TIMED   = tmp_setup_instruments.TIMED;
    BEGIN
        DECLARE v_thread_id bigint unsigned;
        DECLARE v_instrumented enum('YES', 'NO');
        DECLARE c_threads CURSOR FOR SELECT THREAD_ID, INSTRUMENTED FROM tmp_threads;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
        SET v_done = FALSE;
        OPEN c_threads;
        c_threads_loop: LOOP
            FETCH c_threads INTO v_thread_id, v_instrumented;
            IF v_done THEN
               LEAVE c_threads_loop;
            END IF;
            UPDATE performance_schema.threads
               SET INSTRUMENTED = v_instrumented
             WHERE THREAD_ID = v_thread_id;
        END LOOP;
        CLOSE c_threads;
    END;
    UPDATE performance_schema.threads
       SET INSTRUMENTED = IF(PROCESSLIST_USER IS NOT NULL,
                             sys.ps_is_account_enabled(PROCESSLIST_HOST, PROCESSLIST_USER),
                             'YES')
     WHERE THREAD_ID NOT IN (SELECT THREAD_ID FROM tmp_threads);
    DROP TEMPORARY TABLE tmp_setup_actors;
    DROP TEMPORARY TABLE tmp_setup_consumers;
    DROP TEMPORARY TABLE tmp_setup_instruments;
    DROP TEMPORARY TABLE tmp_threads;
    SELECT RELEASE_LOCK('sys.ps_setup_save') INTO v_lock_result;
    SET sql_log_bin = @log_bin;
END�b��b���� jms_demo_masterfuncGetRepairEngineerfuncGetRepairEngineerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    varchar(100) CHARSET latin1BEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
END��?�3���b�U��u�iƆ�iƆ���� TcastironradcenAddStockAuditRecordAddStockAuditRecordutf8mb4utf8mb4_general_cilatin1_swedish_cicastironradcen@%
	IN `nOldNumInStock` INTEGER(11),
	IN `nNewNumInStock` INTEGER(11),
	IN `sAuditType` VARCHAR(6),
	IN `nProductID` INTEGER(11),
	IN `nProductOptionValueID` INTEGER(11),
	IN `bForceAdd` TINYINT(1)
BEGIN
  -- (SS,04/07/2013) to record changes to stock in tables products and product_option_values number to stock_audit table
  -- (SS,02/06/2022) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  -- (SS,17/03/2026) added SourceType, SourceRef, AuditNote tracking
  
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        -- lookup audit record using connection ID where user is still logged on
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
     
     SET @LocalSourceType = @AuditSourceType;
     SET @LocalSourceRef = @AuditSourceRef;
     SET @LocalNote = @AuditNote;
  
     INSERT INTO stock_audit (
         ProductID, AuditType, StockBefore, StockAfter, 
         UserName, PCName, ConnectionID, ProductOptionValueID,
         SourceType, SourceRef, AuditNote
     ) 
     VALUES (
         nProductID, sAuditType, nOldNumInStock, nNewNumInStock, 
         SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID,
         @LocalSourceType, @LocalSourceRef, @LocalNote
     );
     
  END IF; 
ENDBEGIN
  -- (SS,04/07/2013) to record changes to stock in tables products and product_option_values number to stock_audit table
  -- (SS,02/06/2022) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  -- (SS,17/03/2026) added SourceType, SourceRef, AuditNote tracking
  
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        -- lookup audit record using connection ID where user is still logged on
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
     
     SET @LocalSourceType = @AuditSourceType;
     SET @LocalSourceRef = @AuditSourceRef;
     SET @LocalNote = @AuditNote;
  
     INSERT INTO stock_audit (
         ProductID, AuditType, StockBefore, StockAfter, 
         UserName, PCName, ConnectionID, ProductOptionValueID,
         SourceType, SourceRef, AuditNote
     ) 
     VALUES (
         nProductID, sAuditType, nOldNumInStock, nNewNumInStock, 
         SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID,
         @LocalSourceType, @LocalSourceRef, @LocalNote
     );
     
  END IF; 
ENDNO      | NO    |
             | TRIGGER     | mysql.%              | NO      | NO    |
             | TRIGGER     | performance_schema.% | NO      | NO    |
             | TRIGGER     | information_schema.% | NO      | NO    |
             +-------------+----------------------+---------+-------+
             15 rows in set (0.00 sec)
             +----------------------------------+
             | disabled_consumers               |
             +----------------------------------+
             | events_stages_current            |
             | events_stages_history            |
             | events_stages_history_long       |
             | events_statements_history        |
             | events_statements_history_long   |
             | events_transactions_history      |
             | events_transactions_history_long |
             | events_waits_current             |
             | events_waits_history             |
             | events_waits_history_long        |
             +----------------------------------+
             10 rows in set (0.00 sec)
             Empty set (0.00 sec)
             +---------------------------------------------------------------------------------------+-------+
             | disabled_instruments                                                                  | timed |
             +---------------------------------------------------------------------------------------+-------+
             | wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_tc                                             | NO    |
             | wait/synch/mutex/sql/LOCK_des_key_file                                                | NO    |
             | wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit                                       | NO    |
             ...
             | memory/sql/servers_cache                                                              | NO    |
             | memory/sql/udf_mem                                                                    | NO    |
             | wait/lock/metadata/sql/mdl                                                            | NO    |
             +---------------------------------------------------------------------------------------+-------+
             547 rows in set (0.00 sec)
             Query OK, 0 rows affected (0.01 sec)
            BEGIN
    SELECT @@performance_schema AS performance_schema_enabled;
    
    SELECT object_type,
           CONCAT(object_schema, '.', object_name) AS objects,
           enabled,
           timed
      FROM performance_schema.setup_objects
     WHERE enabled = 'NO'
     ORDER BY object_type, objects;
    SELECT name AS disabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'NO'
     ORDER BY disabled_consumers;
    IF (in_show_threads) THEN
        SELECT IF(name = 'thread/sql/one_connection',
                  CONCAT(processlist_user, '@', processlist_host),
                  REPLACE(name, 'thread/', '')) AS disabled_threads,
        TYPE AS thread_type
          FROM performance_schema.threads
         WHERE INSTRUMENTED = 'NO'
         ORDER BY disabled_threads;
    END IF;
    IF (in_show_instruments) THEN
        SELECT name AS disabled_instruments,
               timed
          FROM performance_schema.setup_instruments
         WHERE enabled = 'NO'
         ORDER BY disabled_instruments;
    END IF;
END�b��b���� jms_demo_masterfuncIsJobQuotationfuncIsJobQuotationlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID INT(12)
    int(12)BEGIN
DECLARE nResult INT(12) DEFAULT 0;
SELECT NumberOff INTO nResult FROM jobs JOIN job_types ON jobs.JobTypeID=job_types.JobTypeID WHERE JobID=sJobID AND job_types.JobType="Quotation";
RETURN nResult;
ENDBEGIN
DECLARE nResult INT(12) DEFAULT 0;
SELECT NumberOff INTO nResult FROM jobs JOIN job_types ON jobs.JobTypeID=job_types.JobTypeID WHERE JobID=sJobID AND job_types.JobType="Quotation";
RETURN nResult;
ENDoU
�x� aV��	�i�Gi�G��� TvintagedoorknobsAddStockAuditRecordAddStockAuditRecordutf8mb4utf8mb4_general_cilatin1_swedish_civintagedoorknobs@%
	IN `nOldNumInStock` INTEGER(11),
	IN `nNewNumInStock` INTEGER(11),
	IN `sAuditType` VARCHAR(6),
	IN `nProductID` INTEGER(11),
	IN `nProductOptionValueID` INTEGER(11),
	IN `bForceAdd` TINYINT(1)
BEGIN
  -- (SS,04/07/2013) to record changes to stock in tables products and product_option_values number to stock_audit table
  -- (SS,02/06/2022) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  -- (SS,17/03/2026) added SourceType, SourceRef, AuditNote tracking
  
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        -- lookup audit record using connection ID where user is still logged on
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
     
     SET @LocalSourceType = @AuditSourceType;
     SET @LocalSourceRef = @AuditSourceRef;
     SET @LocalNote = @AuditNote;
  
     INSERT INTO stock_audit (
         ProductID, AuditType, StockBefore, StockAfter, 
         UserName, PCName, ConnectionID, ProductOptionValueID,
         SourceType, SourceRef, AuditNote
     ) 
     VALUES (
         nProductID, sAuditType, nOldNumInStock, nNewNumInStock, 
         SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID,
         @LocalSourceType, @LocalSourceRef, @LocalNote
     );
     
  END IF; 
ENDBEGIN
  -- (SS,04/07/2013) to record changes to stock in tables products and product_option_values number to stock_audit table
  -- (SS,02/06/2022) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  -- (SS,17/03/2026) added SourceType, SourceRef, AuditNote tracking
  
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        -- lookup audit record using connection ID where user is still logged on
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
     
     SET @LocalSourceType = @AuditSourceType;
     SET @LocalSourceRef = @AuditSourceRef;
     SET @LocalNote = @AuditNote;
  
     INSERT INTO stock_audit (
         ProductID, AuditType, StockBefore, StockAfter, 
         UserName, PCName, ConnectionID, ProductOptionValueID,
         SourceType, SourceRef, AuditNote
     ) 
     VALUES (
         nProductID, sAuditType, nOldNumInStock, nNewNumInStock, 
         SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID,
         @LocalSourceType, @LocalSourceRef, @LocalNote
     );
     
  END IF; 
END ' tables') AS summary;
END
             Description
             Truncates all summary tables within Performance Schema,
             resetting all aggregated instrumentation as a snapshot.
             Parameters
             in_verbose (BOOLEAN):
               Whether to print each TRUNCATE statement before running
             Example
             mysql> CALL sys.ps_truncate_all_tables(false);
             +---------------------+
             | summary             |
             +---------------------+
             | Truncated 44 tables |
             +---------------------+
             1 row in set (0.10 sec)
             Query OK, 0 rows affected (0.10 sec)
            BEGIN
    DECLARE v_done INT DEFAULT FALSE;
    DECLARE v_total_tables INT DEFAULT 0;
    DECLARE v_ps_table VARCHAR(64);
    DECLARE ps_tables CURSOR FOR
        SELECT table_name
          FROM INFORMATION_SCHEMA.TABLES
         WHERE table_schema = 'performance_schema'
           AND (table_name LIKE '%summary%'
            OR table_name LIKE '%history%');
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    OPEN ps_tables;
    ps_tables_loop: LOOP
        FETCH ps_tables INTO v_ps_table;
        IF v_done THEN
          LEAVE ps_tables_loop;
        END IF;
        SET @truncate_stmt := CONCAT('TRUNCATE TABLE performance_schema.', v_ps_table);
        IF in_verbose THEN
            SELECT CONCAT('Running: ', @truncate_stmt) AS status;
        END IF;
        PREPARE truncate_stmt FROM @truncate_stmt;
        EXECUTE truncate_stmt;
        DEALLOCATE PREPARE truncate_stmt;
        SET v_total_tables = v_total_tables + 1;
    END LOOP;
    CLOSE ps_tables;
    SELECT CONCAT('Truncated ', v_total_tables, ' tables') AS summary;
END�b��b���� jms_demo_masterfuncJobBecomesWarrantyItemCountfuncJobBecomesWarrantyItemCountlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nResult INT(12);
    SELECT SUM(NumberOff) INTO nResult FROM jobs WHERE CWJobID=sJobID;
    IF nResult IS NULL THEN
        SET nResult=0;
    END IF;
    RETURN nResult;
ENDBEGIN
    DECLARE nResult INT(12);
    SELECT SUM(NumberOff) INTO nResult FROM jobs WHERE CWJobID=sJobID;
    IF nResult IS NULL THEN
        SET nResult=0;
    END IF;
    RETURN nResult;
END�b��b���� jms_demo_masterGetNextInvoiceNoGetNextInvoiceNolatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%nBranchID INTEGER(11)
    int(11)BEGIN
  DECLARE nNextInvoiceNo INTEGER;
  SELECT NextInvoiceNo INTO nNextInvoiceNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextInvoiceNo = NextInvoiceNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextInvoiceNo ;
ENDBEGIN
  DECLARE nNextInvoiceNo INTEGER;
  SELECT NextInvoiceNo INTO nNextInvoiceNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextInvoiceNo = NextInvoiceNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextInvoiceNo ;
END�b��b���� jms_demo_masterGetNextOrderNoGetNextOrderNolatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%nBranchID INTEGER(11)
    int(11)BEGIN
  DECLARE nNextOrderNo INTEGER;
  SELECT NextOrderNo INTO nNextOrderNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextOrderNo = NextOrderNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextOrderNo;
ENDBEGIN
  DECLARE nNextOrderNo INTEGER;
  SELECT NextOrderNo INTO nNextOrderNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextOrderNo = NextOrderNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextOrderNo;
END�b��b��

!�� Pjms_demo_masterAddToEventLogAddToEventLoglatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%
 IN sEventMessage MEDIUMTEXT
 BEGIN
 # (SS,21/8/13) to add message to event_log table
 INSERT INTO event_log (EventDateTime, EventMessage) VALUES(NOW(), sEventMessage);
ENDBEGIN
 # (SS,21/8/13) to add message to event_log table
 INSERT INTO event_log (EventDateTime, EventMessage) VALUES(NOW(), sEventMessage);
END`��xR�R"
�������ZS���b�ܣb�ܣP�0�sysps_setup_show_enabledps_setup_show_enabledutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhost
        IN in_show_instruments BOOLEAN,
        IN in_show_threads BOOLEAN
    BEGIN
    SELECT @@performance_schema AS performance_schema_enabled;
    SELECT CONCAT('\'', user, '\'@\'', host, '\'') AS enabled_users
      FROM performance_schema.setup_actors
      WHERE enabled = 'YES'
     ORDER BY enabled_users;
    SELECT object_type,
           CONCAT(object_schema, '.', object_name) AS objects,
           enabled,
           timed
      FROM performance_schema.setup_objects
     WHERE enabled = 'YES'
     ORDER BY object_type, objects;
    SELECT name AS enabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'YES'
     ORDER BY enabled_consumers;
    IF (in_show_threads) THEN
        SELECT IF(name = 'thread/sql/one_connection',
                  CONCAT(processlist_user, '@', processlist_host),
                  REPLACE(name, 'thread/', '')) AS enabled_threads,
        TYPE AS thread_type
          FROM performance_schema.threads
         WHERE INSTRUMENTED = 'YES' AND name <> 'thread/innodb/thread_pool_thread'
         ORDER BY enabled_threads;
    END IF;
    IF (in_show_instruments) THEN
        SELECT name AS enabled_instruments,
               timed
          FROM performance_schema.setup_instruments
         WHERE enabled = 'YES'
         ORDER BY enabled_instruments;
    END IF;
END
             Description
             Shows all currently enabled Performance Schema configuration.
             Parameters
             in_show_instruments (BOOLEAN):
               Whether to print enabled instruments (can print many items)
             in_show_threads (BOOLEAN):
               Whether to print enabled threads
             Example
             mysql> CALL sys.ps_setup_show_enabled(TRUE, TRUE);
             +----------------------------+
             | performance_schema_enabled |
             +----------------------------+
             |                          1 |
             +----------------------------+
             1 row in set (0.00 sec)
             +---------------+
             | enabled_users |
             +---------------+
             | '%'@'%'       |
             +---------------+
             1 row in set (0.01 sec)
             +-------------+---------+---------+-------+
             | object_type | objects | enabled | timed |
             +-------------+---------+---------+-------+
             | EVENT       | %.%     | YES     | YES   |
             | FUNCTION    | %.%     | YES     | YES   |
             | PROCEDURE   | %.%     | YES     | YES   |
             | TABLE       | %.%     | YES     | YES   |
             | TRIGGER     | %.%     | YES     | YES   |
             +-------------+---------+---------+-------+
             5 rows in set (0.01 sec)
             +---------------------------+
             | enabled_consumers         |
             +---------------------------+
             | events_statements_current |
             | global_instrumentation    |
             | thread_instrumentation    |
             | statements_digest         |
             +---------------------------+
             4 rows in set (0.05 sec)
             +---------------------------------+-------------+
             | enabled_threads                 | thread_type |
             +---------------------------------+-------------+
             | sql/main                        | BACKGROUND  |
             | sql/thread_timer_notifier       | BACKGROUND  |
             | innodb/io_ibuf_thread           | BACKGROUND  |
             | innodb/io_log_thread            | BACKGROUND  |
             | innodb/io_read_thread           | BACKGROUND  |
             | innodb/io_read_thread           | BACKGROUND  |
             | innodb/io_write_thread          | BACKGROUND  |
             | innodb/io_write_thread          | BACKGROUND  |
             | innodb/page_cleaner_thread      | BACKGROUND  |
             | innodb/srv_lock_timeout_thread  | BACKGROUND  |
             | innodb/srv_error_monitor_thread | BACKGROUND  |
             | innodb/srv_monitor_thread       | BACKGROUND  |
             | innodb/srv_master_thread        | BACKGROUND  |
             | innodb/srv_purge_thread         | BACKGROUND  |
             | innodb/srv_worker_thread        | BACKGROUND  |
             | innodb/srv_worker_thread        | BACKGROUND  |
             | innodb/srv_worker_thread        | BACKGROUND  |
             | innodb/buf_dump_thread          | BACKGROUND  |
             | innodb/dict_stats_thread        | BACKGROUND  |
             | sql/signal_handler              | BACKGROUND  |
             | sql/compress_gtid_table         | FOREGROUND  |
             | root@localhost                  | FOREGROUND  |
             +---------------------------------+-------------+
             22 rows in set (0.01 sec)
             +-------------------------------------+-------+
             | enabled_instruments                 | timed |
             +-------------------------------------+-------+
             | wait/io/file/sql/map                | YES   |
             | wait/io/file/sql/binlog             | YES   |
             ...
             | statement/com/Error                 | YES   |
             | statement/com/                      | YES   |
             | idle                                | YES   |
             +-------------------------------------+-------+
             210 rows in set (0.08 sec)
             Query OK, 0 rows affected (0.89 sec)
            BEGIN
    SELECT @@performance_schema AS performance_schema_enabled;
    SELECT CONCAT('''', user, '''@''', host, '''') AS enabled_users
      FROM performance_schema.setup_actors
      WHERE enabled = 'YES'
     ORDER BY enabled_users;
    SELECT object_type,
           CONCAT(object_schema, '.', object_name) AS objects,
           enabled,
           timed
      FROM performance_schema.setup_objects
     WHERE enabled = 'YES'
     ORDER BY object_type, objects;
    SELECT name AS enabled_consumers
      FROM performance_schema.setup_consumers
     WHERE enabled = 'YES'
     ORDER BY enabled_consumers;
    IF (in_show_threads) THEN
        SELECT IF(name = 'thread/sql/one_connection',
                  CONCAT(processlist_user, '@', processlist_host),
                  REPLACE(name, 'thread/', '')) AS enabled_threads,
        TYPE AS thread_type
          FROM performance_schema.threads
         WHERE INSTRUMENTED = 'YES' AND name <> 'thread/innodb/thread_pool_thread'
         ORDER BY enabled_threads;
    END IF;
    IF (in_show_instruments) THEN
        SELECT name AS enabled_instruments,
               timed
          FROM performance_schema.setup_instruments
         WHERE enabled = 'YES'
         ORDER BY enabled_instruments;
    END IF;
END�e��e���� jms_demo_epochfuncDaysToCompletefuncDaysToCompletelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nDaysToComplete INT(12) DEFAULT 0;
    SELECT (TO_DAYS(DateTimeRepairComplete)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDaysToComplete FROM jobs WHERE JobID=sJobID;
    RETURN nDaysToComplete;
ENDBEGIN
    DECLARE nDaysToComplete INT(12) DEFAULT 0;
    SELECT (TO_DAYS(DateTimeRepairComplete)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDaysToComplete FROM jobs WHERE JobID=sJobID;
    RETURN nDaysToComplete;
ENDZ���*���b���b���$$&
yy PcastironradcenAdvanceOrderStatusForPurchaseInvoiceAdvanceOrderStatusForPurchaseInvoiceutf8mb3utf8mb3_general_cilatin1_swedish_cicastironradcen@%`nPurchaseInvoiceID` INTEGER(11)
    tinyint(1)BEGIN
  # (SS,v1.00,2/5/14)
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nPurchaseOrderNo INTEGER(11);
  DECLARE sOrderStatus VARCHAR(20);
  DECLARE sNewStatus VARCHAR(20);
  DECLARE nPurchaseOrderCompleted TINYINT(1);
  DECLARE bOrderStatusChanged TINYINT(1) DEFAULT 0;
  
  # get list of purchase orders for given purchase invoice that haven't been marked as completed   
  DECLARE curPurchaseOrders CURSOR FOR
    SELECT pol.PurchaseOrderNo, po.OrderStatus
    FROM purchase_invoice_lines pil
    INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
    INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
    WHERE pil.PurchaseInvoiceID = nPurchaseInvoiceID AND (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") 
    GROUP BY PurchaseOrderNo;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;     
   
  OPEN curPurchaseOrders;
  
  nextrecord: LOOP 
  
    FETCH curPurchaseOrders INTO nPurchaseOrderNo, sOrderStatus;
    IF bFINISHED THEN
      LEAVE nextrecord;
    END IF;
    
    # call function to check if this purchase order is completed, returns 1 if partially received, 2 if completely received
    SELECT IsPurchaseOrderCompleted(nPurchaseOrderNo) INTO nPurchaseOrderCompleted;
    
    # check if status needs to be changed and what to
    IF nPurchaseOrderCompleted = 1 AND sOrderStatus <> "PARTIALLY RECEIVED" THEN
       SET sNewStatus = "PARTIALLY RECEIVED";
     ELSEIF nPurchaseOrderCompleted = 2 AND sOrderStatus <> "COMPLETED" THEN
       SET sNewStatus = "COMPLETED";
     ELSE
         SET sNewStatus = "";
     END IF;
     
     # if new status to be set then set it
     IF sNewStatus <> "" THEN 
       UPDATE purchase_orders SET OrderStatus = sNewStatus, InternalNotes = CONCAT(IF(COALESCE(InternalNotes, "") = "", "", CONCAT(InternalNotes, "\r\n")), DATE_FORMAT(NOW(), "%d/%m/%Y %H:%i"), ", Status auto-advanced to ", sNewStatus) WHERE PurchaseOrderNo = nPurchaseOrderNo;
       SET bOrderStatusChanged = TRUE;       
     END IF;   
      
  END LOOP nextrecord;  
  
  RETURN bOrderStatusChanged;
ENDBEGIN
  # (SS,v1.00,2/5/14)
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nPurchaseOrderNo INTEGER(11);
  DECLARE sOrderStatus VARCHAR(20);
  DECLARE sNewStatus VARCHAR(20);
  DECLARE nPurchaseOrderCompleted TINYINT(1);
  DECLARE bOrderStatusChanged TINYINT(1) DEFAULT 0;
  
  # get list of purchase orders for given purchase invoice that haven't been marked as completed   
  DECLARE curPurchaseOrders CURSOR FOR
    SELECT pol.PurchaseOrderNo, po.OrderStatus
    FROM purchase_invoice_lines pil
    INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
    INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
    WHERE pil.PurchaseInvoiceID = nPurchaseInvoiceID AND (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") 
    GROUP BY PurchaseOrderNo;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;     
   
  OPEN curPurchaseOrders;
  
  nextrecord: LOOP 
  
    FETCH curPurchaseOrders INTO nPurchaseOrderNo, sOrderStatus;
    IF bFINISHED THEN
      LEAVE nextrecord;
    END IF;
    
    # call function to check if this purchase order is completed, returns 1 if partially received, 2 if completely received
    SELECT IsPurchaseOrderCompleted(nPurchaseOrderNo) INTO nPurchaseOrderCompleted;
    
    # check if status needs to be changed and what to
    IF nPurchaseOrderCompleted = 1 AND sOrderStatus <> "PARTIALLY RECEIVED" THEN
       SET sNewStatus = "PARTIALLY RECEIVED";
     ELSEIF nPurchaseOrderCompleted = 2 AND sOrderStatus <> "COMPLETED" THEN
       SET sNewStatus = "COMPLETED";
     ELSE
         SET sNewStatus = "";
     END IF;
     
     # if new status to be set then set it
     IF sNewStatus <> "" THEN 
       UPDATE purchase_orders SET OrderStatus = sNewStatus, InternalNotes = CONCAT(IF(COALESCE(InternalNotes, "") = "", "", CONCAT(InternalNotes, "\r\n")), DATE_FORMAT(NOW(), "%d/%m/%Y %H:%i"), ", Status auto-advanced to ", sNewStatus) WHERE PurchaseOrderNo = nPurchaseOrderNo;
       SET bOrderStatusChanged = TRUE;       
     END IF;   
      
  END LOOP nextrecord;  
  
  RETURN bOrderStatusChanged;
END�b���b���** castironradcenRenumberPicturesSingleProductRenumberPicturesSingleProductlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sProductCode VARCHAR(75)
    int(11)BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
END�'����'�U��
�b���b���$
YY PcastironradcenIsPurchaseOrderCompletedIsPurchaseOrderCompletedutf8mb3utf8mb3_general_cilatin1_swedish_cicastironradcen@%`nPurchaseOrderNo` INTEGER(11)
    tinyint(1)BEGIN
  # (SS,v1.00,2/5/14) Returns 1 when given purchase order has all ordered items received, 2 when some items received, 0 when nothing received
  DECLARE nCompletelyReceived TINYINT(1) DEFAULT 0;
  DECLARE nPartiallyReceived TINYINT(1) DEFAULT 0;
  DECLARE nResult TINYINT(1) DEFAULT 0;

  SELECT COUNT(*) = 0 INTO nCompletelyReceived FROM
  (
    SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
    FROM purchase_order_lines pol
    LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
    WHERE PurchaseOrderNo = nPurchaseOrderNo
    GROUP BY PurchaseOrderLineID
    HAVING QtyOrdered > QtyReceived
  ) result;
  
  # if not completely received then check if partially received
  IF nCompletelyReceived = FALSE THEN
    SELECT COUNT(*) > 0 INTO nPartiallyReceived FROM
       (
        SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
          FROM purchase_order_lines pol
          LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
          WHERE PurchaseOrderNo = nPurchaseOrderNo
          GROUP BY PurchaseOrderLineID
          HAVING QtyReceived > 0
       ) result;    
  END IF;
  
  IF nCompletelyReceived THEN
    SET nResult = 2;
  ELSEIF nPartiallyReceived THEN
    SET nResult = 1;
  ELSE
    SET nResult = 0;
  END IF;

  RETURN nResult;

ENDBEGIN
  # (SS,v1.00,2/5/14) Returns 1 when given purchase order has all ordered items received, 2 when some items received, 0 when nothing received
  DECLARE nCompletelyReceived TINYINT(1) DEFAULT 0;
  DECLARE nPartiallyReceived TINYINT(1) DEFAULT 0;
  DECLARE nResult TINYINT(1) DEFAULT 0;

  SELECT COUNT(*) = 0 INTO nCompletelyReceived FROM
  (
    SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
    FROM purchase_order_lines pol
    LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
    WHERE PurchaseOrderNo = nPurchaseOrderNo
    GROUP BY PurchaseOrderLineID
    HAVING QtyOrdered > QtyReceived
  ) result;
  
  # if not completely received then check if partially received
  IF nCompletelyReceived = FALSE THEN
    SELECT COUNT(*) > 0 INTO nPartiallyReceived FROM
       (
        SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
          FROM purchase_order_lines pol
          LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
          WHERE PurchaseOrderNo = nPurchaseOrderNo
          GROUP BY PurchaseOrderLineID
          HAVING QtyReceived > 0
       ) result;    
  END IF;
  
  IF nCompletelyReceived THEN
    SET nResult = 2;
  ELSEIF nPartiallyReceived THEN
    SET nResult = 1;
  ELSE
    SET nResult = 0;
  END IF;

  RETURN nResult;

END�b�b��99 TcastironradcenAddStockAuditRecordAddStockAuditRecordutf8mb4utf8mb4_general_cilatin1_swedish_cicastironradcen@%
	IN `nOldNumInStock` INTEGER(11),
	IN `nNewNumInStock` INTEGER(11),
	IN `sAuditType` VARCHAR(6),
	IN `nProductID` INTEGER(11),
	IN `nProductOptionValueID` INTEGER(11),
	IN `bForceAdd` TINYINT(1)
    
BEGIN
  # (SS,4/7/13) to record changes to stock in tables products and product_option_values number to stock_audit table,
  # also tries to get the PC name if applicable using audit table
  # called from insert and update triggers on the products and product_option_values tables
  # bForceAdd is true when inserting a record
  # (SS,2/6/22) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        # lookup audit record using connection ID where user is still logged on and     
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
  
    INSERT INTO stock_audit (ProductID, AuditType, StockBefore, StockAfter, UserName, PCName, ConnectionID, ProductOptionValueID) 
           VALUES (nProductID, sAuditType, nOldNumInStock, nNewNumInStock, SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID);
  END IF; 
ENDBEGIN
  # (SS,4/7/13) to record changes to stock in tables products and product_option_values number to stock_audit table,
  # also tries to get the PC name if applicable using audit table
  # called from insert and update triggers on the products and product_option_values tables
  # bForceAdd is true when inserting a record
  # (SS,2/6/22) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        # lookup audit record using connection ID where user is still logged on and     
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
  
    INSERT INTO stock_audit (ProductID, AuditType, StockBefore, StockAfter, UserName, PCName, ConnectionID, ProductOptionValueID) 
           VALUES (nProductID, sAuditType, nOldNumInStock, nNewNumInStock, SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID);
  END IF; 
END�e��e��5�� jms_demo_epochfuncGetBranchNameByBranchIDfuncGetBranchNameByBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%nBranchID INT(12)
    varchar(200) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
DECLARE sBranchName VARCHAR(200) DEFAULT "UNKNOWN";
SELECT BranchName INTO sBranchName FROM branches WHERE BranchID=nBranchID;
RETURN sBranchName;
ENDBEGIN
DECLARE sBranchName VARCHAR(200) DEFAULT "UNKNOWN";
SELECT BranchName INTO sBranchName FROM branches WHERE BranchID=nBranchID;
RETURN sBranchName;
END�e��e���� jms_demo_epochfuncGetContractValuefuncGetContractValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    doubleBEGIN
DECLARE nResult DOUBLE DEFAULT 0.00;
SELECT SellPrice INTO nResult FROM jobs WHERE InvoiceNet=0 AND JobID=sJobID AND CustomerID IN (1709,2150,1270,2135,678,336,1897);
RETURN nResult;
ENDBEGIN
DECLARE nResult DOUBLE DEFAULT 0.00;
SELECT SellPrice INTO nResult FROM jobs WHERE InvoiceNet=0 AND JobID=sJobID AND CustomerID IN (1709,2150,1270,2135,678,336,1897);
RETURN nResult;
END�9s0���
ס�V���b���b���$�� PcastironradcenGetInStockRatiosGetInStockRatioslatin1latin1_swedish_cilatin1_swedish_cicastironradcen@%
        IN `dStartDate` DATE
    BEGIN

  DROP TEMPORARY TABLE IF EXISTS tmp_total_stock_days;
  CREATE TEMPORARY TABLE tmp_total_stock_days
  SELECT stock_snapshots.ProductID, COUNT(*) AS TotalDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_days;
  CREATE TEMPORARY TABLE tmp_in_stock_days
  SELECT ProductID, COUNT(*) AS InStockDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate AND NumInStock > 0
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_ratios;
  CREATE TEMPORARY TABLE tmp_in_stock_ratios
  SELECT tmp_total_stock_days.*, tmp_in_stock_days.InStockDays, 
  # ROUND in following removes the data truncated warning seen in HeidiSQL
  COALESCE(ROUND(InStockDays / TotalDays * 100, 2), 100) AS InStockRatio
  FROM tmp_total_stock_days
  LEFT JOIN tmp_in_stock_days ON tmp_in_stock_days.ProductID = tmp_total_stock_days.ProductID;

ENDBEGIN

  DROP TEMPORARY TABLE IF EXISTS tmp_total_stock_days;
  CREATE TEMPORARY TABLE tmp_total_stock_days
  SELECT stock_snapshots.ProductID, COUNT(*) AS TotalDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_days;
  CREATE TEMPORARY TABLE tmp_in_stock_days
  SELECT ProductID, COUNT(*) AS InStockDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate AND NumInStock > 0
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_ratios;
  CREATE TEMPORARY TABLE tmp_in_stock_ratios
  SELECT tmp_total_stock_days.*, tmp_in_stock_days.InStockDays, 
  # ROUND in following removes the data truncated warning seen in HeidiSQL
  COALESCE(ROUND(InStockDays / TotalDays * 100, 2), 100) AS InStockRatio
  FROM tmp_total_stock_days
  LEFT JOIN tmp_in_stock_days ON tmp_in_stock_days.ProductID = tmp_total_stock_days.ProductID;

END�b���b����� castironradcenRenumberPicturesAllProductsRenumberPicturesAllProductslatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
END�b���b����� PcommonGetFirstIntegerGetFirstIntegerutf8mb3utf8mb3_general_cilatin1_swedish_cicommon@%`AStr` VARCHAR(255)
    int(11)BEGIN
  /* (SS,1/7/2016) get first integer from given string */
  DECLARE LPos INT DEFAULT 1;
  DECLARE LPosEnd INT DEFAULT 1;
  DECLARE LFound INT DEFAULT 0;
  DECLARE LChar INT;
  WHILE LPos <= LENGTH(AStr) AND LFound = 0 DO
    SET LChar = ASCII(SUBSTR(AStr, LPos, 1));
    IF LChar BETWEEN 48 AND 57 THEN
      SET LFound = 1;
    ELSE
      SET LPos = LPos + 1;
    END IF;
  END WHILE;
  IF LFound = 0 THEN
    RETURN NULL;
  ELSE
    SET LFound = 0;
    SET LPosEnd = LPos + 1;
    /* find end of integer */
  	WHILE LPosEnd <= LENGTH(AStr) AND LFound = 0 DO
      SET LChar = ASCII(SUBSTR(AStr, LPosEnd, 1));
      IF LChar < 48 OR LCHAR > 57 THEN
        SET LFound = 1;
      ELSE
        SET LPosEnd = LPosEnd + 1;
      END IF;
  	END WHILE;
  	
    /* RETURN CONVERT(SUBSTR(AStr, LPos), SIGNED); */
    RETURN CONVERT(SUBSTR(AStr, LPos, LPosEnd - LPos), SIGNED);
  END IF;  
ENDBEGIN
  /* (SS,1/7/2016) get first integer from given string */
  DECLARE LPos INT DEFAULT 1;
  DECLARE LPosEnd INT DEFAULT 1;
  DECLARE LFound INT DEFAULT 0;
  DECLARE LChar INT;
  WHILE LPos <= LENGTH(AStr) AND LFound = 0 DO
    SET LChar = ASCII(SUBSTR(AStr, LPos, 1));
    IF LChar BETWEEN 48 AND 57 THEN
      SET LFound = 1;
    ELSE
      SET LPos = LPos + 1;
    END IF;
  END WHILE;
  IF LFound = 0 THEN
    RETURN NULL;
  ELSE
    SET LFound = 0;
    SET LPosEnd = LPos + 1;
    /* find end of integer */
  	WHILE LPosEnd <= LENGTH(AStr) AND LFound = 0 DO
      SET LChar = ASCII(SUBSTR(AStr, LPosEnd, 1));
      IF LChar < 48 OR LCHAR > 57 THEN
        SET LFound = 1;
      ELSE
        SET LPosEnd = LPosEnd + 1;
      END IF;
  	END WHILE;
  	
    /* RETURN CONVERT(SUBSTR(AStr, LPos), SIGNED); */
    RETURN CONVERT(SUBSTR(AStr, LPos, LPosEnd - LPos), SIGNED);
  END IF;  
END�e��e��5�� jms_demo_epochfuncGetCustomerNamefuncGetCustomerNamelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    varchar(200) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
	DECLARE sCustomerName VARCHAR(200) DEFAULT "UNKNOWN";
	SELECT customers.CustomerName INTO sCustomerName FROM customers JOIN jobs ON customers.CustomerID=jobs.CustomerID WHERE jobs.JobID=sJobID;
	RETURN sCustomerName;
ENDBEGIN
	DECLARE sCustomerName VARCHAR(200) DEFAULT "UNKNOWN";
	SELECT customers.CustomerName INTO sCustomerName FROM customers JOIN jobs ON customers.CustomerID=jobs.CustomerID WHERE jobs.JobID=sJobID;
	RETURN sCustomerName;
END�e��e���� jms_demo_epochfuncGetInvoiceValuefuncGetInvoiceValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    doubleBEGIN
DECLARE nInvoiceValue DOUBLE DEFAULT 0.00;
SELECT (NumberOff*QuotedPrice) INTO nInvoiceValue FROM jobs WHERE JobID=sJobID;
RETURN nInvoiceValue;
ENDBEGIN
DECLARE nInvoiceValue DOUBLE DEFAULT 0.00;
SELECT (NumberOff*QuotedPrice) INTO nInvoiceValue FROM jobs WHERE JobID=sJobID;
RETURN nInvoiceValue;
END
�a���)[�J����b���b�����
�
 PcastironradcenGetStockRequiredGetStockRequiredutf8mb3utf8mb3_general_cilatin1_swedish_cicastironradcen@%IN `nSupplierID` INTEGER(11), IN `nPastWeeks` INTEGER(11), IN `nFollowingWeeks` INTEGER(11), IN `bRequiredOnly` TINYINT(1)
    BEGIN

CALL GetInStockRatios(DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK));

# (SS,3/4/14) made to make the following create a temporary table because
# in Shopping Admin using CALL GetStockRequired(2, 52, 1, TRUE);
# was causing error: "can't return a result set in the given context"
# while testing it seemed to work if coMultiQueries option used, but this causes issues elsewhere
DROP TEMPORARY TABLE IF EXISTS tmp_stock_required;

CREATE TEMPORARY TABLE tmp_stock_required
# (SS,25/4/17) changed orderdetails.ProductName to products.ProductName to ensure the latest product name
# (SS,10/4/18) added products.SortOrder
SELECT products.SupplierID, suppliers.SupplierName, orderdetails.ProductID, orderdetails.ProductCode, products.ProductName, products.SortOrder,
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks + 0.4999) AS QtyRequiredNoRatio, 
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999) AS QtyRequired, 
NumInStock, on_order.QtyOrdered, on_order.QtyReceived, on_order.QtyAddedToStock,
# QtyToOrder calculated using QtyRequired - (NumInStock + COALESCE(QtyOrdered, 0) - COALESCE(QtyAddedToStock, 0))
# i.e. QtyRequired - (NumInStock + QtyOrdered - QtyAddedToStock)
# (SS,5/9/16) replaced QtyAddedToStock with QtyReceived
# i.e. now QtyRequired - (NumInStock + QtyOrdered - QtyReceived)
(ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999)) - (NumInStock + COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0)) AS QtyToOrder,

COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0) AS QtyOnOrder, products.CostPrice,
tmp_in_stock_ratios.InStockRatio
FROM orderdetails
INNER JOIN orders ON orders.OrderNo = orderdetails.OrderNo
INNER JOIN products ON products.ProductCode = orderdetails.ProductCode
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
LEFT JOIN tmp_in_stock_ratios ON tmp_in_stock_ratios.ProductID = products.ProductID
LEFT JOIN

(SELECT pol.ProductID, SUM(QtyOrdered) AS QtyOrdered, ordered_and_received.QtyReceived, ordered_and_received.QtyAddedToStock
FROM purchase_order_lines pol
LEFT JOIN

  (SELECT ProductID, SUM(QtyReceived) AS QtyReceived, SUM(QtyAddedToStock) AS QtyAddedToStock
  FROM purchase_invoice_lines pil
  INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
  INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
  WHERE (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") AND ProductID IS NOT NULL
  GROUP BY ProductID
  ) AS ordered_and_received ON ordered_and_received.ProductID = pol.ProductID
  
INNER JOIN purchase_orders ON purchase_orders.PurchaseOrderNo = pol.PurchaseOrderNo

WHERE OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED"
GROUP BY ProductID
) AS on_order ON on_order.ProductID = orderdetails.ProductID

# (SS,29/4/14) changed nSupplierID = 0 to nSupplierID = -1 (because 0 is when supplier hasn't been chosen to prevent slow adding of new order, -1 when the report is run for all suppliers)
WHERE Status <> "CANCELLED" AND NumInStock IS NOT NULL AND ProductDisabled = False AND DateTimeOrdered >= DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK) 
AND (nSupplierID = -1 OR suppliers.SupplierID = nSupplierID)
GROUP BY suppliers.SupplierName, ProductCode
HAVING NOT bRequiredOnly OR QtyRequired > NumInStock + QtyOnOrder;

ENDBEGIN

CALL GetInStockRatios(DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK));

# (SS,3/4/14) made to make the following create a temporary table because
# in Shopping Admin using CALL GetStockRequired(2, 52, 1, TRUE);
# was causing error: "can't return a result set in the given context"
# while testing it seemed to work if coMultiQueries option used, but this causes issues elsewhere
DROP TEMPORARY TABLE IF EXISTS tmp_stock_required;

CREATE TEMPORARY TABLE tmp_stock_required
# (SS,25/4/17) changed orderdetails.ProductName to products.ProductName to ensure the latest product name
# (SS,10/4/18) added products.SortOrder
SELECT products.SupplierID, suppliers.SupplierName, orderdetails.ProductID, orderdetails.ProductCode, products.ProductName, products.SortOrder,
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks + 0.4999) AS QtyRequiredNoRatio, 
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999) AS QtyRequired, 
NumInStock, on_order.QtyOrdered, on_order.QtyReceived, on_order.QtyAddedToStock,
# QtyToOrder calculated using QtyRequired - (NumInStock + COALESCE(QtyOrdered, 0) - COALESCE(QtyAddedToStock, 0))
# i.e. QtyRequired - (NumInStock + QtyOrdered - QtyAddedToStock)
# (SS,5/9/16) replaced QtyAddedToStock with QtyReceived
# i.e. now QtyRequired - (NumInStock + QtyOrdered - QtyReceived)
(ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999)) - (NumInStock + COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0)) AS QtyToOrder,

COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0) AS QtyOnOrder, products.CostPrice,
tmp_in_stock_ratios.InStockRatio
FROM orderdetails
INNER JOIN orders ON orders.OrderNo = orderdetails.OrderNo
INNER JOIN products ON products.ProductCode = orderdetails.ProductCode
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
LEFT JOIN tmp_in_stock_ratios ON tmp_in_stock_ratios.ProductID = products.ProductID
LEFT JOIN

(SELECT pol.ProductID, SUM(QtyOrdered) AS QtyOrdered, ordered_and_received.QtyReceived, ordered_and_received.QtyAddedToStock
FROM purchase_order_lines pol
LEFT JOIN

  (SELECT ProductID, SUM(QtyReceived) AS QtyReceived, SUM(QtyAddedToStock) AS QtyAddedToStock
  FROM purchase_invoice_lines pil
  INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
  INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
  WHERE (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") AND ProductID IS NOT NULL
  GROUP BY ProductID
  ) AS ordered_and_received ON ordered_and_received.ProductID = pol.ProductID
  
INNER JOIN purchase_orders ON purchase_orders.PurchaseOrderNo = pol.PurchaseOrderNo

WHERE OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED"
GROUP BY ProductID
) AS on_order ON on_order.ProductID = orderdetails.ProductID

# (SS,29/4/14) changed nSupplierID = 0 to nSupplierID = -1 (because 0 is when supplier hasn't been chosen to prevent slow adding of new order, -1 when the report is run for all suppliers)
WHERE Status <> "CANCELLED" AND NumInStock IS NOT NULL AND ProductDisabled = False AND DateTimeOrdered >= DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK) 
AND (nSupplierID = -1 OR suppliers.SupplierID = nSupplierID)
GROUP BY suppliers.SupplierName, ProductCode
HAVING NOT bRequiredOnly OR QtyRequired > NumInStock + QtyOnOrder;

END�e��e��4 jms_demo_epochfuncGetJobTypefuncGetJobTypelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    varchar(50) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE sJobType VARCHAR(50) DEFAULT "Job Type not set!";
SELECT JobTypeID INTO nJobTypeID FROM jobs WHERE JobID=sJobID;
SELECT JobType INTO sJobType FROM job_types WHERE JobTypeID=nJobTypeID;
RETURN sJobType;
ENDBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE sJobType VARCHAR(50) DEFAULT "Job Type not set!";
SELECT JobTypeID INTO nJobTypeID FROM jobs WHERE JobID=sJobID;
SELECT JobType INTO sJobType FROM job_types WHERE JobTypeID=nJobTypeID;
RETURN sJobType;
ENDa�UX��M�i�b���b���9�� PcommonGetNthWorkingDayGetNthWorkingDayutf8mb3utf8mb3_general_cilatin1_swedish_cicommon@%`AStartDate` DATE,
	`ANthWorkingDay` INTEGER(11)
    
dateBEGIN
  
 /* (SS,1/7/2016) get nth working day from calendar table */
  /* used by function GetNthWorkingDay due to limitation of the LIMIT clause in MySQL 5.1 */
  /*
  DECLARE LWorkingDate DATE;
  DECLARE LNthWorkingDay INT;
  */
  
  /* (SS,1/7/2016) following doesn't work in 5.1 (may work in later version)
  SELECT CalendarDate INTO LWorkingDate FROM calendar
  WHERE CalendarDate > CURDATE() AND IsWorkingDay
  ORDER BY CalendarDate
  LIMIT ANthWorkingDay - 1, 1;
  */

  DECLARE LWorkingDay DATE DEFAULT NULL;
  DECLARE LNthDay INT DEFAULT 0;
  
  DECLARE cur1 CURSOR FOR SELECT CalendarDate FROM calendar WHERE CalendarDate > AStartDate AND IsWorkingDay ORDER BY CalendarDate;
                                                                                 
  # (SS,22/4/21) added following for negative/backward use
  DECLARE cur1_b CURSOR FOR SELECT CalendarDate FROM calendar WHERE CalendarDate < AStartDate AND IsWorkingDay ORDER BY CalendarDate DESC;
  
  /* (SS,22/4/21) modified to work backwards for negative values */
  IF ANthWorkingDay >= 0 THEN
     
    OPEN cur1;
  
    read_loop: LOOP
      FETCH cur1 INTO LWorkingDay;
      SET LNthDay = LNthDay + 1;
      IF LNthDay >= ANthWorkingDay THEN
        LEAVE read_loop;
      END IF;  
    END LOOP;
  
    CLOSE cur1;     
    
  ELSE # (SS,22/4/21) backwards for negative ANthWorkingDay
     
    OPEN cur1_b;
  
    read_loop_b: LOOP
      FETCH cur1_b INTO LWorkingDay;
      SET LNthDay = LNthDay - 1;
      IF LNthDay <= ANthWorkingDay THEN
        LEAVE read_loop_b;
      END IF;  
    END LOOP;
  
    CLOSE cur1_b;
    
  END IF;

  RETURN LWorkingDay;  
  
ENDBEGIN
  
 /* (SS,1/7/2016) get nth working day from calendar table */
  /* used by function GetNthWorkingDay due to limitation of the LIMIT clause in MySQL 5.1 */
  /*
  DECLARE LWorkingDate DATE;
  DECLARE LNthWorkingDay INT;
  */
  
  /* (SS,1/7/2016) following doesn't work in 5.1 (may work in later version)
  SELECT CalendarDate INTO LWorkingDate FROM calendar
  WHERE CalendarDate > CURDATE() AND IsWorkingDay
  ORDER BY CalendarDate
  LIMIT ANthWorkingDay - 1, 1;
  */

  DECLARE LWorkingDay DATE DEFAULT NULL;
  DECLARE LNthDay INT DEFAULT 0;
  
  DECLARE cur1 CURSOR FOR SELECT CalendarDate FROM calendar WHERE CalendarDate > AStartDate AND IsWorkingDay ORDER BY CalendarDate;
                                                                                 
  # (SS,22/4/21) added following for negative/backward use
  DECLARE cur1_b CURSOR FOR SELECT CalendarDate FROM calendar WHERE CalendarDate < AStartDate AND IsWorkingDay ORDER BY CalendarDate DESC;
  
  /* (SS,22/4/21) modified to work backwards for negative values */
  IF ANthWorkingDay >= 0 THEN
     
    OPEN cur1;
  
    read_loop: LOOP
      FETCH cur1 INTO LWorkingDay;
      SET LNthDay = LNthDay + 1;
      IF LNthDay >= ANthWorkingDay THEN
        LEAVE read_loop;
      END IF;  
    END LOOP;
  
    CLOSE cur1;     
    
  ELSE # (SS,22/4/21) backwards for negative ANthWorkingDay
     
    OPEN cur1_b;
  
    read_loop_b: LOOP
      FETCH cur1_b INTO LWorkingDay;
      SET LNthDay = LNthDay - 1;
      IF LNthDay <= ANthWorkingDay THEN
        LEAVE read_loop_b;
      END IF;  
    END LOOP;
  
    CLOSE cur1_b;
    
  END IF;

  RETURN LWorkingDay;  
  
END�b���b����� PcommonGetSecondIntegerGetSecondIntegerutf8mb3utf8mb3_general_cilatin1_swedish_cicommon@%`AStr` VARCHAR(255)int(11)BEGIN
  /* (SS,11/10/17) copy of GetFirstInteger modified to return second integer, also calls GetFirstInteger */

  /* (SS,1/7/2016) get first integer from given string */
  DECLARE LPos INT DEFAULT 1;
  DECLARE LPosEnd INT DEFAULT 1;
  DECLARE LFound INT DEFAULT 0;
  DECLARE LChar INT;
  DECLARE LStr VARCHAR(200);  
  
  WHILE LPos <= LENGTH(AStr) AND LFound = 0 DO
    SET LChar = ASCII(SUBSTR(AStr, LPos, 1));
    IF LChar BETWEEN 48 AND 57 THEN
      SET LFound = 1;
    ELSE
      SET LPos = LPos + 1;
    END IF;
  END WHILE;
  IF LFound = 0 THEN
    RETURN NULL;
  ELSE
    SET LFound = 0;
    SET LPosEnd = LPos + 1;
    /* find end of integer */
  	WHILE LPosEnd <= LENGTH(AStr) AND LFound = 0 DO
      SET LChar = ASCII(SUBSTR(AStr, LPosEnd, 1));
      IF LChar < 48 OR LCHAR > 57 THEN
        SET LFound = 1;
      ELSE
        SET LPosEnd = LPosEnd + 1;
      END IF;
  	END WHILE;
    
    /* (SS,11/10/17) added following to get the first integer from remaining string, above code is same as GetFirstString, except DECLARE LStr */
   	SET LStr = SUBSTR(AStr, LPosEnd, LENGTH(AStr));
    
    RETURN GetFirstInteger(LStr);
  END IF; 
ENDBEGIN
  /* (SS,11/10/17) copy of GetFirstInteger modified to return second integer, also calls GetFirstInteger */

  /* (SS,1/7/2016) get first integer from given string */
  DECLARE LPos INT DEFAULT 1;
  DECLARE LPosEnd INT DEFAULT 1;
  DECLARE LFound INT DEFAULT 0;
  DECLARE LChar INT;
  DECLARE LStr VARCHAR(200);  
  
  WHILE LPos <= LENGTH(AStr) AND LFound = 0 DO
    SET LChar = ASCII(SUBSTR(AStr, LPos, 1));
    IF LChar BETWEEN 48 AND 57 THEN
      SET LFound = 1;
    ELSE
      SET LPos = LPos + 1;
    END IF;
  END WHILE;
  IF LFound = 0 THEN
    RETURN NULL;
  ELSE
    SET LFound = 0;
    SET LPosEnd = LPos + 1;
    /* find end of integer */
  	WHILE LPosEnd <= LENGTH(AStr) AND LFound = 0 DO
      SET LChar = ASCII(SUBSTR(AStr, LPosEnd, 1));
      IF LChar < 48 OR LCHAR > 57 THEN
        SET LFound = 1;
      ELSE
        SET LPosEnd = LPosEnd + 1;
      END IF;
  	END WHILE;
    
    /* (SS,11/10/17) added following to get the first integer from remaining string, above code is same as GetFirstString, except DECLARE LStr */
   	SET LStr = SUBSTR(AStr, LPosEnd, LENGTH(AStr));
    
    RETURN GetFirstInteger(LStr);
  END IF; 
END�e��e���� jms_demo_epochfuncGetMaterialCostfuncGetMaterialCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�e��e��

 jms_demo_epochfuncGetMaterialCostNewfuncGetMaterialCostNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  # Serial: 2008-12-19-01

  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID;  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  # Serial: 2008-12-19-01

  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID;  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END����	�6p����b���b���-22 PcommonGetWorkingDaysBetweenGetWorkingDaysBetweenlatin1latin1_swedish_cilatin1_swedish_cicommon@%AStartDate DATE,
        AEndDate DATE
    int(11)BEGIN

  # (SS,22/4/21) gets the working days between the two given dates, excludes the start date
  # Start Date should be before End Date  

  DECLARE LDaysBetween INT DEFAULT 0;
  DECLARE LDate DATE DEFAULT NULL;
  DECLARE LEndOfTable TINYINT(1) DEFAULT FALSE;
  
  DECLARE cur1 CURSOR FOR SELECT CalendarDate FROM calendar WHERE CalendarDate >= AStartDate AND CalendarDate <= AEndDate AND IsWorkingDay ORDER BY CalendarDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET LEndOfTable = TRUE;
     
  OPEN cur1;

  read_loop: LOOP
    FETCH cur1 INTO LDate;
    
    IF LEndOfTable THEN
      LEAVE read_loop;
    END IF;
      
    IF LDate <> AStartDate THEN
      SET LDaysBetween = LDaysBetween + 1;
    END IF;

  END LOOP read_loop;

  CLOSE cur1;
      
  RETURN LDaysBetween;
ENDBEGIN

  # (SS,22/4/21) gets the working days between the two given dates, excludes the start date
  # Start Date should be before End Date  

  DECLARE LDaysBetween INT DEFAULT 0;
  DECLARE LDate DATE DEFAULT NULL;
  DECLARE LEndOfTable TINYINT(1) DEFAULT FALSE;
  
  DECLARE cur1 CURSOR FOR SELECT CalendarDate FROM calendar WHERE CalendarDate >= AStartDate AND CalendarDate <= AEndDate AND IsWorkingDay ORDER BY CalendarDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET LEndOfTable = TRUE;
     
  OPEN cur1;

  read_loop: LOOP
    FETCH cur1 INTO LDate;
    
    IF LEndOfTable THEN
      LEAVE read_loop;
    END IF;
      
    IF LDate <> AStartDate THEN
      SET LDaysBetween = LDaysBetween + 1;
    END IF;

  END LOOP read_loop;

  CLOSE cur1;
      
  RETURN LDaysBetween;
END�b���b���** covdemRenumberPicturesSingleProductRenumberPicturesSingleProductlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sProductCode VARCHAR(75)
    int(11)BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
END�b���b����� covdemRenumberPicturesAllProductsRenumberPicturesAllProductslatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
END�e��e��5�� jms_demo_epochfuncGetRepairEngineerfuncGetRepairEngineerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    varchar(100) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
END��TS5)�L���b��Vb��V** expressmusicRenumberPicturesSingleProductRenumberPicturesSingleProductlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sProductCode VARCHAR(75)
    int(11)BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
END�b��Vb��V�� expressmusicRenumberPicturesAllProductsRenumberPicturesAllProductslatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
END�e��e���� jms_demo_epochfuncIsJobQuotationfuncIsJobQuotationlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID INT(12)
    int(12)BEGIN
DECLARE nResult INT(12) DEFAULT 0;
SELECT NumberOff INTO nResult FROM jobs JOIN job_types ON jobs.JobTypeID=job_types.JobTypeID WHERE JobID=sJobID AND job_types.JobType="Quotation";
RETURN nResult;
ENDBEGIN
DECLARE nResult INT(12) DEFAULT 0;
SELECT NumberOff INTO nResult FROM jobs JOIN job_types ON jobs.JobTypeID=job_types.JobTypeID WHERE JobID=sJobID AND job_types.JobType="Quotation";
RETURN nResult;
END�e��e���� jms_demo_epochfuncJobBecomesWarrantyItemCountfuncJobBecomesWarrantyItemCountlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nResult INT(12);
    SELECT SUM(NumberOff) INTO nResult FROM jobs WHERE CWJobID=sJobID;
    IF nResult IS NULL THEN
        SET nResult=0;
    END IF;
    RETURN nResult;
ENDBEGIN
    DECLARE nResult INT(12);
    SELECT SUM(NumberOff) INTO nResult FROM jobs WHERE CWJobID=sJobID;
    IF nResult IS NULL THEN
        SET nResult=0;
    END IF;
    RETURN nResult;
END�e��e���� jms_demo_epochGetNextInvoiceNoGetNextInvoiceNolatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%nBranchID INTEGER(11)
    int(11)BEGIN
  DECLARE nNextInvoiceNo INTEGER;
  SELECT NextInvoiceNo INTO nNextInvoiceNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextInvoiceNo = NextInvoiceNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextInvoiceNo ;
ENDBEGIN
  DECLARE nNextInvoiceNo INTEGER;
  SELECT NextInvoiceNo INTO nNextInvoiceNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextInvoiceNo = NextInvoiceNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextInvoiceNo ;
END�e��e���� jms_demo_epochGetNextOrderNoGetNextOrderNolatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%nBranchID INTEGER(11)
    int(11)BEGIN
  DECLARE nNextOrderNo INTEGER;
  SELECT NextOrderNo INTO nNextOrderNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextOrderNo = NextOrderNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextOrderNo;
ENDBEGIN
  DECLARE nNextOrderNo INTEGER;
  SELECT NextOrderNo INTO nNextOrderNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextOrderNo = NextOrderNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextOrderNo;
END�v-��P�S1Y%Vt���M�b��ab��a$$%
HH PgroundscrewcenAdvanceOrderStatusForPurchaseInvoiceAdvanceOrderStatusForPurchaseInvoiceutf8mb3utf8mb3_general_cilatin1_swedish_cigroundscrewcen@%`nPurchaseInvoiceID` INTEGER(11)
    tinyint(1)BEGIN
  # (SS,v1.00,2/5/14)
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nPurchaseOrderNo INTEGER(11);
  DECLARE sOrderStatus VARCHAR(20);
  DECLARE sNewStatus VARCHAR(20);
  DECLARE nPurchaseOrderCompleted TINYINT(1);
  DECLARE bOrderStatusChanged TINYINT(1) DEFAULT 0;
  
  # get list of purchase orders for given purchase invoice that haven't been marked as completed   
  DECLARE curPurchaseOrders CURSOR FOR
    SELECT pol.PurchaseOrderNo, po.OrderStatus
    FROM purchase_invoice_lines pil
    INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
    INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
    WHERE pil.PurchaseInvoiceID = nPurchaseInvoiceID AND (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") 
    GROUP BY PurchaseOrderNo;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;     
   
  OPEN curPurchaseOrders;
  
  nextrecord: LOOP 
  
    FETCH curPurchaseOrders INTO nPurchaseOrderNo, sOrderStatus;
    IF bFINISHED THEN
      LEAVE nextrecord;
    END IF;
    
    # call function to check if this purchase order is completed, returns 1 if partially received, 2 if completely received
    SELECT IsPurchaseOrderCompleted(nPurchaseOrderNo) INTO nPurchaseOrderCompleted;
    
    # check if status needs to be changed and what to
    IF nPurchaseOrderCompleted = 1 AND sOrderStatus <> "PARTIALLY RECEIVED" THEN
       SET sNewStatus = "PARTIALLY RECEIVED";
     ELSEIF nPurchaseOrderCompleted = 2 AND sOrderStatus <> "COMPLETED" THEN
       SET sNewStatus = "COMPLETED";
     ELSE
         SET sNewStatus = "";
     END IF;
     
     # if new status to be set then set it
     IF sNewStatus <> "" THEN 
       UPDATE purchase_orders SET OrderStatus = sNewStatus, InternalNotes = CONCAT(IF(COALESCE(InternalNotes, "") = "", "", CONCAT(InternalNotes, "\r\n")), DATE_FORMAT(NOW(), "%d/%m/%Y %H:%i"), ", Status auto-advanced to ", sNewStatus) WHERE PurchaseOrderNo = nPurchaseOrderNo;
       SET bOrderStatusChanged = TRUE;       
     END IF;   
      
  END LOOP nextrecord;  
  
  RETURN bOrderStatusChanged;
ENDBEGIN
  # (SS,v1.00,2/5/14)
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nPurchaseOrderNo INTEGER(11);
  DECLARE sOrderStatus VARCHAR(20);
  DECLARE sNewStatus VARCHAR(20);
  DECLARE nPurchaseOrderCompleted TINYINT(1);
  DECLARE bOrderStatusChanged TINYINT(1) DEFAULT 0;
  
  # get list of purchase orders for given purchase invoice that haven't been marked as completed   
  DECLARE curPurchaseOrders CURSOR FOR
    SELECT pol.PurchaseOrderNo, po.OrderStatus
    FROM purchase_invoice_lines pil
    INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
    INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
    WHERE pil.PurchaseInvoiceID = nPurchaseInvoiceID AND (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") 
    GROUP BY PurchaseOrderNo;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;     
   
  OPEN curPurchaseOrders;
  
  nextrecord: LOOP 
  
    FETCH curPurchaseOrders INTO nPurchaseOrderNo, sOrderStatus;
    IF bFINISHED THEN
      LEAVE nextrecord;
    END IF;
    
    # call function to check if this purchase order is completed, returns 1 if partially received, 2 if completely received
    SELECT IsPurchaseOrderCompleted(nPurchaseOrderNo) INTO nPurchaseOrderCompleted;
    
    # check if status needs to be changed and what to
    IF nPurchaseOrderCompleted = 1 AND sOrderStatus <> "PARTIALLY RECEIVED" THEN
       SET sNewStatus = "PARTIALLY RECEIVED";
     ELSEIF nPurchaseOrderCompleted = 2 AND sOrderStatus <> "COMPLETED" THEN
       SET sNewStatus = "COMPLETED";
     ELSE
         SET sNewStatus = "";
     END IF;
     
     # if new status to be set then set it
     IF sNewStatus <> "" THEN 
       UPDATE purchase_orders SET OrderStatus = sNewStatus, InternalNotes = CONCAT(IF(COALESCE(InternalNotes, "") = "", "", CONCAT(InternalNotes, "\r\n")), DATE_FORMAT(NOW(), "%d/%m/%Y %H:%i"), ", Status auto-advanced to ", sNewStatus) WHERE PurchaseOrderNo = nPurchaseOrderNo;
       SET bOrderStatusChanged = TRUE;       
     END IF;   
      
  END LOOP nextrecord;  
  
  RETURN bOrderStatusChanged;
END�b��ab��a"�� PgroundscrewcenGetInStockRatiosGetInStockRatioslatin1latin1_swedish_cilatin1_swedish_cigroundscrewcen@%
        IN `dStartDate` DATE
    BEGIN

  DROP TEMPORARY TABLE IF EXISTS tmp_total_stock_days;
  CREATE TEMPORARY TABLE tmp_total_stock_days
  SELECT stock_snapshots.ProductID, COUNT(*) AS TotalDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_days;
  CREATE TEMPORARY TABLE tmp_in_stock_days
  SELECT ProductID, COUNT(*) AS InStockDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate AND NumInStock > 0
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_ratios;
  CREATE TEMPORARY TABLE tmp_in_stock_ratios
  SELECT tmp_total_stock_days.*, tmp_in_stock_days.InStockDays, 
  # ROUND in following removes the data truncated warning seen in HeidiSQL
  COALESCE(ROUND(InStockDays / TotalDays * 100, 2), 100) AS InStockRatio
  FROM tmp_total_stock_days
  LEFT JOIN tmp_in_stock_days ON tmp_in_stock_days.ProductID = tmp_total_stock_days.ProductID;

ENDBEGIN

  DROP TEMPORARY TABLE IF EXISTS tmp_total_stock_days;
  CREATE TEMPORARY TABLE tmp_total_stock_days
  SELECT stock_snapshots.ProductID, COUNT(*) AS TotalDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_days;
  CREATE TEMPORARY TABLE tmp_in_stock_days
  SELECT ProductID, COUNT(*) AS InStockDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate AND NumInStock > 0
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_ratios;
  CREATE TEMPORARY TABLE tmp_in_stock_ratios
  SELECT tmp_total_stock_days.*, tmp_in_stock_days.InStockDays, 
  # ROUND in following removes the data truncated warning seen in HeidiSQL
  COALESCE(ROUND(InStockDays / TotalDays * 100, 2), 100) AS InStockRatio
  FROM tmp_total_stock_days
  LEFT JOIN tmp_in_stock_days ON tmp_in_stock_days.ProductID = tmp_total_stock_days.ProductID;

END�e��e��

!�� Pjms_demo_epochAddToEventLogAddToEventLoglatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%
 IN sEventMessage MEDIUMTEXT
 BEGIN
 # (SS,21/8/13) to add message to event_log table
 INSERT INTO event_log (EventDateTime, EventMessage) VALUES(NOW(), sEventMessage);
ENDBEGIN
 # (SS,21/8/13) to add message to event_log table
 INSERT INTO event_log (EventDateTime, EventMessage) VALUES(NOW(), sEventMessage);
END�g�c�g�c�
�� jms_demo_rhesfuncDaysToCompletefuncDaysToCompletelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nDaysToComplete INT(12) DEFAULT 0;
    SELECT (TO_DAYS(DateTimeRepairComplete)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDaysToComplete FROM jobs WHERE JobID=sJobID;
    RETURN nDaysToComplete;
ENDBEGIN
    DECLARE nDaysToComplete INT(12) DEFAULT 0;
    SELECT (TO_DAYS(DateTimeRepairComplete)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDaysToComplete FROM jobs WHERE JobID=sJobID;
    RETURN nDaysToComplete;
ENDGXw����;؏���b��ab��a#
22 PgroundscrewcenIsPurchaseOrderCompletedIsPurchaseOrderCompletedutf8mb3utf8mb3_general_cilatin1_swedish_cigroundscrewcen@%`nPurchaseOrderNo` INTEGER(11)
    tinyint(1)BEGIN
  # (SS,v1.00,2/5/14) Returns 1 when given purchase order has all ordered items received, 2 when some items received, 0 when nothing received
  DECLARE nCompletelyReceived TINYINT(1) DEFAULT 0;
  DECLARE nPartiallyReceived TINYINT(1) DEFAULT 0;
  DECLARE nResult TINYINT(1) DEFAULT 0;

  SELECT COUNT(*) = 0 INTO nCompletelyReceived FROM
  (
    SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
    FROM purchase_order_lines pol
    LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
    WHERE PurchaseOrderNo = nPurchaseOrderNo
    GROUP BY PurchaseOrderLineID
    HAVING QtyOrdered > QtyReceived
  ) result;
  
  # if not completely received then check if partially received
  IF nCompletelyReceived = FALSE THEN
    SELECT COUNT(*) > 0 INTO nPartiallyReceived FROM
       (
        SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
          FROM purchase_order_lines pol
          LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
          WHERE PurchaseOrderNo = nPurchaseOrderNo
          GROUP BY PurchaseOrderLineID
          HAVING QtyReceived > 0
       ) result;    
  END IF;
  
  IF nCompletelyReceived THEN
    SET nResult = 2;
  ELSEIF nPartiallyReceived THEN
    SET nResult = 1;
  ELSE
    SET nResult = 0;
  END IF;

  RETURN nResult;

ENDBEGIN
  # (SS,v1.00,2/5/14) Returns 1 when given purchase order has all ordered items received, 2 when some items received, 0 when nothing received
  DECLARE nCompletelyReceived TINYINT(1) DEFAULT 0;
  DECLARE nPartiallyReceived TINYINT(1) DEFAULT 0;
  DECLARE nResult TINYINT(1) DEFAULT 0;

  SELECT COUNT(*) = 0 INTO nCompletelyReceived FROM
  (
    SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
    FROM purchase_order_lines pol
    LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
    WHERE PurchaseOrderNo = nPurchaseOrderNo
    GROUP BY PurchaseOrderLineID
    HAVING QtyOrdered > QtyReceived
  ) result;
  
  # if not completely received then check if partially received
  IF nCompletelyReceived = FALSE THEN
    SELECT COUNT(*) > 0 INTO nPartiallyReceived FROM
       (
        SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
          FROM purchase_order_lines pol
          LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
          WHERE PurchaseOrderNo = nPurchaseOrderNo
          GROUP BY PurchaseOrderLineID
          HAVING QtyReceived > 0
       ) result;    
  END IF;
  
  IF nCompletelyReceived THEN
    SET nResult = 2;
  ELSEIF nPartiallyReceived THEN
    SET nResult = 1;
  ELSE
    SET nResult = 0;
  END IF;

  RETURN nResult;

END�b���b���** hyperflightRenumberPicturesSingleProductRenumberPicturesSingleProductlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sProductCode VARCHAR(75)
    int(11)BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
END�g�c�g�c�
5�� jms_demo_rhesfuncGetBranchNameByBranchIDfuncGetBranchNameByBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%nBranchID INT(12)
    varchar(200) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
DECLARE sBranchName VARCHAR(200) DEFAULT "UNKNOWN";
SELECT BranchName INTO sBranchName FROM branches WHERE BranchID=nBranchID;
RETURN sBranchName;
ENDBEGIN
DECLARE sBranchName VARCHAR(200) DEFAULT "UNKNOWN";
SELECT BranchName INTO sBranchName FROM branches WHERE BranchID=nBranchID;
RETURN sBranchName;
END�g�c�g�c�
�� jms_demo_rhesfuncGetContractValuefuncGetContractValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    doubleBEGIN
DECLARE nResult DOUBLE DEFAULT 0.00;
SELECT SellPrice INTO nResult FROM jobs WHERE InvoiceNet=0 AND JobID=sJobID AND CustomerID IN (1709,2150,1270,2135,678,336,1897);
RETURN nResult;
ENDBEGIN
DECLARE nResult DOUBLE DEFAULT 0.00;
SELECT SellPrice INTO nResult FROM jobs WHERE InvoiceNet=0 AND JobID=sJobID AND CustomerID IN (1709,2150,1270,2135,678,336,1897);
RETURN nResult;
END�7j.F
$:
���k�Z�b��ab��ah
h
 PgroundscrewcenGetStockRequiredGetStockRequiredutf8mb3utf8mb3_general_cilatin1_swedish_cigroundscrewcen@%IN `nSupplierID` INTEGER(11), IN `nPastWeeks` INTEGER(11), IN `nFollowingWeeks` INTEGER(11), IN `bRequiredOnly` TINYINT(1)
    BEGIN

CALL GetInStockRatios(DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK));

# (SS,3/4/14) made to make the following create a temporary table because
# in Shopping Admin using CALL GetStockRequired(2, 52, 1, TRUE);
# was causing error: "can't return a result set in the given context"
# while testing it seemed to work if coMultiQueries option used, but this causes issues elsewhere
DROP TEMPORARY TABLE IF EXISTS tmp_stock_required;

CREATE TEMPORARY TABLE tmp_stock_required
# (SS,25/4/17) changed orderdetails.ProductName to products.ProductName to ensure the latest product name
# (SS,10/4/18) added products.SortOrder
SELECT products.SupplierID, suppliers.SupplierName, orderdetails.ProductID, orderdetails.ProductCode, products.ProductName, products.SortOrder,
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks + 0.4999) AS QtyRequiredNoRatio, 
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999) AS QtyRequired, 
NumInStock, on_order.QtyOrdered, on_order.QtyReceived, on_order.QtyAddedToStock,
# QtyToOrder calculated using QtyRequired - (NumInStock + COALESCE(QtyOrdered, 0) - COALESCE(QtyAddedToStock, 0))
# i.e. QtyRequired - (NumInStock + QtyOrdered - QtyAddedToStock)
# (SS,5/9/16) replaced QtyAddedToStock with QtyReceived
# i.e. now QtyRequired - (NumInStock + QtyOrdered - QtyReceived)
(ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999)) - (NumInStock + COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0)) AS QtyToOrder,

COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0) AS QtyOnOrder, products.CostPrice,
tmp_in_stock_ratios.InStockRatio
FROM orderdetails
INNER JOIN orders ON orders.OrderNo = orderdetails.OrderNo
INNER JOIN products ON products.ProductCode = orderdetails.ProductCode
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
LEFT JOIN tmp_in_stock_ratios ON tmp_in_stock_ratios.ProductID = products.ProductID
LEFT JOIN

(SELECT pol.ProductID, SUM(QtyOrdered) AS QtyOrdered, ordered_and_received.QtyReceived, ordered_and_received.QtyAddedToStock
FROM purchase_order_lines pol
LEFT JOIN

  (SELECT ProductID, SUM(QtyReceived) AS QtyReceived, SUM(QtyAddedToStock) AS QtyAddedToStock
  FROM purchase_invoice_lines pil
  INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
  INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
  WHERE (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") AND ProductID IS NOT NULL
  GROUP BY ProductID
  ) AS ordered_and_received ON ordered_and_received.ProductID = pol.ProductID
  
INNER JOIN purchase_orders ON purchase_orders.PurchaseOrderNo = pol.PurchaseOrderNo

WHERE OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED"
GROUP BY ProductID
) AS on_order ON on_order.ProductID = orderdetails.ProductID

# (SS,29/4/14) changed nSupplierID = 0 to nSupplierID = -1 (because 0 is when supplier hasn't been chosen to prevent slow adding of new order, -1 when the report is run for all suppliers)
WHERE Status <> "CANCELLED" AND NumInStock IS NOT NULL AND ProductDisabled = False AND DateTimeOrdered >= DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK) 
AND (nSupplierID = -1 OR suppliers.SupplierID = nSupplierID)
GROUP BY suppliers.SupplierName, ProductCode
HAVING NOT bRequiredOnly OR QtyRequired > NumInStock + QtyOnOrder;

ENDBEGIN

CALL GetInStockRatios(DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK));

# (SS,3/4/14) made to make the following create a temporary table because
# in Shopping Admin using CALL GetStockRequired(2, 52, 1, TRUE);
# was causing error: "can't return a result set in the given context"
# while testing it seemed to work if coMultiQueries option used, but this causes issues elsewhere
DROP TEMPORARY TABLE IF EXISTS tmp_stock_required;

CREATE TEMPORARY TABLE tmp_stock_required
# (SS,25/4/17) changed orderdetails.ProductName to products.ProductName to ensure the latest product name
# (SS,10/4/18) added products.SortOrder
SELECT products.SupplierID, suppliers.SupplierName, orderdetails.ProductID, orderdetails.ProductCode, products.ProductName, products.SortOrder,
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks + 0.4999) AS QtyRequiredNoRatio, 
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999) AS QtyRequired, 
NumInStock, on_order.QtyOrdered, on_order.QtyReceived, on_order.QtyAddedToStock,
# QtyToOrder calculated using QtyRequired - (NumInStock + COALESCE(QtyOrdered, 0) - COALESCE(QtyAddedToStock, 0))
# i.e. QtyRequired - (NumInStock + QtyOrdered - QtyAddedToStock)
# (SS,5/9/16) replaced QtyAddedToStock with QtyReceived
# i.e. now QtyRequired - (NumInStock + QtyOrdered - QtyReceived)
(ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999)) - (NumInStock + COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0)) AS QtyToOrder,

COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0) AS QtyOnOrder, products.CostPrice,
tmp_in_stock_ratios.InStockRatio
FROM orderdetails
INNER JOIN orders ON orders.OrderNo = orderdetails.OrderNo
INNER JOIN products ON products.ProductCode = orderdetails.ProductCode
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
LEFT JOIN tmp_in_stock_ratios ON tmp_in_stock_ratios.ProductID = products.ProductID
LEFT JOIN

(SELECT pol.ProductID, SUM(QtyOrdered) AS QtyOrdered, ordered_and_received.QtyReceived, ordered_and_received.QtyAddedToStock
FROM purchase_order_lines pol
LEFT JOIN

  (SELECT ProductID, SUM(QtyReceived) AS QtyReceived, SUM(QtyAddedToStock) AS QtyAddedToStock
  FROM purchase_invoice_lines pil
  INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
  INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
  WHERE (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") AND ProductID IS NOT NULL
  GROUP BY ProductID
  ) AS ordered_and_received ON ordered_and_received.ProductID = pol.ProductID
  
INNER JOIN purchase_orders ON purchase_orders.PurchaseOrderNo = pol.PurchaseOrderNo

WHERE OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED"
GROUP BY ProductID
) AS on_order ON on_order.ProductID = orderdetails.ProductID

# (SS,29/4/14) changed nSupplierID = 0 to nSupplierID = -1 (because 0 is when supplier hasn't been chosen to prevent slow adding of new order, -1 when the report is run for all suppliers)
WHERE Status <> "CANCELLED" AND NumInStock IS NOT NULL AND ProductDisabled = False AND DateTimeOrdered >= DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK) 
AND (nSupplierID = -1 OR suppliers.SupplierID = nSupplierID)
GROUP BY suppliers.SupplierName, ProductCode
HAVING NOT bRequiredOnly OR QtyRequired > NumInStock + QtyOnOrder;

END�g�c�g�c�
5�� jms_demo_rhesfuncGetCustomerNamefuncGetCustomerNamelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    varchar(200) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
	DECLARE sCustomerName VARCHAR(200) DEFAULT "UNKNOWN";
	SELECT customers.CustomerName INTO sCustomerName FROM customers JOIN jobs ON customers.CustomerID=jobs.CustomerID WHERE jobs.JobID=sJobID;
	RETURN sCustomerName;
ENDBEGIN
	DECLARE sCustomerName VARCHAR(200) DEFAULT "UNKNOWN";
	SELECT customers.CustomerName INTO sCustomerName FROM customers JOIN jobs ON customers.CustomerID=jobs.CustomerID WHERE jobs.JobID=sJobID;
	RETURN sCustomerName;
END���_+J@�0�b���b���$$
&
yy PhyperflightAdvanceOrderStatusForPurchaseInvoiceAdvanceOrderStatusForPurchaseInvoiceutf8mb3utf8mb3_general_cilatin1_swedish_cihyperflight@%`nPurchaseInvoiceID` INTEGER(11)
    tinyint(1)BEGIN
  # (SS,v1.00,2/5/14)
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nPurchaseOrderNo INTEGER(11);
  DECLARE sOrderStatus VARCHAR(20);
  DECLARE sNewStatus VARCHAR(20);
  DECLARE nPurchaseOrderCompleted TINYINT(1);
  DECLARE bOrderStatusChanged TINYINT(1) DEFAULT 0;
  
  # get list of purchase orders for given purchase invoice that haven't been marked as completed   
  DECLARE curPurchaseOrders CURSOR FOR
    SELECT pol.PurchaseOrderNo, po.OrderStatus
    FROM purchase_invoice_lines pil
    INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
    INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
    WHERE pil.PurchaseInvoiceID = nPurchaseInvoiceID AND (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") 
    GROUP BY PurchaseOrderNo;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;     
   
  OPEN curPurchaseOrders;
  
  nextrecord: LOOP 
  
    FETCH curPurchaseOrders INTO nPurchaseOrderNo, sOrderStatus;
    IF bFINISHED THEN
      LEAVE nextrecord;
    END IF;
    
    # call function to check if this purchase order is completed, returns 1 if partially received, 2 if completely received
    SELECT IsPurchaseOrderCompleted(nPurchaseOrderNo) INTO nPurchaseOrderCompleted;
    
    # check if status needs to be changed and what to
    IF nPurchaseOrderCompleted = 1 AND sOrderStatus <> "PARTIALLY RECEIVED" THEN
       SET sNewStatus = "PARTIALLY RECEIVED";
     ELSEIF nPurchaseOrderCompleted = 2 AND sOrderStatus <> "COMPLETED" THEN
       SET sNewStatus = "COMPLETED";
     ELSE
         SET sNewStatus = "";
     END IF;
     
     # if new status to be set then set it
     IF sNewStatus <> "" THEN 
       UPDATE purchase_orders SET OrderStatus = sNewStatus, InternalNotes = CONCAT(IF(COALESCE(InternalNotes, "") = "", "", CONCAT(InternalNotes, "\r\n")), DATE_FORMAT(NOW(), "%d/%m/%Y %H:%i"), ", Status auto-advanced to ", sNewStatus) WHERE PurchaseOrderNo = nPurchaseOrderNo;
       SET bOrderStatusChanged = TRUE;       
     END IF;   
      
  END LOOP nextrecord;  
  
  RETURN bOrderStatusChanged;
ENDBEGIN
  # (SS,v1.00,2/5/14)
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nPurchaseOrderNo INTEGER(11);
  DECLARE sOrderStatus VARCHAR(20);
  DECLARE sNewStatus VARCHAR(20);
  DECLARE nPurchaseOrderCompleted TINYINT(1);
  DECLARE bOrderStatusChanged TINYINT(1) DEFAULT 0;
  
  # get list of purchase orders for given purchase invoice that haven't been marked as completed   
  DECLARE curPurchaseOrders CURSOR FOR
    SELECT pol.PurchaseOrderNo, po.OrderStatus
    FROM purchase_invoice_lines pil
    INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
    INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
    WHERE pil.PurchaseInvoiceID = nPurchaseInvoiceID AND (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") 
    GROUP BY PurchaseOrderNo;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;     
   
  OPEN curPurchaseOrders;
  
  nextrecord: LOOP 
  
    FETCH curPurchaseOrders INTO nPurchaseOrderNo, sOrderStatus;
    IF bFINISHED THEN
      LEAVE nextrecord;
    END IF;
    
    # call function to check if this purchase order is completed, returns 1 if partially received, 2 if completely received
    SELECT IsPurchaseOrderCompleted(nPurchaseOrderNo) INTO nPurchaseOrderCompleted;
    
    # check if status needs to be changed and what to
    IF nPurchaseOrderCompleted = 1 AND sOrderStatus <> "PARTIALLY RECEIVED" THEN
       SET sNewStatus = "PARTIALLY RECEIVED";
     ELSEIF nPurchaseOrderCompleted = 2 AND sOrderStatus <> "COMPLETED" THEN
       SET sNewStatus = "COMPLETED";
     ELSE
         SET sNewStatus = "";
     END IF;
     
     # if new status to be set then set it
     IF sNewStatus <> "" THEN 
       UPDATE purchase_orders SET OrderStatus = sNewStatus, InternalNotes = CONCAT(IF(COALESCE(InternalNotes, "") = "", "", CONCAT(InternalNotes, "\r\n")), DATE_FORMAT(NOW(), "%d/%m/%Y %H:%i"), ", Status auto-advanced to ", sNewStatus) WHERE PurchaseOrderNo = nPurchaseOrderNo;
       SET bOrderStatusChanged = TRUE;       
     END IF;   
      
  END LOOP nextrecord;  
  
  RETURN bOrderStatusChanged;
END�b���b���
�� PhyperflightGetInStockRatiosGetInStockRatiosutf8mb3utf8mb3_general_cilatin1_swedish_cihyperflight@%IN `dStartDate` DATE
    BEGIN

  DROP TEMPORARY TABLE IF EXISTS tmp_total_stock_days;
  CREATE TEMPORARY TABLE tmp_total_stock_days
  SELECT stock_snapshots.ProductID, COUNT(*) AS TotalDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_days;
  CREATE TEMPORARY TABLE tmp_in_stock_days
  SELECT ProductID, COUNT(*) AS InStockDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate AND NumInStock > 0
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_ratios;
  CREATE TEMPORARY TABLE tmp_in_stock_ratios
  SELECT tmp_total_stock_days.*, tmp_in_stock_days.InStockDays, 
  # ROUND in following removes the data truncated warning seen in HeidiSQL
  COALESCE(ROUND(InStockDays / TotalDays * 100, 2), 100) AS InStockRatio
  FROM tmp_total_stock_days
  LEFT JOIN tmp_in_stock_days ON tmp_in_stock_days.ProductID = tmp_total_stock_days.ProductID;

ENDBEGIN

  DROP TEMPORARY TABLE IF EXISTS tmp_total_stock_days;
  CREATE TEMPORARY TABLE tmp_total_stock_days
  SELECT stock_snapshots.ProductID, COUNT(*) AS TotalDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_days;
  CREATE TEMPORARY TABLE tmp_in_stock_days
  SELECT ProductID, COUNT(*) AS InStockDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate AND NumInStock > 0
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_ratios;
  CREATE TEMPORARY TABLE tmp_in_stock_ratios
  SELECT tmp_total_stock_days.*, tmp_in_stock_days.InStockDays, 
  # ROUND in following removes the data truncated warning seen in HeidiSQL
  COALESCE(ROUND(InStockDays / TotalDays * 100, 2), 100) AS InStockRatio
  FROM tmp_total_stock_days
  LEFT JOIN tmp_in_stock_days ON tmp_in_stock_days.ProductID = tmp_total_stock_days.ProductID;

END�g�c�g�c�
�� jms_demo_rhesfuncGetInvoiceValuefuncGetInvoiceValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    doubleBEGIN
DECLARE nInvoiceValue DOUBLE DEFAULT 0.00;
SELECT (NumberOff*QuotedPrice) INTO nInvoiceValue FROM jobs WHERE JobID=sJobID;
RETURN nInvoiceValue;
ENDBEGIN
DECLARE nInvoiceValue DOUBLE DEFAULT 0.00;
SELECT (NumberOff*QuotedPrice) INTO nInvoiceValue FROM jobs WHERE JobID=sJobID;
RETURN nInvoiceValue;
END�g�c�g�c�
4 jms_demo_rhesfuncGetJobTypefuncGetJobTypelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    varchar(50) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE sJobType VARCHAR(50) DEFAULT "Job Type not set!";
SELECT JobTypeID INTO nJobTypeID FROM jobs WHERE JobID=sJobID;
SELECT JobType INTO sJobType FROM job_types WHERE JobTypeID=nJobTypeID;
RETURN sJobType;
ENDBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE sJobType VARCHAR(50) DEFAULT "Job Type not set!";
SELECT JobTypeID INTO nJobTypeID FROM jobs WHERE JobID=sJobID;
SELECT JobType INTO sJobType FROM job_types WHERE JobTypeID=nJobTypeID;
RETURN sJobType;
END������09���b���b���
$
YY PhyperflightIsPurchaseOrderCompletedIsPurchaseOrderCompletedutf8mb3utf8mb3_general_cilatin1_swedish_cihyperflight@%`nPurchaseOrderNo` INTEGER(11)
    tinyint(1)BEGIN
  # (SS,v1.00,2/5/14) Returns 1 when given purchase order has all ordered items received, 2 when some items received, 0 when nothing received
  DECLARE nCompletelyReceived TINYINT(1) DEFAULT 0;
  DECLARE nPartiallyReceived TINYINT(1) DEFAULT 0;
  DECLARE nResult TINYINT(1) DEFAULT 0;

  SELECT COUNT(*) = 0 INTO nCompletelyReceived FROM
  (
    SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
    FROM purchase_order_lines pol
    LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
    WHERE PurchaseOrderNo = nPurchaseOrderNo
    GROUP BY PurchaseOrderLineID
    HAVING QtyOrdered > QtyReceived
  ) result;
  
  # if not completely received then check if partially received
  IF nCompletelyReceived = FALSE THEN
    SELECT COUNT(*) > 0 INTO nPartiallyReceived FROM
       (
        SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
          FROM purchase_order_lines pol
          LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
          WHERE PurchaseOrderNo = nPurchaseOrderNo
          GROUP BY PurchaseOrderLineID
          HAVING QtyReceived > 0
       ) result;    
  END IF;
  
  IF nCompletelyReceived THEN
    SET nResult = 2;
  ELSEIF nPartiallyReceived THEN
    SET nResult = 1;
  ELSE
    SET nResult = 0;
  END IF;

  RETURN nResult;

ENDBEGIN
  # (SS,v1.00,2/5/14) Returns 1 when given purchase order has all ordered items received, 2 when some items received, 0 when nothing received
  DECLARE nCompletelyReceived TINYINT(1) DEFAULT 0;
  DECLARE nPartiallyReceived TINYINT(1) DEFAULT 0;
  DECLARE nResult TINYINT(1) DEFAULT 0;

  SELECT COUNT(*) = 0 INTO nCompletelyReceived FROM
  (
    SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
    FROM purchase_order_lines pol
    LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
    WHERE PurchaseOrderNo = nPurchaseOrderNo
    GROUP BY PurchaseOrderLineID
    HAVING QtyOrdered > QtyReceived
  ) result;
  
  # if not completely received then check if partially received
  IF nCompletelyReceived = FALSE THEN
    SELECT COUNT(*) > 0 INTO nPartiallyReceived FROM
       (
        SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
          FROM purchase_order_lines pol
          LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
          WHERE PurchaseOrderNo = nPurchaseOrderNo
          GROUP BY PurchaseOrderLineID
          HAVING QtyReceived > 0
       ) result;    
  END IF;
  
  IF nCompletelyReceived THEN
    SET nResult = 2;
  ELSEIF nPartiallyReceived THEN
    SET nResult = 1;
  ELSE
    SET nResult = 0;
  END IF;

  RETURN nResult;

END�g�c�g�c�
�� jms_demo_rhesfuncGetMaterialCostfuncGetMaterialCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�g�c�g�c�


 jms_demo_rhesfuncGetMaterialCostNewfuncGetMaterialCostNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  # Serial: 2008-12-19-01

  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID;  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  # Serial: 2008-12-19-01

  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID;  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�b���b����� hyperflightRenumberPicturesAllProductsRenumberPicturesAllProductslatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
END�g�c�g�c�
5�� jms_demo_rhesfuncGetRepairEngineerfuncGetRepairEngineerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    varchar(100) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "Repair Engineer not set!";
    SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=sJobID;
    RETURN sEngineerName;
ENDW��X*��
��
4�U����i���i���
��� ThyperflightAddStockAuditRecordAddStockAuditRecordutf8mb4utf8mb4_general_cilatin1_swedish_cihyperflight@%
	IN `nOldNumInStock` INTEGER(11),
	IN `nNewNumInStock` INTEGER(11),
	IN `sAuditType` VARCHAR(6),
	IN `nProductID` INTEGER(11),
	IN `nProductOptionValueID` INTEGER(11),
	IN `bForceAdd` TINYINT(1)
BEGIN
  -- (SS,04/07/2013) to record changes to stock in tables products and product_option_values number to stock_audit table
  -- (SS,02/06/2022) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  -- (SS,17/03/2026) added SourceType, SourceRef, AuditNote tracking, with help from Gemini
  
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        -- lookup audit record using connection ID where user is still logged on
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
     
     SET @LocalSourceType = @AuditSourceType;
     SET @LocalSourceRef = @AuditSourceRef;
     SET @LocalNote = @AuditNote;
  
     INSERT INTO stock_audit (
         ProductID, AuditType, StockBefore, StockAfter, 
         UserName, PCName, ConnectionID, ProductOptionValueID,
         SourceType, SourceRef, AuditNote
     ) 
     VALUES (
         nProductID, sAuditType, nOldNumInStock, nNewNumInStock, 
         SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID,
         @LocalSourceType, @LocalSourceRef, @LocalNote
     );
     
  END IF; 
ENDBEGIN
  -- (SS,04/07/2013) to record changes to stock in tables products and product_option_values number to stock_audit table
  -- (SS,02/06/2022) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  -- (SS,17/03/2026) added SourceType, SourceRef, AuditNote tracking, with help from Gemini
  
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        -- lookup audit record using connection ID where user is still logged on
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
     
     SET @LocalSourceType = @AuditSourceType;
     SET @LocalSourceRef = @AuditSourceRef;
     SET @LocalNote = @AuditNote;
  
     INSERT INTO stock_audit (
         ProductID, AuditType, StockBefore, StockAfter, 
         UserName, PCName, ConnectionID, ProductOptionValueID,
         SourceType, SourceRef, AuditNote
     ) 
     VALUES (
         nProductID, sAuditType, nOldNumInStock, nNewNumInStock, 
         SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID,
         @LocalSourceType, @LocalSourceRef, @LocalNote
     );
     
  END IF; 
END�g�c�g�c�
�� jms_demo_rhesfuncIsJobQuotationfuncIsJobQuotationlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID INT(12)
    int(12)BEGIN
DECLARE nResult INT(12) DEFAULT 0;
SELECT NumberOff INTO nResult FROM jobs JOIN job_types ON jobs.JobTypeID=job_types.JobTypeID WHERE JobID=sJobID AND job_types.JobType="Quotation";
RETURN nResult;
ENDBEGIN
DECLARE nResult INT(12) DEFAULT 0;
SELECT NumberOff INTO nResult FROM jobs JOIN job_types ON jobs.JobTypeID=job_types.JobTypeID WHERE JobID=sJobID AND job_types.JobType="Quotation";
RETURN nResult;
END�b���b���** vintagedoorknobsRenumberPicturesSingleProductRenumberPicturesSingleProductlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sProductCode VARCHAR(75)
    int(11)BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for given product code, starting from blank for first picture, 2 for second, 3 for third etc.
  # to prevent holes when picture deleted and to ensure they appear on the site in sequence
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nPictureCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nPictureID INTEGER;
  DECLARE sPictureNo VARCHAR(5);

  DECLARE cPictures CURSOR FOR
     SELECT PictureID, Code2 FROM pictures
     WHERE Type = "P" AND Code = sProductCode
     ORDER BY Type, Code, Code2;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cPictures;

  record_loop: LOOP
    FETCH cPictures INTO nPictureID, sPictureNo;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;

    SET nPictureCount = nPictureCount + 1;
    # if Code2 does not match the picture count then update it, following works without using the CONVERT function
    IF (nPictureCount = 1 AND sPictureNo <> "") OR (nPictureCount > 1 AND CONVERT(sPictureNo, UNSIGNED) <> nPictureCount) THEN
      SET nRenumberCount = nRenumberCount + 1;
      UPDATE pictures SET Code2 = nPictureCount WHERE PictureID = nPictureID;
    END IF;

  END LOOP record_loop;

  CLOSE cPictures;

  # useful for testing info purposes
  RETURN nRenumberCount;
END�g�c�g�c�
�� jms_demo_rhesfuncJobBecomesWarrantyItemCountfuncJobBecomesWarrantyItemCountlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nResult INT(12);
    SELECT SUM(NumberOff) INTO nResult FROM jobs WHERE CWJobID=sJobID;
    IF nResult IS NULL THEN
        SET nResult=0;
    END IF;
    RETURN nResult;
ENDBEGIN
    DECLARE nResult INT(12);
    SELECT SUM(NumberOff) INTO nResult FROM jobs WHERE CWJobID=sJobID;
    IF nResult IS NULL THEN
        SET nResult=0;
    END IF;
    RETURN nResult;
END�N�)R
QF
���U����b���b���!!EE hyperflightRefreshProductCategoriesMainCacheRefreshProductCategoriesMainCachelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1BEGIN

# (SS,13/4/12) create temp table listing the main category/subcategory for each product
# used for Galleon to determine category for export list to Feefo which requires category
# (SS,16/5/12) later adapted for all sites to hold main category, for use by related product list
# a new table called product_categories_main_cache is updated

DROP TABLE IF EXISTS tmp_productcategories;

CREATE TABLE tmp_productcategories (
  ID INTEGER(11) NOT NULL AUTO_INCREMENT,
  ProductCode VARCHAR(20) NOT NULL DEFAULT '',
  Category VARCHAR(75) NOT NULL DEFAULT '',
  Subcategory VARCHAR(75) NOT NULL DEFAULT '',
  SortOrder INTEGER(11) DEFAULT '0',
  Priority INTEGER(11) DEFAULT '1',
  SortOrder2 INTEGER(11) DEFAULT '0',
  Priority2 INTEGER(11) DEFAULT '1',
  PRIMARY KEY (ID)
);

# following will create table in correct order with most important one with lowest ID
# we don't actually need to add the fields  SortOrder, Priority, SortOrder2, Priority, but useful to help see that it's working correctly
INSERT INTO tmp_productcategories (ProductCode, Category, Subcategory, SortOrder, Priority, SortOrder2, Priority2) 
SELECT pc.ProductCode, pc.Category, pc.Subcategory, c.SortOrder, c.Priority, s.SortOrder AS SortOrder2, s.Priority AS Priority2
FROM productcategories pc
INNER JOIN categories c ON c.Category = pc.Category
INNER JOIN subcategories s ON s.Category = pc.Category AND s.Subcategory = pc.Subcategory
ORDER BY ProductCode, c.Priority, s.Priority, c.SortOrder, s.SortOrder, Category, Subcategory;

# following with help from http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
# temporary table can't be used because you can't refer to same table more than once, as in following
# see http://bugs.mysql.com/bug.php?id=10327             
TRUNCATE product_categories_main_cache;

INSERT INTO product_categories_main_cache
	SELECT ProductCode, Category, Subcategory
	FROM   tmp_productcategories c1
	WHERE  ID = (SELECT MIN(c2.ID)
              FROM tmp_productcategories c2
              WHERE c1.ProductCode = c2.ProductCode);
              
DROP TABLE tmp_productcategories;

ENDBEGIN

# (SS,13/4/12) create temp table listing the main category/subcategory for each product
# used for Galleon to determine category for export list to Feefo which requires category
# (SS,16/5/12) later adapted for all sites to hold main category, for use by related product list
# a new table called product_categories_main_cache is updated

DROP TABLE IF EXISTS tmp_productcategories;

CREATE TABLE tmp_productcategories (
  ID INTEGER(11) NOT NULL AUTO_INCREMENT,
  ProductCode VARCHAR(20) NOT NULL DEFAULT '',
  Category VARCHAR(75) NOT NULL DEFAULT '',
  Subcategory VARCHAR(75) NOT NULL DEFAULT '',
  SortOrder INTEGER(11) DEFAULT '0',
  Priority INTEGER(11) DEFAULT '1',
  SortOrder2 INTEGER(11) DEFAULT '0',
  Priority2 INTEGER(11) DEFAULT '1',
  PRIMARY KEY (ID)
);

# following will create table in correct order with most important one with lowest ID
# we don't actually need to add the fields  SortOrder, Priority, SortOrder2, Priority, but useful to help see that it's working correctly
INSERT INTO tmp_productcategories (ProductCode, Category, Subcategory, SortOrder, Priority, SortOrder2, Priority2) 
SELECT pc.ProductCode, pc.Category, pc.Subcategory, c.SortOrder, c.Priority, s.SortOrder AS SortOrder2, s.Priority AS Priority2
FROM productcategories pc
INNER JOIN categories c ON c.Category = pc.Category
INNER JOIN subcategories s ON s.Category = pc.Category AND s.Subcategory = pc.Subcategory
ORDER BY ProductCode, c.Priority, s.Priority, c.SortOrder, s.SortOrder, Category, Subcategory;

# following with help from http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
# temporary table can't be used because you can't refer to same table more than once, as in following
# see http://bugs.mysql.com/bug.php?id=10327             
TRUNCATE product_categories_main_cache;

INSERT INTO product_categories_main_cache
	SELECT ProductCode, Category, Subcategory
	FROM   tmp_productcategories c1
	WHERE  ID = (SELECT MIN(c2.ID)
              FROM tmp_productcategories c2
              WHERE c1.ProductCode = c2.ProductCode);
              
DROP TABLE tmp_productcategories;

END�b��b���99 TvintagedoorknobsAddStockAuditRecordAddStockAuditRecordutf8mb4utf8mb4_general_cilatin1_swedish_civintagedoorknobs@%
	IN `nOldNumInStock` INTEGER(11),
	IN `nNewNumInStock` INTEGER(11),
	IN `sAuditType` VARCHAR(6),
	IN `nProductID` INTEGER(11),
	IN `nProductOptionValueID` INTEGER(11),
	IN `bForceAdd` TINYINT(1)
    
BEGIN
  # (SS,4/7/13) to record changes to stock in tables products and product_option_values number to stock_audit table,
  # also tries to get the PC name if applicable using audit table
  # called from insert and update triggers on the products and product_option_values tables
  # bForceAdd is true when inserting a record
  # (SS,2/6/22) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        # lookup audit record using connection ID where user is still logged on and     
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
  
    INSERT INTO stock_audit (ProductID, AuditType, StockBefore, StockAfter, UserName, PCName, ConnectionID, ProductOptionValueID) 
           VALUES (nProductID, sAuditType, nOldNumInStock, nNewNumInStock, SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID);
  END IF; 
ENDBEGIN
  # (SS,4/7/13) to record changes to stock in tables products and product_option_values number to stock_audit table,
  # also tries to get the PC name if applicable using audit table
  # called from insert and update triggers on the products and product_option_values tables
  # bForceAdd is true when inserting a record
  # (SS,2/6/22) added AND @username NOT LIKE '%::1' after upgrade to MariaDB
  IF bForceAdd OR COALESCE(nOldNumInStock, "") <> COALESCE(nNewNumInStock, "") THEN
     SET @connectionid = CONNECTION_ID();
     SET @username = USER();
     SET @pcname = NULL;
  
     IF @username NOT LIKE '%127.0.0.1' AND @username NOT LIKE '%localhost' AND @username NOT LIKE '%::1' THEN
        # lookup audit record using connection ID where user is still logged on and     
        SET @pcname = (SELECT PCName FROM audit WHERE ConnectionID = @connectionid AND LogOff = 0 AND DATEDIFF(CURDATE(), LogOn) <= 7 ORDER BY AuditID DESC LIMIT 1);
     ELSE
        SET @pcname = '-WEBSITE-';    
     END IF;
  
    INSERT INTO stock_audit (ProductID, AuditType, StockBefore, StockAfter, UserName, PCName, ConnectionID, ProductOptionValueID) 
           VALUES (nProductID, sAuditType, nOldNumInStock, nNewNumInStock, SUBSTRING(@username, 1, 100), @pcname, @connectionid, nProductOptionValueID);
  END IF; 
ENDD�8���%�b���b���$$&
yy PvintagedoorknobsAdvanceOrderStatusForPurchaseInvoiceAdvanceOrderStatusForPurchaseInvoiceutf8mb3utf8mb3_general_cilatin1_swedish_civintagedoorknobs@%`nPurchaseInvoiceID` INTEGER(11)
    tinyint(1)BEGIN
  # (SS,v1.00,2/5/14)
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nPurchaseOrderNo INTEGER(11);
  DECLARE sOrderStatus VARCHAR(20);
  DECLARE sNewStatus VARCHAR(20);
  DECLARE nPurchaseOrderCompleted TINYINT(1);
  DECLARE bOrderStatusChanged TINYINT(1) DEFAULT 0;
  
  # get list of purchase orders for given purchase invoice that haven't been marked as completed   
  DECLARE curPurchaseOrders CURSOR FOR
    SELECT pol.PurchaseOrderNo, po.OrderStatus
    FROM purchase_invoice_lines pil
    INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
    INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
    WHERE pil.PurchaseInvoiceID = nPurchaseInvoiceID AND (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") 
    GROUP BY PurchaseOrderNo;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;     
   
  OPEN curPurchaseOrders;
  
  nextrecord: LOOP 
  
    FETCH curPurchaseOrders INTO nPurchaseOrderNo, sOrderStatus;
    IF bFINISHED THEN
      LEAVE nextrecord;
    END IF;
    
    # call function to check if this purchase order is completed, returns 1 if partially received, 2 if completely received
    SELECT IsPurchaseOrderCompleted(nPurchaseOrderNo) INTO nPurchaseOrderCompleted;
    
    # check if status needs to be changed and what to
    IF nPurchaseOrderCompleted = 1 AND sOrderStatus <> "PARTIALLY RECEIVED" THEN
       SET sNewStatus = "PARTIALLY RECEIVED";
     ELSEIF nPurchaseOrderCompleted = 2 AND sOrderStatus <> "COMPLETED" THEN
       SET sNewStatus = "COMPLETED";
     ELSE
         SET sNewStatus = "";
     END IF;
     
     # if new status to be set then set it
     IF sNewStatus <> "" THEN 
       UPDATE purchase_orders SET OrderStatus = sNewStatus, InternalNotes = CONCAT(IF(COALESCE(InternalNotes, "") = "", "", CONCAT(InternalNotes, "\r\n")), DATE_FORMAT(NOW(), "%d/%m/%Y %H:%i"), ", Status auto-advanced to ", sNewStatus) WHERE PurchaseOrderNo = nPurchaseOrderNo;
       SET bOrderStatusChanged = TRUE;       
     END IF;   
      
  END LOOP nextrecord;  
  
  RETURN bOrderStatusChanged;
ENDBEGIN
  # (SS,v1.00,2/5/14)
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nPurchaseOrderNo INTEGER(11);
  DECLARE sOrderStatus VARCHAR(20);
  DECLARE sNewStatus VARCHAR(20);
  DECLARE nPurchaseOrderCompleted TINYINT(1);
  DECLARE bOrderStatusChanged TINYINT(1) DEFAULT 0;
  
  # get list of purchase orders for given purchase invoice that haven't been marked as completed   
  DECLARE curPurchaseOrders CURSOR FOR
    SELECT pol.PurchaseOrderNo, po.OrderStatus
    FROM purchase_invoice_lines pil
    INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
    INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
    WHERE pil.PurchaseInvoiceID = nPurchaseInvoiceID AND (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") 
    GROUP BY PurchaseOrderNo;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;     
   
  OPEN curPurchaseOrders;
  
  nextrecord: LOOP 
  
    FETCH curPurchaseOrders INTO nPurchaseOrderNo, sOrderStatus;
    IF bFINISHED THEN
      LEAVE nextrecord;
    END IF;
    
    # call function to check if this purchase order is completed, returns 1 if partially received, 2 if completely received
    SELECT IsPurchaseOrderCompleted(nPurchaseOrderNo) INTO nPurchaseOrderCompleted;
    
    # check if status needs to be changed and what to
    IF nPurchaseOrderCompleted = 1 AND sOrderStatus <> "PARTIALLY RECEIVED" THEN
       SET sNewStatus = "PARTIALLY RECEIVED";
     ELSEIF nPurchaseOrderCompleted = 2 AND sOrderStatus <> "COMPLETED" THEN
       SET sNewStatus = "COMPLETED";
     ELSE
         SET sNewStatus = "";
     END IF;
     
     # if new status to be set then set it
     IF sNewStatus <> "" THEN 
       UPDATE purchase_orders SET OrderStatus = sNewStatus, InternalNotes = CONCAT(IF(COALESCE(InternalNotes, "") = "", "", CONCAT(InternalNotes, "\r\n")), DATE_FORMAT(NOW(), "%d/%m/%Y %H:%i"), ", Status auto-advanced to ", sNewStatus) WHERE PurchaseOrderNo = nPurchaseOrderNo;
       SET bOrderStatusChanged = TRUE;       
     END IF;   
      
  END LOOP nextrecord;  
  
  RETURN bOrderStatusChanged;
END�g�c�g�c�
�� jms_demo_rhesGetNextInvoiceNoGetNextInvoiceNolatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%nBranchID INTEGER(11)
    int(11)BEGIN
  DECLARE nNextInvoiceNo INTEGER;
  SELECT NextInvoiceNo INTO nNextInvoiceNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextInvoiceNo = NextInvoiceNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextInvoiceNo ;
ENDBEGIN
  DECLARE nNextInvoiceNo INTEGER;
  SELECT NextInvoiceNo INTO nNextInvoiceNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextInvoiceNo = NextInvoiceNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextInvoiceNo ;
END�g�c�g�c�
�� jms_demo_rhesGetNextOrderNoGetNextOrderNolatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%nBranchID INTEGER(11)
    int(11)BEGIN
  DECLARE nNextOrderNo INTEGER;
  SELECT NextOrderNo INTO nNextOrderNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextOrderNo = NextOrderNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextOrderNo;
ENDBEGIN
  DECLARE nNextOrderNo INTEGER;
  SELECT NextOrderNo INTO nNextOrderNo FROM branches WHERE BranchID = nBranchID;
  UPDATE branches SET NextOrderNo = NextOrderNo + 1 WHERE BranchID = nBranchID;
  RETURN nNextOrderNo;
END�g�c�g�c�


!�� Pjms_demo_rhesAddToEventLogAddToEventLoglatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%
 IN sEventMessage MEDIUMTEXT
 BEGIN
 # (SS,21/8/13) to add message to event_log table
 INSERT INTO event_log (EventDateTime, EventMessage) VALUES(NOW(), sEventMessage);
ENDBEGIN
 # (SS,21/8/13) to add message to event_log table
 INSERT INTO event_log (EventDateTime, EventMessage) VALUES(NOW(), sEventMessage);
END���t������:
�b���b���$
YY PvintagedoorknobsIsPurchaseOrderCompletedIsPurchaseOrderCompletedutf8mb3utf8mb3_general_cilatin1_swedish_civintagedoorknobs@%`nPurchaseOrderNo` INTEGER(11)
    tinyint(1)BEGIN
  # (SS,v1.00,2/5/14) Returns 1 when given purchase order has all ordered items received, 2 when some items received, 0 when nothing received
  DECLARE nCompletelyReceived TINYINT(1) DEFAULT 0;
  DECLARE nPartiallyReceived TINYINT(1) DEFAULT 0;
  DECLARE nResult TINYINT(1) DEFAULT 0;

  SELECT COUNT(*) = 0 INTO nCompletelyReceived FROM
  (
    SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
    FROM purchase_order_lines pol
    LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
    WHERE PurchaseOrderNo = nPurchaseOrderNo
    GROUP BY PurchaseOrderLineID
    HAVING QtyOrdered > QtyReceived
  ) result;
  
  # if not completely received then check if partially received
  IF nCompletelyReceived = FALSE THEN
    SELECT COUNT(*) > 0 INTO nPartiallyReceived FROM
       (
        SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
          FROM purchase_order_lines pol
          LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
          WHERE PurchaseOrderNo = nPurchaseOrderNo
          GROUP BY PurchaseOrderLineID
          HAVING QtyReceived > 0
       ) result;    
  END IF;
  
  IF nCompletelyReceived THEN
    SET nResult = 2;
  ELSEIF nPartiallyReceived THEN
    SET nResult = 1;
  ELSE
    SET nResult = 0;
  END IF;

  RETURN nResult;

ENDBEGIN
  # (SS,v1.00,2/5/14) Returns 1 when given purchase order has all ordered items received, 2 when some items received, 0 when nothing received
  DECLARE nCompletelyReceived TINYINT(1) DEFAULT 0;
  DECLARE nPartiallyReceived TINYINT(1) DEFAULT 0;
  DECLARE nResult TINYINT(1) DEFAULT 0;

  SELECT COUNT(*) = 0 INTO nCompletelyReceived FROM
  (
    SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
    FROM purchase_order_lines pol
    LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
    WHERE PurchaseOrderNo = nPurchaseOrderNo
    GROUP BY PurchaseOrderLineID
    HAVING QtyOrdered > QtyReceived
  ) result;
  
  # if not completely received then check if partially received
  IF nCompletelyReceived = FALSE THEN
    SELECT COUNT(*) > 0 INTO nPartiallyReceived FROM
       (
        SELECT PurchaseOrderNo, pol.PurchaseOrderLineID, ProductID, QtyOrdered, COALESCE(SUM(pil.QtyInvoiced), 0) AS QtyInvoiced, COALESCE(SUM(pil.QtyReceived), 0) AS QtyReceived
          FROM purchase_order_lines pol
          LEFT JOIN purchase_invoice_lines pil ON pil.PurchaseOrderLineID = pol.PurchaseOrderLineID
          WHERE PurchaseOrderNo = nPurchaseOrderNo
          GROUP BY PurchaseOrderLineID
          HAVING QtyReceived > 0
       ) result;    
  END IF;
  
  IF nCompletelyReceived THEN
    SET nResult = 2;
  ELSEIF nPartiallyReceived THEN
    SET nResult = 1;
  ELSE
    SET nResult = 0;
  END IF;

  RETURN nResult;

END�b���b���$�� PvintagedoorknobsGetInStockRatiosGetInStockRatioslatin1latin1_swedish_cilatin1_swedish_civintagedoorknobs@%
        IN `dStartDate` DATE
    BEGIN

  DROP TEMPORARY TABLE IF EXISTS tmp_total_stock_days;
  CREATE TEMPORARY TABLE tmp_total_stock_days
  SELECT stock_snapshots.ProductID, COUNT(*) AS TotalDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_days;
  CREATE TEMPORARY TABLE tmp_in_stock_days
  SELECT ProductID, COUNT(*) AS InStockDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate AND NumInStock > 0
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_ratios;
  CREATE TEMPORARY TABLE tmp_in_stock_ratios
  SELECT tmp_total_stock_days.*, tmp_in_stock_days.InStockDays, 
  # ROUND in following removes the data truncated warning seen in HeidiSQL
  COALESCE(ROUND(InStockDays / TotalDays * 100, 2), 100) AS InStockRatio
  FROM tmp_total_stock_days
  LEFT JOIN tmp_in_stock_days ON tmp_in_stock_days.ProductID = tmp_total_stock_days.ProductID;

ENDBEGIN

  DROP TEMPORARY TABLE IF EXISTS tmp_total_stock_days;
  CREATE TEMPORARY TABLE tmp_total_stock_days
  SELECT stock_snapshots.ProductID, COUNT(*) AS TotalDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_days;
  CREATE TEMPORARY TABLE tmp_in_stock_days
  SELECT ProductID, COUNT(*) AS InStockDays
  FROM stock_snapshots
  WHERE DateOfStock >= dStartDate AND NumInStock > 0
  GROUP BY ProductID;

  DROP TEMPORARY TABLE IF EXISTS tmp_in_stock_ratios;
  CREATE TEMPORARY TABLE tmp_in_stock_ratios
  SELECT tmp_total_stock_days.*, tmp_in_stock_days.InStockDays, 
  # ROUND in following removes the data truncated warning seen in HeidiSQL
  COALESCE(ROUND(InStockDays / TotalDays * 100, 2), 100) AS InStockRatio
  FROM tmp_total_stock_days
  LEFT JOIN tmp_in_stock_days ON tmp_in_stock_days.ProductID = tmp_total_stock_days.ProductID;

END�
!�
����b���b����� vintagedoorknobsRenumberPicturesAllProductsRenumberPicturesAllProductslatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1BEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
ENDBEGIN
  # (SS,29/6/10)
  # renumbers the pictures for all products, calls RenumberPicturesSingleProduct for each product
  DECLARE nFinished INTEGER DEFAULT 0;
  DECLARE nProductCount INTEGER DEFAULT 0;
  DECLARE nRenumberCount INTEGER DEFAULT 0;
  DECLARE nTotalRenumberCount INTEGER DEFAULT 0;
  DECLARE sProductCode VARCHAR(75);

  DECLARE cProducts CURSOR FOR SELECT DISTINCT Code FROM pictures WHERE Type = "P" ORDER BY Code;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET nFinished = 1;

  OPEN cProducts;

  record_loop: LOOP
    FETCH cProducts INTO sProductCode;
    IF nFinished THEN
      LEAVE record_loop;
    END IF;
    
    SELECT RenumberPicturesSingleProduct(sProductCode) INTO nRenumberCount;
    SET nTotalRenumberCount = nTotalRenumberCount + nRenumberCount;

    SET nProductCount = nProductCount + 1;

  END LOOP record_loop;

  CLOSE cProducts;

  # useful for testing info purposes
  SELECT nProductCount AS ProductCount, nTotalRenumberCount AS RenumberCount;
END�b��b��		
B�� jms_demoClearLockClearLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo@%nLockID INTEGER ( 11 ) ,
        sComputerName VARCHAR ( 20 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  # UPDATE appears before DELETE because of RETURN ROW_COUNT()
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  # UPDATE appears before DELETE because of RETURN ROW_COUNT()
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
END�b��b��MXX jms_demofuncGetAdjustedSalesValuefuncGetAdjustedSalesValuelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1nInvoiceValue DOUBLE,
        nMargin DOUBLE,
        nJobTypeID INT(12)
    doubleBEGIN
DECLARE nAdjustedSalesValue DOUBLE DEFAULT 0.00;
IF nJobTypeID=1 OR nJobTypeID=4 OR nJobTypeID=6 OR nJobTypeID=8 OR nJobTypeID=10 THEN
    SET nAdjustedSalesValue=nInvoiceValue;
    END IF;
    
IF nJobTypeID=2 OR nJobTypeID=3 OR nJobTypeID=12 THEN
    SET nAdjustedSalesValue=(nMargin*2);
    END IF;
    
RETURN nAdjustedSalesValue;
ENDBEGIN
DECLARE nAdjustedSalesValue DOUBLE DEFAULT 0.00;
IF nJobTypeID=1 OR nJobTypeID=4 OR nJobTypeID=6 OR nJobTypeID=8 OR nJobTypeID=10 THEN
    SET nAdjustedSalesValue=nInvoiceValue;
    END IF;
    
IF nJobTypeID=2 OR nJobTypeID=3 OR nJobTypeID=12 THEN
    SET nAdjustedSalesValue=(nMargin*2);
    END IF;
    
RETURN nAdjustedSalesValue;
END�b��b��/ww jms_demofuncGetDaysOverduefuncGetDaysOverduelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1`sJobID` CHAR(12),
        `sNowDate` DATE
    int(12)BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
   DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
   DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
END�w�i]�~�����b���b�����
�
 PvintagedoorknobsGetStockRequiredGetStockRequiredutf8mb3utf8mb3_general_cilatin1_swedish_civintagedoorknobs@%IN `nSupplierID` INTEGER(11), IN `nPastWeeks` INTEGER(11), IN `nFollowingWeeks` INTEGER(11), IN `bRequiredOnly` TINYINT(1)
    BEGIN

CALL GetInStockRatios(DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK));

# (SS,3/4/14) made to make the following create a temporary table because
# in Shopping Admin using CALL GetStockRequired(2, 52, 1, TRUE);
# was causing error: "can't return a result set in the given context"
# while testing it seemed to work if coMultiQueries option used, but this causes issues elsewhere
DROP TEMPORARY TABLE IF EXISTS tmp_stock_required;

CREATE TEMPORARY TABLE tmp_stock_required
# (SS,25/4/17) changed orderdetails.ProductName to products.ProductName to ensure the latest product name
# (SS,10/4/18) added products.SortOrder
SELECT products.SupplierID, suppliers.SupplierName, orderdetails.ProductID, orderdetails.ProductCode, products.ProductName, products.SortOrder,
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks + 0.4999) AS QtyRequiredNoRatio, 
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999) AS QtyRequired, 
NumInStock, on_order.QtyOrdered, on_order.QtyReceived, on_order.QtyAddedToStock,
# QtyToOrder calculated using QtyRequired - (NumInStock + COALESCE(QtyOrdered, 0) - COALESCE(QtyAddedToStock, 0))
# i.e. QtyRequired - (NumInStock + QtyOrdered - QtyAddedToStock)
# (SS,5/9/16) replaced QtyAddedToStock with QtyReceived
# i.e. now QtyRequired - (NumInStock + QtyOrdered - QtyReceived)
(ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999)) - (NumInStock + COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0)) AS QtyToOrder,

COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0) AS QtyOnOrder, products.CostPrice,
tmp_in_stock_ratios.InStockRatio
FROM orderdetails
INNER JOIN orders ON orders.OrderNo = orderdetails.OrderNo
INNER JOIN products ON products.ProductCode = orderdetails.ProductCode
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
LEFT JOIN tmp_in_stock_ratios ON tmp_in_stock_ratios.ProductID = products.ProductID
LEFT JOIN

(SELECT pol.ProductID, SUM(QtyOrdered) AS QtyOrdered, ordered_and_received.QtyReceived, ordered_and_received.QtyAddedToStock
FROM purchase_order_lines pol
LEFT JOIN

  (SELECT ProductID, SUM(QtyReceived) AS QtyReceived, SUM(QtyAddedToStock) AS QtyAddedToStock
  FROM purchase_invoice_lines pil
  INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
  INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
  WHERE (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") AND ProductID IS NOT NULL
  GROUP BY ProductID
  ) AS ordered_and_received ON ordered_and_received.ProductID = pol.ProductID
  
INNER JOIN purchase_orders ON purchase_orders.PurchaseOrderNo = pol.PurchaseOrderNo

WHERE OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED"
GROUP BY ProductID
) AS on_order ON on_order.ProductID = orderdetails.ProductID

# (SS,29/4/14) changed nSupplierID = 0 to nSupplierID = -1 (because 0 is when supplier hasn't been chosen to prevent slow adding of new order, -1 when the report is run for all suppliers)
WHERE Status <> "CANCELLED" AND NumInStock IS NOT NULL AND ProductDisabled = False AND DateTimeOrdered >= DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK) 
AND (nSupplierID = -1 OR suppliers.SupplierID = nSupplierID)
GROUP BY suppliers.SupplierName, ProductCode
HAVING NOT bRequiredOnly OR QtyRequired > NumInStock + QtyOnOrder;

ENDBEGIN

CALL GetInStockRatios(DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK));

# (SS,3/4/14) made to make the following create a temporary table because
# in Shopping Admin using CALL GetStockRequired(2, 52, 1, TRUE);
# was causing error: "can't return a result set in the given context"
# while testing it seemed to work if coMultiQueries option used, but this causes issues elsewhere
DROP TEMPORARY TABLE IF EXISTS tmp_stock_required;

CREATE TEMPORARY TABLE tmp_stock_required
# (SS,25/4/17) changed orderdetails.ProductName to products.ProductName to ensure the latest product name
# (SS,10/4/18) added products.SortOrder
SELECT products.SupplierID, suppliers.SupplierName, orderdetails.ProductID, orderdetails.ProductCode, products.ProductName, products.SortOrder,
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks + 0.4999) AS QtyRequiredNoRatio, 
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999) AS QtyRequired, 
NumInStock, on_order.QtyOrdered, on_order.QtyReceived, on_order.QtyAddedToStock,
# QtyToOrder calculated using QtyRequired - (NumInStock + COALESCE(QtyOrdered, 0) - COALESCE(QtyAddedToStock, 0))
# i.e. QtyRequired - (NumInStock + QtyOrdered - QtyAddedToStock)
# (SS,5/9/16) replaced QtyAddedToStock with QtyReceived
# i.e. now QtyRequired - (NumInStock + QtyOrdered - QtyReceived)
(ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / COALESCE(tmp_in_stock_ratios.InStockRatio, 100) + 0.4999)) - (NumInStock + COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0)) AS QtyToOrder,

COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0) AS QtyOnOrder, products.CostPrice,
tmp_in_stock_ratios.InStockRatio
FROM orderdetails
INNER JOIN orders ON orders.OrderNo = orderdetails.OrderNo
INNER JOIN products ON products.ProductCode = orderdetails.ProductCode
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
LEFT JOIN tmp_in_stock_ratios ON tmp_in_stock_ratios.ProductID = products.ProductID
LEFT JOIN

(SELECT pol.ProductID, SUM(QtyOrdered) AS QtyOrdered, ordered_and_received.QtyReceived, ordered_and_received.QtyAddedToStock
FROM purchase_order_lines pol
LEFT JOIN

  (SELECT ProductID, SUM(QtyReceived) AS QtyReceived, SUM(QtyAddedToStock) AS QtyAddedToStock
  FROM purchase_invoice_lines pil
  INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
  INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
  WHERE (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") AND ProductID IS NOT NULL
  GROUP BY ProductID
  ) AS ordered_and_received ON ordered_and_received.ProductID = pol.ProductID
  
INNER JOIN purchase_orders ON purchase_orders.PurchaseOrderNo = pol.PurchaseOrderNo

WHERE OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED"
GROUP BY ProductID
) AS on_order ON on_order.ProductID = orderdetails.ProductID

# (SS,29/4/14) changed nSupplierID = 0 to nSupplierID = -1 (because 0 is when supplier hasn't been chosen to prevent slow adding of new order, -1 when the report is run for all suppliers)
WHERE Status <> "CANCELLED" AND NumInStock IS NOT NULL AND ProductDisabled = False AND DateTimeOrdered >= DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK) 
AND (nSupplierID = -1 OR suppliers.SupplierID = nSupplierID)
GROUP BY suppliers.SupplierName, ProductCode
HAVING NOT bRequiredOnly OR QtyRequired > NumInStock + QtyOnOrder;

ENDY�Եw���b��b��+ss jms_demofuncGetDaysOverdueQuotefuncGetDaysOverdueQuotelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        sNowDate DATE
    int(12)BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
    DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeOfEntry)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
    DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeOfEntry)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
END�b��b��"" jms_demofuncGetDueDatefuncGetDueDatelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    varchar(10) CHARSET latin1BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE sDueDate VARCHAR(10) DEFAULT "";
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    SELECT DATE_FORMAT(DATE_ADD(DateTimeGoAheadGiven,INTERVAL nDays DAY),'%d/%m/%Y') INTO sDueDate FROM jobs WHERE JobID=sJobID;
    RETURN sDueDate;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE sDueDate VARCHAR(10) DEFAULT "";
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    SELECT DATE_FORMAT(DATE_ADD(DateTimeGoAheadGiven,INTERVAL nDays DAY),'%d/%m/%Y') INTO sDueDate FROM jobs WHERE JobID=sJobID;
    RETURN sDueDate;
END�b��b���� jms_demofuncGetEquipStringfuncGetEquipStringlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    varchar(250) CHARSET latin1BEGIN
    DECLARE sEquipString VARCHAR(250) DEFAULT "Cannot find Equipment Record!";
    DECLARE nManufacturerID INT(12) DEFAULT 0;
    DECLARE sManufacturer VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE sModelNumber VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE nEquipmentID INT(15) DEFAULT 0;
    SELECT EquipmentID INTO nEquipmentID FROM jobs WHERE JobID=sJobID;
    SELECT ManufacturerID INTO nManufacturerID FROM equipment WHERE EquipmentID=nEquipmentID;
    SELECT ManufacturerName INTO sManufacturer FROM manufacturers WHERE ManufacturerID=nManufacturerID;
    SELECT ModelNumber INTO sModelNumber FROM equipment WHERE EquipmentID=nEquipmentID;
    SET sEquipString=CONCAT(sManufacturer," ",sModelNumber);
    RETURN sEquipString;
ENDBEGIN
    DECLARE sEquipString VARCHAR(250) DEFAULT "Cannot find Equipment Record!";
    DECLARE nManufacturerID INT(12) DEFAULT 0;
    DECLARE sManufacturer VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE sModelNumber VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE nEquipmentID INT(15) DEFAULT 0;
    SELECT EquipmentID INTO nEquipmentID FROM jobs WHERE JobID=sJobID;
    SELECT ManufacturerID INTO nManufacturerID FROM equipment WHERE EquipmentID=nEquipmentID;
    SELECT ManufacturerName INTO sManufacturer FROM manufacturers WHERE ManufacturerID=nManufacturerID;
    SELECT ModelNumber INTO sModelNumber FROM equipment WHERE EquipmentID=nEquipmentID;
    SET sEquipString=CONCAT(sManufacturer," ",sModelNumber);
    RETURN sEquipString;
END�����A�u�/�b��b��88 jms_demofuncGetFinalOutcomefuncGetFinalOutcomelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    varchar(50) CHARSET latin1BEGIN
DECLARE sFinalOutcome VARCHAR(50) DEFAULT "Final Outcome not set!";
DECLARE nFinalOutcomeID INT(12) DEFAULT 0;
SELECT FinalOutcomeID INTO nFinalOutcomeID FROM jobs WHERE JobID=sJobID;
SELECT FinalOutcome INTO sFinalOutcome FROM final_outcomes WHERE FinalOutcomeID=nFinalOutcomeID;
RETURN sFinalOutcome;
ENDBEGIN
DECLARE sFinalOutcome VARCHAR(50) DEFAULT "Final Outcome not set!";
DECLARE nFinalOutcomeID INT(12) DEFAULT 0;
SELECT FinalOutcomeID INTO nFinalOutcomeID FROM jobs WHERE JobID=sJobID;
SELECT FinalOutcome INTO sFinalOutcome FROM final_outcomes WHERE FinalOutcomeID=nFinalOutcomeID;
RETURN sFinalOutcome;
END�b��b��
�� jms_demofuncGetLabourCostfuncGetLabourCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END�b��b��0�� jms_demofuncGetLabourCostsForBranchfuncGetLabourCostsForBranchlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID=sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID=sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDL
u
?
3}�s�`�b��b��0�� jms_demofuncGetLabourCostsNotForBranchfuncGetLabourCostsNotForBranchlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID<>sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID<>sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END�b��b���� jms_demofuncGetLabourHoursfuncGetLabourHourslatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END�

}
쵮w��
�b��b��,�� jms_demofuncGetLabourHoursByRatefuncGetLabourHoursByRatelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        sRate CHAR(12)
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END�b��b��@�� jms_demofuncGetLabourHoursByRateEOMfuncGetLabourHoursByRateEOMlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        sRate CHAR(12),
        sDate DATE
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate AND t.workdate <= sDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate AND t.workdate <= sDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDm
�
a
!۽n�e	�b��b��

�� jms_demofuncGetLabourMaterialTotalfuncGetLabourMaterialTotallatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sJobID CHAR(12)
    decimal(11,2)BEGIN

# (SS,22/2/12) new version for new ordering system which calls funcGetMaterialCostNew instead of funcGetMaterialCost

DECLARE nLabourCost DOUBLE DEFAULT 0.00;
DECLARE nMaterialCost DOUBLE DEFAULT 0.00;
DECLARE nTotalCost DOUBLE DEFAULT 0.00;

SELECT funcGetLabourCost(sJobID) INTO nLabourCost;
SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

SET nTotalCost=nLabourCost+nMaterialCost;

RETURN nTotalCost;

ENDBEGIN

# (SS,22/2/12) new version for new ordering system which calls funcGetMaterialCostNew instead of funcGetMaterialCost

DECLARE nLabourCost DOUBLE DEFAULT 0.00;
DECLARE nMaterialCost DOUBLE DEFAULT 0.00;
DECLARE nTotalCost DOUBLE DEFAULT 0.00;

SELECT funcGetLabourCost(sJobID) INTO nLabourCost;
SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

SET nTotalCost=nLabourCost+nMaterialCost;

RETURN nTotalCost;

END�b��b��0KK jms_demofuncGetMaterialCostNotBranchIDfuncGetMaterialCostNotBranchIDlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID<>sBranchID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID<>sBranchID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�b��b��0JJ jms_demofuncGetMaterialCostWithBranchIDfuncGetMaterialCostWithBranchIDlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID=sBranchID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID=sBranchID;
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�b��b��
zz jms_demofuncGetWarrantyCostsfuncGetWarrantyCostslatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nWarrantyCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nJobID CHAR(12) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    DECLARE nSubtotal FLOAT(9,2) DEFAULT 0.00;
    DECLARE curWarrantyJobs CURSOR FOR SELECT JobID,CWJobID FROM jobs WHERE CWJobID=sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished=1;
      OPEN curWarrantyJobs;
      nextrecord: LOOP
          FETCH curWarrantyJobs INTO nJobID,nCWJobID;
        IF bFinished=1 THEN
              LEAVE nextrecord;
          END IF;
        SELECT funcGetMaterialCost(JobID)+funcGetLabourCost(JobID) INTO nSubtotal FROM jobs WHERE JobID=nJobID;
        IF nSubtotal IS NULL THEN
            SET nSubtotal=0.00;
        END IF;
        SET nWarrantyCost=nWarrantyCost+nSubtotal;
    END LOOP nextrecord;
      CLOSE curWarrantyJobs;
    RETURN nWarrantyCost;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nWarrantyCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nJobID CHAR(12) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    DECLARE nSubtotal FLOAT(9,2) DEFAULT 0.00;
    DECLARE curWarrantyJobs CURSOR FOR SELECT JobID,CWJobID FROM jobs WHERE CWJobID=sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished=1;
      OPEN curWarrantyJobs;
      nextrecord: LOOP
          FETCH curWarrantyJobs INTO nJobID,nCWJobID;
        IF bFinished=1 THEN
              LEAVE nextrecord;
          END IF;
        SELECT funcGetMaterialCost(JobID)+funcGetLabourCost(JobID) INTO nSubtotal FROM jobs WHERE JobID=nJobID;
        IF nSubtotal IS NULL THEN
            SET nSubtotal=0.00;
        END IF;
        SET nWarrantyCost=nWarrantyCost+nSubtotal;
    END LOOP nextrecord;
      CLOSE curWarrantyJobs;
    RETURN nWarrantyCost;
END�b��b��ss jms_demofuncGetWarrantyCreatedEngineerfuncGetWarrantyCreatedEngineerlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    varchar(100) CHARSET latin1BEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    SELECT CWJobID INTO nCWJobID FROM jobs WHERE JobID=sJobID;
    IF nCWJobID IS NOT NULL THEN
        SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=nCWJobID;
        END IF;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    SELECT CWJobID INTO nCWJobID FROM jobs WHERE JobID=sJobID;
    IF nCWJobID IS NOT NULL THEN
        SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=nCWJobID;
        END IF;
    RETURN sEngineerName;
END����} }9�,���b��b��


 jms_demofuncGetLossOnJobfuncGetLossOnJoblatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLossOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice<nTotalCost THEN
          SET nLossOnJob=(nTotalCost-nInvoicePrice);
      END IF;
    RETURN nLossOnJob;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLossOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice<nTotalCost THEN
          SET nLossOnJob=(nTotalCost-nInvoicePrice);
      END IF;
    RETURN nLossOnJob;
END�b��b��2�� jms_demofuncGetWorkshopInvoiceValuefuncGetWorkshopInvoiceValuelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        sBranchName CHAR(50)
    doubleBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE nPassedBranchID INT(12) DEFAULT 0;
DECLARE nCommBranchID INT(12) DEFAULT 0;
DECLARE nRepairBranchID INT(12) DEFAULT 0;
DECLARE nNumberOff INT(12) DEFAULT 0;
DECLARE nQuotedPrice DOUBLE DEFAULT 0.00;
DECLARE nFinalValue DOUBLE DEFAULT 0.00;
SELECT BranchID INTO nPassedBranchID FROM branches WHERE BranchName=sBranchName;
SELECT
    JobTypeID,BranchID,RepairBranchID,NumberOff,QuotedPrice
INTO
    nJobTypeID,nCommBranchID,nRepairBranchID,nNumberOff,nQuotedPrice
FROM
    jobs WHERE JobID=sJobID;
    
SET nFinalValue=(nNumberOff*nQuotedPrice);
    
IF nJobTypeID=17 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.2727;
    END IF;
    
IF nJobTypeID=18 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.5714;
    END IF;
IF nJobTypeID=19 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.15;
        END IF;
IF nJobTypeID=20 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.4;
        END IF;
    
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nRepairBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.80;
    END IF;
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nCommBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.20;
    END IF;
SET nFinalValue=ROUND(nFinalValue,2);
RETURN nFinalValue;
ENDBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE nPassedBranchID INT(12) DEFAULT 0;
DECLARE nCommBranchID INT(12) DEFAULT 0;
DECLARE nRepairBranchID INT(12) DEFAULT 0;
DECLARE nNumberOff INT(12) DEFAULT 0;
DECLARE nQuotedPrice DOUBLE DEFAULT 0.00;
DECLARE nFinalValue DOUBLE DEFAULT 0.00;
SELECT BranchID INTO nPassedBranchID FROM branches WHERE BranchName=sBranchName;
SELECT
    JobTypeID,BranchID,RepairBranchID,NumberOff,QuotedPrice
INTO
    nJobTypeID,nCommBranchID,nRepairBranchID,nNumberOff,nQuotedPrice
FROM
    jobs WHERE JobID=sJobID;
    
SET nFinalValue=(nNumberOff*nQuotedPrice);
    
IF nJobTypeID=17 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.2727;
    END IF;
    
IF nJobTypeID=18 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.5714;
    END IF;
IF nJobTypeID=19 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.15;
        END IF;
IF nJobTypeID=20 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.4;
        END IF;
    
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nRepairBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.80;
    END IF;
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nCommBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.20;
    END IF;
SET nFinalValue=ROUND(nFinalValue,2);
RETURN nFinalValue;
END�K���M����b��b��
 jms_demofuncGetProfitOnJobfuncGetProfitOnJoblatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nProfitOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice>nTotalCost THEN
          SET nProfitOnJob=(nInvoicePrice-nTotalCost);
      END IF;
    RETURN nProfitOnJob;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nProfitOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice>nTotalCost THEN
          SET nProfitOnJob=(nInvoicePrice-nTotalCost);
      END IF;
    RETURN nProfitOnJob;
END�b��b��+�� jms_demofuncIsJobOverduefuncIsJobOverduelatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12),
        dNowDate DATE
    varchar(10) CHARSET latin1BEGIN
    DECLARE sResult VARCHAR(10) DEFAULT "FALSE";
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    IF dNowDate="" THEN
        SET dNowDate=CURRENT_DATE;
        END IF;
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET sResult="TRUE";
        END IF;
    RETURN sResult;
ENDBEGIN
    DECLARE sResult VARCHAR(10) DEFAULT "FALSE";
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    IF dNowDate="" THEN
        SET dNowDate=CURRENT_DATE;
        END IF;
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET sResult="TRUE";
        END IF;
    RETURN sResult;
END�h�<��9��b��b��dd jms_demofuncJobBecomesWarrantyfuncJobBecomesWarrantylatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nWarrantyJobs INT(12);
    DECLARE nReturn INT(12);
    SET nWarrantyJobs=0;
    SET nReturn=0;
    SELECT COUNT(JobID) INTO nWarrantyJobs FROM jobs WHERE CWJobID=sJobID;
    IF nWarrantyJobs IS NULL THEN
        SET nReturn=0;
    END IF;
    IF nWarrantyJobs>0 THEN
        SET nReturn=nWarrantyJobs;
    END IF;
    RETURN nReturn;
ENDBEGIN
    DECLARE nWarrantyJobs INT(12);
    DECLARE nReturn INT(12);
    SET nWarrantyJobs=0;
    SET nReturn=0;
    SELECT COUNT(JobID) INTO nWarrantyJobs FROM jobs WHERE CWJobID=sJobID;
    IF nWarrantyJobs IS NULL THEN
        SET nReturn=0;
    END IF;
    IF nWarrantyJobs>0 THEN
        SET nReturn=nWarrantyJobs;
    END IF;
    RETURN nReturn;
END�b��b��8 jms_demofuncJobsInCategoryForCustomerfuncJobsInCategoryForCustomerlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sCategoryName CHAR(50),
        nCustomerID INT(12)
    int(12)BEGIN
DECLARE nJobsInCategory INT(12) DEFAULT 0;
DECLARE nCategoryID INT(12) DEFAULT 0;
SELECT CategoryID INTO nCategoryID FROM categories WHERE CategoryName=sCategoryName;
SELECT COUNT(JobID) INTO nJobsInCategory FROM jobs JOIN    equipment ON jobs.EquipmentID=equipment.EquipmentID WHERE equipment.CategoryID=nCategoryID AND jobs.CustomerID=nCustomerID;
RETURN nJobsInCategory;
ENDBEGIN
DECLARE nJobsInCategory INT(12) DEFAULT 0;
DECLARE nCategoryID INT(12) DEFAULT 0;
SELECT CategoryID INTO nCategoryID FROM categories WHERE CategoryName=sCategoryName;
SELECT COUNT(JobID) INTO nJobsInCategory FROM jobs JOIN    equipment ON jobs.EquipmentID=equipment.EquipmentID WHERE equipment.CategoryID=nCategoryID AND jobs.CustomerID=nCustomerID;
RETURN nJobsInCategory;
END�b��b��*ZZ jms_demofuncMonthEndMaterialCostfuncMonthEndMaterialCostlatin1latin1_swedish_cilatin1_swedish_ciroot@127.0.0.1sJobID CHAR(6),
        dEndDate DATE
    decimal(9,2)BEGIN

    DECLARE nMaterialCost DECIMAL(9,2) DEFAULT 0.00;
    DECLARE nGetIn DATE DEFAULT '1999-12-31';

    #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE (order_items.JobID = sJobID) AND (order_items.itemdeliverydate IS NOT NULL AND order_items.itemdeliverydate <= dEndDate);
    #(SS,6/3/12) replaced above with following
    SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE (job_material.JobID = sJobID) AND (job_material.DateReceived IS NOT NULL AND job_material.DateReceived <= dEndDate);
    
    RETURN nMaterialCost;
    
ENDBEGIN

    DECLARE nMaterialCost DECIMAL(9,2) DEFAULT 0.00;
    DECLARE nGetIn DATE DEFAULT '1999-12-31';

    #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE (order_items.JobID = sJobID) AND (order_items.itemdeliverydate IS NOT NULL AND order_items.itemdeliverydate <= dEndDate);
    #(SS,6/3/12) replaced above with following
    SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE (job_material.JobID = sJobID) AND (job_material.DateReceived IS NOT NULL AND job_material.DateReceived <= dEndDate);
    
    RETURN nMaterialCost;
    
END�b��b��
�33 jms_demoGetLockGetLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sTableName VARCHAR ( 32 ) ,
        sRecordKey VARCHAR ( 64 ) ,
        nAuditID INTEGER ( 11 ) ,
        nUserID INTEGER ( 11 ) ,
        sUserName VARCHAR ( 40 ) ,
        sComputerName VARCHAR ( 20 ) ,
        sIPAddress VARCHAR ( 15 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
END��p���~|������b��b��
�� jms_demoGetOrderPaymentDetailsGetOrderPaymentDetailslatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sOrderID INTEGER(11)
    varchar(200) CHARSET latin1BEGIN
  DECLARE nAmount DECIMAL(11,2);
  DECLARE sRef1 VARCHAR(40);
  DECLARE sRef2 VARCHAR(40);
  DECLARE sRef3 VARCHAR(40);
  DECLARE sPaymentTypeName VARCHAR(20);
  DECLARE sPaymentDetails VARCHAR(200);
 
  SELECT Amount, Ref1, Ref2, Ref3, payment_types.PaymentTypeName INTO nAmount, sRef1, sRef2, sRef3, sPaymentTypeName FROM material_payments
  INNER JOIN payment_types ON payment_types.PaymentTypeID = material_payments.PaymentTypeID
  WHERE OrderID = sOrderID
  ORDER BY MaterialPaymentID DESC LIMIT 1;
  
  IF sPaymentTypeName IS NULL THEN
    SET sPaymentDetails = "";
  ELSE
    # SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount, ", Ref: ", sRef1, " ", COALESCE(sRef2), " ", sRef3);
    SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount);
    IF sRef1 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", Ref: ", sRef1);
    END IF;    
    IF sRef2 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef2); 
    END IF;
    IF sRef3 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef3);
    END IF;
  END IF;
    
  RETURN sPaymentDetails;
ENDBEGIN
  DECLARE nAmount DECIMAL(11,2);
  DECLARE sRef1 VARCHAR(40);
  DECLARE sRef2 VARCHAR(40);
  DECLARE sRef3 VARCHAR(40);
  DECLARE sPaymentTypeName VARCHAR(20);
  DECLARE sPaymentDetails VARCHAR(200);
 
  SELECT Amount, Ref1, Ref2, Ref3, payment_types.PaymentTypeName INTO nAmount, sRef1, sRef2, sRef3, sPaymentTypeName FROM material_payments
  INNER JOIN payment_types ON payment_types.PaymentTypeID = material_payments.PaymentTypeID
  WHERE OrderID = sOrderID
  ORDER BY MaterialPaymentID DESC LIMIT 1;
  
  IF sPaymentTypeName IS NULL THEN
    SET sPaymentDetails = "";
  ELSE
    # SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount, ", Ref: ", sRef1, " ", COALESCE(sRef2), " ", sRef3);
    SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount);
    IF sRef1 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", Ref: ", sRef1);
    END IF;    
    IF sRef2 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef2); 
    END IF;
    IF sRef3 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef3);
    END IF;
  END IF;
    
  RETURN sPaymentDetails;
END�b��b��
Bee jms_demoReleaseLockReleaseLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo@%nLockID INTEGER ( 11 ) ,
        sComputerName VARCHAR ( 20 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
END�b��b��
�55 jms_demoSetLockSetLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sTableName VARCHAR ( 32 ) ,
        sRecordKey VARCHAR ( 64 ) ,
        nAuditID INTEGER ( 11 ) ,
        nUserID INTEGER ( 11 ) ,
        sUserName VARCHAR ( 40 ) ,
        sComputerName VARCHAR ( 20 ) ,
        sIPAddress VARCHAR ( 15 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007
  
  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
ENDBEGIN
  # Version 4.0.32, 11/8/2007
  
  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
END{��	��	��8��f�b��b��
H�� jms_demoGetTranslationGetTranslationlatin1latin1_swedish_cilatin1_swedish_cijms_demo@%sEnglishPhrase VARCHAR ( 255 ) ,
        nLanguageID INTEGER ( 11 )
    varchar(255) CHARSET latin1BEGIN
  # (SS,4/12/07) MySQL version of the translation routine
  # returns the equivalent phrase to the English one specified in requested language
  # if not found in the then the English phrase is returned

  DECLARE sLanguageName VARCHAR(40);
  DECLARE sResult VARCHAR(255);

  # get language name from languages table using language ID
  SELECT LanguageName INTO sLanguageName FROM languages WHERE LanguageID = nLanguageID;

  # get the required language phrase
  # SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase;
  
  # Following does not work: SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase
  # I initially tried dynamic SQL using SET @sSQL, PREPARE, EXECUTE, DEALLOCATE
  # but it was not allowed in stored function, could also create stored function on
  # the fly but seem very messy so I resorted to the following
  # which might be quicker anyway, but will need to update when new language added
  # could also just hard code the LanguageID without having to look up the language name
  # See: http://forge.mysql.com/snippets/download.php?id=13
  #      http://dev.mysql.com/doc/refman/5.1/en/sqlps.html
  #      http://forums.mysql.com/read.php?60,41372,47813#msg-47813
  IF sLanguageName = "English" THEN
    SELECT English INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "German" THEN
    SELECT German INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "French" THEN
    SELECT French INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Italian" THEN
    SELECT Italian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Spanish" THEN
    SELECT Spanish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Polish" THEN
    SELECT Polish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Dutch" THEN
    SELECT Dutch INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Russian" THEN
    SELECT Russian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Swedish" THEN
    SELECT Swedish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Greek" THEN
    SELECT Greek INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Czech" THEN
    SELECT Czech INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Portuguese" THEN
    SELECT Portuguese INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Hungarian" THEN
    SELECT Hungarian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSE
    # if this point reached then it means the SELECT line for this language is missing
    # needs to be added above so return error instead
    SET sResult = CONCAT("!!! Language ", sLanguageName, " not in GetTranslation function !!!");
  END IF;

  # if nothing specified in requested language then return the English phrase
  IF sResult IS NULL OR sResult = "" THEN
    SET sResult = sEnglishPhrase;
  END IF;

  # return the result
  RETURN sResult;
ENDBEGIN
  # (SS,4/12/07) MySQL version of the translation routine
  # returns the equivalent phrase to the English one specified in requested language
  # if not found in the then the English phrase is returned

  DECLARE sLanguageName VARCHAR(40);
  DECLARE sResult VARCHAR(255);

  # get language name from languages table using language ID
  SELECT LanguageName INTO sLanguageName FROM languages WHERE LanguageID = nLanguageID;

  # get the required language phrase
  # SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase;
  
  # Following does not work: SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase
  # I initially tried dynamic SQL using SET @sSQL, PREPARE, EXECUTE, DEALLOCATE
  # but it was not allowed in stored function, could also create stored function on
  # the fly but seem very messy so I resorted to the following
  # which might be quicker anyway, but will need to update when new language added
  # could also just hard code the LanguageID without having to look up the language name
  # See: http://forge.mysql.com/snippets/download.php?id=13
  #      http://dev.mysql.com/doc/refman/5.1/en/sqlps.html
  #      http://forums.mysql.com/read.php?60,41372,47813#msg-47813
  IF sLanguageName = "English" THEN
    SELECT English INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "German" THEN
    SELECT German INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "French" THEN
    SELECT French INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Italian" THEN
    SELECT Italian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Spanish" THEN
    SELECT Spanish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Polish" THEN
    SELECT Polish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Dutch" THEN
    SELECT Dutch INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Russian" THEN
    SELECT Russian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Swedish" THEN
    SELECT Swedish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Greek" THEN
    SELECT Greek INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Czech" THEN
    SELECT Czech INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Portuguese" THEN
    SELECT Portuguese INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Hungarian" THEN
    SELECT Hungarian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSE
    # if this point reached then it means the SELECT line for this language is missing
    # needs to be added above so return error instead
    SET sResult = CONCAT("!!! Language ", sLanguageName, " not in GetTranslation function !!!");
  END IF;

  # if nothing specified in requested language then return the English phrase
  IF sResult IS NULL OR sResult = "" THEN
    SET sResult = sEnglishPhrase;
  END IF;

  # return the result
  RETURN sResult;
END�I�����b��b��
R�� jms_demoGetPriceSummaryGetPriceSummarylatin1latin1_swedish_cilatin1_swedish_cijms_demo@%
        IN nEquipmentID INTEGER ( 11 ) ,
        IN nBranchID INTEGER ( 11 )
    BEGIN

# Serial: 2008-04-23-01
# Original version SS, ITP, 22-02-2007
# This version modified by Jim to use the new equipment_price_summary table
# which is created at the end of the day to help speed up this routine
# which was taking a few seconds where hundreds of jobs existing for same EquipmentID
  
DECLARE nSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nRepairPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalJobCount INT(11) DEFAULT 0;
DECLARE nWorldAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldJobCount INT(11) DEFAULT 0;
DECLARE nLocalSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldSellPrice DECIMAL(11,2) DEFAULT 0.00;

# SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;
# (SS,23/4/08) replaced above with following due to field name changes
SELECT OurRefurbPrice, OurRepairPrice, OurServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;

SELECT LocalAveSellPrice, LocalAveJobCost, LocalJobCount INTO nLocalAveSellPrice, nLocalAveJobCost, nLocalJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID AND BranchID=nBranchID;
# (SS,19/3/08) in following two it may be more accurate to use SUM(LocalAveSellPrice) / SUM(LocalJobCount)
SELECT AVG(LocalAveSellPrice) INTO nWorldAveSellPrice FROM equipment_price_summary WHERE LocalAveSellPrice>0 AND EquipmentID=nEquipmentID;
SELECT AVG(LocalAveJobCost) INTO nWorldAveJobCost FROM equipment_price_summary WHERE LocalAveJobCost>0 AND EquipmentID=nEquipmentID;
SELECT SUM(LocalJobCount) INTO nWorldJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID;

IF nSellPrice IS NULL THEN
   SET nSellPrice="0.00";
END IF;
    
IF nRepairPrice IS NULL THEN
   SET nRepairPrice="0.00";
END IF;

IF nServiceExPrice IS NULL THEN
   SET nServiceExPrice="0.00";
END IF;
    
IF nLocalAveSellPrice IS NULL THEN
   SET nLocalAveSellPrice="0.00";
END IF;    
    
IF nLocalAveJobCost IS NULL THEN
   SET nLocalAveJobCost="0.00";
END IF;
    
IF nLocalJobCount IS NULL THEN
   SET nLocalJobCount="0.00";
END IF;
    
IF nWorldAveSellPrice IS NULL THEN
   SET nWorldAveSellPrice="0.00";
END IF;    
    
IF nWorldAveJobCost IS NULL THEN
   SET nWorldAveJobCost="0.00";
END IF;
    
IF nWorldJobCount IS NULL THEN
   SET nWorldJobCount="0.00";
END IF;

SELECT
    nSellPrice AS SellPrice,
    nRepairPrice AS RepairPrice,
    nServiceExPrice AS ServiceExPrice,
    nLocalAveSellPrice AS AveSellPriceLocal,
    nLocalAveJobCost AS AveJobCostLocal,
    nLocalJobCount AS LocalJobCount,
    nWorldAveSellPrice AS AveSellPriceWorld,
    nWorldAveJobCost AS AveJobCostWorld,
    nWorldJobCount AS WorldJobCount;	

ENDBEGIN

# Serial: 2008-04-23-01
# Original version SS, ITP, 22-02-2007
# This version modified by Jim to use the new equipment_price_summary table
# which is created at the end of the day to help speed up this routine
# which was taking a few seconds where hundreds of jobs existing for same EquipmentID
  
DECLARE nSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nRepairPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalJobCount INT(11) DEFAULT 0;
DECLARE nWorldAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldJobCount INT(11) DEFAULT 0;
DECLARE nLocalSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldSellPrice DECIMAL(11,2) DEFAULT 0.00;

# SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;
# (SS,23/4/08) replaced above with following due to field name changes
SELECT OurRefurbPrice, OurRepairPrice, OurServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;

SELECT LocalAveSellPrice, LocalAveJobCost, LocalJobCount INTO nLocalAveSellPrice, nLocalAveJobCost, nLocalJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID AND BranchID=nBranchID;
# (SS,19/3/08) in following two it may be more accurate to use SUM(LocalAveSellPrice) / SUM(LocalJobCount)
SELECT AVG(LocalAveSellPrice) INTO nWorldAveSellPrice FROM equipment_price_summary WHERE LocalAveSellPrice>0 AND EquipmentID=nEquipmentID;
SELECT AVG(LocalAveJobCost) INTO nWorldAveJobCost FROM equipment_price_summary WHERE LocalAveJobCost>0 AND EquipmentID=nEquipmentID;
SELECT SUM(LocalJobCount) INTO nWorldJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID;

IF nSellPrice IS NULL THEN
   SET nSellPrice="0.00";
END IF;
    
IF nRepairPrice IS NULL THEN
   SET nRepairPrice="0.00";
END IF;

IF nServiceExPrice IS NULL THEN
   SET nServiceExPrice="0.00";
END IF;
    
IF nLocalAveSellPrice IS NULL THEN
   SET nLocalAveSellPrice="0.00";
END IF;    
    
IF nLocalAveJobCost IS NULL THEN
   SET nLocalAveJobCost="0.00";
END IF;
    
IF nLocalJobCount IS NULL THEN
   SET nLocalJobCount="0.00";
END IF;
    
IF nWorldAveSellPrice IS NULL THEN
   SET nWorldAveSellPrice="0.00";
END IF;    
    
IF nWorldAveJobCost IS NULL THEN
   SET nWorldAveJobCost="0.00";
END IF;
    
IF nWorldJobCount IS NULL THEN
   SET nWorldJobCount="0.00";
END IF;

SELECT
    nSellPrice AS SellPrice,
    nRepairPrice AS RepairPrice,
    nServiceExPrice AS ServiceExPrice,
    nLocalAveSellPrice AS AveSellPriceLocal,
    nLocalAveJobCost AS AveJobCostLocal,
    nLocalJobCount AS LocalJobCount,
    nWorldAveSellPrice AS AveSellPriceWorld,
    nWorldAveJobCost AS AveJobCostWorld,
    nWorldJobCount AS WorldJobCount;	

END�q�E����b��b��
R�� jms_demoGetPriceSummaryPrevGetPriceSummaryPrevlatin1latin1_swedish_cilatin1_swedish_cijms_demo@%
        IN nEquipmentID INTEGER ( 11 ) ,
        IN nBranchID INTEGER ( 11 )
    BEGIN

  # Serial: 2008-02-22-01

  DECLARE nSellPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nRepairPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceWorld DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostWorld DECIMAL(11,2) DEFAULT NULL;

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nJobID INTEGER(11);
  DECLARE nJobBranchID INTEGER(11);
  DECLARE nJobSellPrice DOUBLE;

  DECLARE nWorldSellPrice DOUBLE DEFAULT 0;
  DECLARE nWorldJobCost DOUBLE DEFAULT 0;
  DECLARE nWorldJobCount INTEGER DEFAULT 0;
  DECLARE nLocalSellPrice DOUBLE DEFAULT 0;
  DECLARE nLocalJobCost DOUBLE DEFAULT 0;
  DECLARE nLocalJobCount INTEGER DEFAULT 0;

  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(9,2);
  DECLARE nLabourHours DOUBLE;

  # this query selects all invoiced jobs with given equipment ID
  DECLARE curSelectedJobs CURSOR FOR SELECT JobID, BranchID, SellPrice FROM jobs WHERE WIP = FALSE AND EquipmentID = nEquipmentID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  # get SellPrice, RepairPrice, ServiceExPrice from Equipment table
  SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID = nEquipmentID;

  OPEN curSelectedJobs;

  nextrecord: LOOP

    FETCH curSelectedJobs INTO nJobID, nJobBranchID, nJobSellPrice;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    # determine JobCost using GetTotalLabourAndMaterialsMain procedure, i.e. JobCost = LabourCost + MaterialCost
    CALL GetTotalLabourAndMaterialsMain(nJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);

    # treat NULL as zero, else total will end up as NULL, alternative is to ignore records containing NULLs
    IF nJobSellPrice IS NULL THEN
      SET nJobSellPrice = 0;
    END IF;

    SET nWorldSellPrice = nWorldSellPrice + nJobSellPrice;
    SET nWorldJobCost = nWorldJobCost + nLabourCost + nMaterialCost;
    SET nWorldJobCount = nWorldJobCount + 1;

    IF nBranchID = nJobBranchID THEN
       SET nLocalSellPrice = nLocalSellPrice + nJobSellPrice;
       SET nLocalJobCost = nLocalJobCost + nLabourCost + nMaterialCost;
       SET nLocalJobCount = nLocalJobCount + 1;
    END IF;

  END LOOP nextrecord;

  CLOSE curSelectedJobs;

  # calculate the averages

  IF nLocalJobCount > 1 THEN
     SET nAveSellPriceLocal = ROUND(nLocalSellPrice / nLocalJobCount, 2);
     SET nAveJobCostLocal = ROUND(nLocalJobCost / nLocalJobCount, 2);
  END IF;

  IF nWorldJobCount > 1 THEN
     SET nAveSellPriceWorld = ROUND(nWorldSellPrice / nWorldJobCount, 2);
     SET nAveJobCostWorld = ROUND(nWorldJobCost / nWorldJobCount, 2);
  END IF;

  # ------------------
  # Output the results
  # ------------------

  SELECT nSellPrice AS SellPrice, nRepairPrice As RepairPrice, nServiceExPrice AS ServiceExPrice,
         nAveSellPriceLocal AS AveSellPriceLocal, nAveJobCostLocal AS AveJobCostLocal, nLocalJobCount AS LocalJobCount,
         nAveSellPriceWorld AS AveSellPriceWorld, nAveJobCostWorld AS AveJobCostWorld, nWorldJobCount AS WorldJobCount;

ENDBEGIN

  # Serial: 2008-02-22-01

  DECLARE nSellPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nRepairPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceWorld DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostWorld DECIMAL(11,2) DEFAULT NULL;

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nJobID INTEGER(11);
  DECLARE nJobBranchID INTEGER(11);
  DECLARE nJobSellPrice DOUBLE;

  DECLARE nWorldSellPrice DOUBLE DEFAULT 0;
  DECLARE nWorldJobCost DOUBLE DEFAULT 0;
  DECLARE nWorldJobCount INTEGER DEFAULT 0;
  DECLARE nLocalSellPrice DOUBLE DEFAULT 0;
  DECLARE nLocalJobCost DOUBLE DEFAULT 0;
  DECLARE nLocalJobCount INTEGER DEFAULT 0;

  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(9,2);
  DECLARE nLabourHours DOUBLE;

  # this query selects all invoiced jobs with given equipment ID
  DECLARE curSelectedJobs CURSOR FOR SELECT JobID, BranchID, SellPrice FROM jobs WHERE WIP = FALSE AND EquipmentID = nEquipmentID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  # get SellPrice, RepairPrice, ServiceExPrice from Equipment table
  SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID = nEquipmentID;

  OPEN curSelectedJobs;

  nextrecord: LOOP

    FETCH curSelectedJobs INTO nJobID, nJobBranchID, nJobSellPrice;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    # determine JobCost using GetTotalLabourAndMaterialsMain procedure, i.e. JobCost = LabourCost + MaterialCost
    CALL GetTotalLabourAndMaterialsMain(nJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);

    # treat NULL as zero, else total will end up as NULL, alternative is to ignore records containing NULLs
    IF nJobSellPrice IS NULL THEN
      SET nJobSellPrice = 0;
    END IF;

    SET nWorldSellPrice = nWorldSellPrice + nJobSellPrice;
    SET nWorldJobCost = nWorldJobCost + nLabourCost + nMaterialCost;
    SET nWorldJobCount = nWorldJobCount + 1;

    IF nBranchID = nJobBranchID THEN
       SET nLocalSellPrice = nLocalSellPrice + nJobSellPrice;
       SET nLocalJobCost = nLocalJobCost + nLabourCost + nMaterialCost;
       SET nLocalJobCount = nLocalJobCount + 1;
    END IF;

  END LOOP nextrecord;

  CLOSE curSelectedJobs;

  # calculate the averages

  IF nLocalJobCount > 1 THEN
     SET nAveSellPriceLocal = ROUND(nLocalSellPrice / nLocalJobCount, 2);
     SET nAveJobCostLocal = ROUND(nLocalJobCost / nLocalJobCount, 2);
  END IF;

  IF nWorldJobCount > 1 THEN
     SET nAveSellPriceWorld = ROUND(nWorldSellPrice / nWorldJobCount, 2);
     SET nAveJobCostWorld = ROUND(nWorldJobCost / nWorldJobCount, 2);
  END IF;

  # ------------------
  # Output the results
  # ------------------

  SELECT nSellPrice AS SellPrice, nRepairPrice As RepairPrice, nServiceExPrice AS ServiceExPrice,
         nAveSellPriceLocal AS AveSellPriceLocal, nAveJobCostLocal AS AveJobCostLocal, nLocalJobCount AS LocalJobCount,
         nAveSellPriceWorld AS AveSellPriceWorld, nAveJobCostWorld AS AveJobCostWorld, nWorldJobCount AS WorldJobCount;

ENDC<�,���b��b��
 �� jms_demoGetTotalLabourAndMaterialsGetTotalLabourAndMaterialslatin1latin1_swedish_cilatin1_swedish_cijms_demo@%
        sJobID CHAR ( 12 )
    BEGIN
  # Serial: 2008-02-22-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMain(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

ENDBEGIN
  # Serial: 2008-02-22-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMain(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

END�b��b��
�� jms_demoGetTotalLabourAndMaterialsMainGetTotalLabourAndMaterialsMainlatin1latin1_swedish_cilatin1_swedish_cijms_demo@%
        IN sJobID CHAR ( 12 ) ,
        OUT pLabourHours INTEGER ( 11 ) ,
        OUT pLabourCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialSell DECIMAL ( 11 , 2 ) ,
        OUT pMinSellPrice DECIMAL ( 11 , 2 )
    BEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-09-12-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCost(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDBEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-09-12-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCost(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
END)�ݫ����b��b��!!
�� jms_demoGetTotalLabourAndMaterialsMainNewGetTotalLabourAndMaterialsMainNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo@%
        IN sJobID CHAR ( 12 ) ,
        OUT pLabourHours INTEGER ( 11 ) ,
        OUT pLabourCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialSell DECIMAL ( 11 , 2 ) ,
        OUT pMinSellPrice DECIMAL ( 11 , 2 )
    BEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-12-19-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDBEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-12-19-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
END�b��b��
 �� jms_demoGetTotalLabourAndMaterialsNewGetTotalLabourAndMaterialsNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo@%
        sJobID CHAR ( 12 )
    BEGIN
  # Serial: 2008-12-19-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMainNew(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

ENDBEGIN
  # Serial: 2008-12-19-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMainNew(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

END)�v�g���b��b��""
oo jms_demoUpdateEquipmentAveragePriceSummaryUpdateEquipmentAveragePriceSummarylatin1latin1_swedish_cilatin1_swedish_cijms_demo@%BEGIN

# (SS,ITP,19/3/08)
# Serial: 2008-03-19-01
# This routine is to be run at the end of the day
# Creates records used by the GetPriceSummary routine
# to help speed up getting the average prices for a given job

# empty the table
TRUNCATE equipment_price_summary;

# summarise and add the records
INSERT INTO equipment_price_summary
(SELECT EquipmentID, BranchID,
ROUND(AVG(SellPrice),2) AS LocalAveSellPrice, ROUND(AVG(funcGetLabourMaterialTotal(JobID)),2) AS LocalAveJobCost,
COUNT(JobID) AS LocalJobCount, CURDATE() AS LastUpdate
FROM jobs WHERE WIP=FALSE AND CREDITNOTE=FALSE
GROUP BY EquipmentID, BranchID
);

ENDBEGIN

# (SS,ITP,19/3/08)
# Serial: 2008-03-19-01
# This routine is to be run at the end of the day
# Creates records used by the GetPriceSummary routine
# to help speed up getting the average prices for a given job

# empty the table
TRUNCATE equipment_price_summary;

# summarise and add the records
INSERT INTO equipment_price_summary
(SELECT EquipmentID, BranchID,
ROUND(AVG(SellPrice),2) AS LocalAveSellPrice, ROUND(AVG(funcGetLabourMaterialTotal(JobID)),2) AS LocalAveJobCost,
COUNT(JobID) AS LocalJobCount, CURDATE() AS LastUpdate
FROM jobs WHERE WIP=FALSE AND CREDITNOTE=FALSE
GROUP BY EquipmentID, BranchID
);

END�b��b��		B�� jms_demo_masterClearLockClearLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%nLockID INTEGER ( 11 ) ,
        sComputerName VARCHAR ( 20 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  # UPDATE appears before DELETE because of RETURN ROW_COUNT()
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  # UPDATE appears before DELETE because of RETURN ROW_COUNT()
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
END�b��b��MXX jms_demo_masterfuncGetAdjustedSalesValuefuncGetAdjustedSalesValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%nInvoiceValue DOUBLE,
        nMargin DOUBLE,
        nJobTypeID INT(12)
    doubleBEGIN
DECLARE nAdjustedSalesValue DOUBLE DEFAULT 0.00;
IF nJobTypeID=1 OR nJobTypeID=4 OR nJobTypeID=6 OR nJobTypeID=8 OR nJobTypeID=10 THEN
    SET nAdjustedSalesValue=nInvoiceValue;
    END IF;
    
IF nJobTypeID=2 OR nJobTypeID=3 OR nJobTypeID=12 THEN
    SET nAdjustedSalesValue=(nMargin*2);
    END IF;
    
RETURN nAdjustedSalesValue;
ENDBEGIN
DECLARE nAdjustedSalesValue DOUBLE DEFAULT 0.00;
IF nJobTypeID=1 OR nJobTypeID=4 OR nJobTypeID=6 OR nJobTypeID=8 OR nJobTypeID=10 THEN
    SET nAdjustedSalesValue=nInvoiceValue;
    END IF;
    
IF nJobTypeID=2 OR nJobTypeID=3 OR nJobTypeID=12 THEN
    SET nAdjustedSalesValue=(nMargin*2);
    END IF;
    
RETURN nAdjustedSalesValue;
END�b��b��/ww jms_demo_masterfuncGetDaysOverduefuncGetDaysOverduelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%`sJobID` CHAR(12),
        `sNowDate` DATE
    int(12)BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
   DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
   DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
END]������Hu����b��b��+ss jms_demo_masterfuncGetDaysOverdueQuotefuncGetDaysOverdueQuotelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12),
        sNowDate DATE
    int(12)BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
    DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeOfEntry)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
    DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeOfEntry)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
END�b��b��"" jms_demo_masterfuncGetDueDatefuncGetDueDatelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    varchar(10) CHARSET latin1BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE sDueDate VARCHAR(10) DEFAULT "";
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    SELECT DATE_FORMAT(DATE_ADD(DateTimeGoAheadGiven,INTERVAL nDays DAY),'%d/%m/%Y') INTO sDueDate FROM jobs WHERE JobID=sJobID;
    RETURN sDueDate;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE sDueDate VARCHAR(10) DEFAULT "";
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    SELECT DATE_FORMAT(DATE_ADD(DateTimeGoAheadGiven,INTERVAL nDays DAY),'%d/%m/%Y') INTO sDueDate FROM jobs WHERE JobID=sJobID;
    RETURN sDueDate;
END�b��b���� jms_demo_masterfuncGetEquipStringfuncGetEquipStringlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    varchar(250) CHARSET latin1BEGIN
    DECLARE sEquipString VARCHAR(250) DEFAULT "Cannot find Equipment Record!";
    DECLARE nManufacturerID INT(12) DEFAULT 0;
    DECLARE sManufacturer VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE sModelNumber VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE nEquipmentID INT(15) DEFAULT 0;
    SELECT EquipmentID INTO nEquipmentID FROM jobs WHERE JobID=sJobID;
    SELECT ManufacturerID INTO nManufacturerID FROM equipment WHERE EquipmentID=nEquipmentID;
    SELECT ManufacturerName INTO sManufacturer FROM manufacturers WHERE ManufacturerID=nManufacturerID;
    SELECT ModelNumber INTO sModelNumber FROM equipment WHERE EquipmentID=nEquipmentID;
    SET sEquipString=CONCAT(sManufacturer," ",sModelNumber);
    RETURN sEquipString;
ENDBEGIN
    DECLARE sEquipString VARCHAR(250) DEFAULT "Cannot find Equipment Record!";
    DECLARE nManufacturerID INT(12) DEFAULT 0;
    DECLARE sManufacturer VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE sModelNumber VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE nEquipmentID INT(15) DEFAULT 0;
    SELECT EquipmentID INTO nEquipmentID FROM jobs WHERE JobID=sJobID;
    SELECT ManufacturerID INTO nManufacturerID FROM equipment WHERE EquipmentID=nEquipmentID;
    SELECT ManufacturerName INTO sManufacturer FROM manufacturers WHERE ManufacturerID=nManufacturerID;
    SELECT ModelNumber INTO sModelNumber FROM equipment WHERE EquipmentID=nEquipmentID;
    SET sEquipString=CONCAT(sManufacturer," ",sModelNumber);
    RETURN sEquipString;
END�����vm�
�b��b��88 jms_demo_masterfuncGetFinalOutcomefuncGetFinalOutcomelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    varchar(50) CHARSET latin1BEGIN
DECLARE sFinalOutcome VARCHAR(50) DEFAULT "Final Outcome not set!";
DECLARE nFinalOutcomeID INT(12) DEFAULT 0;
SELECT FinalOutcomeID INTO nFinalOutcomeID FROM jobs WHERE JobID=sJobID;
SELECT FinalOutcome INTO sFinalOutcome FROM final_outcomes WHERE FinalOutcomeID=nFinalOutcomeID;
RETURN sFinalOutcome;
ENDBEGIN
DECLARE sFinalOutcome VARCHAR(50) DEFAULT "Final Outcome not set!";
DECLARE nFinalOutcomeID INT(12) DEFAULT 0;
SELECT FinalOutcomeID INTO nFinalOutcomeID FROM jobs WHERE JobID=sJobID;
SELECT FinalOutcome INTO sFinalOutcome FROM final_outcomes WHERE FinalOutcomeID=nFinalOutcomeID;
RETURN sFinalOutcome;
END�b��b���� jms_demo_masterfuncGetLabourCostfuncGetLabourCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END�b��b��0�� jms_demo_masterfuncGetLabourCostsForBranchfuncGetLabourCostsForBranchlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID=sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID=sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDd

I
=]��L�b��b��0�� jms_demo_masterfuncGetLabourCostsNotForBranchfuncGetLabourCostsNotForBranchlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID<>sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID<>sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END�b��b���� jms_demo_masterfuncGetLabourHoursfuncGetLabourHourslatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END�

�
f���
�b��b��,�� jms_demo_masterfuncGetLabourHoursByRatefuncGetLabourHoursByRatelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12),
        sRate CHAR(12)
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END�b��b��@�� jms_demo_masterfuncGetLabourHoursByRateEOMfuncGetLabourHoursByRateEOMlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12),
        sRate CHAR(12),
        sDate DATE
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate AND t.workdate <= sDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate AND t.workdate <= sDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDw
�
k
������
�b��b��
�� jms_demo_masterfuncGetLabourMaterialTotalfuncGetLabourMaterialTotallatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    decimal(11,2)BEGIN

# (SS,22/2/12) new version for new ordering system which calls funcGetMaterialCostNew instead of funcGetMaterialCost

DECLARE nLabourCost DOUBLE DEFAULT 0.00;
DECLARE nMaterialCost DOUBLE DEFAULT 0.00;
DECLARE nTotalCost DOUBLE DEFAULT 0.00;

SELECT funcGetLabourCost(sJobID) INTO nLabourCost;
SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

SET nTotalCost=nLabourCost+nMaterialCost;

RETURN nTotalCost;

ENDBEGIN

# (SS,22/2/12) new version for new ordering system which calls funcGetMaterialCostNew instead of funcGetMaterialCost

DECLARE nLabourCost DOUBLE DEFAULT 0.00;
DECLARE nMaterialCost DOUBLE DEFAULT 0.00;
DECLARE nTotalCost DOUBLE DEFAULT 0.00;

SELECT funcGetLabourCost(sJobID) INTO nLabourCost;
SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

SET nTotalCost=nLabourCost+nMaterialCost;

RETURN nTotalCost;

END�b��b��


 jms_demo_masterfuncGetLossOnJobfuncGetLossOnJoblatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLossOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice<nTotalCost THEN
          SET nLossOnJob=(nTotalCost-nInvoicePrice);
      END IF;
    RETURN nLossOnJob;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLossOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice<nTotalCost THEN
          SET nLossOnJob=(nTotalCost-nInvoicePrice);
      END IF;
    RETURN nLossOnJob;
END.�"f+Q����b��b��4�� Pjms_demo_masterfuncGetMaterialCostNotBranchIDfuncGetMaterialCostNotBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%`sJobID` CHAR(12),
        `sBranchID` CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID<>sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID <> sBranchID;
    
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;

ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID<>sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID <> sBranchID;
    
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;

END�b��b��4�� Pjms_demo_masterfuncGetMaterialCostWithBranchIDfuncGetMaterialCostWithBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%`sJobID` CHAR(12),
        `sBranchID` CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID=sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID = sBranchID;
  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID=sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID = sBranchID;
  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�b��b��
zz jms_demo_masterfuncGetWarrantyCostsfuncGetWarrantyCostslatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nWarrantyCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nJobID CHAR(12) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    DECLARE nSubtotal FLOAT(9,2) DEFAULT 0.00;
    DECLARE curWarrantyJobs CURSOR FOR SELECT JobID,CWJobID FROM jobs WHERE CWJobID=sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished=1;
      OPEN curWarrantyJobs;
      nextrecord: LOOP
          FETCH curWarrantyJobs INTO nJobID,nCWJobID;
        IF bFinished=1 THEN
              LEAVE nextrecord;
          END IF;
        SELECT funcGetMaterialCost(JobID)+funcGetLabourCost(JobID) INTO nSubtotal FROM jobs WHERE JobID=nJobID;
        IF nSubtotal IS NULL THEN
            SET nSubtotal=0.00;
        END IF;
        SET nWarrantyCost=nWarrantyCost+nSubtotal;
    END LOOP nextrecord;
      CLOSE curWarrantyJobs;
    RETURN nWarrantyCost;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nWarrantyCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nJobID CHAR(12) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    DECLARE nSubtotal FLOAT(9,2) DEFAULT 0.00;
    DECLARE curWarrantyJobs CURSOR FOR SELECT JobID,CWJobID FROM jobs WHERE CWJobID=sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished=1;
      OPEN curWarrantyJobs;
      nextrecord: LOOP
          FETCH curWarrantyJobs INTO nJobID,nCWJobID;
        IF bFinished=1 THEN
              LEAVE nextrecord;
          END IF;
        SELECT funcGetMaterialCost(JobID)+funcGetLabourCost(JobID) INTO nSubtotal FROM jobs WHERE JobID=nJobID;
        IF nSubtotal IS NULL THEN
            SET nSubtotal=0.00;
        END IF;
        SET nWarrantyCost=nWarrantyCost+nSubtotal;
    END LOOP nextrecord;
      CLOSE curWarrantyJobs;
    RETURN nWarrantyCost;
END>�)���C�b��b��
 jms_demo_masterfuncGetProfitOnJobfuncGetProfitOnJoblatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nProfitOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice>nTotalCost THEN
          SET nProfitOnJob=(nInvoicePrice-nTotalCost);
      END IF;
    RETURN nProfitOnJob;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nProfitOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice>nTotalCost THEN
          SET nProfitOnJob=(nInvoicePrice-nTotalCost);
      END IF;
    RETURN nProfitOnJob;
END�b��b��ss jms_demo_masterfuncGetWarrantyCreatedEngineerfuncGetWarrantyCreatedEngineerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    varchar(100) CHARSET latin1BEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    SELECT CWJobID INTO nCWJobID FROM jobs WHERE JobID=sJobID;
    IF nCWJobID IS NOT NULL THEN
        SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=nCWJobID;
        END IF;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    SELECT CWJobID INTO nCWJobID FROM jobs WHERE JobID=sJobID;
    IF nCWJobID IS NOT NULL THEN
        SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=nCWJobID;
        END IF;
    RETURN sEngineerName;
END����μ|��	�b��b��2�� jms_demo_masterfuncGetWorkshopInvoiceValuefuncGetWorkshopInvoiceValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12),
        sBranchName CHAR(50)
    doubleBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE nPassedBranchID INT(12) DEFAULT 0;
DECLARE nCommBranchID INT(12) DEFAULT 0;
DECLARE nRepairBranchID INT(12) DEFAULT 0;
DECLARE nNumberOff INT(12) DEFAULT 0;
DECLARE nQuotedPrice DOUBLE DEFAULT 0.00;
DECLARE nFinalValue DOUBLE DEFAULT 0.00;
SELECT BranchID INTO nPassedBranchID FROM branches WHERE BranchName=sBranchName;
SELECT
    JobTypeID,BranchID,RepairBranchID,NumberOff,QuotedPrice
INTO
    nJobTypeID,nCommBranchID,nRepairBranchID,nNumberOff,nQuotedPrice
FROM
    jobs WHERE JobID=sJobID;
    
SET nFinalValue=(nNumberOff*nQuotedPrice);
    
IF nJobTypeID=17 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.2727;
    END IF;
    
IF nJobTypeID=18 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.5714;
    END IF;
IF nJobTypeID=19 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.15;
        END IF;
IF nJobTypeID=20 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.4;
        END IF;
    
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nRepairBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.80;
    END IF;
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nCommBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.20;
    END IF;
SET nFinalValue=ROUND(nFinalValue,2);
RETURN nFinalValue;
ENDBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE nPassedBranchID INT(12) DEFAULT 0;
DECLARE nCommBranchID INT(12) DEFAULT 0;
DECLARE nRepairBranchID INT(12) DEFAULT 0;
DECLARE nNumberOff INT(12) DEFAULT 0;
DECLARE nQuotedPrice DOUBLE DEFAULT 0.00;
DECLARE nFinalValue DOUBLE DEFAULT 0.00;
SELECT BranchID INTO nPassedBranchID FROM branches WHERE BranchName=sBranchName;
SELECT
    JobTypeID,BranchID,RepairBranchID,NumberOff,QuotedPrice
INTO
    nJobTypeID,nCommBranchID,nRepairBranchID,nNumberOff,nQuotedPrice
FROM
    jobs WHERE JobID=sJobID;
    
SET nFinalValue=(nNumberOff*nQuotedPrice);
    
IF nJobTypeID=17 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.2727;
    END IF;
    
IF nJobTypeID=18 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.5714;
    END IF;
IF nJobTypeID=19 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.15;
        END IF;
IF nJobTypeID=20 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.4;
        END IF;
    
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nRepairBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.80;
    END IF;
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nCommBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.20;
    END IF;
SET nFinalValue=ROUND(nFinalValue,2);
RETURN nFinalValue;
END�b��b��+�� jms_demo_masterfuncIsJobOverduefuncIsJobOverduelatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12),
        dNowDate DATE
    varchar(10) CHARSET latin1BEGIN
    DECLARE sResult VARCHAR(10) DEFAULT "FALSE";
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    IF dNowDate="" THEN
        SET dNowDate=CURRENT_DATE;
        END IF;
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET sResult="TRUE";
        END IF;
    RETURN sResult;
ENDBEGIN
    DECLARE sResult VARCHAR(10) DEFAULT "FALSE";
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    IF dNowDate="" THEN
        SET dNowDate=CURRENT_DATE;
        END IF;
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET sResult="TRUE";
        END IF;
    RETURN sResult;
END�b��b��dd jms_demo_masterfuncJobBecomesWarrantyfuncJobBecomesWarrantylatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nWarrantyJobs INT(12);
    DECLARE nReturn INT(12);
    SET nWarrantyJobs=0;
    SET nReturn=0;
    SELECT COUNT(JobID) INTO nWarrantyJobs FROM jobs WHERE CWJobID=sJobID;
    IF nWarrantyJobs IS NULL THEN
        SET nReturn=0;
    END IF;
    IF nWarrantyJobs>0 THEN
        SET nReturn=nWarrantyJobs;
    END IF;
    RETURN nReturn;
ENDBEGIN
    DECLARE nWarrantyJobs INT(12);
    DECLARE nReturn INT(12);
    SET nWarrantyJobs=0;
    SET nReturn=0;
    SELECT COUNT(JobID) INTO nWarrantyJobs FROM jobs WHERE CWJobID=sJobID;
    IF nWarrantyJobs IS NULL THEN
        SET nReturn=0;
    END IF;
    IF nWarrantyJobs>0 THEN
        SET nReturn=nWarrantyJobs;
    END IF;
    RETURN nReturn;
END��arU��t�z
�b��b��8 jms_demo_masterfuncJobsInCategoryForCustomerfuncJobsInCategoryForCustomerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sCategoryName CHAR(50),
        nCustomerID INT(12)
    int(12)BEGIN
DECLARE nJobsInCategory INT(12) DEFAULT 0;
DECLARE nCategoryID INT(12) DEFAULT 0;
SELECT CategoryID INTO nCategoryID FROM categories WHERE CategoryName=sCategoryName;
SELECT COUNT(JobID) INTO nJobsInCategory FROM jobs JOIN    equipment ON jobs.EquipmentID=equipment.EquipmentID WHERE equipment.CategoryID=nCategoryID AND jobs.CustomerID=nCustomerID;
RETURN nJobsInCategory;
ENDBEGIN
DECLARE nJobsInCategory INT(12) DEFAULT 0;
DECLARE nCategoryID INT(12) DEFAULT 0;
SELECT CategoryID INTO nCategoryID FROM categories WHERE CategoryName=sCategoryName;
SELECT COUNT(JobID) INTO nJobsInCategory FROM jobs JOIN    equipment ON jobs.EquipmentID=equipment.EquipmentID WHERE equipment.CategoryID=nCategoryID AND jobs.CustomerID=nCustomerID;
RETURN nJobsInCategory;
END�b��b��*ZZ jms_demo_masterfuncMonthEndMaterialCostfuncMonthEndMaterialCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sJobID CHAR(6),
        dEndDate DATE
    decimal(9,2)BEGIN

    DECLARE nMaterialCost DECIMAL(9,2) DEFAULT 0.00;
    DECLARE nGetIn DATE DEFAULT '1999-12-31';

    #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE (order_items.JobID = sJobID) AND (order_items.itemdeliverydate IS NOT NULL AND order_items.itemdeliverydate <= dEndDate);
    #(SS,6/3/12) replaced above with following
    SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE (job_material.JobID = sJobID) AND (job_material.DateReceived IS NOT NULL AND job_material.DateReceived <= dEndDate);
    
    RETURN nMaterialCost;
    
ENDBEGIN

    DECLARE nMaterialCost DECIMAL(9,2) DEFAULT 0.00;
    DECLARE nGetIn DATE DEFAULT '1999-12-31';

    #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE (order_items.JobID = sJobID) AND (order_items.itemdeliverydate IS NOT NULL AND order_items.itemdeliverydate <= dEndDate);
    #(SS,6/3/12) replaced above with following
    SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE (job_material.JobID = sJobID) AND (job_material.DateReceived IS NOT NULL AND job_material.DateReceived <= dEndDate);
    
    RETURN nMaterialCost;
    
END�b��b���33 jms_demo_masterGetLockGetLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sTableName VARCHAR ( 32 ) ,
        sRecordKey VARCHAR ( 64 ) ,
        nAuditID INTEGER ( 11 ) ,
        nUserID INTEGER ( 11 ) ,
        sUserName VARCHAR ( 40 ) ,
        sComputerName VARCHAR ( 20 ) ,
        sIPAddress VARCHAR ( 15 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
END�	����%Y��n�b��b���� jms_demo_masterGetOrderPaymentDetailsGetOrderPaymentDetailslatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sOrderID INTEGER(11)
    varchar(200) CHARSET latin1BEGIN
  DECLARE nAmount DECIMAL(11,2);
  DECLARE sRef1 VARCHAR(40);
  DECLARE sRef2 VARCHAR(40);
  DECLARE sRef3 VARCHAR(40);
  DECLARE sPaymentTypeName VARCHAR(20);
  DECLARE sPaymentDetails VARCHAR(200);
 
  SELECT Amount, Ref1, Ref2, Ref3, payment_types.PaymentTypeName INTO nAmount, sRef1, sRef2, sRef3, sPaymentTypeName FROM material_payments
  INNER JOIN payment_types ON payment_types.PaymentTypeID = material_payments.PaymentTypeID
  WHERE OrderID = sOrderID
  ORDER BY MaterialPaymentID DESC LIMIT 1;
  
  IF sPaymentTypeName IS NULL THEN
    SET sPaymentDetails = "";
  ELSE
    # SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount, ", Ref: ", sRef1, " ", COALESCE(sRef2), " ", sRef3);
    SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount);
    IF sRef1 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", Ref: ", sRef1);
    END IF;    
    IF sRef2 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef2); 
    END IF;
    IF sRef3 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef3);
    END IF;
  END IF;
    
  RETURN sPaymentDetails;
ENDBEGIN
  DECLARE nAmount DECIMAL(11,2);
  DECLARE sRef1 VARCHAR(40);
  DECLARE sRef2 VARCHAR(40);
  DECLARE sRef3 VARCHAR(40);
  DECLARE sPaymentTypeName VARCHAR(20);
  DECLARE sPaymentDetails VARCHAR(200);
 
  SELECT Amount, Ref1, Ref2, Ref3, payment_types.PaymentTypeName INTO nAmount, sRef1, sRef2, sRef3, sPaymentTypeName FROM material_payments
  INNER JOIN payment_types ON payment_types.PaymentTypeID = material_payments.PaymentTypeID
  WHERE OrderID = sOrderID
  ORDER BY MaterialPaymentID DESC LIMIT 1;
  
  IF sPaymentTypeName IS NULL THEN
    SET sPaymentDetails = "";
  ELSE
    # SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount, ", Ref: ", sRef1, " ", COALESCE(sRef2), " ", sRef3);
    SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount);
    IF sRef1 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", Ref: ", sRef1);
    END IF;    
    IF sRef2 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef2); 
    END IF;
    IF sRef3 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef3);
    END IF;
  END IF;
    
  RETURN sPaymentDetails;
END�b��b��Bee jms_demo_masterReleaseLockReleaseLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%nLockID INTEGER ( 11 ) ,
        sComputerName VARCHAR ( 20 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
END�b��b���55 jms_demo_masterSetLockSetLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sTableName VARCHAR ( 32 ) ,
        sRecordKey VARCHAR ( 64 ) ,
        nAuditID INTEGER ( 11 ) ,
        nUserID INTEGER ( 11 ) ,
        sUserName VARCHAR ( 40 ) ,
        sComputerName VARCHAR ( 20 ) ,
        sIPAddress VARCHAR ( 15 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007
  
  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
ENDBEGIN
  # Version 4.0.32, 11/8/2007
  
  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
END���	��	�-"��X�b��b��H�� jms_demo_masterGetTranslationGetTranslationlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%sEnglishPhrase VARCHAR ( 255 ) ,
        nLanguageID INTEGER ( 11 )
    varchar(255) CHARSET latin1BEGIN
  # (SS,4/12/07) MySQL version of the translation routine
  # returns the equivalent phrase to the English one specified in requested language
  # if not found in the then the English phrase is returned

  DECLARE sLanguageName VARCHAR(40);
  DECLARE sResult VARCHAR(255);

  # get language name from languages table using language ID
  SELECT LanguageName INTO sLanguageName FROM languages WHERE LanguageID = nLanguageID;

  # get the required language phrase
  # SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase;
  
  # Following does not work: SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase
  # I initially tried dynamic SQL using SET @sSQL, PREPARE, EXECUTE, DEALLOCATE
  # but it was not allowed in stored function, could also create stored function on
  # the fly but seem very messy so I resorted to the following
  # which might be quicker anyway, but will need to update when new language added
  # could also just hard code the LanguageID without having to look up the language name
  # See: http://forge.mysql.com/snippets/download.php?id=13
  #      http://dev.mysql.com/doc/refman/5.1/en/sqlps.html
  #      http://forums.mysql.com/read.php?60,41372,47813#msg-47813
  IF sLanguageName = "English" THEN
    SELECT English INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "German" THEN
    SELECT German INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "French" THEN
    SELECT French INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Italian" THEN
    SELECT Italian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Spanish" THEN
    SELECT Spanish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Polish" THEN
    SELECT Polish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Dutch" THEN
    SELECT Dutch INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Russian" THEN
    SELECT Russian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Swedish" THEN
    SELECT Swedish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Greek" THEN
    SELECT Greek INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Czech" THEN
    SELECT Czech INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Portuguese" THEN
    SELECT Portuguese INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Hungarian" THEN
    SELECT Hungarian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSE
    # if this point reached then it means the SELECT line for this language is missing
    # needs to be added above so return error instead
    SET sResult = CONCAT("!!! Language ", sLanguageName, " not in GetTranslation function !!!");
  END IF;

  # if nothing specified in requested language then return the English phrase
  IF sResult IS NULL OR sResult = "" THEN
    SET sResult = sEnglishPhrase;
  END IF;

  # return the result
  RETURN sResult;
ENDBEGIN
  # (SS,4/12/07) MySQL version of the translation routine
  # returns the equivalent phrase to the English one specified in requested language
  # if not found in the then the English phrase is returned

  DECLARE sLanguageName VARCHAR(40);
  DECLARE sResult VARCHAR(255);

  # get language name from languages table using language ID
  SELECT LanguageName INTO sLanguageName FROM languages WHERE LanguageID = nLanguageID;

  # get the required language phrase
  # SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase;
  
  # Following does not work: SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase
  # I initially tried dynamic SQL using SET @sSQL, PREPARE, EXECUTE, DEALLOCATE
  # but it was not allowed in stored function, could also create stored function on
  # the fly but seem very messy so I resorted to the following
  # which might be quicker anyway, but will need to update when new language added
  # could also just hard code the LanguageID without having to look up the language name
  # See: http://forge.mysql.com/snippets/download.php?id=13
  #      http://dev.mysql.com/doc/refman/5.1/en/sqlps.html
  #      http://forums.mysql.com/read.php?60,41372,47813#msg-47813
  IF sLanguageName = "English" THEN
    SELECT English INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "German" THEN
    SELECT German INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "French" THEN
    SELECT French INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Italian" THEN
    SELECT Italian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Spanish" THEN
    SELECT Spanish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Polish" THEN
    SELECT Polish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Dutch" THEN
    SELECT Dutch INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Russian" THEN
    SELECT Russian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Swedish" THEN
    SELECT Swedish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Greek" THEN
    SELECT Greek INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Czech" THEN
    SELECT Czech INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Portuguese" THEN
    SELECT Portuguese INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Hungarian" THEN
    SELECT Hungarian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSE
    # if this point reached then it means the SELECT line for this language is missing
    # needs to be added above so return error instead
    SET sResult = CONCAT("!!! Language ", sLanguageName, " not in GetTranslation function !!!");
  END IF;

  # if nothing specified in requested language then return the English phrase
  IF sResult IS NULL OR sResult = "" THEN
    SET sResult = sEnglishPhrase;
  END IF;

  # return the result
  RETURN sResult;
END�Ҧ����b��b�� Pjms_demo_masterChangeOverdueAwaitsToChaseChangeOverdueAwaitsToChaselatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%BEGIN

# (SS,13/8/13) (JMS: 4.2.13) ChangeOverdueAwaitsToChase moved from executable to scheduled routine
# the UserID set is 0 (i.e. meaning System)
# (SS,21/8/13) amended to add message to log about how many jobs were updated

DECLARE sMessage CHAR(100);

# (SS,21/8/13) log the start
CALL AddToEventLog('Started ChangeOverdueAwaitsToChase');

DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 3) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 4,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 3;

# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Go-ahead advanced to Chase Go-ahead'));
CALL AddToEventLog(sMessage);
 
DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 6) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 7,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 6;
 
# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Order No advanced to Chase Order No'));
CALL AddToEventLog(sMessage); 

# (SS,21/8/13) log the finish
CALL AddToEventLog('Finished ChangeOverdueAwaitsToChase');

ENDBEGIN

# (SS,13/8/13) (JMS: 4.2.13) ChangeOverdueAwaitsToChase moved from executable to scheduled routine
# the UserID set is 0 (i.e. meaning System)
# (SS,21/8/13) amended to add message to log about how many jobs were updated

DECLARE sMessage CHAR(100);

# (SS,21/8/13) log the start
CALL AddToEventLog('Started ChangeOverdueAwaitsToChase');

DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 3) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 4,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 3;

# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Go-ahead advanced to Chase Go-ahead'));
CALL AddToEventLog(sMessage);
 
DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 6) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 7,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 6;
 
# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Order No advanced to Chase Order No'));
CALL AddToEventLog(sMessage); 

# (SS,21/8/13) log the finish
CALL AddToEventLog('Finished ChangeOverdueAwaitsToChase');

END�b��b�� �� jms_demo_masterGetTotalLabourAndMaterialsGetTotalLabourAndMaterialslatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%
        sJobID CHAR ( 12 )
    BEGIN
  # Serial: 2008-02-22-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMain(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

ENDBEGIN
  # Serial: 2008-02-22-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMain(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

END�+���˖���b��b��R�� jms_demo_masterGetPriceSummaryGetPriceSummarylatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%
        IN nEquipmentID INTEGER ( 11 ) ,
        IN nBranchID INTEGER ( 11 )
    BEGIN

# Serial: 2008-04-23-01
# Original version SS, ITP, 22-02-2007
# This version modified by Jim to use the new equipment_price_summary table
# which is created at the end of the day to help speed up this routine
# which was taking a few seconds where hundreds of jobs existing for same EquipmentID
  
DECLARE nSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nRepairPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalJobCount INT(11) DEFAULT 0;
DECLARE nWorldAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldJobCount INT(11) DEFAULT 0;
DECLARE nLocalSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldSellPrice DECIMAL(11,2) DEFAULT 0.00;

# SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;
# (SS,23/4/08) replaced above with following due to field name changes
SELECT OurRefurbPrice, OurRepairPrice, OurServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;

SELECT LocalAveSellPrice, LocalAveJobCost, LocalJobCount INTO nLocalAveSellPrice, nLocalAveJobCost, nLocalJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID AND BranchID=nBranchID;
# (SS,19/3/08) in following two it may be more accurate to use SUM(LocalAveSellPrice) / SUM(LocalJobCount)
SELECT AVG(LocalAveSellPrice) INTO nWorldAveSellPrice FROM equipment_price_summary WHERE LocalAveSellPrice>0 AND EquipmentID=nEquipmentID;
SELECT AVG(LocalAveJobCost) INTO nWorldAveJobCost FROM equipment_price_summary WHERE LocalAveJobCost>0 AND EquipmentID=nEquipmentID;
SELECT SUM(LocalJobCount) INTO nWorldJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID;

IF nSellPrice IS NULL THEN
   SET nSellPrice="0.00";
END IF;
    
IF nRepairPrice IS NULL THEN
   SET nRepairPrice="0.00";
END IF;

IF nServiceExPrice IS NULL THEN
   SET nServiceExPrice="0.00";
END IF;
    
IF nLocalAveSellPrice IS NULL THEN
   SET nLocalAveSellPrice="0.00";
END IF;    
    
IF nLocalAveJobCost IS NULL THEN
   SET nLocalAveJobCost="0.00";
END IF;
    
IF nLocalJobCount IS NULL THEN
   SET nLocalJobCount="0.00";
END IF;
    
IF nWorldAveSellPrice IS NULL THEN
   SET nWorldAveSellPrice="0.00";
END IF;    
    
IF nWorldAveJobCost IS NULL THEN
   SET nWorldAveJobCost="0.00";
END IF;
    
IF nWorldJobCount IS NULL THEN
   SET nWorldJobCount="0.00";
END IF;

SELECT
    nSellPrice AS SellPrice,
    nRepairPrice AS RepairPrice,
    nServiceExPrice AS ServiceExPrice,
    nLocalAveSellPrice AS AveSellPriceLocal,
    nLocalAveJobCost AS AveJobCostLocal,
    nLocalJobCount AS LocalJobCount,
    nWorldAveSellPrice AS AveSellPriceWorld,
    nWorldAveJobCost AS AveJobCostWorld,
    nWorldJobCount AS WorldJobCount;	

ENDBEGIN

# Serial: 2008-04-23-01
# Original version SS, ITP, 22-02-2007
# This version modified by Jim to use the new equipment_price_summary table
# which is created at the end of the day to help speed up this routine
# which was taking a few seconds where hundreds of jobs existing for same EquipmentID
  
DECLARE nSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nRepairPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalJobCount INT(11) DEFAULT 0;
DECLARE nWorldAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldJobCount INT(11) DEFAULT 0;
DECLARE nLocalSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldSellPrice DECIMAL(11,2) DEFAULT 0.00;

# SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;
# (SS,23/4/08) replaced above with following due to field name changes
SELECT OurRefurbPrice, OurRepairPrice, OurServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;

SELECT LocalAveSellPrice, LocalAveJobCost, LocalJobCount INTO nLocalAveSellPrice, nLocalAveJobCost, nLocalJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID AND BranchID=nBranchID;
# (SS,19/3/08) in following two it may be more accurate to use SUM(LocalAveSellPrice) / SUM(LocalJobCount)
SELECT AVG(LocalAveSellPrice) INTO nWorldAveSellPrice FROM equipment_price_summary WHERE LocalAveSellPrice>0 AND EquipmentID=nEquipmentID;
SELECT AVG(LocalAveJobCost) INTO nWorldAveJobCost FROM equipment_price_summary WHERE LocalAveJobCost>0 AND EquipmentID=nEquipmentID;
SELECT SUM(LocalJobCount) INTO nWorldJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID;

IF nSellPrice IS NULL THEN
   SET nSellPrice="0.00";
END IF;
    
IF nRepairPrice IS NULL THEN
   SET nRepairPrice="0.00";
END IF;

IF nServiceExPrice IS NULL THEN
   SET nServiceExPrice="0.00";
END IF;
    
IF nLocalAveSellPrice IS NULL THEN
   SET nLocalAveSellPrice="0.00";
END IF;    
    
IF nLocalAveJobCost IS NULL THEN
   SET nLocalAveJobCost="0.00";
END IF;
    
IF nLocalJobCount IS NULL THEN
   SET nLocalJobCount="0.00";
END IF;
    
IF nWorldAveSellPrice IS NULL THEN
   SET nWorldAveSellPrice="0.00";
END IF;    
    
IF nWorldAveJobCost IS NULL THEN
   SET nWorldAveJobCost="0.00";
END IF;
    
IF nWorldJobCount IS NULL THEN
   SET nWorldJobCount="0.00";
END IF;

SELECT
    nSellPrice AS SellPrice,
    nRepairPrice AS RepairPrice,
    nServiceExPrice AS ServiceExPrice,
    nLocalAveSellPrice AS AveSellPriceLocal,
    nLocalAveJobCost AS AveJobCostLocal,
    nLocalJobCount AS LocalJobCount,
    nWorldAveSellPrice AS AveSellPriceWorld,
    nWorldAveJobCost AS AveJobCostWorld,
    nWorldJobCount AS WorldJobCount;	

END�Y�i{���b��b��R�� jms_demo_masterGetPriceSummaryPrevGetPriceSummaryPrevlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%
        IN nEquipmentID INTEGER ( 11 ) ,
        IN nBranchID INTEGER ( 11 )
    BEGIN

  # Serial: 2008-02-22-01

  DECLARE nSellPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nRepairPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceWorld DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostWorld DECIMAL(11,2) DEFAULT NULL;

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nJobID INTEGER(11);
  DECLARE nJobBranchID INTEGER(11);
  DECLARE nJobSellPrice DOUBLE;

  DECLARE nWorldSellPrice DOUBLE DEFAULT 0;
  DECLARE nWorldJobCost DOUBLE DEFAULT 0;
  DECLARE nWorldJobCount INTEGER DEFAULT 0;
  DECLARE nLocalSellPrice DOUBLE DEFAULT 0;
  DECLARE nLocalJobCost DOUBLE DEFAULT 0;
  DECLARE nLocalJobCount INTEGER DEFAULT 0;

  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(9,2);
  DECLARE nLabourHours DOUBLE;

  # this query selects all invoiced jobs with given equipment ID
  DECLARE curSelectedJobs CURSOR FOR SELECT JobID, BranchID, SellPrice FROM jobs WHERE WIP = FALSE AND EquipmentID = nEquipmentID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  # get SellPrice, RepairPrice, ServiceExPrice from Equipment table
  SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID = nEquipmentID;

  OPEN curSelectedJobs;

  nextrecord: LOOP

    FETCH curSelectedJobs INTO nJobID, nJobBranchID, nJobSellPrice;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    # determine JobCost using GetTotalLabourAndMaterialsMain procedure, i.e. JobCost = LabourCost + MaterialCost
    CALL GetTotalLabourAndMaterialsMain(nJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);

    # treat NULL as zero, else total will end up as NULL, alternative is to ignore records containing NULLs
    IF nJobSellPrice IS NULL THEN
      SET nJobSellPrice = 0;
    END IF;

    SET nWorldSellPrice = nWorldSellPrice + nJobSellPrice;
    SET nWorldJobCost = nWorldJobCost + nLabourCost + nMaterialCost;
    SET nWorldJobCount = nWorldJobCount + 1;

    IF nBranchID = nJobBranchID THEN
       SET nLocalSellPrice = nLocalSellPrice + nJobSellPrice;
       SET nLocalJobCost = nLocalJobCost + nLabourCost + nMaterialCost;
       SET nLocalJobCount = nLocalJobCount + 1;
    END IF;

  END LOOP nextrecord;

  CLOSE curSelectedJobs;

  # calculate the averages

  IF nLocalJobCount > 1 THEN
     SET nAveSellPriceLocal = ROUND(nLocalSellPrice / nLocalJobCount, 2);
     SET nAveJobCostLocal = ROUND(nLocalJobCost / nLocalJobCount, 2);
  END IF;

  IF nWorldJobCount > 1 THEN
     SET nAveSellPriceWorld = ROUND(nWorldSellPrice / nWorldJobCount, 2);
     SET nAveJobCostWorld = ROUND(nWorldJobCost / nWorldJobCount, 2);
  END IF;

  # ------------------
  # Output the results
  # ------------------

  SELECT nSellPrice AS SellPrice, nRepairPrice As RepairPrice, nServiceExPrice AS ServiceExPrice,
         nAveSellPriceLocal AS AveSellPriceLocal, nAveJobCostLocal AS AveJobCostLocal, nLocalJobCount AS LocalJobCount,
         nAveSellPriceWorld AS AveSellPriceWorld, nAveJobCostWorld AS AveJobCostWorld, nWorldJobCount AS WorldJobCount;

ENDBEGIN

  # Serial: 2008-02-22-01

  DECLARE nSellPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nRepairPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceWorld DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostWorld DECIMAL(11,2) DEFAULT NULL;

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nJobID INTEGER(11);
  DECLARE nJobBranchID INTEGER(11);
  DECLARE nJobSellPrice DOUBLE;

  DECLARE nWorldSellPrice DOUBLE DEFAULT 0;
  DECLARE nWorldJobCost DOUBLE DEFAULT 0;
  DECLARE nWorldJobCount INTEGER DEFAULT 0;
  DECLARE nLocalSellPrice DOUBLE DEFAULT 0;
  DECLARE nLocalJobCost DOUBLE DEFAULT 0;
  DECLARE nLocalJobCount INTEGER DEFAULT 0;

  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(9,2);
  DECLARE nLabourHours DOUBLE;

  # this query selects all invoiced jobs with given equipment ID
  DECLARE curSelectedJobs CURSOR FOR SELECT JobID, BranchID, SellPrice FROM jobs WHERE WIP = FALSE AND EquipmentID = nEquipmentID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  # get SellPrice, RepairPrice, ServiceExPrice from Equipment table
  SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID = nEquipmentID;

  OPEN curSelectedJobs;

  nextrecord: LOOP

    FETCH curSelectedJobs INTO nJobID, nJobBranchID, nJobSellPrice;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    # determine JobCost using GetTotalLabourAndMaterialsMain procedure, i.e. JobCost = LabourCost + MaterialCost
    CALL GetTotalLabourAndMaterialsMain(nJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);

    # treat NULL as zero, else total will end up as NULL, alternative is to ignore records containing NULLs
    IF nJobSellPrice IS NULL THEN
      SET nJobSellPrice = 0;
    END IF;

    SET nWorldSellPrice = nWorldSellPrice + nJobSellPrice;
    SET nWorldJobCost = nWorldJobCost + nLabourCost + nMaterialCost;
    SET nWorldJobCount = nWorldJobCount + 1;

    IF nBranchID = nJobBranchID THEN
       SET nLocalSellPrice = nLocalSellPrice + nJobSellPrice;
       SET nLocalJobCost = nLocalJobCost + nLabourCost + nMaterialCost;
       SET nLocalJobCount = nLocalJobCount + 1;
    END IF;

  END LOOP nextrecord;

  CLOSE curSelectedJobs;

  # calculate the averages

  IF nLocalJobCount > 1 THEN
     SET nAveSellPriceLocal = ROUND(nLocalSellPrice / nLocalJobCount, 2);
     SET nAveJobCostLocal = ROUND(nLocalJobCost / nLocalJobCount, 2);
  END IF;

  IF nWorldJobCount > 1 THEN
     SET nAveSellPriceWorld = ROUND(nWorldSellPrice / nWorldJobCount, 2);
     SET nAveJobCostWorld = ROUND(nWorldJobCost / nWorldJobCount, 2);
  END IF;

  # ------------------
  # Output the results
  # ------------------

  SELECT nSellPrice AS SellPrice, nRepairPrice As RepairPrice, nServiceExPrice AS ServiceExPrice,
         nAveSellPriceLocal AS AveSellPriceLocal, nAveJobCostLocal AS AveJobCostLocal, nLocalJobCount AS LocalJobCount,
         nAveSellPriceWorld AS AveSellPriceWorld, nAveJobCostWorld AS AveJobCostWorld, nWorldJobCount AS WorldJobCount;

ENDQ������b��b���� jms_demo_masterGetTotalLabourAndMaterialsMainGetTotalLabourAndMaterialsMainlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%
        IN sJobID CHAR ( 12 ) ,
        OUT pLabourHours INTEGER ( 11 ) ,
        OUT pLabourCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialSell DECIMAL ( 11 , 2 ) ,
        OUT pMinSellPrice DECIMAL ( 11 , 2 )
    BEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-09-12-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCost(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDBEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-09-12-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCost(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
END�b��b�� �� jms_demo_masterGetTotalLabourAndMaterialsNewGetTotalLabourAndMaterialsNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%
        sJobID CHAR ( 12 )
    BEGIN
  # Serial: 2008-12-19-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMainNew(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

ENDBEGIN
  # Serial: 2008-12-19-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMainNew(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

END7.0�C���b��b��!!@@ Pjms_demo_masterGetTotalLabourAndMaterialsMainNewGetTotalLabourAndMaterialsMainNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%
        IN sJobID CHAR ( 12 ) ,
        OUT pLabourHours INTEGER ( 11 ) ,
        OUT pLabourCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialSell DECIMAL ( 11 , 2 ) ,
        OUT pMinSellPrice DECIMAL ( 11 , 2 )
    BEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2013-08-13-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # (SS,13/8/13) added getting of MaterialThreshold, MaterialBelowThresholdSellFactor and MaterialAboveThresholdSellFactor from branches table
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  
  # (SS,13/8/13) added following, these setting are now taken from branches table  
  DECLARE nMaterialThreshold DECIMAL(11,2) DEFAULT 10;
  DECLARE nMaterialBelowThresholdSellFactor DECIMAL(11,2) DEFAULT 1.4;
  DECLARE nMaterialAboveThresholdSellFactor DECIMAL(11,2) DEFAULT 1.3;  
  
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

  # (SS,13/8/13) replaced 10, 1.4, 1.3 constants with variables, settings from branches table      
  SELECT MaterialThreshold, MaterialBelowThresholdSellFactor, MaterialAboveThresholdSellFactor INTO nMaterialThreshold, nMaterialBelowThresholdSellFactor, nMaterialAboveThresholdSellFactor 
      FROM jobs 
    INNER JOIN branches ON branches.BranchID = jobs.BranchID 
    WHERE JobID = sJobID;
  
  If nMaterialCost < nMaterialThreshold THEN
    SET nMaterialSell = nMaterialCost * nMaterialBelowThresholdSellFactor;
  ELSE
    SET nMaterialSell = nMaterialCost * nMaterialAboveThresholdSellFactor;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDBEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2013-08-13-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # (SS,13/8/13) added getting of MaterialThreshold, MaterialBelowThresholdSellFactor and MaterialAboveThresholdSellFactor from branches table
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  
  # (SS,13/8/13) added following, these setting are now taken from branches table  
  DECLARE nMaterialThreshold DECIMAL(11,2) DEFAULT 10;
  DECLARE nMaterialBelowThresholdSellFactor DECIMAL(11,2) DEFAULT 1.4;
  DECLARE nMaterialAboveThresholdSellFactor DECIMAL(11,2) DEFAULT 1.3;  
  
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

  # (SS,13/8/13) replaced 10, 1.4, 1.3 constants with variables, settings from branches table      
  SELECT MaterialThreshold, MaterialBelowThresholdSellFactor, MaterialAboveThresholdSellFactor INTO nMaterialThreshold, nMaterialBelowThresholdSellFactor, nMaterialAboveThresholdSellFactor 
      FROM jobs 
    INNER JOIN branches ON branches.BranchID = jobs.BranchID 
    WHERE JobID = sJobID;
  
  If nMaterialCost < nMaterialThreshold THEN
    SET nMaterialSell = nMaterialCost * nMaterialBelowThresholdSellFactor;
  ELSE
    SET nMaterialSell = nMaterialCost * nMaterialAboveThresholdSellFactor;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDE��&��
�b��b��""oo jms_demo_masterUpdateEquipmentAveragePriceSummaryUpdateEquipmentAveragePriceSummarylatin1latin1_swedish_cilatin1_swedish_cijms_demo_master@%BEGIN

# (SS,ITP,19/3/08)
# Serial: 2008-03-19-01
# This routine is to be run at the end of the day
# Creates records used by the GetPriceSummary routine
# to help speed up getting the average prices for a given job

# empty the table
TRUNCATE equipment_price_summary;

# summarise and add the records
INSERT INTO equipment_price_summary
(SELECT EquipmentID, BranchID,
ROUND(AVG(SellPrice),2) AS LocalAveSellPrice, ROUND(AVG(funcGetLabourMaterialTotal(JobID)),2) AS LocalAveJobCost,
COUNT(JobID) AS LocalJobCount, CURDATE() AS LastUpdate
FROM jobs WHERE WIP=FALSE AND CREDITNOTE=FALSE
GROUP BY EquipmentID, BranchID
);

ENDBEGIN

# (SS,ITP,19/3/08)
# Serial: 2008-03-19-01
# This routine is to be run at the end of the day
# Creates records used by the GetPriceSummary routine
# to help speed up getting the average prices for a given job

# empty the table
TRUNCATE equipment_price_summary;

# summarise and add the records
INSERT INTO equipment_price_summary
(SELECT EquipmentID, BranchID,
ROUND(AVG(SellPrice),2) AS LocalAveSellPrice, ROUND(AVG(funcGetLabourMaterialTotal(JobID)),2) AS LocalAveJobCost,
COUNT(JobID) AS LocalJobCount, CURDATE() AS LastUpdate
FROM jobs WHERE WIP=FALSE AND CREDITNOTE=FALSE
GROUP BY EquipmentID, BranchID
);

END�b�a$b�a$


�xx Thyperflightcollect_statscollect_statsutf8mb4utf8mb4_general_cilatin1_swedish_cihyperflight@%
	IN `sPageType` CHAR(20),
	IN `nPageTime` DOUBLE,
	IN `nSelectQueries` INT,
	IN `nExecQueries` INT,
	IN `sIPAddress` VARCHAR(45),
	IN `sQueryString` VARCHAR(255)







BEGIN
	# (SS,4/6/22) adds to or updates the app_stats memory table
	
	DECLARE nFoundID INTEGER;
	
	SELECT id INTO nFoundID FROM app_stats WHERE stats_date = CURDATE() AND page_type = sPageType LIMIT 1;
	
	# if record for date and page type doesn't exist then insert
	IF (nFoundID IS NULL) THEN
		INSERT INTO app_stats (stats_date, page_type, page_requests, stats_time_min, time_secs_min, time_secs_max, time_secs_sum, select_queries_min, select_queries_max, select_queries_sum, exec_queries_min, exec_queries_max, exec_queries_sum) 
			VALUES (CURDATE(), sPageType, 0, CURTIME(), nPageTime, nPageTime, 0, nSelectQueries, nSelectQueries, 0, nExecQueries, nExecQueries, 0);
		SET nFoundID = (SELECT LAST_INSERT_ID());
	END IF;
	
	UPDATE app_stats SET page_requests = page_requests + 1, time_secs = nPageTime, select_queries = nSelectQueries, exec_queries = nExecQueries, stats_time_max = CURTIME(), query_string_latest = sQueryString, ip_address_latest = sIPAddress,
		time_secs_sum = time_secs_sum	+ nPageTime, select_queries_sum = select_queries_sum + nSelectQueries, exec_queries_sum = exec_queries_sum + nExecQueries,
		time_secs_min = LEAST(time_secs_min, nPageTime), time_secs_max = GREATEST(time_secs_max, nPageTime), 
		select_queries_min = LEAST(select_queries_min, nSelectQueries), select_queries_max = GREATEST(select_queries_max, nSelectQueries), 
		exec_queries_min = LEAST(exec_queries_min, nExecQueries), exec_queries_max = GREATEST(exec_queries_max, nExecQueries)
		WHERE id = nFoundID;
		
	# if outside range then add to app_stats_err table
	IF (nPageTime = 0) OR (nPageTime >= 1) OR (nSelectQueries < 10) THEN
		INSERT INTO app_stats_err (stats_date, stats_time, page_type, time_secs, select_queries, exec_queries, ip_address, query_string) 
			VALUES (CURDATE(), CURTIME(), sPageType, nPageTime, nSelectQueries, nExecQueries, sIPAddress, sQueryString);
	END IF;		

ENDCalled from apputils.aspBEGIN
	# (SS,4/6/22) adds to or updates the app_stats memory table
	
	DECLARE nFoundID INTEGER;
	
	SELECT id INTO nFoundID FROM app_stats WHERE stats_date = CURDATE() AND page_type = sPageType LIMIT 1;
	
	# if record for date and page type doesn't exist then insert
	IF (nFoundID IS NULL) THEN
		INSERT INTO app_stats (stats_date, page_type, page_requests, stats_time_min, time_secs_min, time_secs_max, time_secs_sum, select_queries_min, select_queries_max, select_queries_sum, exec_queries_min, exec_queries_max, exec_queries_sum) 
			VALUES (CURDATE(), sPageType, 0, CURTIME(), nPageTime, nPageTime, 0, nSelectQueries, nSelectQueries, 0, nExecQueries, nExecQueries, 0);
		SET nFoundID = (SELECT LAST_INSERT_ID());
	END IF;
	
	UPDATE app_stats SET page_requests = page_requests + 1, time_secs = nPageTime, select_queries = nSelectQueries, exec_queries = nExecQueries, stats_time_max = CURTIME(), query_string_latest = sQueryString, ip_address_latest = sIPAddress,
		time_secs_sum = time_secs_sum	+ nPageTime, select_queries_sum = select_queries_sum + nSelectQueries, exec_queries_sum = exec_queries_sum + nExecQueries,
		time_secs_min = LEAST(time_secs_min, nPageTime), time_secs_max = GREATEST(time_secs_max, nPageTime), 
		select_queries_min = LEAST(select_queries_min, nSelectQueries), select_queries_max = GREATEST(select_queries_max, nSelectQueries), 
		exec_queries_min = LEAST(exec_queries_min, nExecQueries), exec_queries_max = GREATEST(exec_queries_max, nExecQueries)
		WHERE id = nFoundID;
		
	# if outside range then add to app_stats_err table
	IF (nPageTime = 0) OR (nPageTime >= 1) OR (nSelectQueries < 10) THEN
		INSERT INTO app_stats_err (stats_date, stats_time, page_type, time_secs, select_queries, exec_queries, ip_address, query_string) 
			VALUES (CURDATE(), CURTIME(), sPageType, nPageTime, nSelectQueries, nExecQueries, sIPAddress, sQueryString);
	END IF;		

END�K�˨f����b�vb�v
�dd Thyperflightsave_app_statssave_app_statsutf8mb4utf8mb4_general_cilatin1_swedish_cihyperflight@%
	IN `sPageType` CHAR(20),
	IN `sScriptName` VARCHAR(50),
	IN `nSelectQueries` INT,
	IN `nExecQueries` INT,
	IN `sIPAddress` VARCHAR(45),
	IN `sQueryString` VARCHAR(255)






BEGIN
	# (SS,4/6/22) adds to or updates the app_stats memory table
	
	DECLARE nFoundID INTEGER;
	
	SET @time_taken = TIMESTAMPDIFF(MICROSECOND, @start_time, NOW(6)) / 1000000;		
	
	SELECT id INTO nFoundID FROM app_stats WHERE stats_date = CURDATE() AND page_type = sPageType AND script_name = sScriptName LIMIT 1;
	
	# if record for date and page type doesn't exist then insert
	IF (nFoundID IS NULL) THEN
		INSERT INTO app_stats (stats_date, page_type, script_name, page_requests, stats_time_min, time_secs_min, time_secs_max, time_secs_sum, select_queries_min, select_queries_max, select_queries_sum, exec_queries_min, exec_queries_max, exec_queries_sum) 
			VALUES (CURDATE(), sPageType, sScriptName, 0, CURTIME(), @time_taken, @time_taken, 0, nSelectQueries, nSelectQueries, 0, nExecQueries, nExecQueries, 0);
		SET nFoundID = (SELECT LAST_INSERT_ID());
	END IF;	

	UPDATE app_stats SET page_requests = page_requests + 1, time_secs = @time_taken, select_queries = nSelectQueries, exec_queries = nExecQueries, stats_time_max = CURTIME(), query_string_latest = sQueryString, ip_address_latest = sIPAddress,
		time_secs_sum = time_secs_sum	+ @time_taken, select_queries_sum = select_queries_sum + nSelectQueries, exec_queries_sum = exec_queries_sum + nExecQueries,
		time_secs_min = LEAST(time_secs_min, @time_taken), time_secs_max = GREATEST(time_secs_max, @time_taken), 
		select_queries_min = LEAST(select_queries_min, nSelectQueries), select_queries_max = GREATEST(select_queries_max, nSelectQueries), 
		exec_queries_min = LEAST(exec_queries_min, nExecQueries), exec_queries_max = GREATEST(exec_queries_max, nExecQueries),
		connection_id_latest = CONNECTION_ID()
		WHERE id = nFoundID;
		
	# if outside range then add to app_stats_slow table
	IF (@time_taken = 0) OR (@time_taken >= 1) THEN
		INSERT INTO app_stats_slow (stats_date, stats_time, page_type, script_name, time_secs, select_queries, exec_queries, connection_id, ip_address, query_string) 
			VALUES (CURDATE(), CURTIME(), sPageType, sScriptName, @time_taken, nSelectQueries, nExecQueries, CONNECTION_ID(), sIPAddress, sQueryString);
	END IF;		

ENDCalled from apputils.aspBEGIN
	# (SS,4/6/22) adds to or updates the app_stats memory table
	
	DECLARE nFoundID INTEGER;
	
	SET @time_taken = TIMESTAMPDIFF(MICROSECOND, @start_time, NOW(6)) / 1000000;		
	
	SELECT id INTO nFoundID FROM app_stats WHERE stats_date = CURDATE() AND page_type = sPageType AND script_name = sScriptName LIMIT 1;
	
	# if record for date and page type doesn't exist then insert
	IF (nFoundID IS NULL) THEN
		INSERT INTO app_stats (stats_date, page_type, script_name, page_requests, stats_time_min, time_secs_min, time_secs_max, time_secs_sum, select_queries_min, select_queries_max, select_queries_sum, exec_queries_min, exec_queries_max, exec_queries_sum) 
			VALUES (CURDATE(), sPageType, sScriptName, 0, CURTIME(), @time_taken, @time_taken, 0, nSelectQueries, nSelectQueries, 0, nExecQueries, nExecQueries, 0);
		SET nFoundID = (SELECT LAST_INSERT_ID());
	END IF;	

	UPDATE app_stats SET page_requests = page_requests + 1, time_secs = @time_taken, select_queries = nSelectQueries, exec_queries = nExecQueries, stats_time_max = CURTIME(), query_string_latest = sQueryString, ip_address_latest = sIPAddress,
		time_secs_sum = time_secs_sum	+ @time_taken, select_queries_sum = select_queries_sum + nSelectQueries, exec_queries_sum = exec_queries_sum + nExecQueries,
		time_secs_min = LEAST(time_secs_min, @time_taken), time_secs_max = GREATEST(time_secs_max, @time_taken), 
		select_queries_min = LEAST(select_queries_min, nSelectQueries), select_queries_max = GREATEST(select_queries_max, nSelectQueries), 
		exec_queries_min = LEAST(exec_queries_min, nExecQueries), exec_queries_max = GREATEST(exec_queries_max, nExecQueries),
		connection_id_latest = CONNECTION_ID()
		WHERE id = nFoundID;
		
	# if outside range then add to app_stats_slow table
	IF (@time_taken = 0) OR (@time_taken >= 1) THEN
		INSERT INTO app_stats_slow (stats_date, stats_time, page_type, script_name, time_secs, select_queries, exec_queries, connection_id, ip_address, query_string) 
			VALUES (CURDATE(), CURTIME(), sPageType, sScriptName, @time_taken, nSelectQueries, nExecQueries, CONNECTION_ID(), sIPAddress, sQueryString);
	END IF;		

END�e��e��		B�� jms_demo_epochClearLockClearLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%nLockID INTEGER ( 11 ) ,
        sComputerName VARCHAR ( 20 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  # UPDATE appears before DELETE because of RETURN ROW_COUNT()
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  # UPDATE appears before DELETE because of RETURN ROW_COUNT()
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
END6*��H�
�b�#�b�#��dd Tcastironradcensave_app_statssave_app_statsutf8mb4utf8mb4_general_cilatin1_swedish_cicastironradcen@%
	IN `sPageType` CHAR(20),
	IN `sScriptName` VARCHAR(50),
	IN `nSelectQueries` INT,
	IN `nExecQueries` INT,
	IN `sIPAddress` VARCHAR(45),
	IN `sQueryString` VARCHAR(255)






BEGIN
	# (SS,4/6/22) adds to or updates the app_stats memory table
	
	DECLARE nFoundID INTEGER;
	
	SET @time_taken = TIMESTAMPDIFF(MICROSECOND, @start_time, NOW(6)) / 1000000;		
	
	SELECT id INTO nFoundID FROM app_stats WHERE stats_date = CURDATE() AND page_type = sPageType AND script_name = sScriptName LIMIT 1;
	
	# if record for date and page type doesn't exist then insert
	IF (nFoundID IS NULL) THEN
		INSERT INTO app_stats (stats_date, page_type, script_name, page_requests, stats_time_min, time_secs_min, time_secs_max, time_secs_sum, select_queries_min, select_queries_max, select_queries_sum, exec_queries_min, exec_queries_max, exec_queries_sum) 
			VALUES (CURDATE(), sPageType, sScriptName, 0, CURTIME(), @time_taken, @time_taken, 0, nSelectQueries, nSelectQueries, 0, nExecQueries, nExecQueries, 0);
		SET nFoundID = (SELECT LAST_INSERT_ID());
	END IF;	

	UPDATE app_stats SET page_requests = page_requests + 1, time_secs = @time_taken, select_queries = nSelectQueries, exec_queries = nExecQueries, stats_time_max = CURTIME(), query_string_latest = sQueryString, ip_address_latest = sIPAddress,
		time_secs_sum = time_secs_sum	+ @time_taken, select_queries_sum = select_queries_sum + nSelectQueries, exec_queries_sum = exec_queries_sum + nExecQueries,
		time_secs_min = LEAST(time_secs_min, @time_taken), time_secs_max = GREATEST(time_secs_max, @time_taken), 
		select_queries_min = LEAST(select_queries_min, nSelectQueries), select_queries_max = GREATEST(select_queries_max, nSelectQueries), 
		exec_queries_min = LEAST(exec_queries_min, nExecQueries), exec_queries_max = GREATEST(exec_queries_max, nExecQueries),
		connection_id_latest = CONNECTION_ID()
		WHERE id = nFoundID;
		
	# if outside range then add to app_stats_slow table
	IF (@time_taken = 0) OR (@time_taken >= 1) THEN
		INSERT INTO app_stats_slow (stats_date, stats_time, page_type, script_name, time_secs, select_queries, exec_queries, connection_id, ip_address, query_string) 
			VALUES (CURDATE(), CURTIME(), sPageType, sScriptName, @time_taken, nSelectQueries, nExecQueries, CONNECTION_ID(), sIPAddress, sQueryString);
	END IF;		

ENDCalled from apputils.aspBEGIN
	# (SS,4/6/22) adds to or updates the app_stats memory table
	
	DECLARE nFoundID INTEGER;
	
	SET @time_taken = TIMESTAMPDIFF(MICROSECOND, @start_time, NOW(6)) / 1000000;		
	
	SELECT id INTO nFoundID FROM app_stats WHERE stats_date = CURDATE() AND page_type = sPageType AND script_name = sScriptName LIMIT 1;
	
	# if record for date and page type doesn't exist then insert
	IF (nFoundID IS NULL) THEN
		INSERT INTO app_stats (stats_date, page_type, script_name, page_requests, stats_time_min, time_secs_min, time_secs_max, time_secs_sum, select_queries_min, select_queries_max, select_queries_sum, exec_queries_min, exec_queries_max, exec_queries_sum) 
			VALUES (CURDATE(), sPageType, sScriptName, 0, CURTIME(), @time_taken, @time_taken, 0, nSelectQueries, nSelectQueries, 0, nExecQueries, nExecQueries, 0);
		SET nFoundID = (SELECT LAST_INSERT_ID());
	END IF;	

	UPDATE app_stats SET page_requests = page_requests + 1, time_secs = @time_taken, select_queries = nSelectQueries, exec_queries = nExecQueries, stats_time_max = CURTIME(), query_string_latest = sQueryString, ip_address_latest = sIPAddress,
		time_secs_sum = time_secs_sum	+ @time_taken, select_queries_sum = select_queries_sum + nSelectQueries, exec_queries_sum = exec_queries_sum + nExecQueries,
		time_secs_min = LEAST(time_secs_min, @time_taken), time_secs_max = GREATEST(time_secs_max, @time_taken), 
		select_queries_min = LEAST(select_queries_min, nSelectQueries), select_queries_max = GREATEST(select_queries_max, nSelectQueries), 
		exec_queries_min = LEAST(exec_queries_min, nExecQueries), exec_queries_max = GREATEST(exec_queries_max, nExecQueries),
		connection_id_latest = CONNECTION_ID()
		WHERE id = nFoundID;
		
	# if outside range then add to app_stats_slow table
	IF (@time_taken = 0) OR (@time_taken >= 1) THEN
		INSERT INTO app_stats_slow (stats_date, stats_time, page_type, script_name, time_secs, select_queries, exec_queries, connection_id, ip_address, query_string) 
			VALUES (CURDATE(), CURTIME(), sPageType, sScriptName, @time_taken, nSelectQueries, nExecQueries, CONNECTION_ID(), sIPAddress, sQueryString);
	END IF;		

END�e��e��MXX jms_demo_epochfuncGetAdjustedSalesValuefuncGetAdjustedSalesValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%nInvoiceValue DOUBLE,
        nMargin DOUBLE,
        nJobTypeID INT(12)
    doubleBEGIN
DECLARE nAdjustedSalesValue DOUBLE DEFAULT 0.00;
IF nJobTypeID=1 OR nJobTypeID=4 OR nJobTypeID=6 OR nJobTypeID=8 OR nJobTypeID=10 THEN
    SET nAdjustedSalesValue=nInvoiceValue;
    END IF;
    
IF nJobTypeID=2 OR nJobTypeID=3 OR nJobTypeID=12 THEN
    SET nAdjustedSalesValue=(nMargin*2);
    END IF;
    
RETURN nAdjustedSalesValue;
ENDBEGIN
DECLARE nAdjustedSalesValue DOUBLE DEFAULT 0.00;
IF nJobTypeID=1 OR nJobTypeID=4 OR nJobTypeID=6 OR nJobTypeID=8 OR nJobTypeID=10 THEN
    SET nAdjustedSalesValue=nInvoiceValue;
    END IF;
    
IF nJobTypeID=2 OR nJobTypeID=3 OR nJobTypeID=12 THEN
    SET nAdjustedSalesValue=(nMargin*2);
    END IF;
    
RETURN nAdjustedSalesValue;
END<�0/�@�I�e��e��/ww jms_demo_epochfuncGetDaysOverduefuncGetDaysOverduelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%`sJobID` CHAR(12),
        `sNowDate` DATE
    int(12)BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
   DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
   DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
END�e��e��+ss jms_demo_epochfuncGetDaysOverdueQuotefuncGetDaysOverdueQuotelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12),
        sNowDate DATE
    int(12)BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
    DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeOfEntry)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
    DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeOfEntry)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
END�e��e��4"" jms_demo_epochfuncGetDueDatefuncGetDueDatelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    varchar(10) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE sDueDate VARCHAR(10) DEFAULT "";
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    SELECT DATE_FORMAT(DATE_ADD(DateTimeGoAheadGiven,INTERVAL nDays DAY),'%d/%m/%Y') INTO sDueDate FROM jobs WHERE JobID=sJobID;
    RETURN sDueDate;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE sDueDate VARCHAR(10) DEFAULT "";
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    SELECT DATE_FORMAT(DATE_ADD(DateTimeGoAheadGiven,INTERVAL nDays DAY),'%d/%m/%Y') INTO sDueDate FROM jobs WHERE JobID=sJobID;
    RETURN sDueDate;
END�����������e��e��5�� jms_demo_epochfuncGetEquipStringfuncGetEquipStringlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    varchar(250) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE sEquipString VARCHAR(250) DEFAULT "Cannot find Equipment Record!";
    DECLARE nManufacturerID INT(12) DEFAULT 0;
    DECLARE sManufacturer VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE sModelNumber VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE nEquipmentID INT(15) DEFAULT 0;
    SELECT EquipmentID INTO nEquipmentID FROM jobs WHERE JobID=sJobID;
    SELECT ManufacturerID INTO nManufacturerID FROM equipment WHERE EquipmentID=nEquipmentID;
    SELECT ManufacturerName INTO sManufacturer FROM manufacturers WHERE ManufacturerID=nManufacturerID;
    SELECT ModelNumber INTO sModelNumber FROM equipment WHERE EquipmentID=nEquipmentID;
    SET sEquipString=CONCAT(sManufacturer," ",sModelNumber);
    RETURN sEquipString;
ENDBEGIN
    DECLARE sEquipString VARCHAR(250) DEFAULT "Cannot find Equipment Record!";
    DECLARE nManufacturerID INT(12) DEFAULT 0;
    DECLARE sManufacturer VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE sModelNumber VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE nEquipmentID INT(15) DEFAULT 0;
    SELECT EquipmentID INTO nEquipmentID FROM jobs WHERE JobID=sJobID;
    SELECT ManufacturerID INTO nManufacturerID FROM equipment WHERE EquipmentID=nEquipmentID;
    SELECT ManufacturerName INTO sManufacturer FROM manufacturers WHERE ManufacturerID=nManufacturerID;
    SELECT ModelNumber INTO sModelNumber FROM equipment WHERE EquipmentID=nEquipmentID;
    SET sEquipString=CONCAT(sManufacturer," ",sModelNumber);
    RETURN sEquipString;
END�e��e��488 jms_demo_epochfuncGetFinalOutcomefuncGetFinalOutcomelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    varchar(50) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
DECLARE sFinalOutcome VARCHAR(50) DEFAULT "Final Outcome not set!";
DECLARE nFinalOutcomeID INT(12) DEFAULT 0;
SELECT FinalOutcomeID INTO nFinalOutcomeID FROM jobs WHERE JobID=sJobID;
SELECT FinalOutcome INTO sFinalOutcome FROM final_outcomes WHERE FinalOutcomeID=nFinalOutcomeID;
RETURN sFinalOutcome;
ENDBEGIN
DECLARE sFinalOutcome VARCHAR(50) DEFAULT "Final Outcome not set!";
DECLARE nFinalOutcomeID INT(12) DEFAULT 0;
SELECT FinalOutcomeID INTO nFinalOutcomeID FROM jobs WHERE JobID=sJobID;
SELECT FinalOutcome INTO sFinalOutcome FROM final_outcomes WHERE FinalOutcomeID=nFinalOutcomeID;
RETURN sFinalOutcome;
END�e��e���� jms_demo_epochfuncGetLabourCostfuncGetLabourCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END

�U�ޕ����
�e��e��0�� jms_demo_epochfuncGetLabourCostsForBranchfuncGetLabourCostsForBranchlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID=sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID=sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END�e��e��0�� jms_demo_epochfuncGetLabourCostsNotForBranchfuncGetLabourCostsNotForBranchlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID<>sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID<>sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END�
�
}
�����l�e��e���� jms_demo_epochfuncGetLabourHoursfuncGetLabourHourslatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END�e��e��,�� jms_demo_epochfuncGetLabourHoursByRatefuncGetLabourHoursByRatelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12),
        sRate CHAR(12)
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END
i

����
�e��e��@�� jms_demo_epochfuncGetLabourHoursByRateEOMfuncGetLabourHoursByRateEOMlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12),
        sRate CHAR(12),
        sDate DATE
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate AND t.workdate <= sDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate AND t.workdate <= sDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END�e��e��
�� jms_demo_epochfuncGetLabourMaterialTotalfuncGetLabourMaterialTotallatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    decimal(11,2)BEGIN

# (SS,22/2/12) new version for new ordering system which calls funcGetMaterialCostNew instead of funcGetMaterialCost

DECLARE nLabourCost DOUBLE DEFAULT 0.00;
DECLARE nMaterialCost DOUBLE DEFAULT 0.00;
DECLARE nTotalCost DOUBLE DEFAULT 0.00;

SELECT funcGetLabourCost(sJobID) INTO nLabourCost;
SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

SET nTotalCost=nLabourCost+nMaterialCost;

RETURN nTotalCost;

ENDBEGIN

# (SS,22/2/12) new version for new ordering system which calls funcGetMaterialCostNew instead of funcGetMaterialCost

DECLARE nLabourCost DOUBLE DEFAULT 0.00;
DECLARE nMaterialCost DOUBLE DEFAULT 0.00;
DECLARE nTotalCost DOUBLE DEFAULT 0.00;

SELECT funcGetLabourCost(sJobID) INTO nLabourCost;
SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

SET nTotalCost=nLabourCost+nMaterialCost;

RETURN nTotalCost;

END�e��e��4�� Pjms_demo_epochfuncGetMaterialCostNotBranchIDfuncGetMaterialCostNotBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%`sJobID` CHAR(12),
        `sBranchID` CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID<>sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID <> sBranchID;
    
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;

ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID<>sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID <> sBranchID;
    
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;

END��
 �
3<�	�e��e��


 jms_demo_epochfuncGetLossOnJobfuncGetLossOnJoblatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLossOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice<nTotalCost THEN
          SET nLossOnJob=(nTotalCost-nInvoicePrice);
      END IF;
    RETURN nLossOnJob;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLossOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice<nTotalCost THEN
          SET nLossOnJob=(nTotalCost-nInvoicePrice);
      END IF;
    RETURN nLossOnJob;
END�e��e��4�� Pjms_demo_epochfuncGetMaterialCostWithBranchIDfuncGetMaterialCostWithBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%`sJobID` CHAR(12),
        `sBranchID` CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID=sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID = sBranchID;
  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID=sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID = sBranchID;
  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�����^�e��e��
 jms_demo_epochfuncGetProfitOnJobfuncGetProfitOnJoblatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nProfitOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice>nTotalCost THEN
          SET nProfitOnJob=(nInvoicePrice-nTotalCost);
      END IF;
    RETURN nProfitOnJob;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nProfitOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice>nTotalCost THEN
          SET nProfitOnJob=(nInvoicePrice-nTotalCost);
      END IF;
    RETURN nProfitOnJob;
END�e��e��
zz jms_demo_epochfuncGetWarrantyCostsfuncGetWarrantyCostslatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nWarrantyCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nJobID CHAR(12) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    DECLARE nSubtotal FLOAT(9,2) DEFAULT 0.00;
    DECLARE curWarrantyJobs CURSOR FOR SELECT JobID,CWJobID FROM jobs WHERE CWJobID=sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished=1;
      OPEN curWarrantyJobs;
      nextrecord: LOOP
          FETCH curWarrantyJobs INTO nJobID,nCWJobID;
        IF bFinished=1 THEN
              LEAVE nextrecord;
          END IF;
        SELECT funcGetMaterialCost(JobID)+funcGetLabourCost(JobID) INTO nSubtotal FROM jobs WHERE JobID=nJobID;
        IF nSubtotal IS NULL THEN
            SET nSubtotal=0.00;
        END IF;
        SET nWarrantyCost=nWarrantyCost+nSubtotal;
    END LOOP nextrecord;
      CLOSE curWarrantyJobs;
    RETURN nWarrantyCost;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nWarrantyCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nJobID CHAR(12) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    DECLARE nSubtotal FLOAT(9,2) DEFAULT 0.00;
    DECLARE curWarrantyJobs CURSOR FOR SELECT JobID,CWJobID FROM jobs WHERE CWJobID=sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished=1;
      OPEN curWarrantyJobs;
      nextrecord: LOOP
          FETCH curWarrantyJobs INTO nJobID,nCWJobID;
        IF bFinished=1 THEN
              LEAVE nextrecord;
          END IF;
        SELECT funcGetMaterialCost(JobID)+funcGetLabourCost(JobID) INTO nSubtotal FROM jobs WHERE JobID=nJobID;
        IF nSubtotal IS NULL THEN
            SET nSubtotal=0.00;
        END IF;
        SET nWarrantyCost=nWarrantyCost+nSubtotal;
    END LOOP nextrecord;
      CLOSE curWarrantyJobs;
    RETURN nWarrantyCost;
END���� Y`�%	�e��e��5ss jms_demo_epochfuncGetWarrantyCreatedEngineerfuncGetWarrantyCreatedEngineerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    varchar(100) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    SELECT CWJobID INTO nCWJobID FROM jobs WHERE JobID=sJobID;
    IF nCWJobID IS NOT NULL THEN
        SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=nCWJobID;
        END IF;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    SELECT CWJobID INTO nCWJobID FROM jobs WHERE JobID=sJobID;
    IF nCWJobID IS NOT NULL THEN
        SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=nCWJobID;
        END IF;
    RETURN sEngineerName;
END�e��e��2�� jms_demo_epochfuncGetWorkshopInvoiceValuefuncGetWorkshopInvoiceValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12),
        sBranchName CHAR(50)
    doubleBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE nPassedBranchID INT(12) DEFAULT 0;
DECLARE nCommBranchID INT(12) DEFAULT 0;
DECLARE nRepairBranchID INT(12) DEFAULT 0;
DECLARE nNumberOff INT(12) DEFAULT 0;
DECLARE nQuotedPrice DOUBLE DEFAULT 0.00;
DECLARE nFinalValue DOUBLE DEFAULT 0.00;
SELECT BranchID INTO nPassedBranchID FROM branches WHERE BranchName=sBranchName;
SELECT
    JobTypeID,BranchID,RepairBranchID,NumberOff,QuotedPrice
INTO
    nJobTypeID,nCommBranchID,nRepairBranchID,nNumberOff,nQuotedPrice
FROM
    jobs WHERE JobID=sJobID;
    
SET nFinalValue=(nNumberOff*nQuotedPrice);
    
IF nJobTypeID=17 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.2727;
    END IF;
    
IF nJobTypeID=18 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.5714;
    END IF;
IF nJobTypeID=19 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.15;
        END IF;
IF nJobTypeID=20 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.4;
        END IF;
    
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nRepairBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.80;
    END IF;
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nCommBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.20;
    END IF;
SET nFinalValue=ROUND(nFinalValue,2);
RETURN nFinalValue;
ENDBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE nPassedBranchID INT(12) DEFAULT 0;
DECLARE nCommBranchID INT(12) DEFAULT 0;
DECLARE nRepairBranchID INT(12) DEFAULT 0;
DECLARE nNumberOff INT(12) DEFAULT 0;
DECLARE nQuotedPrice DOUBLE DEFAULT 0.00;
DECLARE nFinalValue DOUBLE DEFAULT 0.00;
SELECT BranchID INTO nPassedBranchID FROM branches WHERE BranchName=sBranchName;
SELECT
    JobTypeID,BranchID,RepairBranchID,NumberOff,QuotedPrice
INTO
    nJobTypeID,nCommBranchID,nRepairBranchID,nNumberOff,nQuotedPrice
FROM
    jobs WHERE JobID=sJobID;
    
SET nFinalValue=(nNumberOff*nQuotedPrice);
    
IF nJobTypeID=17 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.2727;
    END IF;
    
IF nJobTypeID=18 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.5714;
    END IF;
IF nJobTypeID=19 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.15;
        END IF;
IF nJobTypeID=20 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.4;
        END IF;
    
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nRepairBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.80;
    END IF;
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nCommBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.20;
    END IF;
SET nFinalValue=ROUND(nFinalValue,2);
RETURN nFinalValue;
END�e��e��+4�� jms_demo_epochfuncIsJobOverduefuncIsJobOverduelatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12),
        dNowDate DATE
    varchar(10) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE sResult VARCHAR(10) DEFAULT "FALSE";
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    IF dNowDate="" THEN
        SET dNowDate=CURRENT_DATE;
        END IF;
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET sResult="TRUE";
        END IF;
    RETURN sResult;
ENDBEGIN
    DECLARE sResult VARCHAR(10) DEFAULT "FALSE";
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    IF dNowDate="" THEN
        SET dNowDate=CURRENT_DATE;
        END IF;
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET sResult="TRUE";
        END IF;
    RETURN sResult;
ENDA��S�J3�z���e��e��dd jms_demo_epochfuncJobBecomesWarrantyfuncJobBecomesWarrantylatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nWarrantyJobs INT(12);
    DECLARE nReturn INT(12);
    SET nWarrantyJobs=0;
    SET nReturn=0;
    SELECT COUNT(JobID) INTO nWarrantyJobs FROM jobs WHERE CWJobID=sJobID;
    IF nWarrantyJobs IS NULL THEN
        SET nReturn=0;
    END IF;
    IF nWarrantyJobs>0 THEN
        SET nReturn=nWarrantyJobs;
    END IF;
    RETURN nReturn;
ENDBEGIN
    DECLARE nWarrantyJobs INT(12);
    DECLARE nReturn INT(12);
    SET nWarrantyJobs=0;
    SET nReturn=0;
    SELECT COUNT(JobID) INTO nWarrantyJobs FROM jobs WHERE CWJobID=sJobID;
    IF nWarrantyJobs IS NULL THEN
        SET nReturn=0;
    END IF;
    IF nWarrantyJobs>0 THEN
        SET nReturn=nWarrantyJobs;
    END IF;
    RETURN nReturn;
END�e��e��8 jms_demo_epochfuncJobsInCategoryForCustomerfuncJobsInCategoryForCustomerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sCategoryName CHAR(50),
        nCustomerID INT(12)
    int(12)BEGIN
DECLARE nJobsInCategory INT(12) DEFAULT 0;
DECLARE nCategoryID INT(12) DEFAULT 0;
SELECT CategoryID INTO nCategoryID FROM categories WHERE CategoryName=sCategoryName;
SELECT COUNT(JobID) INTO nJobsInCategory FROM jobs JOIN    equipment ON jobs.EquipmentID=equipment.EquipmentID WHERE equipment.CategoryID=nCategoryID AND jobs.CustomerID=nCustomerID;
RETURN nJobsInCategory;
ENDBEGIN
DECLARE nJobsInCategory INT(12) DEFAULT 0;
DECLARE nCategoryID INT(12) DEFAULT 0;
SELECT CategoryID INTO nCategoryID FROM categories WHERE CategoryName=sCategoryName;
SELECT COUNT(JobID) INTO nJobsInCategory FROM jobs JOIN    equipment ON jobs.EquipmentID=equipment.EquipmentID WHERE equipment.CategoryID=nCategoryID AND jobs.CustomerID=nCustomerID;
RETURN nJobsInCategory;
END�e��e��*ZZ jms_demo_epochfuncMonthEndMaterialCostfuncMonthEndMaterialCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sJobID CHAR(6),
        dEndDate DATE
    decimal(9,2)BEGIN

    DECLARE nMaterialCost DECIMAL(9,2) DEFAULT 0.00;
    DECLARE nGetIn DATE DEFAULT '1999-12-31';

    #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE (order_items.JobID = sJobID) AND (order_items.itemdeliverydate IS NOT NULL AND order_items.itemdeliverydate <= dEndDate);
    #(SS,6/3/12) replaced above with following
    SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE (job_material.JobID = sJobID) AND (job_material.DateReceived IS NOT NULL AND job_material.DateReceived <= dEndDate);
    
    RETURN nMaterialCost;
    
ENDBEGIN

    DECLARE nMaterialCost DECIMAL(9,2) DEFAULT 0.00;
    DECLARE nGetIn DATE DEFAULT '1999-12-31';

    #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE (order_items.JobID = sJobID) AND (order_items.itemdeliverydate IS NOT NULL AND order_items.itemdeliverydate <= dEndDate);
    #(SS,6/3/12) replaced above with following
    SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE (job_material.JobID = sJobID) AND (job_material.DateReceived IS NOT NULL AND job_material.DateReceived <= dEndDate);
    
    RETURN nMaterialCost;
    
END�e��e���33 jms_demo_epochGetLockGetLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sTableName VARCHAR ( 32 ) ,
        sRecordKey VARCHAR ( 64 ) ,
        nAuditID INTEGER ( 11 ) ,
        nUserID INTEGER ( 11 ) ,
        sUserName VARCHAR ( 40 ) ,
        sComputerName VARCHAR ( 20 ) ,
        sIPAddress VARCHAR ( 15 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
END
��������TC�Z�e��e��5�� jms_demo_epochGetOrderPaymentDetailsGetOrderPaymentDetailslatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sOrderID INTEGER(11)
    varchar(200) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
  DECLARE nAmount DECIMAL(11,2);
  DECLARE sRef1 VARCHAR(40);
  DECLARE sRef2 VARCHAR(40);
  DECLARE sRef3 VARCHAR(40);
  DECLARE sPaymentTypeName VARCHAR(20);
  DECLARE sPaymentDetails VARCHAR(200);
 
  SELECT Amount, Ref1, Ref2, Ref3, payment_types.PaymentTypeName INTO nAmount, sRef1, sRef2, sRef3, sPaymentTypeName FROM material_payments
  INNER JOIN payment_types ON payment_types.PaymentTypeID = material_payments.PaymentTypeID
  WHERE OrderID = sOrderID
  ORDER BY MaterialPaymentID DESC LIMIT 1;
  
  IF sPaymentTypeName IS NULL THEN
    SET sPaymentDetails = "";
  ELSE
    # SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount, ", Ref: ", sRef1, " ", COALESCE(sRef2), " ", sRef3);
    SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount);
    IF sRef1 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", Ref: ", sRef1);
    END IF;    
    IF sRef2 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef2); 
    END IF;
    IF sRef3 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef3);
    END IF;
  END IF;
    
  RETURN sPaymentDetails;
ENDBEGIN
  DECLARE nAmount DECIMAL(11,2);
  DECLARE sRef1 VARCHAR(40);
  DECLARE sRef2 VARCHAR(40);
  DECLARE sRef3 VARCHAR(40);
  DECLARE sPaymentTypeName VARCHAR(20);
  DECLARE sPaymentDetails VARCHAR(200);
 
  SELECT Amount, Ref1, Ref2, Ref3, payment_types.PaymentTypeName INTO nAmount, sRef1, sRef2, sRef3, sPaymentTypeName FROM material_payments
  INNER JOIN payment_types ON payment_types.PaymentTypeID = material_payments.PaymentTypeID
  WHERE OrderID = sOrderID
  ORDER BY MaterialPaymentID DESC LIMIT 1;
  
  IF sPaymentTypeName IS NULL THEN
    SET sPaymentDetails = "";
  ELSE
    # SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount, ", Ref: ", sRef1, " ", COALESCE(sRef2), " ", sRef3);
    SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount);
    IF sRef1 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", Ref: ", sRef1);
    END IF;    
    IF sRef2 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef2); 
    END IF;
    IF sRef3 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef3);
    END IF;
  END IF;
    
  RETURN sPaymentDetails;
END�e��e��Bee jms_demo_epochReleaseLockReleaseLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%nLockID INTEGER ( 11 ) ,
        sComputerName VARCHAR ( 20 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
END�e��e���55 jms_demo_epochSetLockSetLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sTableName VARCHAR ( 32 ) ,
        sRecordKey VARCHAR ( 64 ) ,
        nAuditID INTEGER ( 11 ) ,
        nUserID INTEGER ( 11 ) ,
        sUserName VARCHAR ( 40 ) ,
        sComputerName VARCHAR ( 20 ) ,
        sIPAddress VARCHAR ( 15 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007
  
  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
ENDBEGIN
  # Version 4.0.32, 11/8/2007
  
  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
END��
�
�PD��@�e��e��H5�� jms_demo_epochGetTranslationGetTranslationlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%sEnglishPhrase VARCHAR ( 255 ) ,
        nLanguageID INTEGER ( 11 )
    varchar(255) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
  # (SS,4/12/07) MySQL version of the translation routine
  # returns the equivalent phrase to the English one specified in requested language
  # if not found in the then the English phrase is returned

  DECLARE sLanguageName VARCHAR(40);
  DECLARE sResult VARCHAR(255);

  # get language name from languages table using language ID
  SELECT LanguageName INTO sLanguageName FROM languages WHERE LanguageID = nLanguageID;

  # get the required language phrase
  # SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase;
  
  # Following does not work: SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase
  # I initially tried dynamic SQL using SET @sSQL, PREPARE, EXECUTE, DEALLOCATE
  # but it was not allowed in stored function, could also create stored function on
  # the fly but seem very messy so I resorted to the following
  # which might be quicker anyway, but will need to update when new language added
  # could also just hard code the LanguageID without having to look up the language name
  # See: http://forge.mysql.com/snippets/download.php?id=13
  #      http://dev.mysql.com/doc/refman/5.1/en/sqlps.html
  #      http://forums.mysql.com/read.php?60,41372,47813#msg-47813
  IF sLanguageName = "English" THEN
    SELECT English INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "German" THEN
    SELECT German INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "French" THEN
    SELECT French INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Italian" THEN
    SELECT Italian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Spanish" THEN
    SELECT Spanish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Polish" THEN
    SELECT Polish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Dutch" THEN
    SELECT Dutch INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Russian" THEN
    SELECT Russian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Swedish" THEN
    SELECT Swedish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Greek" THEN
    SELECT Greek INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Czech" THEN
    SELECT Czech INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Portuguese" THEN
    SELECT Portuguese INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Hungarian" THEN
    SELECT Hungarian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSE
    # if this point reached then it means the SELECT line for this language is missing
    # needs to be added above so return error instead
    SET sResult = CONCAT("!!! Language ", sLanguageName, " not in GetTranslation function !!!");
  END IF;

  # if nothing specified in requested language then return the English phrase
  IF sResult IS NULL OR sResult = "" THEN
    SET sResult = sEnglishPhrase;
  END IF;

  # return the result
  RETURN sResult;
ENDBEGIN
  # (SS,4/12/07) MySQL version of the translation routine
  # returns the equivalent phrase to the English one specified in requested language
  # if not found in the then the English phrase is returned

  DECLARE sLanguageName VARCHAR(40);
  DECLARE sResult VARCHAR(255);

  # get language name from languages table using language ID
  SELECT LanguageName INTO sLanguageName FROM languages WHERE LanguageID = nLanguageID;

  # get the required language phrase
  # SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase;
  
  # Following does not work: SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase
  # I initially tried dynamic SQL using SET @sSQL, PREPARE, EXECUTE, DEALLOCATE
  # but it was not allowed in stored function, could also create stored function on
  # the fly but seem very messy so I resorted to the following
  # which might be quicker anyway, but will need to update when new language added
  # could also just hard code the LanguageID without having to look up the language name
  # See: http://forge.mysql.com/snippets/download.php?id=13
  #      http://dev.mysql.com/doc/refman/5.1/en/sqlps.html
  #      http://forums.mysql.com/read.php?60,41372,47813#msg-47813
  IF sLanguageName = "English" THEN
    SELECT English INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "German" THEN
    SELECT German INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "French" THEN
    SELECT French INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Italian" THEN
    SELECT Italian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Spanish" THEN
    SELECT Spanish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Polish" THEN
    SELECT Polish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Dutch" THEN
    SELECT Dutch INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Russian" THEN
    SELECT Russian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Swedish" THEN
    SELECT Swedish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Greek" THEN
    SELECT Greek INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Czech" THEN
    SELECT Czech INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Portuguese" THEN
    SELECT Portuguese INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Hungarian" THEN
    SELECT Hungarian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSE
    # if this point reached then it means the SELECT line for this language is missing
    # needs to be added above so return error instead
    SET sResult = CONCAT("!!! Language ", sLanguageName, " not in GetTranslation function !!!");
  END IF;

  # if nothing specified in requested language then return the English phrase
  IF sResult IS NULL OR sResult = "" THEN
    SET sResult = sEnglishPhrase;
  END IF;

  # return the result
  RETURN sResult;
END���5��e��e�� Pjms_demo_epochChangeOverdueAwaitsToChaseChangeOverdueAwaitsToChaselatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%BEGIN

# (SS,13/8/13) (JMS: 4.2.13) ChangeOverdueAwaitsToChase moved from executable to scheduled routine
# the UserID set is 0 (i.e. meaning System)
# (SS,21/8/13) amended to add message to log about how many jobs were updated

DECLARE sMessage CHAR(100);

# (SS,21/8/13) log the start
CALL AddToEventLog('Started ChangeOverdueAwaitsToChase');

DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 3) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 4,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 3;

# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Go-ahead advanced to Chase Go-ahead'));
CALL AddToEventLog(sMessage);
 
DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 6) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 7,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 6;
 
# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Order No advanced to Chase Order No'));
CALL AddToEventLog(sMessage); 

# (SS,21/8/13) log the finish
CALL AddToEventLog('Finished ChangeOverdueAwaitsToChase');

ENDBEGIN

# (SS,13/8/13) (JMS: 4.2.13) ChangeOverdueAwaitsToChase moved from executable to scheduled routine
# the UserID set is 0 (i.e. meaning System)
# (SS,21/8/13) amended to add message to log about how many jobs were updated

DECLARE sMessage CHAR(100);

# (SS,21/8/13) log the start
CALL AddToEventLog('Started ChangeOverdueAwaitsToChase');

DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 3) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 4,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 3;

# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Go-ahead advanced to Chase Go-ahead'));
CALL AddToEventLog(sMessage);
 
DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 6) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 7,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 6;
 
# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Order No advanced to Chase Order No'));
CALL AddToEventLog(sMessage); 

# (SS,21/8/13) log the finish
CALL AddToEventLog('Finished ChangeOverdueAwaitsToChase');

END�e��e�� �� jms_demo_epochGetTotalLabourAndMaterialsGetTotalLabourAndMaterialslatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%
        sJobID CHAR ( 12 )
    BEGIN
  # Serial: 2008-02-22-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMain(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

ENDBEGIN
  # Serial: 2008-02-22-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMain(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

END�)�:�u����e��e��R�� jms_demo_epochGetPriceSummaryGetPriceSummarylatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%
        IN nEquipmentID INTEGER ( 11 ) ,
        IN nBranchID INTEGER ( 11 )
    BEGIN

# Serial: 2008-04-23-01
# Original version SS, ITP, 22-02-2007
# This version modified by Jim to use the new equipment_price_summary table
# which is created at the end of the day to help speed up this routine
# which was taking a few seconds where hundreds of jobs existing for same EquipmentID
  
DECLARE nSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nRepairPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalJobCount INT(11) DEFAULT 0;
DECLARE nWorldAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldJobCount INT(11) DEFAULT 0;
DECLARE nLocalSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldSellPrice DECIMAL(11,2) DEFAULT 0.00;

# SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;
# (SS,23/4/08) replaced above with following due to field name changes
SELECT OurRefurbPrice, OurRepairPrice, OurServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;

SELECT LocalAveSellPrice, LocalAveJobCost, LocalJobCount INTO nLocalAveSellPrice, nLocalAveJobCost, nLocalJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID AND BranchID=nBranchID;
# (SS,19/3/08) in following two it may be more accurate to use SUM(LocalAveSellPrice) / SUM(LocalJobCount)
SELECT AVG(LocalAveSellPrice) INTO nWorldAveSellPrice FROM equipment_price_summary WHERE LocalAveSellPrice>0 AND EquipmentID=nEquipmentID;
SELECT AVG(LocalAveJobCost) INTO nWorldAveJobCost FROM equipment_price_summary WHERE LocalAveJobCost>0 AND EquipmentID=nEquipmentID;
SELECT SUM(LocalJobCount) INTO nWorldJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID;

IF nSellPrice IS NULL THEN
   SET nSellPrice="0.00";
END IF;
    
IF nRepairPrice IS NULL THEN
   SET nRepairPrice="0.00";
END IF;

IF nServiceExPrice IS NULL THEN
   SET nServiceExPrice="0.00";
END IF;
    
IF nLocalAveSellPrice IS NULL THEN
   SET nLocalAveSellPrice="0.00";
END IF;    
    
IF nLocalAveJobCost IS NULL THEN
   SET nLocalAveJobCost="0.00";
END IF;
    
IF nLocalJobCount IS NULL THEN
   SET nLocalJobCount="0.00";
END IF;
    
IF nWorldAveSellPrice IS NULL THEN
   SET nWorldAveSellPrice="0.00";
END IF;    
    
IF nWorldAveJobCost IS NULL THEN
   SET nWorldAveJobCost="0.00";
END IF;
    
IF nWorldJobCount IS NULL THEN
   SET nWorldJobCount="0.00";
END IF;

SELECT
    nSellPrice AS SellPrice,
    nRepairPrice AS RepairPrice,
    nServiceExPrice AS ServiceExPrice,
    nLocalAveSellPrice AS AveSellPriceLocal,
    nLocalAveJobCost AS AveJobCostLocal,
    nLocalJobCount AS LocalJobCount,
    nWorldAveSellPrice AS AveSellPriceWorld,
    nWorldAveJobCost AS AveJobCostWorld,
    nWorldJobCount AS WorldJobCount;	

ENDBEGIN

# Serial: 2008-04-23-01
# Original version SS, ITP, 22-02-2007
# This version modified by Jim to use the new equipment_price_summary table
# which is created at the end of the day to help speed up this routine
# which was taking a few seconds where hundreds of jobs existing for same EquipmentID
  
DECLARE nSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nRepairPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalJobCount INT(11) DEFAULT 0;
DECLARE nWorldAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldJobCount INT(11) DEFAULT 0;
DECLARE nLocalSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldSellPrice DECIMAL(11,2) DEFAULT 0.00;

# SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;
# (SS,23/4/08) replaced above with following due to field name changes
SELECT OurRefurbPrice, OurRepairPrice, OurServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;

SELECT LocalAveSellPrice, LocalAveJobCost, LocalJobCount INTO nLocalAveSellPrice, nLocalAveJobCost, nLocalJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID AND BranchID=nBranchID;
# (SS,19/3/08) in following two it may be more accurate to use SUM(LocalAveSellPrice) / SUM(LocalJobCount)
SELECT AVG(LocalAveSellPrice) INTO nWorldAveSellPrice FROM equipment_price_summary WHERE LocalAveSellPrice>0 AND EquipmentID=nEquipmentID;
SELECT AVG(LocalAveJobCost) INTO nWorldAveJobCost FROM equipment_price_summary WHERE LocalAveJobCost>0 AND EquipmentID=nEquipmentID;
SELECT SUM(LocalJobCount) INTO nWorldJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID;

IF nSellPrice IS NULL THEN
   SET nSellPrice="0.00";
END IF;
    
IF nRepairPrice IS NULL THEN
   SET nRepairPrice="0.00";
END IF;

IF nServiceExPrice IS NULL THEN
   SET nServiceExPrice="0.00";
END IF;
    
IF nLocalAveSellPrice IS NULL THEN
   SET nLocalAveSellPrice="0.00";
END IF;    
    
IF nLocalAveJobCost IS NULL THEN
   SET nLocalAveJobCost="0.00";
END IF;
    
IF nLocalJobCount IS NULL THEN
   SET nLocalJobCount="0.00";
END IF;
    
IF nWorldAveSellPrice IS NULL THEN
   SET nWorldAveSellPrice="0.00";
END IF;    
    
IF nWorldAveJobCost IS NULL THEN
   SET nWorldAveJobCost="0.00";
END IF;
    
IF nWorldJobCount IS NULL THEN
   SET nWorldJobCount="0.00";
END IF;

SELECT
    nSellPrice AS SellPrice,
    nRepairPrice AS RepairPrice,
    nServiceExPrice AS ServiceExPrice,
    nLocalAveSellPrice AS AveSellPriceLocal,
    nLocalAveJobCost AS AveJobCostLocal,
    nLocalJobCount AS LocalJobCount,
    nWorldAveSellPrice AS AveSellPriceWorld,
    nWorldAveJobCost AS AveJobCostWorld,
    nWorldJobCount AS WorldJobCount;	

END�������e��e��R�� jms_demo_epochGetPriceSummaryPrevGetPriceSummaryPrevlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%
        IN nEquipmentID INTEGER ( 11 ) ,
        IN nBranchID INTEGER ( 11 )
    BEGIN

  # Serial: 2008-02-22-01

  DECLARE nSellPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nRepairPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceWorld DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostWorld DECIMAL(11,2) DEFAULT NULL;

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nJobID INTEGER(11);
  DECLARE nJobBranchID INTEGER(11);
  DECLARE nJobSellPrice DOUBLE;

  DECLARE nWorldSellPrice DOUBLE DEFAULT 0;
  DECLARE nWorldJobCost DOUBLE DEFAULT 0;
  DECLARE nWorldJobCount INTEGER DEFAULT 0;
  DECLARE nLocalSellPrice DOUBLE DEFAULT 0;
  DECLARE nLocalJobCost DOUBLE DEFAULT 0;
  DECLARE nLocalJobCount INTEGER DEFAULT 0;

  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(9,2);
  DECLARE nLabourHours DOUBLE;

  # this query selects all invoiced jobs with given equipment ID
  DECLARE curSelectedJobs CURSOR FOR SELECT JobID, BranchID, SellPrice FROM jobs WHERE WIP = FALSE AND EquipmentID = nEquipmentID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  # get SellPrice, RepairPrice, ServiceExPrice from Equipment table
  SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID = nEquipmentID;

  OPEN curSelectedJobs;

  nextrecord: LOOP

    FETCH curSelectedJobs INTO nJobID, nJobBranchID, nJobSellPrice;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    # determine JobCost using GetTotalLabourAndMaterialsMain procedure, i.e. JobCost = LabourCost + MaterialCost
    CALL GetTotalLabourAndMaterialsMain(nJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);

    # treat NULL as zero, else total will end up as NULL, alternative is to ignore records containing NULLs
    IF nJobSellPrice IS NULL THEN
      SET nJobSellPrice = 0;
    END IF;

    SET nWorldSellPrice = nWorldSellPrice + nJobSellPrice;
    SET nWorldJobCost = nWorldJobCost + nLabourCost + nMaterialCost;
    SET nWorldJobCount = nWorldJobCount + 1;

    IF nBranchID = nJobBranchID THEN
       SET nLocalSellPrice = nLocalSellPrice + nJobSellPrice;
       SET nLocalJobCost = nLocalJobCost + nLabourCost + nMaterialCost;
       SET nLocalJobCount = nLocalJobCount + 1;
    END IF;

  END LOOP nextrecord;

  CLOSE curSelectedJobs;

  # calculate the averages

  IF nLocalJobCount > 1 THEN
     SET nAveSellPriceLocal = ROUND(nLocalSellPrice / nLocalJobCount, 2);
     SET nAveJobCostLocal = ROUND(nLocalJobCost / nLocalJobCount, 2);
  END IF;

  IF nWorldJobCount > 1 THEN
     SET nAveSellPriceWorld = ROUND(nWorldSellPrice / nWorldJobCount, 2);
     SET nAveJobCostWorld = ROUND(nWorldJobCost / nWorldJobCount, 2);
  END IF;

  # ------------------
  # Output the results
  # ------------------

  SELECT nSellPrice AS SellPrice, nRepairPrice As RepairPrice, nServiceExPrice AS ServiceExPrice,
         nAveSellPriceLocal AS AveSellPriceLocal, nAveJobCostLocal AS AveJobCostLocal, nLocalJobCount AS LocalJobCount,
         nAveSellPriceWorld AS AveSellPriceWorld, nAveJobCostWorld AS AveJobCostWorld, nWorldJobCount AS WorldJobCount;

ENDBEGIN

  # Serial: 2008-02-22-01

  DECLARE nSellPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nRepairPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceWorld DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostWorld DECIMAL(11,2) DEFAULT NULL;

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nJobID INTEGER(11);
  DECLARE nJobBranchID INTEGER(11);
  DECLARE nJobSellPrice DOUBLE;

  DECLARE nWorldSellPrice DOUBLE DEFAULT 0;
  DECLARE nWorldJobCost DOUBLE DEFAULT 0;
  DECLARE nWorldJobCount INTEGER DEFAULT 0;
  DECLARE nLocalSellPrice DOUBLE DEFAULT 0;
  DECLARE nLocalJobCost DOUBLE DEFAULT 0;
  DECLARE nLocalJobCount INTEGER DEFAULT 0;

  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(9,2);
  DECLARE nLabourHours DOUBLE;

  # this query selects all invoiced jobs with given equipment ID
  DECLARE curSelectedJobs CURSOR FOR SELECT JobID, BranchID, SellPrice FROM jobs WHERE WIP = FALSE AND EquipmentID = nEquipmentID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  # get SellPrice, RepairPrice, ServiceExPrice from Equipment table
  SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID = nEquipmentID;

  OPEN curSelectedJobs;

  nextrecord: LOOP

    FETCH curSelectedJobs INTO nJobID, nJobBranchID, nJobSellPrice;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    # determine JobCost using GetTotalLabourAndMaterialsMain procedure, i.e. JobCost = LabourCost + MaterialCost
    CALL GetTotalLabourAndMaterialsMain(nJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);

    # treat NULL as zero, else total will end up as NULL, alternative is to ignore records containing NULLs
    IF nJobSellPrice IS NULL THEN
      SET nJobSellPrice = 0;
    END IF;

    SET nWorldSellPrice = nWorldSellPrice + nJobSellPrice;
    SET nWorldJobCost = nWorldJobCost + nLabourCost + nMaterialCost;
    SET nWorldJobCount = nWorldJobCount + 1;

    IF nBranchID = nJobBranchID THEN
       SET nLocalSellPrice = nLocalSellPrice + nJobSellPrice;
       SET nLocalJobCost = nLocalJobCost + nLabourCost + nMaterialCost;
       SET nLocalJobCount = nLocalJobCount + 1;
    END IF;

  END LOOP nextrecord;

  CLOSE curSelectedJobs;

  # calculate the averages

  IF nLocalJobCount > 1 THEN
     SET nAveSellPriceLocal = ROUND(nLocalSellPrice / nLocalJobCount, 2);
     SET nAveJobCostLocal = ROUND(nLocalJobCost / nLocalJobCount, 2);
  END IF;

  IF nWorldJobCount > 1 THEN
     SET nAveSellPriceWorld = ROUND(nWorldSellPrice / nWorldJobCount, 2);
     SET nAveJobCostWorld = ROUND(nWorldJobCost / nWorldJobCount, 2);
  END IF;

  # ------------------
  # Output the results
  # ------------------

  SELECT nSellPrice AS SellPrice, nRepairPrice As RepairPrice, nServiceExPrice AS ServiceExPrice,
         nAveSellPriceLocal AS AveSellPriceLocal, nAveJobCostLocal AS AveJobCostLocal, nLocalJobCount AS LocalJobCount,
         nAveSellPriceWorld AS AveSellPriceWorld, nAveJobCostWorld AS AveJobCostWorld, nWorldJobCount AS WorldJobCount;

ENDO0����e��e���� jms_demo_epochGetTotalLabourAndMaterialsMainGetTotalLabourAndMaterialsMainlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%
        IN sJobID CHAR ( 12 ) ,
        OUT pLabourHours INTEGER ( 11 ) ,
        OUT pLabourCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialSell DECIMAL ( 11 , 2 ) ,
        OUT pMinSellPrice DECIMAL ( 11 , 2 )
    BEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-09-12-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCost(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDBEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-09-12-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCost(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
END�e��e�� �� jms_demo_epochGetTotalLabourAndMaterialsNewGetTotalLabourAndMaterialsNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%
        sJobID CHAR ( 12 )
    BEGIN
  # Serial: 2008-12-19-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMainNew(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

ENDBEGIN
  # Serial: 2008-12-19-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMainNew(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

END53h�H���e��e��!!@@ Pjms_demo_epochGetTotalLabourAndMaterialsMainNewGetTotalLabourAndMaterialsMainNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%
        IN sJobID CHAR ( 12 ) ,
        OUT pLabourHours INTEGER ( 11 ) ,
        OUT pLabourCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialSell DECIMAL ( 11 , 2 ) ,
        OUT pMinSellPrice DECIMAL ( 11 , 2 )
    BEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2013-08-13-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # (SS,13/8/13) added getting of MaterialThreshold, MaterialBelowThresholdSellFactor and MaterialAboveThresholdSellFactor from branches table
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  
  # (SS,13/8/13) added following, these setting are now taken from branches table  
  DECLARE nMaterialThreshold DECIMAL(11,2) DEFAULT 10;
  DECLARE nMaterialBelowThresholdSellFactor DECIMAL(11,2) DEFAULT 1.4;
  DECLARE nMaterialAboveThresholdSellFactor DECIMAL(11,2) DEFAULT 1.3;  
  
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

  # (SS,13/8/13) replaced 10, 1.4, 1.3 constants with variables, settings from branches table      
  SELECT MaterialThreshold, MaterialBelowThresholdSellFactor, MaterialAboveThresholdSellFactor INTO nMaterialThreshold, nMaterialBelowThresholdSellFactor, nMaterialAboveThresholdSellFactor 
      FROM jobs 
    INNER JOIN branches ON branches.BranchID = jobs.BranchID 
    WHERE JobID = sJobID;
  
  If nMaterialCost < nMaterialThreshold THEN
    SET nMaterialSell = nMaterialCost * nMaterialBelowThresholdSellFactor;
  ELSE
    SET nMaterialSell = nMaterialCost * nMaterialAboveThresholdSellFactor;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDBEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2013-08-13-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # (SS,13/8/13) added getting of MaterialThreshold, MaterialBelowThresholdSellFactor and MaterialAboveThresholdSellFactor from branches table
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  
  # (SS,13/8/13) added following, these setting are now taken from branches table  
  DECLARE nMaterialThreshold DECIMAL(11,2) DEFAULT 10;
  DECLARE nMaterialBelowThresholdSellFactor DECIMAL(11,2) DEFAULT 1.4;
  DECLARE nMaterialAboveThresholdSellFactor DECIMAL(11,2) DEFAULT 1.3;  
  
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

  # (SS,13/8/13) replaced 10, 1.4, 1.3 constants with variables, settings from branches table      
  SELECT MaterialThreshold, MaterialBelowThresholdSellFactor, MaterialAboveThresholdSellFactor INTO nMaterialThreshold, nMaterialBelowThresholdSellFactor, nMaterialAboveThresholdSellFactor 
      FROM jobs 
    INNER JOIN branches ON branches.BranchID = jobs.BranchID 
    WHERE JobID = sJobID;
  
  If nMaterialCost < nMaterialThreshold THEN
    SET nMaterialSell = nMaterialCost * nMaterialBelowThresholdSellFactor;
  ELSE
    SET nMaterialSell = nMaterialCost * nMaterialAboveThresholdSellFactor;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDC4`�����e��e��""oo jms_demo_epochUpdateEquipmentAveragePriceSummaryUpdateEquipmentAveragePriceSummarylatin1latin1_swedish_cilatin1_swedish_cijms_demo_epoch@%BEGIN

# (SS,ITP,19/3/08)
# Serial: 2008-03-19-01
# This routine is to be run at the end of the day
# Creates records used by the GetPriceSummary routine
# to help speed up getting the average prices for a given job

# empty the table
TRUNCATE equipment_price_summary;

# summarise and add the records
INSERT INTO equipment_price_summary
(SELECT EquipmentID, BranchID,
ROUND(AVG(SellPrice),2) AS LocalAveSellPrice, ROUND(AVG(funcGetLabourMaterialTotal(JobID)),2) AS LocalAveJobCost,
COUNT(JobID) AS LocalJobCount, CURDATE() AS LastUpdate
FROM jobs WHERE WIP=FALSE AND CREDITNOTE=FALSE
GROUP BY EquipmentID, BranchID
);

ENDBEGIN

# (SS,ITP,19/3/08)
# Serial: 2008-03-19-01
# This routine is to be run at the end of the day
# Creates records used by the GetPriceSummary routine
# to help speed up getting the average prices for a given job

# empty the table
TRUNCATE equipment_price_summary;

# summarise and add the records
INSERT INTO equipment_price_summary
(SELECT EquipmentID, BranchID,
ROUND(AVG(SellPrice),2) AS LocalAveSellPrice, ROUND(AVG(funcGetLabourMaterialTotal(JobID)),2) AS LocalAveJobCost,
COUNT(JobID) AS LocalJobCount, CURDATE() AS LastUpdate
FROM jobs WHERE WIP=FALSE AND CREDITNOTE=FALSE
GROUP BY EquipmentID, BranchID
);

END�g�c�g�c�
		B�� jms_demo_rhesClearLockClearLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%nLockID INTEGER ( 11 ) ,
        sComputerName VARCHAR ( 20 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  # UPDATE appears before DELETE because of RETURN ROW_COUNT()
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  # UPDATE appears before DELETE because of RETURN ROW_COUNT()
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
END�g�c�g�c�
MXX jms_demo_rhesfuncGetAdjustedSalesValuefuncGetAdjustedSalesValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%nInvoiceValue DOUBLE,
        nMargin DOUBLE,
        nJobTypeID INT(12)
    doubleBEGIN
DECLARE nAdjustedSalesValue DOUBLE DEFAULT 0.00;
IF nJobTypeID=1 OR nJobTypeID=4 OR nJobTypeID=6 OR nJobTypeID=8 OR nJobTypeID=10 THEN
    SET nAdjustedSalesValue=nInvoiceValue;
    END IF;
    
IF nJobTypeID=2 OR nJobTypeID=3 OR nJobTypeID=12 THEN
    SET nAdjustedSalesValue=(nMargin*2);
    END IF;
    
RETURN nAdjustedSalesValue;
ENDBEGIN
DECLARE nAdjustedSalesValue DOUBLE DEFAULT 0.00;
IF nJobTypeID=1 OR nJobTypeID=4 OR nJobTypeID=6 OR nJobTypeID=8 OR nJobTypeID=10 THEN
    SET nAdjustedSalesValue=nInvoiceValue;
    END IF;
    
IF nJobTypeID=2 OR nJobTypeID=3 OR nJobTypeID=12 THEN
    SET nAdjustedSalesValue=(nMargin*2);
    END IF;
    
RETURN nAdjustedSalesValue;
END�g�c�g�c�
/ww jms_demo_rhesfuncGetDaysOverduefuncGetDaysOverduelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%`sJobID` CHAR(12),
        `sNowDate` DATE
    int(12)BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
   DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
   DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDa�����3Ex�h�g�c�g�c�
+ss jms_demo_rhesfuncGetDaysOverdueQuotefuncGetDaysOverdueQuotelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12),
        sNowDate DATE
    int(12)BEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
    DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeOfEntry)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    DECLARE nOverdueDays INT(12) DEFAULT 0;
    DECLARE dNowDate DATE;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
        
        IF sNowDate="" THEN
            SET dNowDate=CURRENT_DATE;
            END IF;
            
        IF sNowDate<>"" THEN
            SET dNowDate=sNowDate;
            END IF;
            
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeOfEntry)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET nOverdueDays=(nDiffDays-nDays);
        END IF;
    RETURN nOverdueDays;
END�g�c�g�c�
4"" jms_demo_rhesfuncGetDueDatefuncGetDueDatelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    varchar(10) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE sDueDate VARCHAR(10) DEFAULT "";
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    SELECT DATE_FORMAT(DATE_ADD(DateTimeGoAheadGiven,INTERVAL nDays DAY),'%d/%m/%Y') INTO sDueDate FROM jobs WHERE JobID=sJobID;
    RETURN sDueDate;
ENDBEGIN
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE sDueDate VARCHAR(10) DEFAULT "";
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    SELECT DATE_FORMAT(DATE_ADD(DateTimeGoAheadGiven,INTERVAL nDays DAY),'%d/%m/%Y') INTO sDueDate FROM jobs WHERE JobID=sJobID;
    RETURN sDueDate;
END�g�c�g�c�
5�� jms_demo_rhesfuncGetEquipStringfuncGetEquipStringlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    varchar(250) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE sEquipString VARCHAR(250) DEFAULT "Cannot find Equipment Record!";
    DECLARE nManufacturerID INT(12) DEFAULT 0;
    DECLARE sManufacturer VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE sModelNumber VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE nEquipmentID INT(15) DEFAULT 0;
    SELECT EquipmentID INTO nEquipmentID FROM jobs WHERE JobID=sJobID;
    SELECT ManufacturerID INTO nManufacturerID FROM equipment WHERE EquipmentID=nEquipmentID;
    SELECT ManufacturerName INTO sManufacturer FROM manufacturers WHERE ManufacturerID=nManufacturerID;
    SELECT ModelNumber INTO sModelNumber FROM equipment WHERE EquipmentID=nEquipmentID;
    SET sEquipString=CONCAT(sManufacturer," ",sModelNumber);
    RETURN sEquipString;
ENDBEGIN
    DECLARE sEquipString VARCHAR(250) DEFAULT "Cannot find Equipment Record!";
    DECLARE nManufacturerID INT(12) DEFAULT 0;
    DECLARE sManufacturer VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE sModelNumber VARCHAR(100) DEFAULT "UNKNOWN";
    DECLARE nEquipmentID INT(15) DEFAULT 0;
    SELECT EquipmentID INTO nEquipmentID FROM jobs WHERE JobID=sJobID;
    SELECT ManufacturerID INTO nManufacturerID FROM equipment WHERE EquipmentID=nEquipmentID;
    SELECT ManufacturerName INTO sManufacturer FROM manufacturers WHERE ManufacturerID=nManufacturerID;
    SELECT ModelNumber INTO sModelNumber FROM equipment WHERE EquipmentID=nEquipmentID;
    SET sEquipString=CONCAT(sManufacturer," ",sModelNumber);
    RETURN sEquipString;
END����fn�����g�c�g�c�
488 jms_demo_rhesfuncGetFinalOutcomefuncGetFinalOutcomelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    varchar(50) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
DECLARE sFinalOutcome VARCHAR(50) DEFAULT "Final Outcome not set!";
DECLARE nFinalOutcomeID INT(12) DEFAULT 0;
SELECT FinalOutcomeID INTO nFinalOutcomeID FROM jobs WHERE JobID=sJobID;
SELECT FinalOutcome INTO sFinalOutcome FROM final_outcomes WHERE FinalOutcomeID=nFinalOutcomeID;
RETURN sFinalOutcome;
ENDBEGIN
DECLARE sFinalOutcome VARCHAR(50) DEFAULT "Final Outcome not set!";
DECLARE nFinalOutcomeID INT(12) DEFAULT 0;
SELECT FinalOutcomeID INTO nFinalOutcomeID FROM jobs WHERE JobID=sJobID;
SELECT FinalOutcome INTO sFinalOutcome FROM final_outcomes WHERE FinalOutcomeID=nFinalOutcomeID;
RETURN sFinalOutcome;
END�g�c�g�c�
�� jms_demo_rhesfuncGetLabourCostfuncGetLabourCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END�g�c�g�c�
0�� jms_demo_rhesfuncGetLabourCostsForBranchfuncGetLabourCostsForBranchlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID=sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID=sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDv
{
_
S��vn�T�g�c�g�c�
0�� jms_demo_rhesfuncGetLabourCostsNotForBranchfuncGetLabourCostsNotForBranchlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12),
        sBranchID CHAR(12)
    decimal(9,2)BEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID<>sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t JOIN users u ON t.UserID=u.UserID WHERE t.jobid=sJobID AND u.BranchID<>sBranchID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + (nHours * nUseRate);
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalLabour;
END�g�c�g�c�
�� jms_demo_rhesfuncGetLabourHoursfuncGetLabourHourslatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END�

�
�U��
�g�c�g�c�
,�� jms_demo_rhesfuncGetLabourHoursByRatefuncGetLabourHoursByRatelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12),
        sRate CHAR(12)
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
END�g�c�g�c�
@�� jms_demo_rhesfuncGetLabourHoursByRateEOMfuncGetLabourHoursByRateEOMlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12),
        sRate CHAR(12),
        sDate DATE
    doubleBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate AND t.workdate <= sDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDBEGIN
  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(9,2) DEFAULT 0;
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID AND t.ChargeRate = sRate AND t.workdate <= sDate;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
  OPEN curTimesheetsWithCharge;
  nextrecord: LOOP
    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;
    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;
    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;
  END LOOP nextrecord;
  CLOSE curTimesheetsWithCharge;
  RETURN nTotalHours;
ENDs
�
g
oe���g�c�g�c�

�� jms_demo_rhesfuncGetLabourMaterialTotalfuncGetLabourMaterialTotallatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    decimal(11,2)BEGIN

# (SS,22/2/12) new version for new ordering system which calls funcGetMaterialCostNew instead of funcGetMaterialCost

DECLARE nLabourCost DOUBLE DEFAULT 0.00;
DECLARE nMaterialCost DOUBLE DEFAULT 0.00;
DECLARE nTotalCost DOUBLE DEFAULT 0.00;

SELECT funcGetLabourCost(sJobID) INTO nLabourCost;
SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

SET nTotalCost=nLabourCost+nMaterialCost;

RETURN nTotalCost;

ENDBEGIN

# (SS,22/2/12) new version for new ordering system which calls funcGetMaterialCostNew instead of funcGetMaterialCost

DECLARE nLabourCost DOUBLE DEFAULT 0.00;
DECLARE nMaterialCost DOUBLE DEFAULT 0.00;
DECLARE nTotalCost DOUBLE DEFAULT 0.00;

SELECT funcGetLabourCost(sJobID) INTO nLabourCost;
SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

SET nTotalCost=nLabourCost+nMaterialCost;

RETURN nTotalCost;

END�g�c�g�c�



 jms_demo_rhesfuncGetLossOnJobfuncGetLossOnJoblatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLossOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice<nTotalCost THEN
          SET nLossOnJob=(nTotalCost-nInvoicePrice);
      END IF;
    RETURN nLossOnJob;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLossOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice<nTotalCost THEN
          SET nLossOnJob=(nTotalCost-nInvoicePrice);
      END IF;
    RETURN nLossOnJob;
END*���a���g�c�g�c�
4�� Pjms_demo_rhesfuncGetMaterialCostNotBranchIDfuncGetMaterialCostNotBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%`sJobID` CHAR(12),
        `sBranchID` CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID<>sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID <> sBranchID;
    
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;

ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID<>sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID <> sBranchID;
    
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;

END�g�c�g�c�
4�� Pjms_demo_rhesfuncGetMaterialCostWithBranchIDfuncGetMaterialCostWithBranchIDlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%`sJobID` CHAR(12),
        `sBranchID` CHAR(12)
    decimal(9,2)BEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID=sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID = sBranchID;
  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
ENDBEGIN
  DECLARE nMaterialCost DECIMAL(9,2);
  #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items JOIN orders ON order_items.OrderID=orders.OrderID WHERE order_items.JobID=sJobID AND orders.BranchID=sBranchID;
  # (ITP,SS,2/10/12) originally created by Lektronix, replaced above with following to work with new orders system, to produce correct figures for report "Zero-Value Invoices (Detailed) by Job Type"
  SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE JobID = sJobID AND BranchID = sBranchID;
  
  IF nMaterialCost IS NULL THEN
    SET nMaterialCost = 0;
  END IF;
  RETURN nMaterialCost;
END�g�c�g�c�

zz jms_demo_rhesfuncGetWarrantyCostsfuncGetWarrantyCostslatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nWarrantyCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nJobID CHAR(12) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    DECLARE nSubtotal FLOAT(9,2) DEFAULT 0.00;
    DECLARE curWarrantyJobs CURSOR FOR SELECT JobID,CWJobID FROM jobs WHERE CWJobID=sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished=1;
      OPEN curWarrantyJobs;
      nextrecord: LOOP
          FETCH curWarrantyJobs INTO nJobID,nCWJobID;
        IF bFinished=1 THEN
              LEAVE nextrecord;
          END IF;
        SELECT funcGetMaterialCost(JobID)+funcGetLabourCost(JobID) INTO nSubtotal FROM jobs WHERE JobID=nJobID;
        IF nSubtotal IS NULL THEN
            SET nSubtotal=0.00;
        END IF;
        SET nWarrantyCost=nWarrantyCost+nSubtotal;
    END LOOP nextrecord;
      CLOSE curWarrantyJobs;
    RETURN nWarrantyCost;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nWarrantyCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nJobID CHAR(12) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    DECLARE nSubtotal FLOAT(9,2) DEFAULT 0.00;
    DECLARE curWarrantyJobs CURSOR FOR SELECT JobID,CWJobID FROM jobs WHERE CWJobID=sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished=1;
      OPEN curWarrantyJobs;
      nextrecord: LOOP
          FETCH curWarrantyJobs INTO nJobID,nCWJobID;
        IF bFinished=1 THEN
              LEAVE nextrecord;
          END IF;
        SELECT funcGetMaterialCost(JobID)+funcGetLabourCost(JobID) INTO nSubtotal FROM jobs WHERE JobID=nJobID;
        IF nSubtotal IS NULL THEN
            SET nSubtotal=0.00;
        END IF;
        SET nWarrantyCost=nWarrantyCost+nSubtotal;
    END LOOP nextrecord;
      CLOSE curWarrantyJobs;
    RETURN nWarrantyCost;
END6�%���h�1�g�c�g�c�

 jms_demo_rhesfuncGetProfitOnJobfuncGetProfitOnJoblatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    float(9,2)BEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nProfitOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice>nTotalCost THEN
          SET nProfitOnJob=(nInvoicePrice-nTotalCost);
      END IF;
    RETURN nProfitOnJob;
ENDBEGIN
    DECLARE bFinished INTEGER DEFAULT 0;
    DECLARE nMaterialCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nLabourCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nTotalCost FLOAT(9,2) DEFAULT 0.00;
    DECLARE nInvoicePrice FLOAT(9,2) DEFAULT 0.00;
    DECLARE nProfitOnJob FLOAT(9,2) DEFAULT 0.00;
    DECLARE sChargeRate CHAR(1) DEFAULT "";
     DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
      DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
      DECLARE curTimesheetsWithCharge CURSOR FOR
           SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
         HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
         FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;
      OPEN curTimesheetsWithCharge;
          nextrecord: LOOP
        FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
        IF bFinished THEN
              LEAVE nextrecord;
            END IF;
        IF sChargeRate = "A" THEN
              SET nUseRate = nRateA;
        ELSEIF sChargeRate = "B" THEN
              SET nUseRate = nRateB;
        ELSEIF sChargeRate = "C" THEN
              SET nUseRate = nRateC;
        ELSE
              SET nUseRate = 0;
        END IF;
        SET nTotalHours = nTotalHours + nHours;
        SET nLabourCost = nLabourCost + nHours * nUseRate;
          END LOOP nextrecord;
      CLOSE curTimesheetsWithCharge;
    SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE JobID = sJobID;
      IF nMaterialCost IS NULL THEN
        SET nMaterialCost = 0;
      END IF;
      SET nTotalCost=nLabourCost+nMaterialCost;
      SELECT (NumberOff*QuotedPrice)+QuotedDelivery INTO nInvoicePrice FROM jobs WHERE JobID=sJobID;
      IF nInvoicePrice IS NULL THEN
          SET nInvoicePrice=0;
      END IF;
      IF nInvoicePrice>nTotalCost THEN
          SET nProfitOnJob=(nInvoicePrice-nTotalCost);
      END IF;
    RETURN nProfitOnJob;
END�g�c�g�c�
5ss jms_demo_rhesfuncGetWarrantyCreatedEngineerfuncGetWarrantyCreatedEngineerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    varchar(100) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    SELECT CWJobID INTO nCWJobID FROM jobs WHERE JobID=sJobID;
    IF nCWJobID IS NOT NULL THEN
        SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=nCWJobID;
        END IF;
    RETURN sEngineerName;
ENDBEGIN
    DECLARE sEngineerName VARCHAR(100) DEFAULT "";
    DECLARE nCWJobID CHAR(12) DEFAULT "";
    SELECT CWJobID INTO nCWJobID FROM jobs WHERE JobID=sJobID;
    IF nCWJobID IS NOT NULL THEN
        SELECT users.Username INTO sEngineerName FROM users JOIN jobs ON users.UserID=jobs.RepairedByID WHERE jobs.JobID=nCWJobID;
        END IF;
    RETURN sEngineerName;
END���|����	�g�c�g�c�
2�� jms_demo_rhesfuncGetWorkshopInvoiceValuefuncGetWorkshopInvoiceValuelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12),
        sBranchName CHAR(50)
    doubleBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE nPassedBranchID INT(12) DEFAULT 0;
DECLARE nCommBranchID INT(12) DEFAULT 0;
DECLARE nRepairBranchID INT(12) DEFAULT 0;
DECLARE nNumberOff INT(12) DEFAULT 0;
DECLARE nQuotedPrice DOUBLE DEFAULT 0.00;
DECLARE nFinalValue DOUBLE DEFAULT 0.00;
SELECT BranchID INTO nPassedBranchID FROM branches WHERE BranchName=sBranchName;
SELECT
    JobTypeID,BranchID,RepairBranchID,NumberOff,QuotedPrice
INTO
    nJobTypeID,nCommBranchID,nRepairBranchID,nNumberOff,nQuotedPrice
FROM
    jobs WHERE JobID=sJobID;
    
SET nFinalValue=(nNumberOff*nQuotedPrice);
    
IF nJobTypeID=17 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.2727;
    END IF;
    
IF nJobTypeID=18 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.5714;
    END IF;
IF nJobTypeID=19 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.15;
        END IF;
IF nJobTypeID=20 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.4;
        END IF;
    
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nRepairBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.80;
    END IF;
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nCommBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.20;
    END IF;
SET nFinalValue=ROUND(nFinalValue,2);
RETURN nFinalValue;
ENDBEGIN
DECLARE nJobTypeID INT(12) DEFAULT 0;
DECLARE nPassedBranchID INT(12) DEFAULT 0;
DECLARE nCommBranchID INT(12) DEFAULT 0;
DECLARE nRepairBranchID INT(12) DEFAULT 0;
DECLARE nNumberOff INT(12) DEFAULT 0;
DECLARE nQuotedPrice DOUBLE DEFAULT 0.00;
DECLARE nFinalValue DOUBLE DEFAULT 0.00;
SELECT BranchID INTO nPassedBranchID FROM branches WHERE BranchName=sBranchName;
SELECT
    JobTypeID,BranchID,RepairBranchID,NumberOff,QuotedPrice
INTO
    nJobTypeID,nCommBranchID,nRepairBranchID,nNumberOff,nQuotedPrice
FROM
    jobs WHERE JobID=sJobID;
    
SET nFinalValue=(nNumberOff*nQuotedPrice);
    
IF nJobTypeID=17 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.2727;
    END IF;
    
IF nJobTypeID=18 THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.5714;
    END IF;
IF nJobTypeID=19 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.15;
        END IF;
IF nJobTypeID=20 THEN
        SET nFinalValue=(nNumberOff*nQuotedPrice)*0.4;
        END IF;
    
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nRepairBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.80;
    END IF;
IF nJobTypeID<>17 AND nJobTypeID<>18 AND nJobTypeID<>19 AND nJobTypeID<>20 AND nCommBranchID<>nRepairBranchID AND nCommBranchID=nPassedBranchID THEN
    SET nFinalValue=(nNumberOff*nQuotedPrice)*0.20;
    END IF;
SET nFinalValue=ROUND(nFinalValue,2);
RETURN nFinalValue;
END�g�c�g�c�
+4�� jms_demo_rhesfuncIsJobOverduefuncIsJobOverduelatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12),
        dNowDate DATE
    varchar(10) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
    DECLARE sResult VARCHAR(10) DEFAULT "FALSE";
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    IF dNowDate="" THEN
        SET dNowDate=CURRENT_DATE;
        END IF;
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET sResult="TRUE";
        END IF;
    RETURN sResult;
ENDBEGIN
    DECLARE sResult VARCHAR(10) DEFAULT "FALSE";
    DECLARE nJobClass INT(12) DEFAULT 0;
    DECLARE nDays INT(12) DEFAULT 0;
    DECLARE nDiffDays INT(12) DEFAULT 0;
    SELECT DeliveryDays INTO nDays FROM jobs WHERE JobID=sJobID;
    IF nDays IS NULL THEN
        SELECT JobClassID INTO nJobClass FROM jobs WHERE JobID=sJobID;
        SELECT RepairTimeDays INTO nDays FROM job_classes WHERE JobClassID=nJobClass;
        END IF;
    IF dNowDate="" THEN
        SET dNowDate=CURRENT_DATE;
        END IF;
    SELECT (TO_DAYS(dNowDate)-TO_DAYS(DateTimeGoAheadGiven)) INTO nDiffDays FROM jobs WHERE JobID=sJobID;
    IF nDiffDays>nDays THEN
        SET sResult="TRUE";
        END IF;
    RETURN sResult;
END�g�c�g�c�
dd jms_demo_rhesfuncJobBecomesWarrantyfuncJobBecomesWarrantylatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(12)
    int(12)BEGIN
    DECLARE nWarrantyJobs INT(12);
    DECLARE nReturn INT(12);
    SET nWarrantyJobs=0;
    SET nReturn=0;
    SELECT COUNT(JobID) INTO nWarrantyJobs FROM jobs WHERE CWJobID=sJobID;
    IF nWarrantyJobs IS NULL THEN
        SET nReturn=0;
    END IF;
    IF nWarrantyJobs>0 THEN
        SET nReturn=nWarrantyJobs;
    END IF;
    RETURN nReturn;
ENDBEGIN
    DECLARE nWarrantyJobs INT(12);
    DECLARE nReturn INT(12);
    SET nWarrantyJobs=0;
    SET nReturn=0;
    SELECT COUNT(JobID) INTO nWarrantyJobs FROM jobs WHERE CWJobID=sJobID;
    IF nWarrantyJobs IS NULL THEN
        SET nReturn=0;
    END IF;
    IF nWarrantyJobs>0 THEN
        SET nReturn=nWarrantyJobs;
    END IF;
    RETURN nReturn;
END��]�Q�<;���
�g�c�g�c�
8 jms_demo_rhesfuncJobsInCategoryForCustomerfuncJobsInCategoryForCustomerlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sCategoryName CHAR(50),
        nCustomerID INT(12)
    int(12)BEGIN
DECLARE nJobsInCategory INT(12) DEFAULT 0;
DECLARE nCategoryID INT(12) DEFAULT 0;
SELECT CategoryID INTO nCategoryID FROM categories WHERE CategoryName=sCategoryName;
SELECT COUNT(JobID) INTO nJobsInCategory FROM jobs JOIN    equipment ON jobs.EquipmentID=equipment.EquipmentID WHERE equipment.CategoryID=nCategoryID AND jobs.CustomerID=nCustomerID;
RETURN nJobsInCategory;
ENDBEGIN
DECLARE nJobsInCategory INT(12) DEFAULT 0;
DECLARE nCategoryID INT(12) DEFAULT 0;
SELECT CategoryID INTO nCategoryID FROM categories WHERE CategoryName=sCategoryName;
SELECT COUNT(JobID) INTO nJobsInCategory FROM jobs JOIN    equipment ON jobs.EquipmentID=equipment.EquipmentID WHERE equipment.CategoryID=nCategoryID AND jobs.CustomerID=nCustomerID;
RETURN nJobsInCategory;
END�g�c�g�c�
*ZZ jms_demo_rhesfuncMonthEndMaterialCostfuncMonthEndMaterialCostlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sJobID CHAR(6),
        dEndDate DATE
    decimal(9,2)BEGIN

    DECLARE nMaterialCost DECIMAL(9,2) DEFAULT 0.00;
    DECLARE nGetIn DATE DEFAULT '1999-12-31';

    #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE (order_items.JobID = sJobID) AND (order_items.itemdeliverydate IS NOT NULL AND order_items.itemdeliverydate <= dEndDate);
    #(SS,6/3/12) replaced above with following
    SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE (job_material.JobID = sJobID) AND (job_material.DateReceived IS NOT NULL AND job_material.DateReceived <= dEndDate);
    
    RETURN nMaterialCost;
    
ENDBEGIN

    DECLARE nMaterialCost DECIMAL(9,2) DEFAULT 0.00;
    DECLARE nGetIn DATE DEFAULT '1999-12-31';

    #SELECT SUM(QuantityOrdered * UnitPrice) INTO nMaterialCost FROM order_items WHERE (order_items.JobID = sJobID) AND (order_items.itemdeliverydate IS NOT NULL AND order_items.itemdeliverydate <= dEndDate);
    #(SS,6/3/12) replaced above with following
    SELECT SUM(QtyRequired * UnitPrice) INTO nMaterialCost FROM job_material WHERE (job_material.JobID = sJobID) AND (job_material.DateReceived IS NOT NULL AND job_material.DateReceived <= dEndDate);
    
    RETURN nMaterialCost;
    
END�g�c�g�c�
�33 jms_demo_rhesGetLockGetLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sTableName VARCHAR ( 32 ) ,
        sRecordKey VARCHAR ( 64 ) ,
        nAuditID INTEGER ( 11 ) ,
        nUserID INTEGER ( 11 ) ,
        sUserName VARCHAR ( 40 ) ,
        sComputerName VARCHAR ( 20 ) ,
        sIPAddress VARCHAR ( 15 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
END�	����&~2�`�g�c�g�c�
5�� jms_demo_rhesGetOrderPaymentDetailsGetOrderPaymentDetailslatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sOrderID INTEGER(11)
    varchar(200) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
  DECLARE nAmount DECIMAL(11,2);
  DECLARE sRef1 VARCHAR(40);
  DECLARE sRef2 VARCHAR(40);
  DECLARE sRef3 VARCHAR(40);
  DECLARE sPaymentTypeName VARCHAR(20);
  DECLARE sPaymentDetails VARCHAR(200);
 
  SELECT Amount, Ref1, Ref2, Ref3, payment_types.PaymentTypeName INTO nAmount, sRef1, sRef2, sRef3, sPaymentTypeName FROM material_payments
  INNER JOIN payment_types ON payment_types.PaymentTypeID = material_payments.PaymentTypeID
  WHERE OrderID = sOrderID
  ORDER BY MaterialPaymentID DESC LIMIT 1;
  
  IF sPaymentTypeName IS NULL THEN
    SET sPaymentDetails = "";
  ELSE
    # SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount, ", Ref: ", sRef1, " ", COALESCE(sRef2), " ", sRef3);
    SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount);
    IF sRef1 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", Ref: ", sRef1);
    END IF;    
    IF sRef2 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef2); 
    END IF;
    IF sRef3 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef3);
    END IF;
  END IF;
    
  RETURN sPaymentDetails;
ENDBEGIN
  DECLARE nAmount DECIMAL(11,2);
  DECLARE sRef1 VARCHAR(40);
  DECLARE sRef2 VARCHAR(40);
  DECLARE sRef3 VARCHAR(40);
  DECLARE sPaymentTypeName VARCHAR(20);
  DECLARE sPaymentDetails VARCHAR(200);
 
  SELECT Amount, Ref1, Ref2, Ref3, payment_types.PaymentTypeName INTO nAmount, sRef1, sRef2, sRef3, sPaymentTypeName FROM material_payments
  INNER JOIN payment_types ON payment_types.PaymentTypeID = material_payments.PaymentTypeID
  WHERE OrderID = sOrderID
  ORDER BY MaterialPaymentID DESC LIMIT 1;
  
  IF sPaymentTypeName IS NULL THEN
    SET sPaymentDetails = "";
  ELSE
    # SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount, ", Ref: ", sRef1, " ", COALESCE(sRef2), " ", sRef3);
    SET sPaymentDetails = CONCAT(sPaymentTypeName, " for ", nAmount);
    IF sRef1 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", Ref: ", sRef1);
    END IF;    
    IF sRef2 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef2); 
    END IF;
    IF sRef3 IS NOT NULL THEN
       SET sPaymentDetails = CONCAT(sPaymentDetails, ", ", sRef3);
    END IF;
  END IF;
    
  RETURN sPaymentDetails;
END�g�c�g�c�
Bee jms_demo_rhesReleaseLockReleaseLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%nLockID INTEGER ( 11 ) ,
        sComputerName VARCHAR ( 20 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
ENDBEGIN
  # Version 4.0.32, 11/8/2007

  # if computer name not specified then delete the specified LockID
  # else delete all lock records for given computer name
  # also set Released DateTime field in lock_history
  IF sComputerName = "" THEN
    UPDATE lock_history SET Released = NOW() WHERE LockID = nLockID;
    DELETE FROM locks WHERE LockID = nLockID;
  ELSE
    UPDATE lock_history SET Released = NOW() WHERE ComputerName = sComputerName AND Released IS NULL;
    DELETE FROM locks WHERE ComputerName = sComputerName;
  END IF;

  # return the number of records that were deleted
  RETURN ROW_COUNT();
END�g�c�g�c�
�55 jms_demo_rhesSetLockSetLocklatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sTableName VARCHAR ( 32 ) ,
        sRecordKey VARCHAR ( 64 ) ,
        nAuditID INTEGER ( 11 ) ,
        nUserID INTEGER ( 11 ) ,
        sUserName VARCHAR ( 40 ) ,
        sComputerName VARCHAR ( 20 ) ,
        sIPAddress VARCHAR ( 15 )
    int(11)BEGIN
  # Version 4.0.32, 11/8/2007
  
  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
ENDBEGIN
  # Version 4.0.32, 11/8/2007
  
  DECLARE nLockID INTEGER(11);
  DECLARE sUserHost VARCHAR(100);
  DECLARE nConnectionID INTEGER(11);
  DECLARE dCreated DATETIME;

  # check if lock already placed by looking for record with matching table name and key
  SELECT LockID INTO nLockID FROM locks WHERE TableName = sTableName AND RecordKey = sRecordKey;

  # if lock not already placed then place it
  IF nLockID IS NULL THEN
    SET sUserHost = USER();
    SET nConnectionID = CONNECTION_ID();
    SET dCreated = NOW();
    INSERT INTO locks SET TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUsername, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
    SET nLockID = LAST_INSERT_ID();
    # add to lock history
    INSERT INTO lock_history SET LockID = nLockID, TableName = sTableName, RecordKey = sRecordKey, AuditID = nAuditID,
           UserID = nUserID, UserName = sUserName, ComputerName = sComputerName, IPAddress = sIPAddress,
           UserHost = sUserHost, ConnectionID = nConnectionID, Created = dCreated;
  ELSE
    # if already locked then return the found LockID as negative
    SET nLockID = -nLockID;
  END IF;

  # returns the new LockID
  RETURN nLockID;
END��
��	��`�B�g�c�g�c�
H5�� jms_demo_rhesGetTranslationGetTranslationlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%sEnglishPhrase VARCHAR ( 255 ) ,
        nLanguageID INTEGER ( 11 )
    varchar(255) CHARSET latin1 COLLATE latin1_swedish_ciBEGIN
  # (SS,4/12/07) MySQL version of the translation routine
  # returns the equivalent phrase to the English one specified in requested language
  # if not found in the then the English phrase is returned

  DECLARE sLanguageName VARCHAR(40);
  DECLARE sResult VARCHAR(255);

  # get language name from languages table using language ID
  SELECT LanguageName INTO sLanguageName FROM languages WHERE LanguageID = nLanguageID;

  # get the required language phrase
  # SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase;
  
  # Following does not work: SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase
  # I initially tried dynamic SQL using SET @sSQL, PREPARE, EXECUTE, DEALLOCATE
  # but it was not allowed in stored function, could also create stored function on
  # the fly but seem very messy so I resorted to the following
  # which might be quicker anyway, but will need to update when new language added
  # could also just hard code the LanguageID without having to look up the language name
  # See: http://forge.mysql.com/snippets/download.php?id=13
  #      http://dev.mysql.com/doc/refman/5.1/en/sqlps.html
  #      http://forums.mysql.com/read.php?60,41372,47813#msg-47813
  IF sLanguageName = "English" THEN
    SELECT English INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "German" THEN
    SELECT German INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "French" THEN
    SELECT French INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Italian" THEN
    SELECT Italian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Spanish" THEN
    SELECT Spanish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Polish" THEN
    SELECT Polish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Dutch" THEN
    SELECT Dutch INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Russian" THEN
    SELECT Russian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Swedish" THEN
    SELECT Swedish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Greek" THEN
    SELECT Greek INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Czech" THEN
    SELECT Czech INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Portuguese" THEN
    SELECT Portuguese INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Hungarian" THEN
    SELECT Hungarian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSE
    # if this point reached then it means the SELECT line for this language is missing
    # needs to be added above so return error instead
    SET sResult = CONCAT("!!! Language ", sLanguageName, " not in GetTranslation function !!!");
  END IF;

  # if nothing specified in requested language then return the English phrase
  IF sResult IS NULL OR sResult = "" THEN
    SET sResult = sEnglishPhrase;
  END IF;

  # return the result
  RETURN sResult;
ENDBEGIN
  # (SS,4/12/07) MySQL version of the translation routine
  # returns the equivalent phrase to the English one specified in requested language
  # if not found in the then the English phrase is returned

  DECLARE sLanguageName VARCHAR(40);
  DECLARE sResult VARCHAR(255);

  # get language name from languages table using language ID
  SELECT LanguageName INTO sLanguageName FROM languages WHERE LanguageID = nLanguageID;

  # get the required language phrase
  # SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase;
  
  # Following does not work: SELECT sLanguageName INTO sResult FROM translation WHERE English = sEnglishPhrase
  # I initially tried dynamic SQL using SET @sSQL, PREPARE, EXECUTE, DEALLOCATE
  # but it was not allowed in stored function, could also create stored function on
  # the fly but seem very messy so I resorted to the following
  # which might be quicker anyway, but will need to update when new language added
  # could also just hard code the LanguageID without having to look up the language name
  # See: http://forge.mysql.com/snippets/download.php?id=13
  #      http://dev.mysql.com/doc/refman/5.1/en/sqlps.html
  #      http://forums.mysql.com/read.php?60,41372,47813#msg-47813
  IF sLanguageName = "English" THEN
    SELECT English INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "German" THEN
    SELECT German INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "French" THEN
    SELECT French INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Italian" THEN
    SELECT Italian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Spanish" THEN
    SELECT Spanish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Polish" THEN
    SELECT Polish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Dutch" THEN
    SELECT Dutch INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Russian" THEN
    SELECT Russian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Swedish" THEN
    SELECT Swedish INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Greek" THEN
    SELECT Greek INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Czech" THEN
    SELECT Czech INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Portuguese" THEN
    SELECT Portuguese INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSEIF sLanguageName = "Hungarian" THEN
    SELECT Hungarian INTO sResult FROM translation WHERE English = sEnglishPhrase;
  ELSE
    # if this point reached then it means the SELECT line for this language is missing
    # needs to be added above so return error instead
    SET sResult = CONCAT("!!! Language ", sLanguageName, " not in GetTranslation function !!!");
  END IF;

  # if nothing specified in requested language then return the English phrase
  IF sResult IS NULL OR sResult = "" THEN
    SET sResult = sEnglishPhrase;
  END IF;

  # return the result
  RETURN sResult;
END�!XBN��g�c�g�c�
 Pjms_demo_rhesChangeOverdueAwaitsToChaseChangeOverdueAwaitsToChaselatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%BEGIN

# (SS,13/8/13) (JMS: 4.2.13) ChangeOverdueAwaitsToChase moved from executable to scheduled routine
# the UserID set is 0 (i.e. meaning System)
# (SS,21/8/13) amended to add message to log about how many jobs were updated

DECLARE sMessage CHAR(100);

# (SS,21/8/13) log the start
CALL AddToEventLog('Started ChangeOverdueAwaitsToChase');

DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 3) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 4,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 3;

# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Go-ahead advanced to Chase Go-ahead'));
CALL AddToEventLog(sMessage);
 
DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 6) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 7,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 6;
 
# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Order No advanced to Chase Order No'));
CALL AddToEventLog(sMessage); 

# (SS,21/8/13) log the finish
CALL AddToEventLog('Finished ChangeOverdueAwaitsToChase');

ENDBEGIN

# (SS,13/8/13) (JMS: 4.2.13) ChangeOverdueAwaitsToChase moved from executable to scheduled routine
# the UserID set is 0 (i.e. meaning System)
# (SS,21/8/13) amended to add message to log about how many jobs were updated

DECLARE sMessage CHAR(100);

# (SS,21/8/13) log the start
CALL AddToEventLog('Started ChangeOverdueAwaitsToChase');

DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 3) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 4,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 3;

# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Go-ahead advanced to Chase Go-ahead'));
CALL AddToEventLog(sMessage);
 
DROP TEMPORARY TABLE IF EXISTS tmp_chasing_updates;

CREATE TEMPORARY TABLE tmp_chasing_updates 
SELECT j.JobID, NextChaseDate, ComStatusID FROM jobs j 
INNER JOIN (
  SELECT JobID, NextChaseDate FROM job_chasing jc1 
  WHERE DateTimeChased = 
    (SELECT MAX(DateTimeChased) FROM job_chasing jc2 WHERE jc1.JobID = jc2.JobID) 
  ) AS lastchase 
ON j.JobID = lastchase.JobID 
WHERE WIP = TRUE AND (ComStatusID = 6) 
AND NextChaseDate <= NOW();

UPDATE jobs j, tmp_chasing_updates t SET j.ComStatusID = 7,
 j.ComStatusDateTime = NOW(), j.ComStatusUserID = 0
 WHERE t.JobID = j.JobID AND t.ComStatusID = 6;
 
# (SS,21/8/13) add rows affected to log
SET sMessage = (SELECT CONCAT(ROW_COUNT(), ' job(s) Awaiting Order No advanced to Chase Order No'));
CALL AddToEventLog(sMessage); 

# (SS,21/8/13) log the finish
CALL AddToEventLog('Finished ChangeOverdueAwaitsToChase');

END�g�c�g�c�
 �� jms_demo_rhesGetTotalLabourAndMaterialsGetTotalLabourAndMaterialslatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%
        sJobID CHAR ( 12 )
    BEGIN
  # Serial: 2008-02-22-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMain(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

ENDBEGIN
  # Serial: 2008-02-22-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMain(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

END�'��;��g�c�g�c�
R�� jms_demo_rhesGetPriceSummaryGetPriceSummarylatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%
        IN nEquipmentID INTEGER ( 11 ) ,
        IN nBranchID INTEGER ( 11 )
    BEGIN

# Serial: 2008-04-23-01
# Original version SS, ITP, 22-02-2007
# This version modified by Jim to use the new equipment_price_summary table
# which is created at the end of the day to help speed up this routine
# which was taking a few seconds where hundreds of jobs existing for same EquipmentID
  
DECLARE nSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nRepairPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalJobCount INT(11) DEFAULT 0;
DECLARE nWorldAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldJobCount INT(11) DEFAULT 0;
DECLARE nLocalSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldSellPrice DECIMAL(11,2) DEFAULT 0.00;

# SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;
# (SS,23/4/08) replaced above with following due to field name changes
SELECT OurRefurbPrice, OurRepairPrice, OurServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;

SELECT LocalAveSellPrice, LocalAveJobCost, LocalJobCount INTO nLocalAveSellPrice, nLocalAveJobCost, nLocalJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID AND BranchID=nBranchID;
# (SS,19/3/08) in following two it may be more accurate to use SUM(LocalAveSellPrice) / SUM(LocalJobCount)
SELECT AVG(LocalAveSellPrice) INTO nWorldAveSellPrice FROM equipment_price_summary WHERE LocalAveSellPrice>0 AND EquipmentID=nEquipmentID;
SELECT AVG(LocalAveJobCost) INTO nWorldAveJobCost FROM equipment_price_summary WHERE LocalAveJobCost>0 AND EquipmentID=nEquipmentID;
SELECT SUM(LocalJobCount) INTO nWorldJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID;

IF nSellPrice IS NULL THEN
   SET nSellPrice="0.00";
END IF;
    
IF nRepairPrice IS NULL THEN
   SET nRepairPrice="0.00";
END IF;

IF nServiceExPrice IS NULL THEN
   SET nServiceExPrice="0.00";
END IF;
    
IF nLocalAveSellPrice IS NULL THEN
   SET nLocalAveSellPrice="0.00";
END IF;    
    
IF nLocalAveJobCost IS NULL THEN
   SET nLocalAveJobCost="0.00";
END IF;
    
IF nLocalJobCount IS NULL THEN
   SET nLocalJobCount="0.00";
END IF;
    
IF nWorldAveSellPrice IS NULL THEN
   SET nWorldAveSellPrice="0.00";
END IF;    
    
IF nWorldAveJobCost IS NULL THEN
   SET nWorldAveJobCost="0.00";
END IF;
    
IF nWorldJobCount IS NULL THEN
   SET nWorldJobCount="0.00";
END IF;

SELECT
    nSellPrice AS SellPrice,
    nRepairPrice AS RepairPrice,
    nServiceExPrice AS ServiceExPrice,
    nLocalAveSellPrice AS AveSellPriceLocal,
    nLocalAveJobCost AS AveJobCostLocal,
    nLocalJobCount AS LocalJobCount,
    nWorldAveSellPrice AS AveSellPriceWorld,
    nWorldAveJobCost AS AveJobCostWorld,
    nWorldJobCount AS WorldJobCount;	

ENDBEGIN

# Serial: 2008-04-23-01
# Original version SS, ITP, 22-02-2007
# This version modified by Jim to use the new equipment_price_summary table
# which is created at the end of the day to help speed up this routine
# which was taking a few seconds where hundreds of jobs existing for same EquipmentID
  
DECLARE nSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nRepairPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nLocalJobCount INT(11) DEFAULT 0;
DECLARE nWorldAveSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldAveJobCost DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldJobCount INT(11) DEFAULT 0;
DECLARE nLocalSellPrice DECIMAL(11,2) DEFAULT 0.00;
DECLARE nWorldSellPrice DECIMAL(11,2) DEFAULT 0.00;

# SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;
# (SS,23/4/08) replaced above with following due to field name changes
SELECT OurRefurbPrice, OurRepairPrice, OurServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID=nEquipmentID;

SELECT LocalAveSellPrice, LocalAveJobCost, LocalJobCount INTO nLocalAveSellPrice, nLocalAveJobCost, nLocalJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID AND BranchID=nBranchID;
# (SS,19/3/08) in following two it may be more accurate to use SUM(LocalAveSellPrice) / SUM(LocalJobCount)
SELECT AVG(LocalAveSellPrice) INTO nWorldAveSellPrice FROM equipment_price_summary WHERE LocalAveSellPrice>0 AND EquipmentID=nEquipmentID;
SELECT AVG(LocalAveJobCost) INTO nWorldAveJobCost FROM equipment_price_summary WHERE LocalAveJobCost>0 AND EquipmentID=nEquipmentID;
SELECT SUM(LocalJobCount) INTO nWorldJobCount FROM equipment_price_summary WHERE EquipmentID=nEquipmentID;

IF nSellPrice IS NULL THEN
   SET nSellPrice="0.00";
END IF;
    
IF nRepairPrice IS NULL THEN
   SET nRepairPrice="0.00";
END IF;

IF nServiceExPrice IS NULL THEN
   SET nServiceExPrice="0.00";
END IF;
    
IF nLocalAveSellPrice IS NULL THEN
   SET nLocalAveSellPrice="0.00";
END IF;    
    
IF nLocalAveJobCost IS NULL THEN
   SET nLocalAveJobCost="0.00";
END IF;
    
IF nLocalJobCount IS NULL THEN
   SET nLocalJobCount="0.00";
END IF;
    
IF nWorldAveSellPrice IS NULL THEN
   SET nWorldAveSellPrice="0.00";
END IF;    
    
IF nWorldAveJobCost IS NULL THEN
   SET nWorldAveJobCost="0.00";
END IF;
    
IF nWorldJobCount IS NULL THEN
   SET nWorldJobCount="0.00";
END IF;

SELECT
    nSellPrice AS SellPrice,
    nRepairPrice AS RepairPrice,
    nServiceExPrice AS ServiceExPrice,
    nLocalAveSellPrice AS AveSellPriceLocal,
    nLocalAveJobCost AS AveJobCostLocal,
    nLocalJobCount AS LocalJobCount,
    nWorldAveSellPrice AS AveSellPriceWorld,
    nWorldAveJobCost AS AveJobCostWorld,
    nWorldJobCount AS WorldJobCount;	

END�������g�c�g�c�
R�� jms_demo_rhesGetPriceSummaryPrevGetPriceSummaryPrevlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%
        IN nEquipmentID INTEGER ( 11 ) ,
        IN nBranchID INTEGER ( 11 )
    BEGIN

  # Serial: 2008-02-22-01

  DECLARE nSellPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nRepairPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceWorld DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostWorld DECIMAL(11,2) DEFAULT NULL;

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nJobID INTEGER(11);
  DECLARE nJobBranchID INTEGER(11);
  DECLARE nJobSellPrice DOUBLE;

  DECLARE nWorldSellPrice DOUBLE DEFAULT 0;
  DECLARE nWorldJobCost DOUBLE DEFAULT 0;
  DECLARE nWorldJobCount INTEGER DEFAULT 0;
  DECLARE nLocalSellPrice DOUBLE DEFAULT 0;
  DECLARE nLocalJobCost DOUBLE DEFAULT 0;
  DECLARE nLocalJobCount INTEGER DEFAULT 0;

  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(9,2);
  DECLARE nLabourHours DOUBLE;

  # this query selects all invoiced jobs with given equipment ID
  DECLARE curSelectedJobs CURSOR FOR SELECT JobID, BranchID, SellPrice FROM jobs WHERE WIP = FALSE AND EquipmentID = nEquipmentID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  # get SellPrice, RepairPrice, ServiceExPrice from Equipment table
  SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID = nEquipmentID;

  OPEN curSelectedJobs;

  nextrecord: LOOP

    FETCH curSelectedJobs INTO nJobID, nJobBranchID, nJobSellPrice;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    # determine JobCost using GetTotalLabourAndMaterialsMain procedure, i.e. JobCost = LabourCost + MaterialCost
    CALL GetTotalLabourAndMaterialsMain(nJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);

    # treat NULL as zero, else total will end up as NULL, alternative is to ignore records containing NULLs
    IF nJobSellPrice IS NULL THEN
      SET nJobSellPrice = 0;
    END IF;

    SET nWorldSellPrice = nWorldSellPrice + nJobSellPrice;
    SET nWorldJobCost = nWorldJobCost + nLabourCost + nMaterialCost;
    SET nWorldJobCount = nWorldJobCount + 1;

    IF nBranchID = nJobBranchID THEN
       SET nLocalSellPrice = nLocalSellPrice + nJobSellPrice;
       SET nLocalJobCost = nLocalJobCost + nLabourCost + nMaterialCost;
       SET nLocalJobCount = nLocalJobCount + 1;
    END IF;

  END LOOP nextrecord;

  CLOSE curSelectedJobs;

  # calculate the averages

  IF nLocalJobCount > 1 THEN
     SET nAveSellPriceLocal = ROUND(nLocalSellPrice / nLocalJobCount, 2);
     SET nAveJobCostLocal = ROUND(nLocalJobCost / nLocalJobCount, 2);
  END IF;

  IF nWorldJobCount > 1 THEN
     SET nAveSellPriceWorld = ROUND(nWorldSellPrice / nWorldJobCount, 2);
     SET nAveJobCostWorld = ROUND(nWorldJobCost / nWorldJobCount, 2);
  END IF;

  # ------------------
  # Output the results
  # ------------------

  SELECT nSellPrice AS SellPrice, nRepairPrice As RepairPrice, nServiceExPrice AS ServiceExPrice,
         nAveSellPriceLocal AS AveSellPriceLocal, nAveJobCostLocal AS AveJobCostLocal, nLocalJobCount AS LocalJobCount,
         nAveSellPriceWorld AS AveSellPriceWorld, nAveJobCostWorld AS AveJobCostWorld, nWorldJobCount AS WorldJobCount;

ENDBEGIN

  # Serial: 2008-02-22-01

  DECLARE nSellPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nRepairPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nServiceExPrice DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostLocal DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveSellPriceWorld DECIMAL(11,2) DEFAULT NULL;
  DECLARE nAveJobCostWorld DECIMAL(11,2) DEFAULT NULL;

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nJobID INTEGER(11);
  DECLARE nJobBranchID INTEGER(11);
  DECLARE nJobSellPrice DOUBLE;

  DECLARE nWorldSellPrice DOUBLE DEFAULT 0;
  DECLARE nWorldJobCost DOUBLE DEFAULT 0;
  DECLARE nWorldJobCount INTEGER DEFAULT 0;
  DECLARE nLocalSellPrice DOUBLE DEFAULT 0;
  DECLARE nLocalJobCost DOUBLE DEFAULT 0;
  DECLARE nLocalJobCount INTEGER DEFAULT 0;

  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(9,2);
  DECLARE nLabourHours DOUBLE;

  # this query selects all invoiced jobs with given equipment ID
  DECLARE curSelectedJobs CURSOR FOR SELECT JobID, BranchID, SellPrice FROM jobs WHERE WIP = FALSE AND EquipmentID = nEquipmentID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  # get SellPrice, RepairPrice, ServiceExPrice from Equipment table
  SELECT SellPrice, RepairPrice, ServiceExPrice INTO nSellPrice, nRepairPrice, nServiceExPrice FROM equipment WHERE EquipmentID = nEquipmentID;

  OPEN curSelectedJobs;

  nextrecord: LOOP

    FETCH curSelectedJobs INTO nJobID, nJobBranchID, nJobSellPrice;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    # determine JobCost using GetTotalLabourAndMaterialsMain procedure, i.e. JobCost = LabourCost + MaterialCost
    CALL GetTotalLabourAndMaterialsMain(nJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);

    # treat NULL as zero, else total will end up as NULL, alternative is to ignore records containing NULLs
    IF nJobSellPrice IS NULL THEN
      SET nJobSellPrice = 0;
    END IF;

    SET nWorldSellPrice = nWorldSellPrice + nJobSellPrice;
    SET nWorldJobCost = nWorldJobCost + nLabourCost + nMaterialCost;
    SET nWorldJobCount = nWorldJobCount + 1;

    IF nBranchID = nJobBranchID THEN
       SET nLocalSellPrice = nLocalSellPrice + nJobSellPrice;
       SET nLocalJobCost = nLocalJobCost + nLabourCost + nMaterialCost;
       SET nLocalJobCount = nLocalJobCount + 1;
    END IF;

  END LOOP nextrecord;

  CLOSE curSelectedJobs;

  # calculate the averages

  IF nLocalJobCount > 1 THEN
     SET nAveSellPriceLocal = ROUND(nLocalSellPrice / nLocalJobCount, 2);
     SET nAveJobCostLocal = ROUND(nLocalJobCost / nLocalJobCount, 2);
  END IF;

  IF nWorldJobCount > 1 THEN
     SET nAveSellPriceWorld = ROUND(nWorldSellPrice / nWorldJobCount, 2);
     SET nAveJobCostWorld = ROUND(nWorldJobCost / nWorldJobCount, 2);
  END IF;

  # ------------------
  # Output the results
  # ------------------

  SELECT nSellPrice AS SellPrice, nRepairPrice As RepairPrice, nServiceExPrice AS ServiceExPrice,
         nAveSellPriceLocal AS AveSellPriceLocal, nAveJobCostLocal AS AveJobCostLocal, nLocalJobCount AS LocalJobCount,
         nAveSellPriceWorld AS AveSellPriceWorld, nAveJobCostWorld AS AveJobCostWorld, nWorldJobCount AS WorldJobCount;

ENDM�������g�c�g�c�
�� jms_demo_rhesGetTotalLabourAndMaterialsMainGetTotalLabourAndMaterialsMainlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%
        IN sJobID CHAR ( 12 ) ,
        OUT pLabourHours INTEGER ( 11 ) ,
        OUT pLabourCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialSell DECIMAL ( 11 , 2 ) ,
        OUT pMinSellPrice DECIMAL ( 11 , 2 )
    BEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-09-12-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCost(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDBEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2009-09-12-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCost(sJobID) INTO nMaterialCost;

  # *** the 10, 1.4, 1.3 constants in following to be made into globals
  If nMaterialCost < 10 THEN
    SET nMaterialSell = nMaterialCost * 1.4;
  ELSE
    SET nMaterialSell = nMaterialCost * 1.3;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
END�g�c�g�c�
 �� jms_demo_rhesGetTotalLabourAndMaterialsNewGetTotalLabourAndMaterialsNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%
        sJobID CHAR ( 12 )
    BEGIN
  # Serial: 2008-12-19-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMainNew(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

ENDBEGIN
  # Serial: 2008-12-19-01
  
  DECLARE nLabourHours DOUBLE;
  DECLARE nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice DECIMAL(11,2);

  CALL GetTotalLabourAndMaterialsMainNew(sJobID, nLabourHours, nLabourCost, nMaterialCost, nMaterialSell, nMinSellPrice);
  
  SELECT nLabourHours AS LabourHours, nLabourCost As LabourCost, nMaterialCost AS MaterialCost, nMaterialSell AS MaterialSell, nMinSellPrice AS MinSellPrice;

END
3��
�6���g�c�g�c�
!!@@ Pjms_demo_rhesGetTotalLabourAndMaterialsMainNewGetTotalLabourAndMaterialsMainNewlatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%
        IN sJobID CHAR ( 12 ) ,
        OUT pLabourHours INTEGER ( 11 ) ,
        OUT pLabourCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialCost DECIMAL ( 11 , 2 ) ,
        OUT pMaterialSell DECIMAL ( 11 , 2 ) ,
        OUT pMinSellPrice DECIMAL ( 11 , 2 )
    BEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2013-08-13-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # (SS,13/8/13) added getting of MaterialThreshold, MaterialBelowThresholdSellFactor and MaterialAboveThresholdSellFactor from branches table
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  
  # (SS,13/8/13) added following, these setting are now taken from branches table  
  DECLARE nMaterialThreshold DECIMAL(11,2) DEFAULT 10;
  DECLARE nMaterialBelowThresholdSellFactor DECIMAL(11,2) DEFAULT 1.4;
  DECLARE nMaterialAboveThresholdSellFactor DECIMAL(11,2) DEFAULT 1.3;  
  
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

  # (SS,13/8/13) replaced 10, 1.4, 1.3 constants with variables, settings from branches table      
  SELECT MaterialThreshold, MaterialBelowThresholdSellFactor, MaterialAboveThresholdSellFactor INTO nMaterialThreshold, nMaterialBelowThresholdSellFactor, nMaterialAboveThresholdSellFactor 
      FROM jobs 
    INNER JOIN branches ON branches.BranchID = jobs.BranchID 
    WHERE JobID = sJobID;
  
  If nMaterialCost < nMaterialThreshold THEN
    SET nMaterialSell = nMaterialCost * nMaterialBelowThresholdSellFactor;
  ELSE
    SET nMaterialSell = nMaterialCost * nMaterialAboveThresholdSellFactor;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDBEGIN
  # GetTotalLabourAndMaterialsMain
  # Serial: 2013-08-13-01
  # Now called from GetPriceSummary as well as GetTotalLabourAndMaterials, values passed via parameters
  
  # (SS,13/8/13) added getting of MaterialThreshold, MaterialBelowThresholdSellFactor and MaterialAboveThresholdSellFactor from branches table
  
  # ----------------------
  # Calculate Total Labour
  # ----------------------

  DECLARE bFinished INTEGER DEFAULT 0;
  DECLARE nTotalLabour DECIMAL(11,2) DEFAULT 0;
  DECLARE nMaterialCost, nMaterialSell DECIMAL(11,2);
  DECLARE sChargeRate CHAR(1) DEFAULT "";
  DECLARE nRateA, nRateB, nRateC, nUseRate DOUBLE DEFAULT 0;
  DECLARE nHours, nTotalHours DOUBLE DEFAULT 0;
  
  # (SS,13/8/13) added following, these setting are now taken from branches table  
  DECLARE nMaterialThreshold DECIMAL(11,2) DEFAULT 10;
  DECLARE nMaterialBelowThresholdSellFactor DECIMAL(11,2) DEFAULT 1.4;
  DECLARE nMaterialAboveThresholdSellFactor DECIMAL(11,2) DEFAULT 1.3;  
  
  DECLARE curTimesheetsWithCharge CURSOR FOR
   SELECT t.ChargeRate, u.RateA, u.RateB, u.RateC,
     HOUR(SUBTIME(t.WorkEndTime, t.WorkStartTime)) + MINUTE(SUBTIME(t.WorkEndTime, t.WorkStartTime)) / 60 AS Hours
     FROM timesheets t, users u WHERE t.UserID = u.UserID AND t.jobid = sJobID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET bFinished = 1;

  OPEN curTimesheetsWithCharge;

  nextrecord: LOOP

    FETCH curTimesheetsWithCharge INTO sChargeRate, nRateA, nRateB, nRateC, nHours;
    IF bFinished THEN
      LEAVE nextrecord;
    END IF;

    IF sChargeRate = "A" THEN
      SET nUseRate = nRateA;
    ELSEIF sChargeRate = "B" THEN
      SET nUseRate = nRateB;
    ELSEIF sChargeRate = "C" THEN
      SET nUseRate = nRateC;
    ELSE
      SET nUseRate = 0;
    END IF;

    SET nTotalHours = nTotalHours + nHours;
    SET nTotalLabour = nTotalLabour + nHours * nUseRate;

  END LOOP nextrecord;

  CLOSE curTimesheetsWithCharge;

  # ------------------------
  # Calculate Total Material
  # ------------------------

  # UnitPrice is held as 4 dp, total amount returned is 2 dp 
  SELECT funcGetMaterialCostNew(sJobID) INTO nMaterialCost;

  # (SS,13/8/13) replaced 10, 1.4, 1.3 constants with variables, settings from branches table      
  SELECT MaterialThreshold, MaterialBelowThresholdSellFactor, MaterialAboveThresholdSellFactor INTO nMaterialThreshold, nMaterialBelowThresholdSellFactor, nMaterialAboveThresholdSellFactor 
      FROM jobs 
    INNER JOIN branches ON branches.BranchID = jobs.BranchID 
    WHERE JobID = sJobID;
  
  If nMaterialCost < nMaterialThreshold THEN
    SET nMaterialSell = nMaterialCost * nMaterialBelowThresholdSellFactor;
  ELSE
    SET nMaterialSell = nMaterialCost * nMaterialAboveThresholdSellFactor;
  END IF;

  # ------------------
  # Output the results
  # ------------------
  SET pLabourHours = nTotalHours;
  SET pLabourCost = ROUND(nTotalLabour, 2);
  SET pMaterialCost = ROUND(nMaterialCost, 2);
  SET pMaterialSell = ROUND(nMaterialSell, 2);
  SET pMinSellPrice = ROUND(nTotalLabour + nMaterialSell, 2);
  
ENDA��s{vJ���g�c�g�c�
""oo jms_demo_rhesUpdateEquipmentAveragePriceSummaryUpdateEquipmentAveragePriceSummarylatin1latin1_swedish_cilatin1_swedish_cijms_demo_rhes@%BEGIN

# (SS,ITP,19/3/08)
# Serial: 2008-03-19-01
# This routine is to be run at the end of the day
# Creates records used by the GetPriceSummary routine
# to help speed up getting the average prices for a given job

# empty the table
TRUNCATE equipment_price_summary;

# summarise and add the records
INSERT INTO equipment_price_summary
(SELECT EquipmentID, BranchID,
ROUND(AVG(SellPrice),2) AS LocalAveSellPrice, ROUND(AVG(funcGetLabourMaterialTotal(JobID)),2) AS LocalAveJobCost,
COUNT(JobID) AS LocalJobCount, CURDATE() AS LastUpdate
FROM jobs WHERE WIP=FALSE AND CREDITNOTE=FALSE
GROUP BY EquipmentID, BranchID
);

ENDBEGIN

# (SS,ITP,19/3/08)
# Serial: 2008-03-19-01
# This routine is to be run at the end of the day
# Creates records used by the GetPriceSummary routine
# to help speed up getting the average prices for a given job

# empty the table
TRUNCATE equipment_price_summary;

# summarise and add the records
INSERT INTO equipment_price_summary
(SELECT EquipmentID, BranchID,
ROUND(AVG(SellPrice),2) AS LocalAveSellPrice, ROUND(AVG(funcGetLabourMaterialTotal(JobID)),2) AS LocalAveJobCost,
COUNT(JobID) AS LocalJobCount, CURDATE() AS LastUpdate
FROM jobs WHERE WIP=FALSE AND CREDITNOTE=FALSE
GROUP BY EquipmentID, BranchID
);

END�i��3i��36k�jsysextract_schema_from_file_nameextract_schema_from_file_nameutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostpath VARCHAR(512)
    varchar(64) CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    RETURN LEFT(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(path, '\\', '/'), '/', -2), '/', 1), 64);
END
             Description
             Takes a raw file path, and attempts to extract the schema name from it.
             Useful for when interacting with Performance Schema data
             concerning IO statistics, for example.
             Currently relies on the fact that a table data file will be within a
             specified database directory (will not work with partitions or tables
             that specify an individual DATA_DIRECTORY).
             Parameters
             path (VARCHAR(512)):
               The full file path to a data file to extract the schema name from.
             Returns
             VARCHAR(64)
             Example
             mysql> SELECT sys.extract_schema_from_file_name('/var/lib/mysql/employees/employee.ibd');
             +----------------------------------------------------------------------------+
             | sys.extract_schema_from_file_name('/var/lib/mysql/employees/employee.ibd') |
             +----------------------------------------------------------------------------+
             | employees                                                                  |
             +----------------------------------------------------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN LEFT(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(path, '\', '/'), '/', -2), '/', 1), 64);
END�i��3i��33���sysformat_statementformat_statementutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhoststatement LONGTEXT
    longtext CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
  IF @sys.statement_truncate_len IS NULL THEN
      SET @sys.statement_truncate_len = sys_get_config('statement_truncate_len', 64);
  END IF;
  IF CHAR_LENGTH(statement) > @sys.statement_truncate_len THEN
      RETURN REPLACE(CONCAT(LEFT(statement, (@sys.statement_truncate_len/2)-2), ' ... ', RIGHT(statement, (@sys.statement_truncate_len/2)-2)), '\n', ' ');
  ELSE
      RETURN REPLACE(statement, '\n', ' ');
  END IF;
END
             Description
             Formats a normalized statement, truncating it if it is > 64 characters long by default.
             To configure the length to truncate the statement to by default, update the `statement_truncate_len`
             variable with `sys_config` table to a different value. Alternatively, to change it just for just
             your particular session, use `SET @sys.statement_truncate_len := <some new value>`.
             Useful for printing statement related data from Performance Schema from
             the command line.
             Parameters
             statement (LONGTEXT):
               The statement to format.
             Returns
             LONGTEXT
             Example
             mysql> SELECT sys.format_statement(digest_text)
                 ->   FROM performance_schema.events_statements_summary_by_digest
                 ->  ORDER by sum_timer_wait DESC limit 5;
             +-------------------------------------------------------------------+
             | sys.format_statement(digest_text)                                 |
             +-------------------------------------------------------------------+
             | CREATE SQL SECURITY INVOKER VI ... KE ? AND `variable_value` > ?  |
             | CREATE SQL SECURITY INVOKER VI ... ait` IS NOT NULL , `esc` . ... |
             | CREATE SQL SECURITY INVOKER VI ... ait` IS NOT NULL , `sys` . ... |
             | CREATE SQL SECURITY INVOKER VI ...  , `compressed_size` ) ) DESC  |
             | CREATE SQL SECURITY INVOKER VI ... LIKE ? ORDER BY `timer_start`  |
             +-------------------------------------------------------------------+
             5 rows in set (0.00 sec)
            BEGIN
  IF @sys.statement_truncate_len IS NULL THEN
      SET @sys.statement_truncate_len = sys_get_config('statement_truncate_len', 64);
  END IF;
  IF CHAR_LENGTH(statement) > @sys.statement_truncate_len THEN
      RETURN REPLACE(CONCAT(LEFT(statement, (@sys.statement_truncate_len/2)-2), ' ... ', RIGHT(statement, (@sys.statement_truncate_len/2)-2)), '\n', ' ');
  ELSE
      RETURN REPLACE(statement, '\n', ' ');
  END IF;
END`�
���zJ����i���i���
�]] ThyperflightGetStockRequiredGetStockRequiredutf8mb4utf8mb4_general_cilatin1_swedish_cihyperflight@%
	IN `nSupplierID` INTEGER(11),
	IN `nPastWeeks` INTEGER(11),
	IN `nFollowingWeeks` INTEGER(11),
	IN `bRequiredOnly` TINYINT(1)
BEGIN

CALL GetInStockRatios(DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK));

# (SS,3/4/14) made to make the following create a temporary table because
# in Shopping Admin using CALL GetStockRequired(2, 52, 1, TRUE);
# was causing error: "can't return a result set in the given context"
# while testing it seemed to work if coMultiQueries option used, but this causes issues elsewhere
DROP TEMPORARY TABLE IF EXISTS tmp_stock_required;

CREATE TEMPORARY TABLE tmp_stock_required
# (SS,25/4/17) changed orderdetails.ProductName to products.ProductName to ensure the latest product name
# (SS,10/4/18) added products.SortOrder
# (SS,4/2/26) added GREATEST(50, ..) to ensure In Stock ratio of less than 50 isn't applied, in two places before COALESCE(tmp_in_stock_ratios.InStockRatio, 100)
SELECT products.SupplierID, suppliers.SupplierName, orderdetails.ProductID, orderdetails.ProductCode, products.ProductName, products.SortOrder,
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks + 0.4999) AS QtyRequiredNoRatio, 
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / GREATEST(50, COALESCE(tmp_in_stock_ratios.InStockRatio, 100)) + 0.4999) AS QtyRequired, 
NumInStock, on_order.QtyOrdered, on_order.QtyReceived, on_order.QtyAddedToStock,
# QtyToOrder calculated using QtyRequired - (NumInStock + COALESCE(QtyOrdered, 0) - COALESCE(QtyAddedToStock, 0))
# i.e. QtyRequired - (NumInStock + QtyOrdered - QtyAddedToStock)
# (SS,5/9/16) replaced QtyAddedToStock with QtyReceived
# i.e. now QtyRequired - (NumInStock + QtyOrdered - QtyReceived)
(ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / GREATEST(50, COALESCE(tmp_in_stock_ratios.InStockRatio, 100)) + 0.4999)) - (NumInStock + COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0)) AS QtyToOrder,
COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0) AS QtyOnOrder, products.CostPrice,
tmp_in_stock_ratios.InStockRatio
FROM orderdetails
INNER JOIN orders ON orders.OrderNo = orderdetails.OrderNo
INNER JOIN products ON products.ProductCode = orderdetails.ProductCode
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
LEFT JOIN tmp_in_stock_ratios ON tmp_in_stock_ratios.ProductID = products.ProductID
LEFT JOIN

(SELECT pol.ProductID, SUM(QtyOrdered) AS QtyOrdered, ordered_and_received.QtyReceived, ordered_and_received.QtyAddedToStock
FROM purchase_order_lines pol
LEFT JOIN

  (SELECT ProductID, SUM(QtyReceived) AS QtyReceived, SUM(QtyAddedToStock) AS QtyAddedToStock
  FROM purchase_invoice_lines pil
  INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
  INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
  WHERE (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") AND ProductID IS NOT NULL
  GROUP BY ProductID
  ) AS ordered_and_received ON ordered_and_received.ProductID = pol.ProductID
  
INNER JOIN purchase_orders ON purchase_orders.PurchaseOrderNo = pol.PurchaseOrderNo

WHERE OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED"
GROUP BY ProductID
) AS on_order ON on_order.ProductID = orderdetails.ProductID

# (SS,29/4/14) changed nSupplierID = 0 to nSupplierID = -1 (because 0 is when supplier hasn't been chosen to prevent slow adding of new order, -1 when the report is run for all suppliers)
WHERE Status <> "CANCELLED" AND NumInStock IS NOT NULL AND ProductDisabled = False AND DateTimeOrdered >= DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK) 
AND (nSupplierID = -1 OR suppliers.SupplierID = nSupplierID)
GROUP BY suppliers.SupplierName, ProductCode
HAVING NOT bRequiredOnly OR QtyRequired > NumInStock + QtyOnOrder;

ENDBEGIN

CALL GetInStockRatios(DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK));

# (SS,3/4/14) made to make the following create a temporary table because
# in Shopping Admin using CALL GetStockRequired(2, 52, 1, TRUE);
# was causing error: "can't return a result set in the given context"
# while testing it seemed to work if coMultiQueries option used, but this causes issues elsewhere
DROP TEMPORARY TABLE IF EXISTS tmp_stock_required;

CREATE TEMPORARY TABLE tmp_stock_required
# (SS,25/4/17) changed orderdetails.ProductName to products.ProductName to ensure the latest product name
# (SS,10/4/18) added products.SortOrder
# (SS,4/2/26) added GREATEST(50, ..) to ensure In Stock ratio of less than 50 isn't applied, in two places before COALESCE(tmp_in_stock_ratios.InStockRatio, 100)
SELECT products.SupplierID, suppliers.SupplierName, orderdetails.ProductID, orderdetails.ProductCode, products.ProductName, products.SortOrder,
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks + 0.4999) AS QtyRequiredNoRatio, 
ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / GREATEST(50, COALESCE(tmp_in_stock_ratios.InStockRatio, 100)) + 0.4999) AS QtyRequired, 
NumInStock, on_order.QtyOrdered, on_order.QtyReceived, on_order.QtyAddedToStock,
# QtyToOrder calculated using QtyRequired - (NumInStock + COALESCE(QtyOrdered, 0) - COALESCE(QtyAddedToStock, 0))
# i.e. QtyRequired - (NumInStock + QtyOrdered - QtyAddedToStock)
# (SS,5/9/16) replaced QtyAddedToStock with QtyReceived
# i.e. now QtyRequired - (NumInStock + QtyOrdered - QtyReceived)
(ROUND(SUM(Qty) / nPastWeeks * nFollowingWeeks * 100 / GREATEST(50, COALESCE(tmp_in_stock_ratios.InStockRatio, 100)) + 0.4999)) - (NumInStock + COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0)) AS QtyToOrder,
COALESCE(on_order.QtyOrdered, 0) - COALESCE(on_order.QtyReceived, 0) AS QtyOnOrder, products.CostPrice,
tmp_in_stock_ratios.InStockRatio
FROM orderdetails
INNER JOIN orders ON orders.OrderNo = orderdetails.OrderNo
INNER JOIN products ON products.ProductCode = orderdetails.ProductCode
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
LEFT JOIN tmp_in_stock_ratios ON tmp_in_stock_ratios.ProductID = products.ProductID
LEFT JOIN

(SELECT pol.ProductID, SUM(QtyOrdered) AS QtyOrdered, ordered_and_received.QtyReceived, ordered_and_received.QtyAddedToStock
FROM purchase_order_lines pol
LEFT JOIN

  (SELECT ProductID, SUM(QtyReceived) AS QtyReceived, SUM(QtyAddedToStock) AS QtyAddedToStock
  FROM purchase_invoice_lines pil
  INNER JOIN purchase_order_lines pol ON pol.PurchaseOrderLineID = pil.PurchaseOrderLineID
  INNER JOIN purchase_orders po ON po.PurchaseOrderNo = pol.PurchaseOrderNo
  WHERE (OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED") AND ProductID IS NOT NULL
  GROUP BY ProductID
  ) AS ordered_and_received ON ordered_and_received.ProductID = pol.ProductID
  
INNER JOIN purchase_orders ON purchase_orders.PurchaseOrderNo = pol.PurchaseOrderNo

WHERE OrderStatus = "PLACED" OR OrderStatus = "PARTIALLY RECEIVED"
GROUP BY ProductID
) AS on_order ON on_order.ProductID = orderdetails.ProductID

# (SS,29/4/14) changed nSupplierID = 0 to nSupplierID = -1 (because 0 is when supplier hasn't been chosen to prevent slow adding of new order, -1 when the report is run for all suppliers)
WHERE Status <> "CANCELLED" AND NumInStock IS NOT NULL AND ProductDisabled = False AND DateTimeOrdered >= DATE_SUB(CURDATE(), INTERVAL nPastWeeks WEEK) 
AND (nSupplierID = -1 OR suppliers.SupplierID = nSupplierID)
GROUP BY suppliers.SupplierName, ProductCode
HAVING NOT bRequiredOnly OR QtyRequired > NumInStock + QtyOnOrder;

END���9H�J���i��3i��3/�k�sysformat_timeformat_timeutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostpicoseconds TEXT
    text CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
  IF picoseconds IS NULL THEN RETURN NULL;
  ELSEIF picoseconds >= 604800000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 604800000000000000, 2), ' w');
  ELSEIF picoseconds >= 86400000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 86400000000000000, 2), ' d');
  ELSEIF picoseconds >= 3600000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 3600000000000000, 2), ' h');
  ELSEIF picoseconds >= 60000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 60000000000000, 2), ' m');
  ELSEIF picoseconds >= 1000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000000, 2), ' s');
  ELSEIF picoseconds >= 1000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000, 2), ' ms');
  ELSEIF picoseconds >= 1000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000, 2), ' us');
  ELSEIF picoseconds >= 1000 THEN RETURN CONCAT(ROUND(picoseconds / 1000, 2), ' ns');
  ELSE RETURN CONCAT(picoseconds, ' ps');
  END IF;
END
             Description
             Takes a raw picoseconds value, and converts it to a human readable form.
             Picoseconds are the precision that all latency values are printed in
             within Performance Schema, however are not user friendly when wanting
             to scan output from the command line.
             Parameters
             picoseconds (TEXT):
               The raw picoseconds value to convert.
             Returns
             TEXT
             Example
             mysql> select format_time(342342342342345);
             +------------------------------+
             | format_time(342342342342345) |
             +------------------------------+
             | 00:05:42                     |
             +------------------------------+
             1 row in set (0.00 sec)
             mysql> select format_time(342342342);
             +------------------------+
             | format_time(342342342) |
             +------------------------+
             | 342.34 us              |
             +------------------------+
             1 row in set (0.00 sec)
             mysql> select format_time(34234);
              +--------------------+
             | format_time(34234) |
             +--------------------+
             | 34.23 ns           |
             +--------------------+
             1 row in set (0.00 sec)
            BEGIN
  IF picoseconds IS NULL THEN RETURN NULL;
  ELSEIF picoseconds >= 604800000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 604800000000000000, 2), ' w');
  ELSEIF picoseconds >= 86400000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 86400000000000000, 2), ' d');
  ELSEIF picoseconds >= 3600000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 3600000000000000, 2), ' h');
  ELSEIF picoseconds >= 60000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 60000000000000, 2), ' m');
  ELSEIF picoseconds >= 1000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000000, 2), ' s');
  ELSEIF picoseconds >= 1000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000, 2), ' ms');
  ELSEIF picoseconds >= 1000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000, 2), ' us');
  ELSEIF picoseconds >= 1000 THEN RETURN CONCAT(ROUND(picoseconds / 1000, 2), ' ns');
  ELSE RETURN CONCAT(picoseconds, ' ps');
  END IF;
END�i��3i��35;#c#sysps_is_account_enabledps_is_account_enabledutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_host VARCHAR(60),
        in_user VARCHAR(16)
    enum('YES','NO') CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    RETURN IF(EXISTS(SELECT 1
                       FROM performance_schema.setup_actors
                      WHERE (`HOST` = '%' OR in_host LIKE `HOST`)
                        AND (`USER` = '%' OR `USER` = in_user)
                    ),
              'YES', 'NO'
           );
END
             Description
             Determines whether instrumentation of an account is enabled
             within Performance Schema.
             Parameters
             in_host VARCHAR(60):
               The hostname of the account to check.
             in_user (VARCHAR(16)):
               The username of the account to check.
             Returns
             ENUM('YES', 'NO', 'PARTIAL')
             Example
             mysql> SELECT sys.ps_is_account_enabled('localhost', 'root');
             +------------------------------------------------+
             | sys.ps_is_account_enabled('localhost', 'root') |
             +------------------------------------------------+
             | YES                                            |
             +------------------------------------------------+
             1 row in set (0.01 sec)
            BEGIN
    RETURN IF(EXISTS(SELECT 1
                       FROM performance_schema.setup_actors
                      WHERE (`HOST` = '%' OR in_host LIKE `HOST`)
                        AND (`USER` = '%' OR `USER` = in_user)
                    ),
              'YES', 'NO'
           );
ENDq
�e
Va=2FL���i��4i��4%���sysps_thread_idps_thread_idutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_connection_id BIGINT UNSIGNED
    bigint(20) unsignedBEGIN
    RETURN (SELECT THREAD_ID
              FROM `performance_schema`.`threads`
             WHERE PROCESSLIST_ID = IFNULL(in_connection_id, CONNECTION_ID())
           );
END
             Description
             Return the Performance Schema THREAD_ID for the specified connection ID.
             Parameters
             in_connection_id (BIGINT UNSIGNED):
               The id of the connection to return the thread id for. If NULL, the current
               connection thread id is returned.
             Example
             mysql> SELECT sys.ps_thread_id(79);
             +----------------------+
             | sys.ps_thread_id(79) |
             +----------------------+
             |                   98 |
             +----------------------+
             1 row in set (0.00 sec)
             mysql> SELECT sys.ps_thread_id(CONNECTION_ID());
             +-----------------------------------+
             | sys.ps_thread_id(CONNECTION_ID()) |
             +-----------------------------------+
             |                                98 |
             +-----------------------------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN (SELECT THREAD_ID
              FROM `performance_schema`.`threads`
             WHERE PROCESSLIST_ID = IFNULL(in_connection_id, CONNECTION_ID())
           );
END�i��4i��4/I{Isysquote_identifierquote_identifierutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_identifier TEXTtext CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    RETURN CONCAT('`', REPLACE(in_identifier, '`', '``'), '`');
END
             Description
             Takes an unquoted identifier (schema name, table name, etc.) and
             returns the identifier quoted with backticks.
             Parameters
             in_identifier (TEXT):
               The identifier to quote.
             Returns
             TEXT
             Example
             mysql> SELECT sys.quote_identifier('my_identifier') AS Identifier;
             +-----------------+
             | Identifier      |
             +-----------------+
             | `my_identifier` |
             +-----------------+
             1 row in set (0.00 sec)
             mysql> SELECT sys.quote_identifier('my`idenfier') AS Identifier;
             +----------------+
             | Identifier     |
             +----------------+
             | `my``idenfier` |
             +----------------+
             1 row in set (0.00 sec)
            BEGIN
    RETURN CONCAT('`', REPLACE(in_identifier, '`', '``'), '`');
END�i��4i��4I7 � syssys_get_configsys_get_configutf8mb3utf8mb3_general_ciutf8mb3_general_cimariadb.sys@localhostin_variable_name VARCHAR(128),
        in_default_value VARCHAR(128)
    varchar(128) CHARSET utf8mb3 COLLATE utf8mb3_general_ciBEGIN
    DECLARE v_value VARCHAR(128) DEFAULT NULL;
    DECLARE old_val INTEGER DEFAULT NULL;
    SET v_value = (SELECT value FROM sys.sys_config WHERE variable = in_variable_name);
    IF (v_value IS NULL) THEN
        SET v_value = in_default_value;
    END IF;
    RETURN v_value;
END
             Description
             Returns the value for the requested variable using the following logic:
                1. If the option exists in sys.sys_config return the value from there.
                2. Else fall back on the provided default value.
             Notes for using sys_get_config():
                * If the default value argument to sys_get_config() is NULL and case 2. is reached, NULL is returned.
                  It is then expected that the caller is able to handle NULL for the given configuration option.
                * The convention is to name the user variables @sys.<name of variable>. It is <name of variable> that
                  is stored in the sys_config table and is what is expected as the argument to sys_get_config().
                * If you want to check whether the configuration option has already been set and if not assign with
                  the return value of sys_get_config() you can use IFNULL(...) (see example below). However this should
                  not be done inside a loop (e.g. for each row in a result set) as for repeated calls where assignment
                  is only needed in the first iteration using IFNULL(...) is expected to be significantly slower than
                  using an IF (...) THEN ... END IF; block (see example below).
             Parameters
             in_variable_name (VARCHAR(128)):
               The name of the config option to return the value for.
             in_default_value (VARCHAR(128)):
               The default value to return if the variable does not exist in sys.sys_config.
             Returns
             VARCHAR(128)
             Example
             mysql> SELECT sys.sys_get_config('statement_truncate_len', 128) AS Value;
             +-------+
             | Value |
             +-------+
             | 64    |
             +-------+
             1 row in set (0.00 sec)
             mysql> SET @sys.statement_truncate_len = IFNULL(@sys.statement_truncate_len, sys.sys_get_config('statement_truncate_len', 64));
             Query OK, 0 rows affected (0.00 sec)
             IF (@sys.statement_truncate_len IS NULL) THEN
                 SET @sys.statement_truncate_len = sys.sys_get_config('statement_truncate_len', 64);
             END IF;
            BEGIN
    DECLARE v_value VARCHAR(128) DEFAULT NULL;
    DECLARE old_val INTEGER DEFAULT NULL;
    SET v_value = (SELECT value FROM sys.sys_config WHERE variable = in_variable_name);
    IF (v_value IS NULL) THEN
        SET v_value = in_default_value;
    END IF;
    RETURN v_value;
END�
A
��X30"�K�q	BEGIN
    DECLARE json_objects LONGTEXT;
    
    UPDATE performance_schema.threads
       SET instrumented = 'NO'
     WHERE processlist_id = CONNECTION_ID();
    
    SET SESSION group_concat_max_len=@@global.max_allowed_packet;
    SELECT GROUP_CONCAT(CONCAT( '{'
              , CONCAT_WS( ', '
              , CONCAT('"nesting_event_id": "', IF(nesting_event_id IS NULL, '0', nesting_event_id), '"')
              , CONCAT('"event_id": "', event_id, '"')
              , CONCAT( '"timer_wait": ', ROUND(timer_wait/1000000, 2))
              , CONCAT( '"event_info": "'
                  , CASE
                        WHEN event_name NOT LIKE 'wait/io%' THEN REPLACE(SUBSTRING_INDEX(event_name, '/', -2), '\\', '\\\\')
                        WHEN event_name NOT LIKE 'wait/io/file%' OR event_name NOT LIKE 'wait/io/socket%' THEN REPLACE(SUBSTRING_INDEX(event_name, '/', -4), '\\', '\\\\')
                        ELSE event_name
                    END
                  , '"'
              )
              , CONCAT( '"wait_info": "', IFNULL(wait_info, ''), '"')
              , CONCAT( '"source": "', IF(true AND event_name LIKE 'wait%', IFNULL(wait_info, ''), ''), '"')
              , CASE
                     WHEN event_name LIKE 'wait/io/file%'      THEN '"event_type": "io/file"'
                     WHEN event_name LIKE 'wait/io/table%'     THEN '"event_type": "io/table"'
                     WHEN event_name LIKE 'wait/io/socket%'    THEN '"event_type": "io/socket"'
                     WHEN event_name LIKE 'wait/synch/mutex%'  THEN '"event_type": "synch/mutex"'
                     WHEN event_name LIKE 'wait/synch/cond%'   THEN '"event_type": "synch/cond"'
                     WHEN event_name LIKE 'wait/synch/rwlock%' THEN '"event_type": "synch/rwlock"'
                     WHEN event_name LIKE 'wait/lock%'         THEN '"event_type": "lock"'
                     WHEN event_name LIKE 'statement/%'        THEN '"event_type": "stmt"'
                     WHEN event_name LIKE 'stage/%'            THEN '"event_type": "stage"'
                     WHEN event_name LIKE '%idle%'             THEN '"event_type": "idle"'
                     ELSE ''
                END
            )
            , '}'
          )
          ORDER BY event_id ASC SEPARATOR ',') event
    INTO json_objects
    FROM (
          
          (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id,
                  CONCAT(sql_text, '\\n',
                         'errors: ', errors, '\\n',
                         'warnings: ', warnings, '\\n',
                         'lock time: ', ROUND(lock_time/1000000, 2),'us\\n',
                         'rows affected: ', rows_affected, '\\n',
                         'rows sent: ', rows_sent, '\\n',
                         'rows examined: ', rows_examined, '\\n',
                         'tmp tables: ', created_tmp_tables, '\\n',
                         'tmp disk tables: ', created_tmp_disk_tables, '\\n',
                         'select scan: ', select_scan, '\\n',
                         'select full join: ', select_full_join, '\\n',
                         'select full range join: ', select_full_range_join, '\\n',
                         'select range: ', select_range, '\\n',
                         'select range check: ', select_range_check, '\\n',
                         'sort merge passes: ', sort_merge_passes, '\\n',
                         'sort rows: ', sort_rows, '\\n',
                         'sort range: ', sort_range, '\\n',
                         'sort scan: ', sort_scan, '\\n',
                         'no index used: ', IF(no_index_used, 'TRUE', 'FALSE'), '\\n',
                         'no good index used: ', IF(no_good_index_used, 'TRUE', 'FALSE'), '\\n'
                         ) AS wait_info
             FROM performance_schema.events_statements_history_long WHERE thread_id = thd_id)
          UNION
          (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id, null AS wait_info
             FROM performance_schema.events_stages_history_long WHERE thread_id = thd_id)
          UNION 
          (SELECT thread_id, event_id,
                  CONCAT(event_name ,
                         IF(event_name NOT LIKE 'wait/synch/mutex%', IFNULL(CONCAT(' - ', operation), ''), ''),
                         IF(number_of_bytes IS NOT NULL, CONCAT(' ', number_of_bytes, ' bytes'), ''),
                         IF(event_name LIKE 'wait/io/file%', '\\n', ''),
                         IF(object_schema IS NOT NULL, CONCAT('\\nObject: ', object_schema, '.'), ''),
                         IF(object_name IS NOT NULL,
                            IF (event_name LIKE 'wait/io/socket%',
                                CONCAT(IF (object_name LIKE ':0%', @@socket, object_name)),
                                object_name),
                            ''),
                          IF(index_name IS NOT NULL, CONCAT(' Index: ', index_name), ''),'\\n'
                         ) AS event_name,
                  timer_wait, timer_start, nesting_event_id, source AS wait_info
             FROM performance_schema.events_waits_history_long WHERE thread_id = thd_id)) events
    ORDER BY event_id;
    RETURN CONCAT('{',
                  CONCAT_WS(',',
                            '"rankdir": "LR"',
                            '"nodesep": "0.10"',
                            CONCAT('"stack_created": "', NOW(), '"'),
                            CONCAT('"mysql_version": "', VERSION(), '"'),
                            CONCAT('"mysql_user": "', CURRENT_USER(), '"'),
                            CONCAT('"events": [', IFNULL(json_objects,''), ']')
                           ),
                  '}');
END{��x�"�K��	BEGIN
    DECLARE json_objects LONGTEXT;
    
    UPDATE performance_schema.threads
       SET instrumented = 'NO'
     WHERE processlist_id = CONNECTION_ID();
    
    SET SESSION group_concat_max_len=@@global.max_allowed_packet;
    SELECT GROUP_CONCAT(CONCAT( '{'
              , CONCAT_WS( ', '
              , CONCAT('"nesting_event_id": "', IF(nesting_event_id IS NULL, '0', nesting_event_id), '"')
              , CONCAT('"event_id": "', event_id, '"')
              , CONCAT( '"timer_wait": ', ROUND(timer_wait/1000000, 2))
              , CONCAT( '"event_info": "'
                  , CASE
                        WHEN event_name NOT LIKE 'wait/io%' THEN REPLACE(SUBSTRING_INDEX(event_name, '/', -2), '\', '\\')
                        WHEN event_name NOT LIKE 'wait/io/file%' OR event_name NOT LIKE 'wait/io/socket%' THEN REPLACE(SUBSTRING_INDEX(event_name, '/', -4), '\', '\\')
                        ELSE event_name
                    END
                  , '"'
              )
              , CONCAT( '"wait_info": "', IFNULL(wait_info, ''), '"')
              , CONCAT( '"source": "', IF(true AND event_name LIKE 'wait%', IFNULL(wait_info, ''), ''), '"')
              , CASE
                     WHEN event_name LIKE 'wait/io/file%'      THEN '"event_type": "io/file"'
                     WHEN event_name LIKE 'wait/io/table%'     THEN '"event_type": "io/table"'
                     WHEN event_name LIKE 'wait/io/socket%'    THEN '"event_type": "io/socket"'
                     WHEN event_name LIKE 'wait/synch/mutex%'  THEN '"event_type": "synch/mutex"'
                     WHEN event_name LIKE 'wait/synch/cond%'   THEN '"event_type": "synch/cond"'
                     WHEN event_name LIKE 'wait/synch/rwlock%' THEN '"event_type": "synch/rwlock"'
                     WHEN event_name LIKE 'wait/lock%'         THEN '"event_type": "lock"'
                     WHEN event_name LIKE 'statement/%'        THEN '"event_type": "stmt"'
                     WHEN event_name LIKE 'stage/%'            THEN '"event_type": "stage"'
                     WHEN event_name LIKE '%idle%'             THEN '"event_type": "idle"'
                     ELSE ''
                END
            )
            , '}'
          )
          ORDER BY event_id ASC SEPARATOR ',') event
    INTO json_objects
    FROM (
          
          (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id,
                  CONCAT(sql_text, '\n',
                         'errors: ', errors, '\n',
                         'warnings: ', warnings, '\n',
                         'lock time: ', ROUND(lock_time/1000000, 2),'us\n',
                         'rows affected: ', rows_affected, '\n',
                         'rows sent: ', rows_sent, '\n',
                         'rows examined: ', rows_examined, '\n',
                         'tmp tables: ', created_tmp_tables, '\n',
                         'tmp disk tables: ', created_tmp_disk_tables, '\n',
                         'select scan: ', select_scan, '\n',
                         'select full join: ', select_full_join, '\n',
                         'select full range join: ', select_full_range_join, '\n',
                         'select range: ', select_range, '\n',
                         'select range check: ', select_range_check, '\n',
                         'sort merge passes: ', sort_merge_passes, '\n',
                         'sort rows: ', sort_rows, '\n',
                         'sort range: ', sort_range, '\n',
                         'sort scan: ', sort_scan, '\n',
                         'no index used: ', IF(no_index_used, 'TRUE', 'FALSE'), '\n',
                         'no good index used: ', IF(no_good_index_used, 'TRUE', 'FALSE'), '\n'
                         ) AS wait_info
             FROM performance_schema.events_statements_history_long WHERE thread_id = thd_id)
          UNION
          (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id, null AS wait_info
             FROM performance_schema.events_stages_history_long WHERE thread_id = thd_id)
          UNION 
          (SELECT thread_id, event_id,
                  CONCAT(event_name ,
                         IF(event_name NOT LIKE 'wait/synch/mutex%', IFNULL(CONCAT(' - ', operation), ''), ''),
                         IF(number_of_bytes IS NOT NULL, CONCAT(' ', number_of_bytes, ' bytes'), ''),
                         IF(event_name LIKE 'wait/io/file%', '\n', ''),
                         IF(object_schema IS NOT NULL, CONCAT('\nObject: ', object_schema, '.'), ''),
                         IF(object_name IS NOT NULL,
                            IF (event_name LIKE 'wait/io/socket%',
                                CONCAT(IF (object_name LIKE ':0%', @@socket, object_name)),
                                object_name),
                            ''),
                          IF(index_name IS NOT NULL, CONCAT(' Index: ', index_name), ''),'\n'
                         ) AS event_name,
                  timer_wait, timer_start, nesting_event_id, source AS wait_info
             FROM performance_schema.events_waits_history_long WHERE thread_id = thd_id)) events
    ORDER BY event_id;
    RETURN CONCAT('{',
                  CONCAT_WS(',',
                            '"rankdir": "LR"',
                            '"nodesep": "0.10"',
                            CONCAT('"stack_created": "', NOW(), '"'),
                            CONCAT('"mysql_version": "', VERSION(), '"'),
                            CONCAT('"mysql_user": "', CURRENT_USER(), '"'),
                            CONCAT('"events": [', IFNULL(json_objects,''), ']')
                           ),
                  '}');
END^ɣ�Z"L��BEGIN
    DECLARE v_output LONGTEXT DEFAULT '{}';
    DECLARE v_msg_text TEXT DEFAULT '';
    DECLARE v_signal_msg TEXT DEFAULT '';
    DECLARE v_mysql_errno INT;
    DECLARE v_max_output_len BIGINT;
    DECLARE EXIT HANDLER FOR SQLWARNING, SQLEXCEPTION
    BEGIN
        GET DIAGNOSTICS CONDITION 1
            v_msg_text = MESSAGE_TEXT,
            v_mysql_errno = MYSQL_ERRNO;
        IF v_mysql_errno = 1260 THEN
            SET v_signal_msg = CONCAT('{ "error": "Trx info truncated: ', v_msg_text, '" }');
        ELSE
            SET v_signal_msg = CONCAT('{ "error": "', v_msg_text, '" }');
        END IF;
        RETURN v_signal_msg;
    END;
    IF (@sys.ps_thread_trx_info.max_length IS NULL) THEN
        SET @sys.ps_thread_trx_info.max_length = sys.sys_get_config('ps_thread_trx_info.max_length', 65535);
    END IF;
    IF (@sys.ps_thread_trx_info.max_length != @@session.group_concat_max_len) THEN
        SET @old_group_concat_max_len = @@session.group_concat_max_len;
        SET v_max_output_len = (@sys.ps_thread_trx_info.max_length - 5);
        SET SESSION group_concat_max_len = v_max_output_len;
    END IF;
    SET v_output = (
        SELECT CONCAT('[', IFNULL(GROUP_CONCAT(trx_info ORDER BY event_id), ''), '\n]') AS trx_info
          FROM (SELECT trxi.thread_id,
                       trxi.event_id,
                       GROUP_CONCAT(
                         IFNULL(
                           CONCAT('\n  {\n',
                                  '    "time": "', IFNULL(sys.format_time(trxi.timer_wait), ''), '",\n',
                                  '    "state": "', IFNULL(trxi.state, ''), '",\n',
                                  '    "mode": "', IFNULL(trxi.access_mode, ''), '",\n',
                                  '    "autocommitted": "', IFNULL(trxi.autocommit, ''), '",\n',
                                  '    "gtid": "', IFNULL(trxi.gtid, ''), '",\n',
                                  '    "isolation": "', IFNULL(trxi.isolation_level, ''), '",\n',
                                  '    "statements_executed": [', IFNULL(s.stmts, ''), IF(s.stmts IS NULL, ' ]\n', '\n    ]\n'),
                                  '  }'
                           ),
                           '')
                         ORDER BY event_id) AS trx_info
                  FROM (
                        (SELECT thread_id, event_id, timer_wait, state,access_mode, autocommit, gtid, isolation_level
                           FROM performance_schema.events_transactions_current
                          WHERE thread_id = in_thread_id
                            AND end_event_id IS NULL)
                        UNION
                        (SELECT thread_id, event_id, timer_wait, state,access_mode, autocommit, gtid, isolation_level
                           FROM performance_schema.events_transactions_history
                          WHERE thread_id = in_thread_id)
                       ) AS trxi
                  LEFT JOIN (SELECT thread_id,
                                    nesting_event_id,
                                    GROUP_CONCAT(
                                      IFNULL(
                                        CONCAT('\n      {\n',
                                               '        "sql_text": "', IFNULL(sys.format_statement(REPLACE(sql_text, '\', '\\')), ''), '",\n',
                                               '        "time": "', IFNULL(sys.format_time(timer_wait), ''), '",\n',
                                               '        "schema": "', IFNULL(current_schema, ''), '",\n',
                                               '        "rows_examined": ', IFNULL(rows_examined, ''), ',\n',
                                               '        "rows_affected": ', IFNULL(rows_affected, ''), ',\n',
                                               '        "rows_sent": ', IFNULL(rows_sent, ''), ',\n',
                                               '        "tmp_tables": ', IFNULL(created_tmp_tables, ''), ',\n',
                                               '        "tmp_disk_tables": ', IFNULL(created_tmp_disk_tables, ''), ',\n',
                                               '        "sort_rows": ', IFNULL(sort_rows, ''), ',\n',
                                               '        "sort_merge_passes": ', IFNULL(sort_merge_passes, ''), '\n',
                                               '      }'), '') ORDER BY event_id) AS stmts
                               FROM performance_schema.events_statements_history
                              WHERE sql_text IS NOT NULL
                                AND thread_id = in_thread_id
                              GROUP BY thread_id, nesting_event_id
                            ) AS s
                    ON trxi.thread_id = s.thread_id
                   AND trxi.event_id = s.nesting_event_id
                 WHERE trxi.thread_id = in_thread_id
                 GROUP BY trxi.thread_id, trxi.event_id
                ) trxs
          GROUP BY thread_id
    );
    IF (@old_group_concat_max_len IS NOT NULL) THEN
        SET SESSION group_concat_max_len = @old_group_concat_max_len;
    END IF;
    RETURN v_output;
END-%�1��OBEGIN
    DECLARE v_start, v_runtime, v_iter_start, v_sleep DECIMAL(20,2) DEFAULT 0.0;
    DECLARE v_has_innodb, v_has_ndb, v_has_ps, v_has_replication, v_has_ps_replication VARCHAR(8) CHARSET utf8 DEFAULT 'NO';
    DECLARE v_this_thread_enabled, v_has_ps_vars, v_has_metrics ENUM('YES', 'NO');
    DECLARE v_table_name, v_banner VARCHAR(64) CHARSET utf8;
    DECLARE v_sql_status_summary_select, v_sql_status_summary_delta, v_sql_status_summary_from, v_no_delta_names TEXT;
    DECLARE v_output_time, v_output_time_prev DECIMAL(20,3) UNSIGNED;
    DECLARE v_output_count, v_count, v_old_group_concat_max_len INT UNSIGNED DEFAULT 0;
    DECLARE v_status_summary_width TINYINT UNSIGNED DEFAULT 50;
    DECLARE v_done BOOLEAN DEFAULT FALSE;
    DECLARE c_ndbinfo CURSOR FOR
        SELECT TABLE_NAME
          FROM information_schema.TABLES
         WHERE TABLE_SCHEMA = 'ndbinfo'
               AND TABLE_NAME NOT IN (
                  'blocks',
                  'config_params',
                  'dict_obj_types',
                  'disk_write_speed_base',
                  'memory_per_fragment',
                  'memoryusage',
                  'operations_per_fragment',
                  'threadblocks'
               );
    DECLARE c_sysviews_w_delta CURSOR FOR
        SELECT table_name
          FROM tmp_sys_views_delta
         ORDER BY table_name;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    SELECT INSTRUMENTED INTO v_this_thread_enabled FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    END IF;
    IF (in_max_runtime < in_interval) THEN
        SIGNAL SQLSTATE '45000'
           SET MESSAGE_TEXT = 'in_max_runtime must be greater than or equal to in_interval';
    END IF;
    IF (in_max_runtime = 0) THEN
        SIGNAL SQLSTATE '45000'
           SET MESSAGE_TEXT = 'in_max_runtime must be greater than 0';
    END IF;
    IF (in_interval = 0) THEN
        SIGNAL SQLSTATE '45000'
           SET MESSAGE_TEXT = 'in_interval must be greater than 0';
    END IF;
    IF (@sys.diagnostics.allow_i_s_tables IS NULL) THEN
        SET @sys.diagnostics.allow_i_s_tables = sys.sys_get_config('diagnostics.allow_i_s_tables', 'OFF');
    END IF;
    IF (@sys.diagnostics.include_raw IS NULL) THEN
        SET @sys.diagnostics.include_raw      = sys.sys_get_config('diagnostics.include_raw'     , 'OFF');
    END IF;
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug                        = sys.sys_get_config('debug'                       , 'OFF');
    END IF;
    IF (@sys.statement_truncate_len IS NULL) THEN
        SET @sys.statement_truncate_len       = sys.sys_get_config('statement_truncate_len'      , '64' );
    END IF;
    SET @log_bin := @@sql_log_bin;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = 0;
    END IF;
    SET v_no_delta_names = CONCAT('s%{COUNT}.Variable_name NOT IN (',
        '''innodb_buffer_pool_pages_total'', ',
        '''innodb_page_size'', ',
        '''last_query_cost'', ',
        '''last_query_partial_plans'', ',
        '''qcache_total_blocks'', ',
        '''slave_last_heartbeat'', ',
        '''ssl_ctx_verify_depth'', ',
        '''ssl_ctx_verify_mode'', ',
        '''ssl_session_cache_size'', ',
        '''ssl_verify_depth'', ',
        '''ssl_verify_mode'', ',
        '''ssl_version'', ',
        '''buffer_flush_lsn_avg_rate'', ',
        '''buffer_flush_pct_for_dirty'', ',
        '''buffer_flush_pct_for_lsn'', ',
        '''buffer_pool_pages_total'', ',
        '''lock_row_lock_time_avg'', ',
        '''lock_row_lock_time_max'', ',
        '''innodb_page_size''',
    ')');
    IF (in_auto_config <> 'current') THEN
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('Updating Performance Schema configuration to ', in_auto_config) AS 'Debug';
        END IF;
        CALL sys.ps_setup_save(0);
        IF (in_auto_config = 'medium') THEN
            UPDATE performance_schema.setup_consumers
                SET ENABLED = 'YES'
            WHERE NAME NOT LIKE '%\_history%';
            UPDATE performance_schema.setup_instruments
                SET ENABLED = 'YES',
                    TIMED   = 'YES'
            WHERE NAME NOT LIKE 'wait/synch/%';
        ELSEIF (in_auto_config = 'full') THEN
            UPDATE performance_schema.setup_consumers
                SET ENABLED = 'YES';
            UPDATE performance_schema.setup_instruments
                SET ENABLED = 'YES',
                    TIMED   = 'YES';
        END IF;
        UPDATE performance_schema.threads
           SET INSTRUMENTED = 'YES'
         WHERE PROCESSLIST_ID <> CONNECTION_ID();
    END IF;
    SET v_start        = UNIX_TIMESTAMP(NOW(2)),
        in_interval    = IFNULL(in_interval, 30),
        in_max_runtime = IFNULL(in_max_runtime, 60);
    SET v_banner = REPEAT(
                      '-',
                      LEAST(
                         GREATEST(
                            36,
                            CHAR_LENGTH(VERSION()),
                            CHAR_LENGTH(@@global.version_comment),
                            CHAR_LENGTH(@@global.version_compile_os),
                            CHAR_LENGTH(@@global.version_compile_machine),
                            CHAR_LENGTH(@@global.socket),
                            CHAR_LENGTH(@@global.datadir)
                         ),
                         64
                      )
                   );
    SELECT 'Hostname' AS 'Name', @@global.hostname AS 'Value'
    UNION ALL
    SELECT 'Port' AS 'Name', @@global.port AS 'Value'
    UNION ALL
    SELECT 'Socket' AS 'Name', @@global.socket AS 'Value'
    UNION ALL
    SELECT 'Datadir' AS 'Name', @@global.datadir AS 'Value'
    UNION ALL
    SELECT REPEAT('-', 23) AS 'Name', v_banner AS 'Value'
    UNION ALL
    SELECT 'MySQL Version' AS 'Name', VERSION() AS 'Value'
    UNION ALL
    SELECT 'Sys Schema Version' AS 'Name', (SELECT sys_version FROM sys.version) AS 'Value'
    UNION ALL
    SELECT 'Version Comment' AS 'Name', @@global.version_comment AS 'Value'
    UNION ALL
    SELECT 'Version Compile OS' AS 'Name', @@global.version_compile_os AS 'Value'
    UNION ALL
    SELECT 'Version Compile Machine' AS 'Name', @@global.version_compile_machine AS 'Value'
    UNION ALL
    SELECT REPEAT('-', 23) AS 'Name', v_banner AS 'Value'
    UNION ALL
    SELECT 'UTC Time' AS 'Name', UTC_TIMESTAMP() AS 'Value'
    UNION ALL
    SELECT 'Local Time' AS 'Name', NOW() AS 'Value'
    UNION ALL
    SELECT 'Time Zone' AS 'Name', @@global.time_zone AS 'Value'
    UNION ALL
    SELECT 'System Time Zone' AS 'Name', @@global.system_time_zone AS 'Value'
    UNION ALL
    SELECT 'Time Zone Offset' AS 'Name', TIMEDIFF(NOW(), UTC_TIMESTAMP()) AS 'Value';
    SET v_has_innodb         = IFNULL((SELECT SUPPORT FROM information_schema.ENGINES WHERE ENGINE = 'InnoDB'), 'NO'),
        v_has_ndb            = IFNULL((SELECT SUPPORT FROM information_schema.ENGINES WHERE ENGINE = 'NDBCluster'), 'NO'),
        v_has_ps             = IFNULL((SELECT SUPPORT FROM information_schema.ENGINES WHERE ENGINE = 'PERFORMANCE_SCHEMA'), 'NO'),
        v_has_ps_replication = IF(v_has_ps = 'YES'
                                   AND EXISTS(SELECT 1 FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'replication_applier_status'),
                                   'YES',
                                   'NO'
                               ),
        v_has_replication    = 'MAYBE',
        v_has_metrics        = IF(v_has_ps = 'YES' OR (sys.version_major() = 5 AND sys.version_minor() = 6), 'YES', 'NO'),
        v_has_ps_vars        = 'NO';
    
    
    IF (@sys.debug = 'ON') THEN
       SELECT v_has_innodb AS 'Has_InnoDB', v_has_ndb AS 'Has_NDBCluster',
              v_has_ps AS 'Has_Performance_Schema', v_has_ps_vars AS 'Has_P_S_SHOW_Variables',
              v_has_metrics AS 'Has_metrics',
              v_has_ps_replication 'AS Has_P_S_Replication', v_has_replication AS 'Has_Replication';
    END IF;
    IF (v_has_innodb IN ('DEFAULT', 'YES')) THEN
        SET @sys.diagnostics.sql = 'SHO>,�)��OW ENGINE InnoDB STATUS';
        PREPARE stmt_innodb_status FROM @sys.diagnostics.sql;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SET @sys.diagnostics.sql = 'SHOW ENGINE PERFORMANCE_SCHEMA STATUS';
        PREPARE stmt_ps_status FROM @sys.diagnostics.sql;
    END IF;
    IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
        SET @sys.diagnostics.sql = 'SHOW ENGINE NDBCLUSTER STATUS';
        PREPARE stmt_ndbcluster_status FROM @sys.diagnostics.sql;
    END IF;
    SET @sys.diagnostics.sql_gen_query_template = 'SELECT CONCAT(
           ''SELECT '',
           GROUP_CONCAT(
               CASE WHEN (SUBSTRING(TABLE_NAME, 3), COLUMN_NAME) IN (
                                (''io_global_by_file_by_bytes'', ''total''),
                                (''io_global_by_wait_by_bytes'', ''total_requested'')
                         )
                         THEN CONCAT(''sys.format_bytes('', COLUMN_NAME, '') AS '', COLUMN_NAME)
                    WHEN SUBSTRING(COLUMN_NAME, -8) = ''_latency''
                         THEN CONCAT(''sys.format_time('', COLUMN_NAME, '') AS '', COLUMN_NAME)
                    WHEN SUBSTRING(COLUMN_NAME, -7) = ''_memory'' OR SUBSTRING(COLUMN_NAME, -17) = ''_memory_allocated''
                         OR ((SUBSTRING(COLUMN_NAME, -5) = ''_read'' OR SUBSTRING(COLUMN_NAME, -8) = ''_written'' OR SUBSTRING(COLUMN_NAME, -6) = ''_write'') AND SUBSTRING(COLUMN_NAME, 1, 6) <> ''COUNT_'')
                         THEN CONCAT(''sys.format_bytes('', COLUMN_NAME, '') AS '', COLUMN_NAME)
                    ELSE COLUMN_NAME
               END
               ORDER BY ORDINAL_POSITION
               SEPARATOR '',\n       ''
           ),
           ''\n  FROM tmp_'', SUBSTRING(TABLE_NAME FROM 3), ''_%{OUTPUT}''
       ) AS Query INTO @sys.diagnostics.sql_select
  FROM information_schema.COLUMNS
 WHERE TABLE_SCHEMA = ''sys'' AND TABLE_NAME = ?
 GROUP BY TABLE_NAME';
    SET @sys.diagnostics.sql_gen_query_delta = 'SELECT CONCAT(
           ''SELECT '',
           GROUP_CONCAT(
               CASE WHEN FIND_IN_SET(COLUMN_NAME, diag.pk)
                         THEN COLUMN_NAME
                    WHEN diag.TABLE_NAME = ''io_global_by_file_by_bytes'' AND COLUMN_NAME = ''write_pct''
                         THEN CONCAT(''IFNULL(ROUND(100-(((e.total_read-IFNULL(s.total_read, 0))'',
                                     ''/NULLIF(((e.total_read-IFNULL(s.total_read, 0))+(e.total_written-IFNULL(s.total_written, 0))), 0))*100), 2), 0.00) AS '',
                                     COLUMN_NAME)
                    WHEN (diag.TABLE_NAME, COLUMN_NAME) IN (
                                (''io_global_by_file_by_bytes'', ''total''),
                                (''io_global_by_wait_by_bytes'', ''total_requested'')
                         )
                         THEN CONCAT(''sys.format_bytes(e.'', COLUMN_NAME, ''-IFNULL(s.'', COLUMN_NAME, '', 0)) AS '', COLUMN_NAME)
                    WHEN SUBSTRING(COLUMN_NAME, 1, 4) IN (''max_'', ''min_'') AND SUBSTRING(COLUMN_NAME, -8) = ''_latency''
                         THEN CONCAT(''sys.format_time(e.'', COLUMN_NAME, '') AS '', COLUMN_NAME)
                    WHEN COLUMN_NAME = ''avg_latency''
                         THEN CONCAT(''sys.format_time((e.total_latency - IFNULL(s.total_latency, 0))'',
                                     ''/NULLIF(e.total - IFNULL(s.total, 0), 0)) AS '', COLUMN_NAME)
                    WHEN SUBSTRING(COLUMN_NAME, -12) = ''_avg_latency''
                         THEN CONCAT(''sys.format_time((e.'', SUBSTRING(COLUMN_NAME FROM 1 FOR CHAR_LENGTH(COLUMN_NAME)-12), ''_latency - IFNULL(s.'', SUBSTRING(COLUMN_NAME FROM 1 FOR CHAR_LENGTH(COLUMN_NAME)-12), ''_latency, 0))'',
                                     ''/NULLIF(e.'', SUBSTRING(COLUMN_NAME FROM 1 FOR CHAR_LENGTH(COLUMN_NAME)-12), ''s - IFNULL(s.'', SUBSTRING(COLUMN_NAME FROM 1 FOR CHAR_LENGTH(COLUMN_NAME)-12), ''s, 0), 0)) AS '', COLUMN_NAME)
                    WHEN SUBSTRING(COLUMN_NAME, -8) = ''_latency''
                         THEN CONCAT(''sys.format_time(e.'', COLUMN_NAME, '' - IFNULL(s.'', COLUMN_NAME, '', 0)) AS '', COLUMN_NAME)
                    WHEN COLUMN_NAME IN (''avg_read'', ''avg_write'', ''avg_written'')
                         THEN CONCAT(''sys.format_bytes(IFNULL((e.total_'', IF(COLUMN_NAME = ''avg_read'', ''read'', ''written''), ''-IFNULL(s.total_'', IF(COLUMN_NAME = ''avg_read'', ''read'', ''written''), '', 0))'',
                                     ''/NULLIF(e.count_'', IF(COLUMN_NAME = ''avg_read'', ''read'', ''write''), ''-IFNULL(s.count_'', IF(COLUMN_NAME = ''avg_read'', ''read'', ''write''), '', 0), 0), 0)) AS '',
                                     COLUMN_NAME)
                    WHEN SUBSTRING(COLUMN_NAME, -7) = ''_memory'' OR SUBSTRING(COLUMN_NAME, -17) = ''_memory_allocated''
                         OR ((SUBSTRING(COLUMN_NAME, -5) = ''_read'' OR SUBSTRING(COLUMN_NAME, -8) = ''_written'' OR SUBSTRING(COLUMN_NAME, -6) = ''_write'') AND SUBSTRING(COLUMN_NAME, 1, 6) <> ''COUNT_'')
                         THEN CONCAT(''sys.format_bytes(e.'', COLUMN_NAME, '' - IFNULL(s.'', COLUMN_NAME, '', 0)) AS '', COLUMN_NAME)
                    ELSE CONCAT(''(e.'', COLUMN_NAME, '' - IFNULL(s.'', COLUMN_NAME, '', 0)) AS '', COLUMN_NAME)
               END
               ORDER BY ORDINAL_POSITION
               SEPARATOR '',\n       ''
           ),
           ''\n  FROM tmp_'', diag.TABLE_NAME, ''_end e
       LEFT OUTER JOIN tmp_'', diag.TABLE_NAME, ''_start s USING ('', diag.pk, '')''
       ) AS Query INTO @sys.diagnostics.sql_select
  FROM tmp_sys_views_delta diag
       INNER JOIN information_schema.COLUMNS c ON c.TABLE_NAME = CONCAT(''x$'', diag.TABLE_NAME)
 WHERE c.TABLE_SCHEMA = ''sys'' AND diag.TABLE_NAME = ?
 GROUP BY diag.TABLE_NAME';
    IF (v_has_ps = 'YES') THEN
        DROP TEMPORARY TABLE IF EXISTS tmp_sys_views_delta;
        CREATE TEMPORARY TABLE tmp_sys_views_delta (
            TABLE_NAME varchar(64) NOT NULL,
            order_by text COMMENT 'ORDER BY clause for the initial and overall views',
            order_by_delta text COMMENT 'ORDER BY clause for the delta views',
            where_delta text COMMENT 'WHERE clause to use for delta views to only include rows with a "count" > 0',
            limit_rows int unsigned COMMENT 'The maximum number of rows to include for the view',
            pk varchar(128) COMMENT 'Used with the FIND_IN_SET() function so use comma separated list without whitespace',
            PRIMARY KEY (TABLE_NAME)
        );
        IF (@sys.debug = 'ON') THEN
            SELECT 'Populating tmp_sys_views_delta' AS 'Debug';
        END IF;
        INSERT INTO tmp_sys_views_delta
        VALUES ('host_summary'                       , '%{TABLE}.statement_latency DESC',
                                                       '(e.statement_latency-IFNULL(s.statement_latency, 0)) DESC',
                                                       '(e.statements - IFNULL(s.statements, 0)) > 0', NULL, 'host'),
               ('host_summary_by_file_io'            , '%{TABLE}.io_latency DESC',
                                                       '(e.io_latency-IFNULL(s.io_latency, 0)) DESC',
                                                       '(e.ios - IFNULL(s.ios, 0)) > 0', NULL, 'host'),
               ('host_summary_by_file_io_type'       , '%{TABLE}.host, %{TABLE}.total_latency DESC',
                                                       'e.host, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host,event_name'),
               ('host_summary_by_stages'             , '%{TABLE}.host, %{TABLE}.total_latency DESC',
                                                       'e.host, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host,event_name'),
               ('host_summary_by_statement_latency'  , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
    =����O                                                   '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host'),
               ('host_summary_by_statement_type'     , '%{TABLE}.host, %{TABLE}.total_latency DESC',
                                                       'e.host, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host,statement'),
               ('io_by_thread_by_latency'            , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,thread_id,processlist_id'),
               ('io_global_by_file_by_bytes'         , '%{TABLE}.total DESC',
                                                       '(e.total-IFNULL(s.total, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', 100, 'file'),
               ('io_global_by_file_by_latency'       , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', 100, 'file'),
               ('io_global_by_wait_by_bytes'         , '%{TABLE}.total_requested DESC',
                                                       '(e.total_requested-IFNULL(s.total_requested, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'event_name'),
               ('io_global_by_wait_by_latency'       , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'event_name'),
               ('schema_index_statistics'            , '(%{TABLE}.select_latency+%{TABLE}.insert_latency+%{TABLE}.update_latency+%{TABLE}.delete_latency) DESC',
                                                       '((e.select_latency+e.insert_latency+e.update_latency+e.delete_latency)-IFNULL(s.select_latency+s.insert_latency+s.update_latency+s.delete_latency, 0)) DESC',
                                                       '((e.rows_selected+e.insert_latency+e.rows_updated+e.rows_deleted)-IFNULL(s.rows_selected+s.rows_inserted+s.rows_updated+s.rows_deleted, 0)) > 0',
                                                       100, 'table_schema,table_name,index_name'),
               ('schema_table_statistics'            , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) > 0', 100, 'table_schema,table_name'),
               ('schema_tables_with_full_table_scans', '%{TABLE}.rows_full_scanned DESC',
                                                       '(e.rows_full_scanned-IFNULL(s.rows_full_scanned, 0)) DESC',
                                                       '(e.rows_full_scanned-IFNULL(s.rows_full_scanned, 0)) > 0', 100, 'object_schema,object_name'),
               ('user_summary'                       , '%{TABLE}.statement_latency DESC',
                                                       '(e.statement_latency-IFNULL(s.statement_latency, 0)) DESC',
                                                       '(e.statements - IFNULL(s.statements, 0)) > 0', NULL, 'user'),
               ('user_summary_by_file_io'            , '%{TABLE}.io_latency DESC',
                                                       '(e.io_latency-IFNULL(s.io_latency, 0)) DESC',
                                                       '(e.ios - IFNULL(s.ios, 0)) > 0', NULL, 'user'),
               ('user_summary_by_file_io_type'       , '%{TABLE}.user, %{TABLE}.latency DESC',
                                                       'e.user, (e.latency-IFNULL(s.latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,event_name'),
               ('user_summary_by_stages'             , '%{TABLE}.user, %{TABLE}.total_latency DESC',
                                                       'e.user, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,event_name'),
               ('user_summary_by_statement_latency'  , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user'),
               ('user_summary_by_statement_type'     , '%{TABLE}.user, %{TABLE}.total_latency DESC',
                                                       'e.user, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,statement'),
               ('wait_classes_global_by_avg_latency' , 'IFNULL(%{TABLE}.total_latency / NULLIF(%{TABLE}.total, 0), 0) DESC',
                                                       'IFNULL((e.total_latency-IFNULL(s.total_latency, 0)) / NULLIF((e.total - IFNULL(s.total, 0)), 0), 0) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'event_class'),
               ('wait_classes_global_by_latency'     , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'event_class'),
               ('waits_by_host_by_latency'           , '%{TABLE}.host, %{TABLE}.total_latency DESC',
                                                       'e.host, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host,event'),
               ('waits_by_user_by_latency'           , '%{TABLE}.user, %{TABLE}.total_latency DESC',
                                                       'e.user, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,event'),
               ('waits_global_by_latency'            , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'events')
        ;
    END IF;
    SELECT '
=======================
     Configuration
=======================
' AS '';
    SELECT 'GLOBAL VARIABLES' AS 'The following output is:';
    IF (v_has_ps_vars = 'YES') THEN
        SELECT LOWER(VARIABLE_NAME) AS Variable_name, VARIABLE_VALUE AS Variable_value FROM performance_schema.global_variables ORDER BY VARIABLE_NAME;
    ELSE
        SELECT LOWER(VARIABLE_NAME) AS Variable_name, VARIABLE_VALUE AS Variable_value FROM information_schema.GLOBAL_VARIABLES ORDER BY VARIABLE_NAME;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SELECT 'Performance Schema Setup - Actors' AS 'The following output is:';
        SELECT * FROM performance_schema.setup_actors;
        SELECT 'Performance Schema Setup - Consumers' AS 'The following output is:';
        SELECT NAME AS Consumer, ENABLED, sys.ps_is_consumer_enabled(NAME) AS COLLECTS
          FROM performance_schema.setup_consumers;
        SELECT 'Performance Schema Setup - Instruments' AS 'The following output is:';
        SELECT SUBSTRING_INDEX(NAME, '/', 2) AS 'InstrumentClass',
               ROUND(100*SUM(IF(ENABLED = 'YES', 1, 0))/COUNT(*), 2) AS 'EnabledPct',
               ROUND(100*SUM(IF(TIMED = 'YES', 1, 0))/COUNT(*), 2) AS 'TimedPct'
          FROM perform��j'��Oance_schema.setup_instruments
         GROUP BY SUBSTRING_INDEX(NAME, '/', 2)
         ORDER BY SUBSTRING_INDEX(NAME, '/', 2);
        SELECT 'Performance Schema Setup - Objects' AS 'The following output is:';
        SELECT * FROM performance_schema.setup_objects;
        SELECT 'Performance Schema Setup - Threads' AS 'The following output is:';
        SELECT `TYPE` AS ThreadType, COUNT(*) AS 'Total', ROUND(100*SUM(IF(INSTRUMENTED = 'YES', 1, 0))/COUNT(*), 2) AS 'InstrumentedPct'
          FROM performance_schema.threads
        GROUP BY TYPE;
    END IF;
    IF (v_has_replication = 'NO') THEN
        SELECT 'No Replication Configured' AS 'Replication Status';
    ELSE
        SELECT CONCAT('Replication Configured: ', v_has_replication, ' - Performance Schema Replication Tables: ', v_has_ps_replication) AS 'Replication Status';
        IF (v_has_ps_replication = 'YES') THEN
            SELECT 'Replication - Connection Configuration' AS 'The following output is:';
            SELECT * FROM performance_schema.replication_connection_configuration;
        END IF;
        IF (v_has_ps_replication = 'YES') THEN
            SELECT 'Replication - Applier Configuration' AS 'The following output is:';
            SELECT * FROM performance_schema.replication_applier_configuration ORDER BY CHANNEL_NAME;
        END IF;
    END IF;
    IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
       SELECT 'Cluster Thread Blocks' AS 'The following output is:';
       SELECT * FROM ndbinfo.threadblocks;
    END IF;
    IF (v_has_ps = 'YES') THEN
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            SELECT '
========================
     Initial Status
========================
' AS '';
        END IF;
        DROP TEMPORARY TABLE IF EXISTS tmp_digests_start;
        CALL sys.statement_performance_analyzer('create_tmp', 'tmp_digests_start', NULL);
        CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
        CALL sys.statement_performance_analyzer('save', 'tmp_digests_start', NULL);
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            SET @sys.diagnostics.sql = REPLACE(@sys.diagnostics.sql_gen_query_template, '%{OUTPUT}', 'start');
            IF (@sys.debug = 'ON') THEN
                SELECT 'The following query will be used to generate the query for each sys view' AS 'Debug';
                SELECT @sys.diagnostics.sql AS 'Debug';
            END IF;
            PREPARE stmt_gen_query FROM @sys.diagnostics.sql;
        END IF;
        SET v_done = FALSE;
        OPEN c_sysviews_w_delta;
        c_sysviews_w_delta_loop: LOOP
            FETCH c_sysviews_w_delta INTO v_table_name;
            IF v_done THEN
                LEAVE c_sysviews_w_delta_loop;
            END IF;
            IF (@sys.debug = 'ON') THEN
                SELECT CONCAT('The following queries are for storing the initial content of ', v_table_name) AS 'Debug';
            END IF;
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS `tmp_', v_table_name, '_start`'));
            CALL sys.execute_prepared_stmt(CONCAT('CREATE TEMPORARY TABLE `tmp_', v_table_name, '_start` SELECT * FROM `sys`.`x$', v_table_name, '`'));
            IF (@sys.diagnostics.include_raw = 'ON') THEN
                SET @sys.diagnostics.table_name = CONCAT('x$', v_table_name);
                EXECUTE stmt_gen_query USING @sys.diagnostics.table_name;
                SELECT CONCAT(@sys.diagnostics.sql_select,
                              IF(order_by IS NOT NULL, CONCAT('\n ORDER BY ', REPLACE(order_by, '%{TABLE}', CONCAT('tmp_', v_table_name, '_start'))), ''),
                              IF(limit_rows IS NOT NULL, CONCAT('\n LIMIT ', limit_rows), '')
                       )
                  INTO @sys.diagnostics.sql_select
                  FROM tmp_sys_views_delta
                 WHERE TABLE_NAME = v_table_name;
                SELECT CONCAT('Initial ', v_table_name) AS 'The following output is:';
                CALL sys.execute_prepared_stmt(@sys.diagnostics.sql_select);
            END IF;
        END LOOP;
        CLOSE c_sysviews_w_delta;
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            DEALLOCATE PREPARE stmt_gen_query;
        END IF;
    END IF;
    SET v_sql_status_summary_select = 'SELECT Variable_name',
        v_sql_status_summary_delta  = '',
        v_sql_status_summary_from   = '';
    REPEAT
        SET v_output_count = v_output_count + 1;
        IF (v_output_count > 1) THEN
            SET v_sleep = in_interval-(UNIX_TIMESTAMP(NOW(2))-v_iter_start);
            SELECT NOW() AS 'Time', CONCAT('Going to sleep for ', v_sleep, ' seconds. Please do not interrupt') AS 'The following output is:';
            DO SLEEP(in_interval);
        END IF;
        SET v_iter_start = UNIX_TIMESTAMP(NOW(2));
        SELECT NOW(), CONCAT('Iteration Number ', IFNULL(v_output_count, 'NULL')) AS 'The following output is:';
        IF (@@log_bin = 1) THEN
            SELECT 'SHOW MASTER STATUS' AS 'The following output is:';
            SHOW MASTER STATUS;
        END IF;
        IF (v_has_replication <> 'NO') THEN
            SELECT 'SHOW SLAVE STATUS' AS 'The following output is:';
            SHOW SLAVE STATUS;
        END IF;
        SET v_table_name = CONCAT('tmp_metrics_', v_output_count);
        CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS ', v_table_name));
        CALL sys.execute_prepared_stmt(CONCAT('CREATE TEMPORARY TABLE ', v_table_name, ' (
  Variable_name VARCHAR(193) NOT NULL,
  Variable_value VARCHAR(1024),
  Type VARCHAR(100) NOT NULL,
  Enabled ENUM(''YES'', ''NO'', ''PARTIAL'') NOT NULL,
  PRIMARY KEY (Type, Variable_name)
) ENGINE = InnoDB DEFAULT CHARSET=utf8'));
        IF (v_has_metrics) THEN
            SET @sys.diagnostics.sql = CONCAT(
                'INSERT INTO ', v_table_name,
                ' SELECT Variable_name, REPLACE(Variable_value, ''\n'', ''\\\\n'') AS Variable_value, Type, Enabled FROM sys.metrics'
            );
        ELSE
            SET @sys.diagnostics.sql = CONCAT(
                'INSERT INTO ', v_table_name,
                '(SELECT LOWER(VARIABLE_NAME) AS Variable_name, REPLACE(VARIABLE_VALUE, ''\n'', ''\\\\n'') AS Variable_value,
                         ''Global Status'' AS Type, ''YES'' AS Enabled
  FROM performance_schema.global_status
) UNION ALL (
SELECT NAME AS Variable_name, COUNT AS Variable_value,
       CONCAT(''InnoDB Metrics - '', SUBSYSTEM) AS Type,
       IF(STATUS = ''enabled'', ''YES'', ''NO'') AS Enabled
  FROM information_schema.INNODB_METRICS
 WHERE NAME NOT IN (
     ''lock_row_lock_time'', ''lock_row_lock_time_avg'', ''lock_row_lock_time_max'', ''lock_row_lock_waits'',
     ''buffer_pool_reads'', ''buffer_pool_read_requests'', ''buffer_pool_write_requests'', ''buffer_pool_wait_free'',
     ''buffer_pool_read_ahead'', ''buffer_pool_read_ahead_evicted'', ''buffer_pool_pages_total'', ''buffer_pool_pages_misc'',
     ''buffer_pool_pages_data'', ''buffer_pool_bytes_data'', ''buffer_pool_pages_dirty'', ''buffer_pool_bytes_dirty'',
     ''buffer_pool_pages_free'', ''buffer_pages_created'', ''buffer_pages_written'', ''buffer_pages_read'',
     ''buffer_data_reads'', ''buffer_data_written'', ''file_num_open_files'',
     ''os_log_bytes_written'', ''os_log_fsyncs'', ''os_log_pending_fsyncs'', ''os_log_pending_writes'',
     ''log_waits'', ''log_write_requests'', ''log_writes'', ''innodb_dblwr_writes'', ''innodb_dblwr_pages_written'', ''innodb_page_size'')
) UNION ALL (
SELECT ''NOW()'' AS Variable_name, NOW(3) AS Variable_value, ''System Time'' AS Type, ''YES'' AS Enabled
) UNION ALL (
SELECT ''UNIX_TIMESTAMP()'' AS Variable_name, ROUND(UNIX_TIMESTAMP(NOW(3)), 3) AS Variable_value, ''System Time'' AS Type, ''YES'' AS Enabled
)
 ORDER BY Type, Variable_name;'
            );
        END IF;
        CALL sys.execute_prepared_stmt(@sys.diagnostics.sql);
        CALL sys.execute_prepared_stmt(
            CONCAT('SELECT Variable_value INTO @sys.diagnostics.output_time FROM ', v_table_name, ' WHERE Type = ''System Time'' AND Variable_name = ''UNIX_TIMESTAMP()''')
        );
        SET v_output_time = @sys.diagnostics.output_time;
        SEn��7��OT v_sql_status_summary_select = CONCAT(v_sql_status_summary_select, ',
       CONCAT(
           LEFT(s', v_output_count, '.Variable_value, ', v_status_summary_width, '),
           IF(', REPLACE(v_no_delta_names, '%{COUNT}', v_output_count), ' AND s', v_output_count, '.Variable_value REGEXP ''^[0-9]+(\\\\.[0-9]+)?$'', CONCAT('' ('', ROUND(s', v_output_count, '.Variable_value/', v_output_time, ', 2), ''/sec)''), '''')
       ) AS ''Output ', v_output_count, ''''),
            v_sql_status_summary_from   = CONCAT(v_sql_status_summary_from, '
',
                                                    IF(v_output_count = 1, '  FROM ', '       INNER JOIN '),
                                                    v_table_name, ' s', v_output_count,
                                                    IF (v_output_count = 1, '', ' USING (Type, Variable_name)'));
        IF (v_output_count > 1) THEN
            SET v_sql_status_summary_delta  = CONCAT(v_sql_status_summary_delta, ',
       IF(', REPLACE(v_no_delta_names, '%{COUNT}', v_output_count), ' AND s', (v_output_count-1), '.Variable_value REGEXP ''^[0-9]+(\\\\.[0-9]+)?$'' AND s', v_output_count, '.Variable_value REGEXP ''^[0-9]+(\\\\.[0-9]+)?$'',
          CONCAT(IF(s', (v_output_count-1), '.Variable_value REGEXP ''^[0-9]+\\\\.[0-9]+$'' OR s', v_output_count, '.Variable_value REGEXP ''^[0-9]+\\\\.[0-9]+$'',
                    ROUND((s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value), 2),
                    (s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value)
                   ),
                 '' ('', ROUND((s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value)/(', v_output_time, '-', v_output_time_prev, '), 2), ''/sec)''
          ),
          ''''
       ) AS ''Delta (', (v_output_count-1), ' -> ', v_output_count, ')''');
        END IF;
        SET v_output_time_prev = v_output_time;
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            IF (v_has_metrics) THEN
                SELECT 'SELECT * FROM sys.metrics' AS 'The following output is:';
            ELSE
                SELECT 'sys.metrics equivalent' AS 'The following output is:';
            END IF;
            CALL sys.execute_prepared_stmt(CONCAT('SELECT Type, Variable_name, Enabled, Variable_value FROM ', v_table_name, ' ORDER BY Type, Variable_name'));
        END IF;
        IF (v_has_innodb IN ('DEFAULT', 'YES')) THEN
            SELECT 'SHOW ENGINE INNODB STATUS' AS 'The following output is:';
            EXECUTE stmt_innodb_status;
            SELECT 'InnoDB - Transactions' AS 'The following output is:';
            SELECT * FROM information_schema.INNODB_TRX;
        END IF;
        IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
            SELECT 'SHOW ENGINE NDBCLUSTER STATUS' AS 'The following output is:';
            EXECUTE stmt_ndbcluster_status;
            SELECT 'ndbinfo.memoryusage' AS 'The following output is:';
            SELECT node_id, memory_type, sys.format_bytes(used) AS used, used_pages, sys.format_bytes(total) AS total, total_pages,
                   ROUND(100*(used/total), 2) AS 'Used %'
            FROM ndbinfo.memoryusage;
            SET v_done = FALSE;
            OPEN c_ndbinfo;
            c_ndbinfo_loop: LOOP
                FETCH c_ndbinfo INTO v_table_name;
                IF v_done THEN
                LEAVE c_ndbinfo_loop;
                END IF;
                SELECT CONCAT('SELECT * FROM ndbinfo.', v_table_name) AS 'The following output is:';
                CALL sys.execute_prepared_stmt(CONCAT('SELECT * FROM `ndbinfo`.`', v_table_name, '`'));
            END LOOP;
            CLOSE c_ndbinfo;
            SELECT * FROM information_schema.FILES;
        END IF;
        SELECT 'SELECT * FROM sys.processlist' AS 'The following output is:';
        SELECT processlist.* FROM sys.processlist;
        IF (v_has_ps = 'YES') THEN
            IF (sys.ps_is_consumer_enabled('events_waits_history_long') = 'YES') THEN
                SELECT 'SELECT * FROM sys.latest_file_io' AS 'The following output is:';
                SELECT * FROM sys.latest_file_io;
            END IF;
            IF (EXISTS(SELECT 1 FROM performance_schema.setup_instruments WHERE NAME LIKE 'memory/%' AND ENABLED = 'YES')) THEN
                SELECT 'SELECT * FROM sys.memory_by_host_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_host_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_by_thread_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_thread_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_by_user_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_user_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_global_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_global_by_current_bytes;
            END IF;
        END IF;
        SET v_runtime = (UNIX_TIMESTAMP(NOW(2)) - v_start);
    UNTIL (v_runtime + in_interval >= in_max_runtime) END REPEAT;
    IF (v_has_ps = 'YES') THEN
        SELECT 'SHOW ENGINE PERFORMANCE_SCHEMA STATUS' AS 'The following output is:';
        EXECUTE stmt_ps_status;
    END IF;
    IF (v_has_innodb IN ('DEFAULT', 'YES')) THEN
        DEALLOCATE PREPARE stmt_innodb_status;
    END IF;
    IF (v_has_ps = 'YES') THEN
        DEALLOCATE PREPARE stmt_ps_status;
    END IF;
    IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
        DEALLOCATE PREPARE stmt_ndbcluster_status;
    END IF;
    SELECT '
============================
     Schema Information
============================
' AS '';
    SELECT COUNT(*) AS 'Total Number of Tables' FROM information_schema.TABLES;
    IF (@sys.diagnostics.allow_i_s_tables = 'ON') THEN
        SELECT 'Storage Engine Usage' AS 'The following output is:';
        SELECT ENGINE, COUNT(*) AS NUM_TABLES,
                sys.format_bytes(SUM(DATA_LENGTH)) AS DATA_LENGTH,
                sys.format_bytes(SUM(INDEX_LENGTH)) AS INDEX_LENGTH,
                sys.format_bytes(SUM(DATA_LENGTH+INDEX_LENGTH)) AS TOTAL
            FROM information_schema.TABLES
            GROUP BY ENGINE;
        SELECT 'Schema Object Overview' AS 'The following output is:';
        SELECT * FROM sys.schema_object_overview;
        SELECT 'Tables without a PRIMARY KEY' AS 'The following output is:';
        SELECT TABLES.TABLE_SCHEMA, ENGINE, COUNT(*) AS NumTables
          FROM information_schema.TABLES
               LEFT OUTER JOIN information_schema.STATISTICS ON STATISTICS.TABLE_SCHEMA = TABLES.TABLE_SCHEMA
                                                                AND STATISTICS.TABLE_NAME = TABLES.TABLE_NAME
                                                                AND STATISTICS.INDEX_NAME = 'PRIMARY'
         WHERE STATISTICS.TABLE_NAME IS NULL
               AND TABLES.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
               AND TABLES.TABLE_TYPE = 'BASE TABLE'
         GROUP BY TABLES.TABLE_SCHEMA, ENGINE;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SELECT 'Unused Indexes' AS 'The following output is:';
        SELECT object_schema, COUNT(*) AS NumUnusedIndexes
          FROM performance_schema.table_io_waits_summary_by_index_usage
         WHERE index_name IS NOT NULL
               AND count_star = 0
               AND object_schema NOT IN ('mysql', 'sys')
               AND index_name != 'PRIMARY'
         GROUP BY object_schema;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SELECT '
=========================
     Overall Status
=========================
' AS '';
        SELECT 'CALL sys.ps_statement_avg_latency_histogram()' AS 'The following output is:';
        CALL sys.ps_statement_avg_latency_histogram();
        CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
        CALL sys.statement_performance_analyzer('overall', NULL, 'with_runtimes_in_95th_percentile');
        SET @sys.diagnostics.sql = REPLACE(@sys.diagnostics.sql_gen_query_template, '%{OUTPUT}', 'end');
        IF (@sys.debug = 'ON') TH^�;J��OBEGIN
    DECLARE v_start, v_runtime, v_iter_start, v_sleep DECIMAL(20,2) DEFAULT 0.0;
    DECLARE v_has_innodb, v_has_ndb, v_has_ps, v_has_replication, v_has_ps_replication VARCHAR(8) CHARSET utf8 DEFAULT 'NO';
    DECLARE v_this_thread_enabled, v_has_ps_vars, v_has_metrics ENUM('YES', 'NO');
    DECLARE v_table_name, v_banner VARCHAR(64) CHARSET utf8;
    DECLARE v_sql_status_summary_select, v_sql_status_summary_delta, v_sql_status_summary_from, v_no_delta_names TEXT;
    DECLARE v_output_time, v_output_time_prev DECIMAL(20,3) UNSIGNED;
    DECLARE v_output_count, v_count, v_old_group_concat_max_len INT UNSIGNED DEFAULT 0;
    DECLARE v_status_summary_width TINYINT UNSIGNED DEFAULT 50;
    DECLARE v_done BOOLEAN DEFAULT FALSE;
    DECLARE c_ndbinfo CURSOR FOR
        SELECT TABLE_NAME
          FROM information_schema.TABLES
         WHERE TABLE_SCHEMA = 'ndbinfo'
               AND TABLE_NAME NOT IN (
                  'blocks',
                  'config_params',
                  'dict_obj_types',
                  'disk_write_speed_base',
                  'memory_per_fragment',
                  'memoryusage',
                  'operations_per_fragment',
                  'threadblocks'
               );
    DECLARE c_sysviews_w_delta CURSOR FOR
        SELECT table_name
          FROM tmp_sys_views_delta
         ORDER BY table_name;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    SELECT INSTRUMENTED INTO v_this_thread_enabled FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    END IF;
    IF (in_max_runtime < in_interval) THEN
        SIGNAL SQLSTATE '45000'
           SET MESSAGE_TEXT = 'in_max_runtime must be greater than or equal to in_interval';
    END IF;
    IF (in_max_runtime = 0) THEN
        SIGNAL SQLSTATE '45000'
           SET MESSAGE_TEXT = 'in_max_runtime must be greater than 0';
    END IF;
    IF (in_interval = 0) THEN
        SIGNAL SQLSTATE '45000'
           SET MESSAGE_TEXT = 'in_interval must be greater than 0';
    END IF;
    IF (@sys.diagnostics.allow_i_s_tables IS NULL) THEN
        SET @sys.diagnostics.allow_i_s_tables = sys.sys_get_config('diagnostics.allow_i_s_tables', 'OFF');
    END IF;
    IF (@sys.diagnostics.include_raw IS NULL) THEN
        SET @sys.diagnostics.include_raw      = sys.sys_get_config('diagnostics.include_raw'     , 'OFF');
    END IF;
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug                        = sys.sys_get_config('debug'                       , 'OFF');
    END IF;
    IF (@sys.statement_truncate_len IS NULL) THEN
        SET @sys.statement_truncate_len       = sys.sys_get_config('statement_truncate_len'      , '64' );
    END IF;
    SET @log_bin := @@sql_log_bin;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = 0;
    END IF;
    SET v_no_delta_names = CONCAT('s%{COUNT}.Variable_name NOT IN (',
        '''innodb_buffer_pool_pages_total'', ',
        '''innodb_page_size'', ',
        '''last_query_cost'', ',
        '''last_query_partial_plans'', ',
        '''qcache_total_blocks'', ',
        '''slave_last_heartbeat'', ',
        '''ssl_ctx_verify_depth'', ',
        '''ssl_ctx_verify_mode'', ',
        '''ssl_session_cache_size'', ',
        '''ssl_verify_depth'', ',
        '''ssl_verify_mode'', ',
        '''ssl_version'', ',
        '''buffer_flush_lsn_avg_rate'', ',
        '''buffer_flush_pct_for_dirty'', ',
        '''buffer_flush_pct_for_lsn'', ',
        '''buffer_pool_pages_total'', ',
        '''lock_row_lock_time_avg'', ',
        '''lock_row_lock_time_max'', ',
        '''innodb_page_size''',
    ')');
    IF (in_auto_config <> 'current') THEN
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('Updating Performance Schema configuration to ', in_auto_config) AS 'Debug';
        END IF;
        CALL sys.ps_setup_save(0);
        IF (in_auto_config = 'medium') THEN
            UPDATE performance_schema.setup_consumers
                SET ENABLED = 'YES'
            WHERE NAME NOT LIKE '%\_history%';
            UPDATE performance_schema.setup_instruments
                SET ENABLED = 'YES',
                    TIMED   = 'YES'
            WHERE NAME NOT LIKE 'wait/synch/%';
        ELSEIF (in_auto_config = 'full') THEN
            UPDATE performance_schema.setup_consumers
                SET ENABLED = 'YES';
            UPDATE performance_schema.setup_instruments
                SET ENABLED = 'YES',
                    TIMED   = 'YES';
        END IF;
        UPDATE performance_schema.threads
           SET INSTRUMENTED = 'YES'
         WHERE PROCESSLIST_ID <> CONNECTION_ID();
    END IF;
    SET v_start        = UNIX_TIMESTAMP(NOW(2)),
        in_interval    = IFNULL(in_interval, 30),
        in_max_runtime = IFNULL(in_max_runtime, 60);
    SET v_banner = REPEAT(
                      '-',
                      LEAST(
                         GREATEST(
                            36,
                            CHAR_LENGTH(VERSION()),
                            CHAR_LENGTH(@@global.version_comment),
                            CHAR_LENGTH(@@global.version_compile_os),
                            CHAR_LENGTH(@@global.version_compile_machine),
                            CHAR_LENGTH(@@global.socket),
                            CHAR_LENGTH(@@global.datadir)
                         ),
                         64
                      )
                   );
    SELECT 'Hostname' AS 'Name', @@global.hostname AS 'Value'
    UNION ALL
    SELECT 'Port' AS 'Name', @@global.port AS 'Value'
    UNION ALL
    SELECT 'Socket' AS 'Name', @@global.socket AS 'Value'
    UNION ALL
    SELECT 'Datadir' AS 'Name', @@global.datadir AS 'Value'
    UNION ALL
    SELECT REPEAT('-', 23) AS 'Name', v_banner AS 'Value'
    UNION ALL
    SELECT 'MySQL Version' AS 'Name', VERSION() AS 'Value'
    UNION ALL
    SELECT 'Sys Schema Version' AS 'Name', (SELECT sys_version FROM sys.version) AS 'Value'
    UNION ALL
    SELECT 'Version Comment' AS 'Name', @@global.version_comment AS 'Value'
    UNION ALL
    SELECT 'Version Compile OS' AS 'Name', @@global.version_compile_os AS 'Value'
    UNION ALL
    SELECT 'Version Compile Machine' AS 'Name', @@global.version_compile_machine AS 'Value'
    UNION ALL
    SELECT REPEAT('-', 23) AS 'Name', v_banner AS 'Value'
    UNION ALL
    SELECT 'UTC Time' AS 'Name', UTC_TIMESTAMP() AS 'Value'
    UNION ALL
    SELECT 'Local Time' AS 'Name', NOW() AS 'Value'
    UNION ALL
    SELECT 'Time Zone' AS 'Name', @@global.time_zone AS 'Value'
    UNION ALL
    SELECT 'System Time Zone' AS 'Name', @@global.system_time_zone AS 'Value'
    UNION ALL
    SELECT 'Time Zone Offset' AS 'Name', TIMEDIFF(NOW(), UTC_TIMESTAMP()) AS 'Value';
    SET v_has_innodb         = IFNULL((SELECT SUPPORT FROM information_schema.ENGINES WHERE ENGINE = 'InnoDB'), 'NO'),
        v_has_ndb            = IFNULL((SELECT SUPPORT FROM information_schema.ENGINES WHERE ENGINE = 'NDBCluster'), 'NO'),
        v_has_ps             = IFNULL((SELECT SUPPORT FROM information_schema.ENGINES WHERE ENGINE = 'PERFORMANCE_SCHEMA'), 'NO'),
        v_has_ps_replication = IF(v_has_ps = 'YES'
                                   AND EXISTS(SELECT 1 FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'replication_applier_status'),
                                   'YES',
                                   'NO'
                               ),
        v_has_replication    = 'MAYBE',
        v_has_metrics        = IF(v_has_ps = 'YES' OR (sys.version_major() = 5 AND sys.version_minor() = 6), 'YES', 'NO'),
        v_has_ps_vars        = 'NO';
    
    
    IF (@sys.debug = 'ON') THEN
       SELECT v_has_innodb AS 'Has_InnoDB', v_has_ndb AS 'Has_NDBCluster',
              v_has_ps AS 'Has_Performance_Schema', v_has_ps_vars AS 'Has_P_S_SHOW_Variables',
              v_has_metrics AS 'Has_metrics',
              v_has_ps_replication 'AS Has_P_S_Replication', v_has_replication AS 'Has_Replication';
    END IF;
    IF (v_has_innodb IN ('DEFAULT', 'YES')) THEN
        SET @sys.diagnostics.sql = 'SHO�NB��OW ENGINE InnoDB STATUS';
        PREPARE stmt_innodb_status FROM @sys.diagnostics.sql;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SET @sys.diagnostics.sql = 'SHOW ENGINE PERFORMANCE_SCHEMA STATUS';
        PREPARE stmt_ps_status FROM @sys.diagnostics.sql;
    END IF;
    IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
        SET @sys.diagnostics.sql = 'SHOW ENGINE NDBCLUSTER STATUS';
        PREPARE stmt_ndbcluster_status FROM @sys.diagnostics.sql;
    END IF;
    SET @sys.diagnostics.sql_gen_query_template = 'SELECT CONCAT(\n           ''SELECT '',\n           GROUP_CONCAT(\n               CASE WHEN (SUBSTRING(TABLE_NAME, 3), COLUMN_NAME) IN (\n                                (''io_global_by_file_by_bytes'', ''total''),\n                                (''io_global_by_wait_by_bytes'', ''total_requested'')\n                         )\n                         THEN CONCAT(''sys.format_bytes('', COLUMN_NAME, '') AS '', COLUMN_NAME)\n                    WHEN SUBSTRING(COLUMN_NAME, -8) = ''_latency''\n                         THEN CONCAT(''sys.format_time('', COLUMN_NAME, '') AS '', COLUMN_NAME)\n                    WHEN SUBSTRING(COLUMN_NAME, -7) = ''_memory'' OR SUBSTRING(COLUMN_NAME, -17) = ''_memory_allocated''\n                         OR ((SUBSTRING(COLUMN_NAME, -5) = ''_read'' OR SUBSTRING(COLUMN_NAME, -8) = ''_written'' OR SUBSTRING(COLUMN_NAME, -6) = ''_write'') AND SUBSTRING(COLUMN_NAME, 1, 6) <> ''COUNT_'')\n                         THEN CONCAT(''sys.format_bytes('', COLUMN_NAME, '') AS '', COLUMN_NAME)\n                    ELSE COLUMN_NAME\n               END\n               ORDER BY ORDINAL_POSITION\n               SEPARATOR '',\n       ''\n           ),\n           ''\n  FROM tmp_'', SUBSTRING(TABLE_NAME FROM 3), ''_%{OUTPUT}''\n       ) AS Query INTO @sys.diagnostics.sql_select\n  FROM information_schema.COLUMNS\n WHERE TABLE_SCHEMA = ''sys'' AND TABLE_NAME = ?\n GROUP BY TABLE_NAME';
    SET @sys.diagnostics.sql_gen_query_delta = 'SELECT CONCAT(\n           ''SELECT '',\n           GROUP_CONCAT(\n               CASE WHEN FIND_IN_SET(COLUMN_NAME, diag.pk)\n                         THEN COLUMN_NAME\n                    WHEN diag.TABLE_NAME = ''io_global_by_file_by_bytes'' AND COLUMN_NAME = ''write_pct''\n                         THEN CONCAT(''IFNULL(ROUND(100-(((e.total_read-IFNULL(s.total_read, 0))'',\n                                     ''/NULLIF(((e.total_read-IFNULL(s.total_read, 0))+(e.total_written-IFNULL(s.total_written, 0))), 0))*100), 2), 0.00) AS '',\n                                     COLUMN_NAME)\n                    WHEN (diag.TABLE_NAME, COLUMN_NAME) IN (\n                                (''io_global_by_file_by_bytes'', ''total''),\n                                (''io_global_by_wait_by_bytes'', ''total_requested'')\n                         )\n                         THEN CONCAT(''sys.format_bytes(e.'', COLUMN_NAME, ''-IFNULL(s.'', COLUMN_NAME, '', 0)) AS '', COLUMN_NAME)\n                    WHEN SUBSTRING(COLUMN_NAME, 1, 4) IN (''max_'', ''min_'') AND SUBSTRING(COLUMN_NAME, -8) = ''_latency''\n                         THEN CONCAT(''sys.format_time(e.'', COLUMN_NAME, '') AS '', COLUMN_NAME)\n                    WHEN COLUMN_NAME = ''avg_latency''\n                         THEN CONCAT(''sys.format_time((e.total_latency - IFNULL(s.total_latency, 0))'',\n                                     ''/NULLIF(e.total - IFNULL(s.total, 0), 0)) AS '', COLUMN_NAME)\n                    WHEN SUBSTRING(COLUMN_NAME, -12) = ''_avg_latency''\n                         THEN CONCAT(''sys.format_time((e.'', SUBSTRING(COLUMN_NAME FROM 1 FOR CHAR_LENGTH(COLUMN_NAME)-12), ''_latency - IFNULL(s.'', SUBSTRING(COLUMN_NAME FROM 1 FOR CHAR_LENGTH(COLUMN_NAME)-12), ''_latency, 0))'',\n                                     ''/NULLIF(e.'', SUBSTRING(COLUMN_NAME FROM 1 FOR CHAR_LENGTH(COLUMN_NAME)-12), ''s - IFNULL(s.'', SUBSTRING(COLUMN_NAME FROM 1 FOR CHAR_LENGTH(COLUMN_NAME)-12), ''s, 0), 0)) AS '', COLUMN_NAME)\n                    WHEN SUBSTRING(COLUMN_NAME, -8) = ''_latency''\n                         THEN CONCAT(''sys.format_time(e.'', COLUMN_NAME, '' - IFNULL(s.'', COLUMN_NAME, '', 0)) AS '', COLUMN_NAME)\n                    WHEN COLUMN_NAME IN (''avg_read'', ''avg_write'', ''avg_written'')\n                         THEN CONCAT(''sys.format_bytes(IFNULL((e.total_'', IF(COLUMN_NAME = ''avg_read'', ''read'', ''written''), ''-IFNULL(s.total_'', IF(COLUMN_NAME = ''avg_read'', ''read'', ''written''), '', 0))'',\n                                     ''/NULLIF(e.count_'', IF(COLUMN_NAME = ''avg_read'', ''read'', ''write''), ''-IFNULL(s.count_'', IF(COLUMN_NAME = ''avg_read'', ''read'', ''write''), '', 0), 0), 0)) AS '',\n                                     COLUMN_NAME)\n                    WHEN SUBSTRING(COLUMN_NAME, -7) = ''_memory'' OR SUBSTRING(COLUMN_NAME, -17) = ''_memory_allocated''\n                         OR ((SUBSTRING(COLUMN_NAME, -5) = ''_read'' OR SUBSTRING(COLUMN_NAME, -8) = ''_written'' OR SUBSTRING(COLUMN_NAME, -6) = ''_write'') AND SUBSTRING(COLUMN_NAME, 1, 6) <> ''COUNT_'')\n                         THEN CONCAT(''sys.format_bytes(e.'', COLUMN_NAME, '' - IFNULL(s.'', COLUMN_NAME, '', 0)) AS '', COLUMN_NAME)\n                    ELSE CONCAT(''(e.'', COLUMN_NAME, '' - IFNULL(s.'', COLUMN_NAME, '', 0)) AS '', COLUMN_NAME)\n               END\n               ORDER BY ORDINAL_POSITION\n               SEPARATOR '',\n       ''\n           ),\n           ''\n  FROM tmp_'', diag.TABLE_NAME, ''_end e\n       LEFT OUTER JOIN tmp_'', diag.TABLE_NAME, ''_start s USING ('', diag.pk, '')''\n       ) AS Query INTO @sys.diagnostics.sql_select\n  FROM tmp_sys_views_delta diag\n       INNER JOIN information_schema.COLUMNS c ON c.TABLE_NAME = CONCAT(''x$'', diag.TABLE_NAME)\n WHERE c.TABLE_SCHEMA = ''sys'' AND diag.TABLE_NAME = ?\n GROUP BY diag.TABLE_NAME';
    IF (v_has_ps = 'YES') THEN
        DROP TEMPORARY TABLE IF EXISTS tmp_sys_views_delta;
        CREATE TEMPORARY TABLE tmp_sys_views_delta (
            TABLE_NAME varchar(64) NOT NULL,
            order_by text COMMENT 'ORDER BY clause for the initial and overall views',
            order_by_delta text COMMENT 'ORDER BY clause for the delta views',
            where_delta text COMMENT 'WHERE clause to use for delta views to only include rows with a "count" > 0',
            limit_rows int unsigned COMMENT 'The maximum number of rows to include for the view',
            pk varchar(128) COMMENT 'Used with the FIND_IN_SET() function so use comma separated list without whitespace',
            PRIMARY KEY (TABLE_NAME)
        );
        IF (@sys.debug = 'ON') THEN
            SELECT 'Populating tmp_sys_views_delta' AS 'Debug';
        END IF;
        INSERT INTO tmp_sys_views_delta
        VALUES ('host_summary'                       , '%{TABLE}.statement_latency DESC',
                                                       '(e.statement_latency-IFNULL(s.statement_latency, 0)) DESC',
                                                       '(e.statements - IFNULL(s.statements, 0)) > 0', NULL, 'host'),
               ('host_summary_by_file_io'            , '%{TABLE}.io_latency DESC',
                                                       '(e.io_latency-IFNULL(s.io_latency, 0)) DESC',
                                                       '(e.ios - IFNULL(s.ios, 0)) > 0', NULL, 'host'),
               ('host_summary_by_file_io_type'       , '%{TABLE}.host, %{TABLE}.total_latency DESC',
                                                       'e.host, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host,event_name'),
               ('host_summary_by_stages'             , '%{TABLE}.host, %{TABLE}.total_latency DESC',
                                                       'e.host, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host,event_name'),
               ('host_summary_by_statement_latency'  , '%{TABLE}.total_latency DESC',
                                                �ꖩ��O       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host'),
               ('host_summary_by_statement_type'     , '%{TABLE}.host, %{TABLE}.total_latency DESC',
                                                       'e.host, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host,statement'),
               ('io_by_thread_by_latency'            , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,thread_id,processlist_id'),
               ('io_global_by_file_by_bytes'         , '%{TABLE}.total DESC',
                                                       '(e.total-IFNULL(s.total, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', 100, 'file'),
               ('io_global_by_file_by_latency'       , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', 100, 'file'),
               ('io_global_by_wait_by_bytes'         , '%{TABLE}.total_requested DESC',
                                                       '(e.total_requested-IFNULL(s.total_requested, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'event_name'),
               ('io_global_by_wait_by_latency'       , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'event_name'),
               ('schema_index_statistics'            , '(%{TABLE}.select_latency+%{TABLE}.insert_latency+%{TABLE}.update_latency+%{TABLE}.delete_latency) DESC',
                                                       '((e.select_latency+e.insert_latency+e.update_latency+e.delete_latency)-IFNULL(s.select_latency+s.insert_latency+s.update_latency+s.delete_latency, 0)) DESC',
                                                       '((e.rows_selected+e.insert_latency+e.rows_updated+e.rows_deleted)-IFNULL(s.rows_selected+s.rows_inserted+s.rows_updated+s.rows_deleted, 0)) > 0',
                                                       100, 'table_schema,table_name,index_name'),
               ('schema_table_statistics'            , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) > 0', 100, 'table_schema,table_name'),
               ('schema_tables_with_full_table_scans', '%{TABLE}.rows_full_scanned DESC',
                                                       '(e.rows_full_scanned-IFNULL(s.rows_full_scanned, 0)) DESC',
                                                       '(e.rows_full_scanned-IFNULL(s.rows_full_scanned, 0)) > 0', 100, 'object_schema,object_name'),
               ('user_summary'                       , '%{TABLE}.statement_latency DESC',
                                                       '(e.statement_latency-IFNULL(s.statement_latency, 0)) DESC',
                                                       '(e.statements - IFNULL(s.statements, 0)) > 0', NULL, 'user'),
               ('user_summary_by_file_io'            , '%{TABLE}.io_latency DESC',
                                                       '(e.io_latency-IFNULL(s.io_latency, 0)) DESC',
                                                       '(e.ios - IFNULL(s.ios, 0)) > 0', NULL, 'user'),
               ('user_summary_by_file_io_type'       , '%{TABLE}.user, %{TABLE}.latency DESC',
                                                       'e.user, (e.latency-IFNULL(s.latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,event_name'),
               ('user_summary_by_stages'             , '%{TABLE}.user, %{TABLE}.total_latency DESC',
                                                       'e.user, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,event_name'),
               ('user_summary_by_statement_latency'  , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user'),
               ('user_summary_by_statement_type'     , '%{TABLE}.user, %{TABLE}.total_latency DESC',
                                                       'e.user, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,statement'),
               ('wait_classes_global_by_avg_latency' , 'IFNULL(%{TABLE}.total_latency / NULLIF(%{TABLE}.total, 0), 0) DESC',
                                                       'IFNULL((e.total_latency-IFNULL(s.total_latency, 0)) / NULLIF((e.total - IFNULL(s.total, 0)), 0), 0) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'event_class'),
               ('wait_classes_global_by_latency'     , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'event_class'),
               ('waits_by_host_by_latency'           , '%{TABLE}.host, %{TABLE}.total_latency DESC',
                                                       'e.host, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'host,event'),
               ('waits_by_user_by_latency'           , '%{TABLE}.user, %{TABLE}.total_latency DESC',
                                                       'e.user, (e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'user,event'),
               ('waits_global_by_latency'            , '%{TABLE}.total_latency DESC',
                                                       '(e.total_latency-IFNULL(s.total_latency, 0)) DESC',
                                                       '(e.total - IFNULL(s.total, 0)) > 0', NULL, 'events')
        ;
    END IF;
    SELECT '\n=======================\n     Configuration\n=======================\n' AS '';
    SELECT 'GLOBAL VARIABLES' AS 'The following output is:';
    IF (v_has_ps_vars = 'YES') THEN
        SELECT LOWER(VARIABLE_NAME) AS Variable_name, VARIABLE_VALUE AS Variable_value FROM performance_schema.global_variables ORDER BY VARIABLE_NAME;
    ELSE
        SELECT LOWER(VARIABLE_NAME) AS Variable_name, VARIABLE_VALUE AS Variable_value FROM information_schema.GLOBAL_VARIABLES ORDER BY VARIABLE_NAME;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SELECT 'Performance Schema Setup - Actors' AS 'The following output is:';
        SELECT * FROM performance_schema.setup_actors;
        SELECT 'Performance Schema Setup - Consumers' AS 'The following output is:';
        SELECT NAME AS Consumer, ENABLED, sys.ps_is_consumer_enabled(NAME) AS COLLECTS
          FROM performance_schema.setup_consumers;
        SELECT 'Performance Schema Setup - Instruments' AS 'The following output is:';
        SELECT SUBSTRING_INDEX(NAME, '/', 2) AS 'InstrumentClass',
               ROUND(100*SUM(IF(ENABLED = 'YES', 1, 0))/COUNT(*), 2) AS 'EnabledPct',
               ROUND(100*SUM(IF(TIM�.IX��OED = 'YES', 1, 0))/COUNT(*), 2) AS 'TimedPct'
          FROM performance_schema.setup_instruments
         GROUP BY SUBSTRING_INDEX(NAME, '/', 2)
         ORDER BY SUBSTRING_INDEX(NAME, '/', 2);
        SELECT 'Performance Schema Setup - Objects' AS 'The following output is:';
        SELECT * FROM performance_schema.setup_objects;
        SELECT 'Performance Schema Setup - Threads' AS 'The following output is:';
        SELECT `TYPE` AS ThreadType, COUNT(*) AS 'Total', ROUND(100*SUM(IF(INSTRUMENTED = 'YES', 1, 0))/COUNT(*), 2) AS 'InstrumentedPct'
          FROM performance_schema.threads
        GROUP BY TYPE;
    END IF;
    IF (v_has_replication = 'NO') THEN
        SELECT 'No Replication Configured' AS 'Replication Status';
    ELSE
        SELECT CONCAT('Replication Configured: ', v_has_replication, ' - Performance Schema Replication Tables: ', v_has_ps_replication) AS 'Replication Status';
        IF (v_has_ps_replication = 'YES') THEN
            SELECT 'Replication - Connection Configuration' AS 'The following output is:';
            SELECT * FROM performance_schema.replication_connection_configuration;
        END IF;
        IF (v_has_ps_replication = 'YES') THEN
            SELECT 'Replication - Applier Configuration' AS 'The following output is:';
            SELECT * FROM performance_schema.replication_applier_configuration ORDER BY CHANNEL_NAME;
        END IF;
    END IF;
    IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
       SELECT 'Cluster Thread Blocks' AS 'The following output is:';
       SELECT * FROM ndbinfo.threadblocks;
    END IF;
    IF (v_has_ps = 'YES') THEN
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            SELECT '\n========================\n     Initial Status\n========================\n' AS '';
        END IF;
        DROP TEMPORARY TABLE IF EXISTS tmp_digests_start;
        CALL sys.statement_performance_analyzer('create_tmp', 'tmp_digests_start', NULL);
        CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
        CALL sys.statement_performance_analyzer('save', 'tmp_digests_start', NULL);
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            SET @sys.diagnostics.sql = REPLACE(@sys.diagnostics.sql_gen_query_template, '%{OUTPUT}', 'start');
            IF (@sys.debug = 'ON') THEN
                SELECT 'The following query will be used to generate the query for each sys view' AS 'Debug';
                SELECT @sys.diagnostics.sql AS 'Debug';
            END IF;
            PREPARE stmt_gen_query FROM @sys.diagnostics.sql;
        END IF;
        SET v_done = FALSE;
        OPEN c_sysviews_w_delta;
        c_sysviews_w_delta_loop: LOOP
            FETCH c_sysviews_w_delta INTO v_table_name;
            IF v_done THEN
                LEAVE c_sysviews_w_delta_loop;
            END IF;
            IF (@sys.debug = 'ON') THEN
                SELECT CONCAT('The following queries are for storing the initial content of ', v_table_name) AS 'Debug';
            END IF;
            CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS `tmp_', v_table_name, '_start`'));
            CALL sys.execute_prepared_stmt(CONCAT('CREATE TEMPORARY TABLE `tmp_', v_table_name, '_start` SELECT * FROM `sys`.`x$', v_table_name, '`'));
            IF (@sys.diagnostics.include_raw = 'ON') THEN
                SET @sys.diagnostics.table_name = CONCAT('x$', v_table_name);
                EXECUTE stmt_gen_query USING @sys.diagnostics.table_name;
                SELECT CONCAT(@sys.diagnostics.sql_select,
                              IF(order_by IS NOT NULL, CONCAT('\n ORDER BY ', REPLACE(order_by, '%{TABLE}', CONCAT('tmp_', v_table_name, '_start'))), ''),
                              IF(limit_rows IS NOT NULL, CONCAT('\n LIMIT ', limit_rows), '')
                       )
                  INTO @sys.diagnostics.sql_select
                  FROM tmp_sys_views_delta
                 WHERE TABLE_NAME = v_table_name;
                SELECT CONCAT('Initial ', v_table_name) AS 'The following output is:';
                CALL sys.execute_prepared_stmt(@sys.diagnostics.sql_select);
            END IF;
        END LOOP;
        CLOSE c_sysviews_w_delta;
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            DEALLOCATE PREPARE stmt_gen_query;
        END IF;
    END IF;
    SET v_sql_status_summary_select = 'SELECT Variable_name',
        v_sql_status_summary_delta  = '',
        v_sql_status_summary_from   = '';
    REPEAT
        SET v_output_count = v_output_count + 1;
        IF (v_output_count > 1) THEN
            SET v_sleep = in_interval-(UNIX_TIMESTAMP(NOW(2))-v_iter_start);
            SELECT NOW() AS 'Time', CONCAT('Going to sleep for ', v_sleep, ' seconds. Please do not interrupt') AS 'The following output is:';
            DO SLEEP(in_interval);
        END IF;
        SET v_iter_start = UNIX_TIMESTAMP(NOW(2));
        SELECT NOW(), CONCAT('Iteration Number ', IFNULL(v_output_count, 'NULL')) AS 'The following output is:';
        IF (@@log_bin = 1) THEN
            SELECT 'SHOW MASTER STATUS' AS 'The following output is:';
            SHOW MASTER STATUS;
        END IF;
        IF (v_has_replication <> 'NO') THEN
            SELECT 'SHOW SLAVE STATUS' AS 'The following output is:';
            SHOW SLAVE STATUS;
        END IF;
        SET v_table_name = CONCAT('tmp_metrics_', v_output_count);
        CALL sys.execute_prepared_stmt(CONCAT('DROP TEMPORARY TABLE IF EXISTS ', v_table_name));
        CALL sys.execute_prepared_stmt(CONCAT('CREATE TEMPORARY TABLE ', v_table_name, ' (\n  Variable_name VARCHAR(193) NOT NULL,\n  Variable_value VARCHAR(1024),\n  Type VARCHAR(100) NOT NULL,\n  Enabled ENUM(''YES'', ''NO'', ''PARTIAL'') NOT NULL,\n  PRIMARY KEY (Type, Variable_name)\n) ENGINE = InnoDB DEFAULT CHARSET=utf8'));
        IF (v_has_metrics) THEN
            SET @sys.diagnostics.sql = CONCAT(
                'INSERT INTO ', v_table_name,
                ' SELECT Variable_name, REPLACE(Variable_value, ''\n'', ''\\n'') AS Variable_value, Type, Enabled FROM sys.metrics'
            );
        ELSE
            SET @sys.diagnostics.sql = CONCAT(
                'INSERT INTO ', v_table_name,
                '(SELECT LOWER(VARIABLE_NAME) AS Variable_name, REPLACE(VARIABLE_VALUE, ''\n'', ''\\n'') AS Variable_value,\n                         ''Global Status'' AS Type, ''YES'' AS Enabled\n  FROM performance_schema.global_status\n) UNION ALL (\nSELECT NAME AS Variable_name, COUNT AS Variable_value,\n       CONCAT(''InnoDB Metrics - '', SUBSYSTEM) AS Type,\n       IF(STATUS = ''enabled'', ''YES'', ''NO'') AS Enabled\n  FROM information_schema.INNODB_METRICS\n WHERE NAME NOT IN (\n     ''lock_row_lock_time'', ''lock_row_lock_time_avg'', ''lock_row_lock_time_max'', ''lock_row_lock_waits'',\n     ''buffer_pool_reads'', ''buffer_pool_read_requests'', ''buffer_pool_write_requests'', ''buffer_pool_wait_free'',\n     ''buffer_pool_read_ahead'', ''buffer_pool_read_ahead_evicted'', ''buffer_pool_pages_total'', ''buffer_pool_pages_misc'',\n     ''buffer_pool_pages_data'', ''buffer_pool_bytes_data'', ''buffer_pool_pages_dirty'', ''buffer_pool_bytes_dirty'',\n     ''buffer_pool_pages_free'', ''buffer_pages_created'', ''buffer_pages_written'', ''buffer_pages_read'',\n     ''buffer_data_reads'', ''buffer_data_written'', ''file_num_open_files'',\n     ''os_log_bytes_written'', ''os_log_fsyncs'', ''os_log_pending_fsyncs'', ''os_log_pending_writes'',\n     ''log_waits'', ''log_write_requests'', ''log_writes'', ''innodb_dblwr_writes'', ''innodb_dblwr_pages_written'', ''innodb_page_size'')\n) UNION ALL (\nSELECT ''NOW()'' AS Variable_name, NOW(3) AS Variable_value, ''System Time'' AS Type, ''YES'' AS Enabled\n) UNION ALL (\nSELECT ''UNIX_TIMESTAMP()'' AS Variable_name, ROUND(UNIX_TIMESTAMP(NOW(3)), 3) AS Variable_value, ''System Time'' AS Type, ''YES'' AS Enabled\n)\n ORDER BY Type, Variable_name;'
            );
        END IF;
        CALL sys.execute_prepared_stmt(@sys.diagnostics.sql);
        CALL sys.execute_prepared_stmt(
            CONCAT('SELECT Variable_value INTO @sys.diagnostics.output_time FROM ', v_table_name, ' WHERE Type = ''System Time'' AND Variable_name = ''UNIX�+����O_TIMESTAMP()''')
        );
        SET v_output_time = @sys.diagnostics.output_time;
        SET v_sql_status_summary_select = CONCAT(v_sql_status_summary_select, ',\n       CONCAT(\n           LEFT(s', v_output_count, '.Variable_value, ', v_status_summary_width, '),\n           IF(', REPLACE(v_no_delta_names, '%{COUNT}', v_output_count), ' AND s', v_output_count, '.Variable_value REGEXP ''^[0-9]+(\\.[0-9]+)?$'', CONCAT('' ('', ROUND(s', v_output_count, '.Variable_value/', v_output_time, ', 2), ''/sec)''), '''')\n       ) AS ''Output ', v_output_count, ''''),
            v_sql_status_summary_from   = CONCAT(v_sql_status_summary_from, '\n',
                                                    IF(v_output_count = 1, '  FROM ', '       INNER JOIN '),
                                                    v_table_name, ' s', v_output_count,
                                                    IF (v_output_count = 1, '', ' USING (Type, Variable_name)'));
        IF (v_output_count > 1) THEN
            SET v_sql_status_summary_delta  = CONCAT(v_sql_status_summary_delta, ',\n       IF(', REPLACE(v_no_delta_names, '%{COUNT}', v_output_count), ' AND s', (v_output_count-1), '.Variable_value REGEXP ''^[0-9]+(\\.[0-9]+)?$'' AND s', v_output_count, '.Variable_value REGEXP ''^[0-9]+(\\.[0-9]+)?$'',\n          CONCAT(IF(s', (v_output_count-1), '.Variable_value REGEXP ''^[0-9]+\\.[0-9]+$'' OR s', v_output_count, '.Variable_value REGEXP ''^[0-9]+\\.[0-9]+$'',\n                    ROUND((s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value), 2),\n                    (s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value)\n                   ),\n                 '' ('', ROUND((s', v_output_count, '.Variable_value-s', (v_output_count-1), '.Variable_value)/(', v_output_time, '-', v_output_time_prev, '), 2), ''/sec)''\n          ),\n          ''''\n       ) AS ''Delta (', (v_output_count-1), ' -> ', v_output_count, ')''');
        END IF;
        SET v_output_time_prev = v_output_time;
        IF (@sys.diagnostics.include_raw = 'ON') THEN
            IF (v_has_metrics) THEN
                SELECT 'SELECT * FROM sys.metrics' AS 'The following output is:';
            ELSE
                SELECT 'sys.metrics equivalent' AS 'The following output is:';
            END IF;
            CALL sys.execute_prepared_stmt(CONCAT('SELECT Type, Variable_name, Enabled, Variable_value FROM ', v_table_name, ' ORDER BY Type, Variable_name'));
        END IF;
        IF (v_has_innodb IN ('DEFAULT', 'YES')) THEN
            SELECT 'SHOW ENGINE INNODB STATUS' AS 'The following output is:';
            EXECUTE stmt_innodb_status;
            SELECT 'InnoDB - Transactions' AS 'The following output is:';
            SELECT * FROM information_schema.INNODB_TRX;
        END IF;
        IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
            SELECT 'SHOW ENGINE NDBCLUSTER STATUS' AS 'The following output is:';
            EXECUTE stmt_ndbcluster_status;
            SELECT 'ndbinfo.memoryusage' AS 'The following output is:';
            SELECT node_id, memory_type, sys.format_bytes(used) AS used, used_pages, sys.format_bytes(total) AS total, total_pages,
                   ROUND(100*(used/total), 2) AS 'Used %'
            FROM ndbinfo.memoryusage;
            SET v_done = FALSE;
            OPEN c_ndbinfo;
            c_ndbinfo_loop: LOOP
                FETCH c_ndbinfo INTO v_table_name;
                IF v_done THEN
                LEAVE c_ndbinfo_loop;
                END IF;
                SELECT CONCAT('SELECT * FROM ndbinfo.', v_table_name) AS 'The following output is:';
                CALL sys.execute_prepared_stmt(CONCAT('SELECT * FROM `ndbinfo`.`', v_table_name, '`'));
            END LOOP;
            CLOSE c_ndbinfo;
            SELECT * FROM information_schema.FILES;
        END IF;
        SELECT 'SELECT * FROM sys.processlist' AS 'The following output is:';
        SELECT processlist.* FROM sys.processlist;
        IF (v_has_ps = 'YES') THEN
            IF (sys.ps_is_consumer_enabled('events_waits_history_long') = 'YES') THEN
                SELECT 'SELECT * FROM sys.latest_file_io' AS 'The following output is:';
                SELECT * FROM sys.latest_file_io;
            END IF;
            IF (EXISTS(SELECT 1 FROM performance_schema.setup_instruments WHERE NAME LIKE 'memory/%' AND ENABLED = 'YES')) THEN
                SELECT 'SELECT * FROM sys.memory_by_host_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_host_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_by_thread_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_thread_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_by_user_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_by_user_by_current_bytes;
                SELECT 'SELECT * FROM sys.memory_global_by_current_bytes' AS 'The following output is:';
                SELECT * FROM sys.memory_global_by_current_bytes;
            END IF;
        END IF;
        SET v_runtime = (UNIX_TIMESTAMP(NOW(2)) - v_start);
    UNTIL (v_runtime + in_interval >= in_max_runtime) END REPEAT;
    IF (v_has_ps = 'YES') THEN
        SELECT 'SHOW ENGINE PERFORMANCE_SCHEMA STATUS' AS 'The following output is:';
        EXECUTE stmt_ps_status;
    END IF;
    IF (v_has_innodb IN ('DEFAULT', 'YES')) THEN
        DEALLOCATE PREPARE stmt_innodb_status;
    END IF;
    IF (v_has_ps = 'YES') THEN
        DEALLOCATE PREPARE stmt_ps_status;
    END IF;
    IF (v_has_ndb IN ('DEFAULT', 'YES')) THEN
        DEALLOCATE PREPARE stmt_ndbcluster_status;
    END IF;
    SELECT '\n============================\n     Schema Information\n============================\n' AS '';
    SELECT COUNT(*) AS 'Total Number of Tables' FROM information_schema.TABLES;
    IF (@sys.diagnostics.allow_i_s_tables = 'ON') THEN
        SELECT 'Storage Engine Usage' AS 'The following output is:';
        SELECT ENGINE, COUNT(*) AS NUM_TABLES,
                sys.format_bytes(SUM(DATA_LENGTH)) AS DATA_LENGTH,
                sys.format_bytes(SUM(INDEX_LENGTH)) AS INDEX_LENGTH,
                sys.format_bytes(SUM(DATA_LENGTH+INDEX_LENGTH)) AS TOTAL
            FROM information_schema.TABLES
            GROUP BY ENGINE;
        SELECT 'Schema Object Overview' AS 'The following output is:';
        SELECT * FROM sys.schema_object_overview;
        SELECT 'Tables without a PRIMARY KEY' AS 'The following output is:';
        SELECT TABLES.TABLE_SCHEMA, ENGINE, COUNT(*) AS NumTables
          FROM information_schema.TABLES
               LEFT OUTER JOIN information_schema.STATISTICS ON STATISTICS.TABLE_SCHEMA = TABLES.TABLE_SCHEMA
                                                                AND STATISTICS.TABLE_NAME = TABLES.TABLE_NAME
                                                                AND STATISTICS.INDEX_NAME = 'PRIMARY'
         WHERE STATISTICS.TABLE_NAME IS NULL
               AND TABLES.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
               AND TABLES.TABLE_TYPE = 'BASE TABLE'
         GROUP BY TABLES.TABLE_SCHEMA, ENGINE;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SELECT 'Unused Indexes' AS 'The following output is:';
        SELECT object_schema, COUNT(*) AS NumUnusedIndexes
          FROM performance_schema.table_io_waits_summary_by_index_usage
         WHERE index_name IS NOT NULL
               AND count_star = 0
               AND object_schema NOT IN ('mysql', 'sys')
               AND index_name != 'PRIMARY'
         GROUP BY object_schema;
    END IF;
    IF (v_has_ps = 'YES') THEN
        SELECT '\n=========================\n     Overall Status\n=========================\n' AS '';
        SELECT 'CALL sys.ps_statement_avg_latency_histogram()' AS 'The following output is:';
        CALL sys.ps_statement_avg_latency_histogram();
        CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
        CALL sys.statement_performance_analyzer('overall', NULL, 'with_runtimes_in_95th_percentile');
        SET @sys.diagnostics.sIR\R�PBEGIN
SELECT CONCAT('\n',
       '\n  . = 1 unit',
       '\n  * = 2 units',
       '\n  # = 3 units\n',
       @label := CONCAT(@label_inner := CONCAT('\n(0 - ',
                                               ROUND((@bucket_size := (SELECT ROUND((MAX(avg_us) - MIN(avg_us)) / (@buckets := 16)) AS size
                                                                         FROM sys.x$ps_digest_avg_latency_distribution)) / (@unit_div := 1000)),
                                                (@unit := 'ms'), ')'),
                        REPEAT(' ', (@max_label_size := ((1 + LENGTH(ROUND((@bucket_size * 15) / @unit_div)) + 3 + LENGTH(ROUND(@bucket_size * 16) / @unit_div)) + 1)) - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us <= @bucket_size), 0)),
       REPEAT(' ', (@max_label_len := (@max_label_size + LENGTH((@total_queries := (SELECT SUM(cnt) FROM sys.x$ps_digest_avg_latency_distribution)))) + 1) - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < (@one_unit := 40), '.', IF(@count_in_bucket < (@two_unit := 80), '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND(@bucket_size / @unit_div), ' - ', ROUND((@bucket_size * 2) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size AND b1.avg_us <= @bucket_size * 2), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 2) / @unit_div), ' - ', ROUND((@bucket_size * 3) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 2 AND b1.avg_us <= @bucket_size * 3), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 3) / @unit_div), ' - ', ROUND((@bucket_size * 4) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 3 AND b1.avg_us <= @bucket_size * 4), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 4) / @unit_div), ' - ', ROUND((@bucket_size * 5) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 4 AND b1.avg_us <= @bucket_size * 5), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 5) / @unit_div), ' - ', ROUND((@bucket_size * 6) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 5 AND b1.avg_us <= @bucket_size * 6), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 6) / @unit_div), ' - ', ROUND((@bucket_size * 7) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 6 AND b1.avg_us <= @bucket_size * 7), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 7) / @unit_div), ' - ', ROUND((@bucket_size * 8) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 7 AND b1.avg_us <= @bucket_size * 8), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 8) / @unit_div), ' - ', ROUND((@bucket_size * 9) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 8 AND b1.avg_us <= @bucket_size * 9), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@�VRR�Pcount_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 9) / @unit_div), ' - ', ROUND((@bucket_size * 10) / @unit_div), @unit, ')'),
                         REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                         @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                       FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                      WHERE b1.avg_us > @bucket_size * 9 AND b1.avg_us <= @bucket_size * 10), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 10) / @unit_div), ' - ', ROUND((@bucket_size * 11) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 10 AND b1.avg_us <= @bucket_size * 11), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 11) / @unit_div), ' - ', ROUND((@bucket_size * 12) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 11 AND b1.avg_us <= @bucket_size * 12), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 12) / @unit_div), ' - ', ROUND((@bucket_size * 13) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 12 AND b1.avg_us <= @bucket_size * 13), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 13) / @unit_div), ' - ', ROUND((@bucket_size * 14) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 13 AND b1.avg_us <= @bucket_size * 14), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 14) / @unit_div), ' - ', ROUND((@bucket_size * 15) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 14 AND b1.avg_us <= @bucket_size * 15), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 15) / @unit_div), ' - ', ROUND((@bucket_size * 16) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 15 AND b1.avg_us <= @bucket_size * 16), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       '\n\n  Total Statements: ', @total_queries, '; Buckets: ', @buckets , '; Bucket Size: ', ROUND(@bucket_size / @unit_div) , ' ', @unit, ';\n'
      ) AS `Performance Schema Statement Digest Average Latency Histogram`;
END��yR�PBEGIN
SELECT CONCAT('\n',
       '\n  . = 1 unit',
       '\n  * = 2 units',
       '\n  # = 3 units\n',
       @label := CONCAT(@label_inner := CONCAT('\n(0 - ',
                                               ROUND((@bucket_size := (SELECT ROUND((MAX(avg_us) - MIN(avg_us)) / (@buckets := 16)) AS size
                                                                         FROM sys.x$ps_digest_avg_latency_distribution)) / (@unit_div := 1000)),
                                                (@unit := 'ms'), ')'),
                        REPEAT(' ', (@max_label_size := ((1 + LENGTH(ROUND((@bucket_size * 15) / @unit_div)) + 3 + LENGTH(ROUND(@bucket_size * 16) / @unit_div)) + 1)) - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us <= @bucket_size), 0)),
       REPEAT(' ', (@max_label_len := (@max_label_size + LENGTH((@total_queries := (SELECT SUM(cnt) FROM sys.x$ps_digest_avg_latency_distribution)))) + 1) - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < (@one_unit := 40), '.', IF(@count_in_bucket < (@two_unit := 80), '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND(@bucket_size / @unit_div), ' - ', ROUND((@bucket_size * 2) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size AND b1.avg_us <= @bucket_size * 2), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 2) / @unit_div), ' - ', ROUND((@bucket_size * 3) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 2 AND b1.avg_us <= @bucket_size * 3), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 3) / @unit_div), ' - ', ROUND((@bucket_size * 4) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 3 AND b1.avg_us <= @bucket_size * 4), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 4) / @unit_div), ' - ', ROUND((@bucket_size * 5) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 4 AND b1.avg_us <= @bucket_size * 5), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 5) / @unit_div), ' - ', ROUND((@bucket_size * 6) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 5 AND b1.avg_us <= @bucket_size * 6), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 6) / @unit_div), ' - ', ROUND((@bucket_size * 7) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 6 AND b1.avg_us <= @bucket_size * 7), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 7) / @unit_div), ' - ', ROUND((@bucket_size * 8) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 7 AND b1.avg_us <= @bucket_size * 8), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 8) / @unit_div), ' - ', ROUND((@bucket_size * 9) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 8 AND b1.avg_us <= @bucket_size * 9), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@�c��R�Pcount_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 9) / @unit_div), ' - ', ROUND((@bucket_size * 10) / @unit_div), @unit, ')'),
                         REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                         @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                       FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                      WHERE b1.avg_us > @bucket_size * 9 AND b1.avg_us <= @bucket_size * 10), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 10) / @unit_div), ' - ', ROUND((@bucket_size * 11) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 10 AND b1.avg_us <= @bucket_size * 11), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 11) / @unit_div), ' - ', ROUND((@bucket_size * 12) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 11 AND b1.avg_us <= @bucket_size * 12), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 12) / @unit_div), ' - ', ROUND((@bucket_size * 13) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 12 AND b1.avg_us <= @bucket_size * 13), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 13) / @unit_div), ' - ', ROUND((@bucket_size * 14) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 13 AND b1.avg_us <= @bucket_size * 14), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 14) / @unit_div), ' - ', ROUND((@bucket_size * 15) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 14 AND b1.avg_us <= @bucket_size * 15), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       @label := CONCAT(@label_inner := CONCAT('\n(', ROUND((@bucket_size * 15) / @unit_div), ' - ', ROUND((@bucket_size * 16) / @unit_div), @unit, ')'),
                        REPEAT(' ', @max_label_size - LENGTH(@label_inner)),
                        @count_in_bucket := IFNULL((SELECT SUM(cnt)
                                                      FROM sys.x$ps_digest_avg_latency_distribution AS b1
                                                     WHERE b1.avg_us > @bucket_size * 15 AND b1.avg_us <= @bucket_size * 16), 0)),
       REPEAT(' ', @max_label_len - LENGTH(@label)), '| ',
       IFNULL(REPEAT(IF(@count_in_bucket < @one_unit, '.', IF(@count_in_bucket < @two_unit, '*', '#')),
       	             IF(@count_in_bucket < @one_unit, @count_in_bucket,
       	             	IF(@count_in_bucket < @two_unit, @count_in_bucket / 2, @count_in_bucket / 3))), ''),
       '\n\n  Total Statements: ', @total_queries, '; Buckets: ', @buckets , '; Bucket Size: ', ROUND(@bucket_size / @unit_div) , ' ', @unit, ';\n'
      ) AS `Performance Schema Statement Digest Average Latency Histogram`;
END��3Q
             Description
             Traces all instrumentation within Performance Schema for a specific
             Statement Digest.
             When finding a statement of interest within the
             performance_schema.events_statements_summary_by_digest table, feed
             the DIGEST MD5 value in to this procedure, set how long to poll for,
             and at what interval to poll, and it will generate a report of all
             statistics tracked within Performance Schema for that digest for the
             interval.
             It will also attempt to generate an EXPLAIN for the longest running
             example of the digest during the interval. Note this may fail, as:
                * Performance Schema truncates long SQL_TEXT values (and hence the
                  EXPLAIN will fail due to parse errors)
                * the default schema is sys (so tables that are not fully qualified
                  in the query may not be found)
                * some queries such as SHOW are not supported in EXPLAIN.
             When the EXPLAIN fails, the error will be ignored and no EXPLAIN
             output generated.
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Parameters
             in_digest (VARCHAR(32)):
               The statement digest identifier you would like to analyze
             in_runtime (INT):
               The number of seconds to run analysis for
             in_interval (DECIMAL(2,2)):
               The interval (in seconds, may be fractional) at which to try
               and take snapshots
             in_start_fresh (BOOLEAN):
               Whether to TRUNCATE the events_statements_history_long and
               events_stages_history_long tables before starting
             in_auto_enable (BOOLEAN):
               Whether to automatically turn on required consumers
             Example
             mysql> call ps_trace_statement_digest('891ec6860f98ba46d89dd20b0c03652c', 10, 0.1, true, true);
             +--------------------+
             | SUMMARY STATISTICS |
             +--------------------+
             | SUMMARY STATISTICS |
             +--------------------+
             1 row in set (9.11 sec)
             +------------+-----------+-----------+-----------+---------------+------------+------------+
             | executions | exec_time | lock_time | rows_sent | rows_examined | tmp_tables | full_scans |
             +------------+-----------+-----------+-----------+---------------+------------+------------+
             |         21 | 4.11 ms   | 2.00 ms   |         0 |            21 |          0 |          0 |
             +------------+-----------+-----------+-----------+---------------+------------+------------+
             1 row in set (9.11 sec)
             +------------------------------------------+-------+-----------+
             | event_name                               | count | latency   |
             +------------------------------------------+-------+-----------+
             | stage/sql/checking query cache for query |    16 | 724.37 us |
             | stage/sql/statistics                     |    16 | 546.92 us |
             | stage/sql/freeing items                  |    18 | 520.11 us |
             | stage/sql/init                           |    51 | 466.80 us |
             ...
             | stage/sql/cleaning up                    |    18 | 11.92 us  |
             | stage/sql/executing                      |    16 | 6.95 us   |
             +------------------------------------------+-------+-----------+
             17 rows in set (9.12 sec)
             +---------------------------+
             | LONGEST RUNNING STATEMENT |
             +---------------------------+
             | LONGEST RUNNING STATEMENT |
             +---------------------------+
             1 row in set (9.16 sec)
             +-----------+-----------+-----------+-----------+---------------+------------+-----------+
             | thread_id | exec_time | lock_time | rows_sent | rows_examined | tmp_tables | full_scan |
             +-----------+-----------+-----------+-----------+---------------+------------+-----------+
             |    166646 | 618.43 us | 1.00 ms   |         0 |             1 |          0 |         0 |
             +-----------+-----------+-----------+-----------+---------------+------------+-----------+
             1 row in set (9.16 sec)
             // Truncated for clarity...
             +-----------------------------------------------------------------+
             | sql_text                                                        |
             +-----------------------------------------------------------------+
             | select hibeventhe0_.id as id1382_, hibeventhe0_.createdTime ... |
             +-----------------------------------------------------------------+
             1 row in set (9.17 sec)
             +------------------------------------------+-----------+
             | event_name                               | latency   |
             +------------------------------------------+-----------+
             | stage/sql/init                           | 8.61 us   |
             | stage/sql/Waiting for query cache lock   | 453.23 us |
             | stage/sql/init                           | 331.07 ns |
             | stage/sql/checking query cache for query | 43.04 us  |
             ...
             | stage/sql/freeing items                  | 30.46 us  |
             | stage/sql/cleaning up                    | 662.13 ns |
             +------------------------------------------+-----------+
             18 rows in set (9.23 sec)
             +----+-------------+--------------+-------+---------------+-----------+---------+-------------+------+-------+
             | id | select_type | table        | type  | possible_keys | key       | key_len | ref         | rows | Extra |
             +----+-------------+--------------+-------+---------------+-----------+---------+-------------+------+-------+
             |  1 | SIMPLE      | hibeventhe0_ | const | fixedTime     | fixedTime | 775     | const,const |    1 | NULL  |
             +----+-------------+--------------+-------+---------------+-----------+---------+-------------+------+-------+
             1 row in set (9.27 sec)
             Query OK, 0 rows affected (9.28 sec)
            =����QBEGIN
    DECLARE v_done bool DEFAULT FALSE;
    DECLARE v_start, v_runtime DECIMAL(20,2) DEFAULT 0.0;
    DECLARE v_min_event_id bigint unsigned DEFAULT 0;
    DECLARE v_this_thread_enabed ENUM('YES', 'NO');
    DECLARE v_event longtext;
    DECLARE c_stack CURSOR FOR
        SELECT CONCAT(IF(nesting_event_id IS NOT NULL, CONCAT(nesting_event_id, ' -> '), ''),
                    event_id, '; ', event_id, ' [label="',
                    '(', sys.format_time(timer_wait), ') ',
                    IF (event_name NOT LIKE 'wait/io%',
                        SUBSTRING_INDEX(event_name, '/', -2),
                        IF (event_name NOT LIKE 'wait/io/file%' OR event_name NOT LIKE 'wait/io/socket%',
                            SUBSTRING_INDEX(event_name, '/', -4),
                            event_name)
                        ),
                    IF (event_name LIKE 'statement/%', IFNULL(CONCAT('\\n', wait_info), ''), ''),
                    IF (in_debug AND event_name LIKE 'wait%', wait_info, ''),
                    '", ',
                    CASE WHEN event_name LIKE 'wait/io/file%' THEN
                           'shape=box, style=filled, color=red'
                         WHEN event_name LIKE 'wait/io/table%' THEN
                           'shape=box, style=filled, color=green'
                         WHEN event_name LIKE 'wait/io/socket%' THEN
                           'shape=box, style=filled, color=yellow'
                         WHEN event_name LIKE 'wait/synch/mutex%' THEN
                           'style=filled, color=lightskyblue'
                         WHEN event_name LIKE 'wait/synch/cond%' THEN
                           'style=filled, color=darkseagreen3'
                         WHEN event_name LIKE 'wait/synch/rwlock%' THEN
                           'style=filled, color=orchid'
                         WHEN event_name LIKE 'wait/lock%' THEN
                           'shape=box, style=filled, color=tan'
                         WHEN event_name LIKE 'statement/%' THEN
                           CONCAT('shape=box, style=bold',
                                  CASE WHEN event_name LIKE 'statement/com/%' THEN
                                         ' style=filled, color=darkseagreen'
                                       ELSE
                                         IF((timer_wait/1000000000000) > @@long_query_time,
                                            ' style=filled, color=red',
                                            ' style=filled, color=lightblue')
                                  END
                           )
                         WHEN event_name LIKE 'stage/%' THEN
                           'style=filled, color=slategray3'
                         WHEN event_name LIKE '%idle%' THEN
                           'shape=box, style=filled, color=firebrick3'
                         ELSE '' END,
                     '];\n'
                   ) event, event_id
        FROM (
             (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id,
                     CONCAT(sql_text, '\\n',
                            'errors: ', errors, '\\n',
                            'warnings: ', warnings, '\\n',
                            'lock time: ', sys.format_time(lock_time),'\\n',
                            'rows affected: ', rows_affected, '\\n',
                            'rows sent: ', rows_sent, '\\n',
                            'rows examined: ', rows_examined, '\\n',
                            'tmp tables: ', created_tmp_tables, '\\n',
                            'tmp disk tables: ', created_tmp_disk_tables, '\\n'
                            'select scan: ', select_scan, '\\n',
                            'select full join: ', select_full_join, '\\n',
                            'select full range join: ', select_full_range_join, '\\n',
                            'select range: ', select_range, '\\n',
                            'select range check: ', select_range_check, '\\n',
                            'sort merge passes: ', sort_merge_passes, '\\n',
                            'sort rows: ', sort_rows, '\\n',
                            'sort range: ', sort_range, '\\n',
                            'sort scan: ', sort_scan, '\\n',
                            'no index used: ', IF(no_index_used, 'TRUE', 'FALSE'), '\\n',
                            'no good index used: ', IF(no_good_index_used, 'TRUE', 'FALSE'), '\\n'
                     ) AS wait_info
                FROM performance_schema.events_statements_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
             UNION
             (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id, null AS wait_info
                FROM performance_schema.events_stages_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
             UNION
             (SELECT thread_id, event_id,
                     CONCAT(event_name,
                            IF(event_name NOT LIKE 'wait/synch/mutex%', IFNULL(CONCAT(' - ', operation), ''), ''),
                            IF(number_of_bytes IS NOT NULL, CONCAT(' ', number_of_bytes, ' bytes'), ''),
                            IF(event_name LIKE 'wait/io/file%', '\\n', ''),
                            IF(object_schema IS NOT NULL, CONCAT('\\nObject: ', object_schema, '.'), ''),
                            IF(object_name IS NOT NULL,
                               IF (event_name LIKE 'wait/io/socket%',
                                   CONCAT('\\n', IF (object_name LIKE ':0%', @@socket, object_name)),
                                   object_name),
                               ''
                            ),
                            IF(index_name IS NOT NULL, CONCAT(' Index: ', index_name), ''), '\\n'
                     ) AS event_name,
                     timer_wait, timer_start, nesting_event_id, source AS wait_info
                FROM performance_schema.events_waits_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
           ) events
       ORDER BY event_id;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    IF (in_auto_setup) THEN
        CALL sys.ps_setup_save(0);
        DELETE FROM performance_schema.setup_actors;
        UPDATE performance_schema.threads
           SET INSTRUMENTED = IF(THREAD_ID = in_thread_id, 'YES', 'NO');
        UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
         WHERE NAME NOT LIKE '%\_history';
        UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES',
               TIMED   = 'YES';
    END IF;
    IF (in_start_fresh) THEN
        TRUNCATE performance_schema.events_statements_history_long;
        TRUNCATE performance_schema.events_stages_history_long;
        TRUNCATE performance_schema.events_waits_history_long;
    END IF;
    DROP TEMPORARY TABLE IF EXISTS tmp_events;
    CREATE TEMPORARY TABLE tmp_events (
      event_id bigint unsigned NOT NULL,
      event longblob,
      PRIMARY KEY (event_id)
    );
    INSERT INTO tmp_events VALUES (0, CONCAT('digraph events { rankdir=LR; nodesep=0.10;\n',
                                             '// Stack created .....: ', NOW(), '\n',
                                             '// MySQL version .....: ', VERSION(), '\n',
                                             '// MySQL hostname ....: ', @@hostname, '\n',
                                             '// MySQL port ........: ', @@port, '\n',
                                             '// MySQL socket ......: ', @@socket, '\n',
                                             '// MySQL user ........: ', CURRENT_USER(), '\n'));
    SELECT CONCAT('Data collection starting for THREAD_ID = ', in_thread_id) AS 'Info';
    SET v_min_event_id = 0,
        v_start        = UNIX_T7����QBEGIN
    DECLARE v_done bool DEFAULT FALSE;
    DECLARE v_start, v_runtime DECIMAL(20,2) DEFAULT 0.0;
    DECLARE v_min_event_id bigint unsigned DEFAULT 0;
    DECLARE v_this_thread_enabed ENUM('YES', 'NO');
    DECLARE v_event longtext;
    DECLARE c_stack CURSOR FOR
        SELECT CONCAT(IF(nesting_event_id IS NOT NULL, CONCAT(nesting_event_id, ' -> '), ''),
                    event_id, '; ', event_id, ' [label="',
                    '(', sys.format_time(timer_wait), ') ',
                    IF (event_name NOT LIKE 'wait/io%',
                        SUBSTRING_INDEX(event_name, '/', -2),
                        IF (event_name NOT LIKE 'wait/io/file%' OR event_name NOT LIKE 'wait/io/socket%',
                            SUBSTRING_INDEX(event_name, '/', -4),
                            event_name)
                        ),
                    IF (event_name LIKE 'statement/%', IFNULL(CONCAT('\n', wait_info), ''), ''),
                    IF (in_debug AND event_name LIKE 'wait%', wait_info, ''),
                    '", ',
                    CASE WHEN event_name LIKE 'wait/io/file%' THEN
                           'shape=box, style=filled, color=red'
                         WHEN event_name LIKE 'wait/io/table%' THEN
                           'shape=box, style=filled, color=green'
                         WHEN event_name LIKE 'wait/io/socket%' THEN
                           'shape=box, style=filled, color=yellow'
                         WHEN event_name LIKE 'wait/synch/mutex%' THEN
                           'style=filled, color=lightskyblue'
                         WHEN event_name LIKE 'wait/synch/cond%' THEN
                           'style=filled, color=darkseagreen3'
                         WHEN event_name LIKE 'wait/synch/rwlock%' THEN
                           'style=filled, color=orchid'
                         WHEN event_name LIKE 'wait/lock%' THEN
                           'shape=box, style=filled, color=tan'
                         WHEN event_name LIKE 'statement/%' THEN
                           CONCAT('shape=box, style=bold',
                                  CASE WHEN event_name LIKE 'statement/com/%' THEN
                                         ' style=filled, color=darkseagreen'
                                       ELSE
                                         IF((timer_wait/1000000000000) > @@long_query_time,
                                            ' style=filled, color=red',
                                            ' style=filled, color=lightblue')
                                  END
                           )
                         WHEN event_name LIKE 'stage/%' THEN
                           'style=filled, color=slategray3'
                         WHEN event_name LIKE '%idle%' THEN
                           'shape=box, style=filled, color=firebrick3'
                         ELSE '' END,
                     '];\n'
                   ) event, event_id
        FROM (
             (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id,
                     CONCAT(sql_text, '\n',
                            'errors: ', errors, '\n',
                            'warnings: ', warnings, '\n',
                            'lock time: ', sys.format_time(lock_time),'\n',
                            'rows affected: ', rows_affected, '\n',
                            'rows sent: ', rows_sent, '\n',
                            'rows examined: ', rows_examined, '\n',
                            'tmp tables: ', created_tmp_tables, '\n',
                            'tmp disk tables: ', created_tmp_disk_tables, '\n'
                            'select scan: ', select_scan, '\n',
                            'select full join: ', select_full_join, '\n',
                            'select full range join: ', select_full_range_join, '\n',
                            'select range: ', select_range, '\n',
                            'select range check: ', select_range_check, '\n',
                            'sort merge passes: ', sort_merge_passes, '\n',
                            'sort rows: ', sort_rows, '\n',
                            'sort range: ', sort_range, '\n',
                            'sort scan: ', sort_scan, '\n',
                            'no index used: ', IF(no_index_used, 'TRUE', 'FALSE'), '\n',
                            'no good index used: ', IF(no_good_index_used, 'TRUE', 'FALSE'), '\n'
                     ) AS wait_info
                FROM performance_schema.events_statements_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
             UNION
             (SELECT thread_id, event_id, event_name, timer_wait, timer_start, nesting_event_id, null AS wait_info
                FROM performance_schema.events_stages_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
             UNION
             (SELECT thread_id, event_id,
                     CONCAT(event_name,
                            IF(event_name NOT LIKE 'wait/synch/mutex%', IFNULL(CONCAT(' - ', operation), ''), ''),
                            IF(number_of_bytes IS NOT NULL, CONCAT(' ', number_of_bytes, ' bytes'), ''),
                            IF(event_name LIKE 'wait/io/file%', '\n', ''),
                            IF(object_schema IS NOT NULL, CONCAT('\nObject: ', object_schema, '.'), ''),
                            IF(object_name IS NOT NULL,
                               IF (event_name LIKE 'wait/io/socket%',
                                   CONCAT('\n', IF (object_name LIKE ':0%', @@socket, object_name)),
                                   object_name),
                               ''
                            ),
                            IF(index_name IS NOT NULL, CONCAT(' Index: ', index_name), ''), '\n'
                     ) AS event_name,
                     timer_wait, timer_start, nesting_event_id, source AS wait_info
                FROM performance_schema.events_waits_history_long
               WHERE thread_id = in_thread_id AND event_id > v_min_event_id)
           ) events
       ORDER BY event_id;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
    SET @log_bin := @@sql_log_bin;
    SET sql_log_bin = 0;
    SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    IF (in_auto_setup) THEN
        CALL sys.ps_setup_save(0);
        DELETE FROM performance_schema.setup_actors;
        UPDATE performance_schema.threads
           SET INSTRUMENTED = IF(THREAD_ID = in_thread_id, 'YES', 'NO');
        UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
         WHERE NAME NOT LIKE '%\_history';
        UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES',
               TIMED   = 'YES';
    END IF;
    IF (in_start_fresh) THEN
        TRUNCATE performance_schema.events_statements_history_long;
        TRUNCATE performance_schema.events_stages_history_long;
        TRUNCATE performance_schema.events_waits_history_long;
    END IF;
    DROP TEMPORARY TABLE IF EXISTS tmp_events;
    CREATE TEMPORARY TABLE tmp_events (
      event_id bigint unsigned NOT NULL,
      event longblob,
      PRIMARY KEY (event_id)
    );
    INSERT INTO tmp_events VALUES (0, CONCAT('digraph events { rankdir=LR; nodesep=0.10;\n',
                                             '// Stack created .....: ', NOW(), '\n',
                                             '// MySQL version .....: ', VERSION(), '\n',
                                             '// MySQL hostname ....: ', @@hostname, '\n',
                                             '// MySQL port ........: ', @@port, '\n',
                                             '// MySQL socket ......: ', @@socket, '\n',
                                             '// MySQL user ........: ', CURRENT_USER(), '\n'));
    SELECT CONCAT('Data collection starting for THREAD_ID = ', in_thread_id) AS 'Info';
    SET v_min_event_id = 0,
        v_start        = UNIX_TIMESTAMP(),
        in_in�$`~ޏUBEGIN
    DECLARE v_table_exists, v_tmp_digests_table_exists, v_custom_view_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY') DEFAULT '';
    DECLARE v_this_thread_enabled ENUM('YES', 'NO');
    DECLARE v_force_new_snapshot BOOLEAN DEFAULT FALSE;
    DECLARE v_digests_table VARCHAR(133);
    DECLARE v_quoted_table, v_quoted_custom_view VARCHAR(133) DEFAULT '';
    DECLARE v_table_db, v_table_name, v_custom_db, v_custom_name VARCHAR(64);
    DECLARE v_digest_table_template, v_checksum_ref, v_checksum_table text;
    DECLARE v_sql longtext;
    DECLARE v_error_msg VARCHAR(128);
    SELECT INSTRUMENTED INTO v_this_thread_enabled FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    END IF;
    SET @log_bin := @@sql_log_bin;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = 0;
    END IF;
    IF (@sys.statement_performance_analyzer.limit IS NULL) THEN
        SET @sys.statement_performance_analyzer.limit = sys.sys_get_config('statement_performance_analyzer.limit', '100');
    END IF;
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug                                = sys.sys_get_config('debug'                               , 'OFF');
    END IF;
    IF (in_table = 'NOW()') THEN
        SET v_force_new_snapshot = TRUE,
            in_table             = NULL;
    ELSEIF (in_table IS NOT NULL) THEN
        IF (NOT INSTR(in_table, '.')) THEN
            SET v_table_db   = DATABASE(),
                v_table_name = in_table;
        ELSE
            SET v_table_db   = SUBSTRING_INDEX(in_table, '.', 1);
            SET v_table_name = SUBSTRING(in_table, CHAR_LENGTH(v_table_db)+2);
        END IF;
        SET v_quoted_table = CONCAT('`', v_table_db, '`.`', v_table_name, '`');
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('in_table is: db = ''', v_table_db, ''', table = ''', v_table_name, '''') AS 'Debug';
        END IF;
        IF (v_table_db = DATABASE() AND (v_table_name = 'tmp_digests' OR v_table_name = 'tmp_digests_delta')) THEN
            SET v_error_msg = CONCAT('Invalid value for in_table: ', v_quoted_table, ' is reserved table name.');
            SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = v_error_msg;
        END IF;
        CALL sys.table_exists(v_table_db, v_table_name, v_table_exists);
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('v_table_exists = ', v_table_exists) AS 'Debug';
        END IF;
        IF (v_table_exists = 'BASE TABLE') THEN
            SET v_checksum_ref = (
                 SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, COLUMN_TYPE) ORDER BY ORDINAL_POSITION) AS Checksum
                   FROM information_schema.COLUMNS
                  WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'events_statements_summary_by_digest'
                ),
                v_checksum_table = (
                 SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, COLUMN_TYPE) ORDER BY ORDINAL_POSITION) AS Checksum
                   FROM information_schema.COLUMNS
                  WHERE TABLE_SCHEMA = v_table_db AND TABLE_NAME = v_table_name
                );
            IF (v_checksum_ref <> v_checksum_table) THEN
                SET v_error_msg = CONCAT('The table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 93, CONCAT('...', SUBSTRING(v_quoted_table, -90)), v_quoted_table),
                                         ' has the wrong definition.');
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        END IF;
    END IF;
    IF (in_views IS NULL OR in_views = '') THEN
        SET in_views = 'with_runtimes_in_95th_percentile,analysis,with_errors_or_warnings,with_full_table_scans,with_sorting,with_temp_tables';
    END IF;
    CALL sys.table_exists(DATABASE(), 'tmp_digests', v_tmp_digests_table_exists);
    IF (@sys.debug = 'ON') THEN
        SELECT CONCAT('v_tmp_digests_table_exists = ', v_tmp_digests_table_exists) AS 'Debug';
    END IF;
    CASE
        WHEN in_action IN ('snapshot', 'overall') THEN
            IF (in_table IS NOT NULL) THEN
                IF (NOT v_table_exists IN ('TEMPORARY', 'BASE TABLE')) THEN
                    SET v_error_msg = CONCAT('The ', in_action, ' action requires in_table to be NULL, NOW() or specify an existing table.',
                                             ' The table ',
                                             IF(CHAR_LENGTH(v_quoted_table) > 16, CONCAT('...', SUBSTRING(v_quoted_table, -13)), v_quoted_table),
                                             ' does not exist.');
                    SIGNAL SQLSTATE '45000'
                       SET MESSAGE_TEXT = v_error_msg;
                END IF;
            END IF;
        WHEN in_action IN ('delta', 'save') THEN
            IF (v_table_exists NOT IN ('TEMPORARY', 'BASE TABLE')) THEN
                SET v_error_msg = CONCAT('The ', in_action, ' action requires in_table to be an existing table.',
                                         IF(in_table IS NOT NULL, CONCAT(' The table ',
                                             IF(CHAR_LENGTH(v_quoted_table) > 39, CONCAT('...', SUBSTRING(v_quoted_table, -36)), v_quoted_table),
                                             ' does not exist.'), ''));
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
            IF (in_action = 'delta' AND v_tmp_digests_table_exists <> 'TEMPORARY') THEN
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = 'An existing snapshot generated with the statement_performance_analyzer() must exist.';
            END IF;
        WHEN in_action = 'create_tmp' THEN
            IF (v_table_exists = 'TEMPORARY') THEN
                SET v_error_msg = CONCAT('Cannot create the table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 72, CONCAT('...', SUBSTRING(v_quoted_table, -69)), v_quoted_table),
                                         ' as it already exists.');
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        WHEN in_action = 'create_table' THEN
            IF (v_table_exists <> '') THEN
                SET v_error_msg = CONCAT('Cannot create the table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 52, CONCAT('...', SUBSTRING(v_quoted_table, -49)), v_quoted_table),
                                         ' as it already exists',
                                         IF(v_table_exists = 'TEMPORARY', ' as a temporary table.', '.'));
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        WHEN in_action = 'cleanup' THEN
            DO (SELECT 1);
        ELSE
            SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = 'Unknown action. Supported actions are: cleanup, create_table, create_tmp, delta, overall, save, snapshot';
    END CASE;
    SET v_digest_table_template = 'CREATE %{TEMPORARY}TABLE %{TABLE_NAME} (
  `SCHEMA_NAME` varchar(64) DEFAULT NULL,
  `DIGEST` varchar(32) DEFAULT NULL,
  `DIGEST_TEXT` longtext,
  `COUNT_STAR` bigint(20) unsigned NOT NULL,
  `SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,
  `MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL,
  `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL,
  `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL,
  `SUM_LOCK_TIME` bigint(20) unsigned NOT NULL,
  `SUM_ERRORS` bigint(20) unsigned NOT NULL,
  `SUM_WARNINGS` bigint(20) unsigned NOT NULL,
  `SUM_ROWS_AFFECTED` bigint(20) unsigned NOT NULL,
  `SUM_ROWS_SENT` bigint(20) unsigned NOT NULL,
  `SUM_ROWS_EXAMINED` bigint(20) unsigned NOT NULL,
  `SUM_CREATED_TMP_DISK_TABLES` bigint(20) unsigned NOT NULL,
  `SUM_CREATED_TMP_TABLES` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_FULL_JOIN` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_FULL_RANGE_JOIN` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_RANGE` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_RANGE_CHECK` bigint(20) unsigned NOT NULL,
  `SUM_SELECT_SCAN` bigint(20) unsign�ynޏUed NOT NULL,
  `SUM_SORT_MERGE_PASSES` bigint(20) unsigned NOT NULL,
  `SUM_SORT_RANGE` bigint(20) unsigned NOT NULL,
  `SUM_SORT_ROWS` bigint(20) unsigned NOT NULL,
  `SUM_SORT_SCAN` bigint(20) unsigned NOT NULL,
  `SUM_NO_INDEX_USED` bigint(20) unsigned NOT NULL,
  `SUM_NO_GOOD_INDEX_USED` bigint(20) unsigned NOT NULL,
  `FIRST_SEEN` timestamp NULL DEFAULT NULL,
  `LAST_SEEN` timestamp NULL DEFAULT NULL,
  INDEX (SCHEMA_NAME, DIGEST)
) DEFAULT CHARSET=utf8';
    IF (v_force_new_snapshot
           OR in_action = 'snapshot'
           OR (in_action = 'overall' AND in_table IS NULL)
           OR (in_action = 'save' AND v_tmp_digests_table_exists <> 'TEMPORARY')
       ) THEN
        IF (v_tmp_digests_table_exists = 'TEMPORARY') THEN
            IF (@sys.debug = 'ON') THEN
                SELECT 'DROP TEMPORARY TABLE IF EXISTS tmp_digests' AS 'Debug';
            END IF;
            DROP TEMPORARY TABLE IF EXISTS tmp_digests;
        END IF;
        CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', 'TEMPORARY '), '%{TABLE_NAME}', 'tmp_digests'));
        SET v_sql = CONCAT('INSERT INTO tmp_digests SELECT * FROM ',
                           IF(in_table IS NULL OR in_action = 'save', 'performance_schema.events_statements_summary_by_digest', v_quoted_table));
        CALL sys.execute_prepared_stmt(v_sql);
    END IF;
    IF (in_action IN ('create_table', 'create_tmp')) THEN
        IF (in_action = 'create_table') THEN
            CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', ''), '%{TABLE_NAME}', v_quoted_table));
        ELSE
            CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', 'TEMPORARY '), '%{TABLE_NAME}', v_quoted_table));
        END IF;
    ELSEIF (in_action = 'save') THEN
        CALL sys.execute_prepared_stmt(CONCAT('DELETE FROM ', v_quoted_table));
        CALL sys.execute_prepared_stmt(CONCAT('INSERT INTO ', v_quoted_table, ' SELECT * FROM tmp_digests'));
    ELSEIF (in_action = 'cleanup') THEN
        DROP TEMPORARY TABLE IF EXISTS sys.tmp_digests;
        DROP TEMPORARY TABLE IF EXISTS sys.tmp_digests_delta;
    ELSEIF (in_action IN ('overall', 'delta')) THEN
        IF (in_action = 'overall') THEN
            IF (in_table IS NULL) THEN
                SET v_digests_table = 'tmp_digests';
            ELSE
                SET v_digests_table = v_quoted_table;
            END IF;
        ELSE
            SET v_digests_table = 'tmp_digests_delta';
            DROP TEMPORARY TABLE IF EXISTS tmp_digests_delta;
            CREATE TEMPORARY TABLE tmp_digests_delta LIKE tmp_digests;
            SET v_sql = CONCAT('INSERT INTO tmp_digests_delta
SELECT `d_end`.`SCHEMA_NAME`,
       `d_end`.`DIGEST`,
       `d_end`.`DIGEST_TEXT`,
       `d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0) AS ''COUNT_STAR'',
       `d_end`.`SUM_TIMER_WAIT`-IFNULL(`d_start`.`SUM_TIMER_WAIT`, 0) AS ''SUM_TIMER_WAIT'',
       `d_end`.`MIN_TIMER_WAIT` AS ''MIN_TIMER_WAIT'',
       IFNULL((`d_end`.`SUM_TIMER_WAIT`-IFNULL(`d_start`.`SUM_TIMER_WAIT`, 0))/NULLIF(`d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0), 0), 0) AS ''AVG_TIMER_WAIT'',
       `d_end`.`MAX_TIMER_WAIT` AS ''MAX_TIMER_WAIT'',
       `d_end`.`SUM_LOCK_TIME`-IFNULL(`d_start`.`SUM_LOCK_TIME`, 0) AS ''SUM_LOCK_TIME'',
       `d_end`.`SUM_ERRORS`-IFNULL(`d_start`.`SUM_ERRORS`, 0) AS ''SUM_ERRORS'',
       `d_end`.`SUM_WARNINGS`-IFNULL(`d_start`.`SUM_WARNINGS`, 0) AS ''SUM_WARNINGS'',
       `d_end`.`SUM_ROWS_AFFECTED`-IFNULL(`d_start`.`SUM_ROWS_AFFECTED`, 0) AS ''SUM_ROWS_AFFECTED'',
       `d_end`.`SUM_ROWS_SENT`-IFNULL(`d_start`.`SUM_ROWS_SENT`, 0) AS ''SUM_ROWS_SENT'',
       `d_end`.`SUM_ROWS_EXAMINED`-IFNULL(`d_start`.`SUM_ROWS_EXAMINED`, 0) AS ''SUM_ROWS_EXAMINED'',
       `d_end`.`SUM_CREATED_TMP_DISK_TABLES`-IFNULL(`d_start`.`SUM_CREATED_TMP_DISK_TABLES`, 0) AS ''SUM_CREATED_TMP_DISK_TABLES'',
       `d_end`.`SUM_CREATED_TMP_TABLES`-IFNULL(`d_start`.`SUM_CREATED_TMP_TABLES`, 0) AS ''SUM_CREATED_TMP_TABLES'',
       `d_end`.`SUM_SELECT_FULL_JOIN`-IFNULL(`d_start`.`SUM_SELECT_FULL_JOIN`, 0) AS ''SUM_SELECT_FULL_JOIN'',
       `d_end`.`SUM_SELECT_FULL_RANGE_JOIN`-IFNULL(`d_start`.`SUM_SELECT_FULL_RANGE_JOIN`, 0) AS ''SUM_SELECT_FULL_RANGE_JOIN'',
       `d_end`.`SUM_SELECT_RANGE`-IFNULL(`d_start`.`SUM_SELECT_RANGE`, 0) AS ''SUM_SELECT_RANGE'',
       `d_end`.`SUM_SELECT_RANGE_CHECK`-IFNULL(`d_start`.`SUM_SELECT_RANGE_CHECK`, 0) AS ''SUM_SELECT_RANGE_CHECK'',
       `d_end`.`SUM_SELECT_SCAN`-IFNULL(`d_start`.`SUM_SELECT_SCAN`, 0) AS ''SUM_SELECT_SCAN'',
       `d_end`.`SUM_SORT_MERGE_PASSES`-IFNULL(`d_start`.`SUM_SORT_MERGE_PASSES`, 0) AS ''SUM_SORT_MERGE_PASSES'',
       `d_end`.`SUM_SORT_RANGE`-IFNULL(`d_start`.`SUM_SORT_RANGE`, 0) AS ''SUM_SORT_RANGE'',
       `d_end`.`SUM_SORT_ROWS`-IFNULL(`d_start`.`SUM_SORT_ROWS`, 0) AS ''SUM_SORT_ROWS'',
       `d_end`.`SUM_SORT_SCAN`-IFNULL(`d_start`.`SUM_SORT_SCAN`, 0) AS ''SUM_SORT_SCAN'',
       `d_end`.`SUM_NO_INDEX_USED`-IFNULL(`d_start`.`SUM_NO_INDEX_USED`, 0) AS ''SUM_NO_INDEX_USED'',
       `d_end`.`SUM_NO_GOOD_INDEX_USED`-IFNULL(`d_start`.`SUM_NO_GOOD_INDEX_USED`, 0) AS ''SUM_NO_GOOD_INDEX_USED'',
       `d_end`.`FIRST_SEEN`,
       `d_end`.`LAST_SEEN`
  FROM tmp_digests d_end
       LEFT OUTER JOIN ', v_quoted_table, ' d_start ON `d_start`.`DIGEST` = `d_end`.`DIGEST`
                                                    AND (`d_start`.`SCHEMA_NAME` = `d_end`.`SCHEMA_NAME`
                                                          OR (`d_start`.`SCHEMA_NAME` IS NULL AND `d_end`.`SCHEMA_NAME` IS NULL)
                                                        )
 WHERE `d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0) > 0');
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_runtimes_in_95th_percentile', in_views)) THEN
            SELECT 'Queries with Runtime in 95th Percentile' AS 'Next Output';
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_avg_latency_distribution1;
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_avg_latency_distribution2;
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_95th_percentile_by_avg_us;
            CREATE TEMPORARY TABLE tmp_digest_avg_latency_distribution1 (
              cnt bigint unsigned NOT NULL,
              avg_us decimal(21,0) NOT NULL,
              PRIMARY KEY (avg_us)
            ) ENGINE=InnoDB;
            SET v_sql = CONCAT('INSERT INTO tmp_digest_avg_latency_distribution1
SELECT COUNT(*) cnt,
       ROUND(avg_timer_wait/1000000) AS avg_us
  FROM ', v_digests_table, '
 GROUP BY avg_us');
            CALL sys.execute_prepared_stmt(v_sql);
            CREATE TEMPORARY TABLE tmp_digest_avg_latency_distribution2 LIKE tmp_digest_avg_latency_distribution1;
            INSERT INTO tmp_digest_avg_latency_distribution2 SELECT * FROM tmp_digest_avg_latency_distribution1;
            CREATE TEMPORARY TABLE tmp_digest_95th_percentile_by_avg_us (
              avg_us decimal(21,0) NOT NULL,
              percentile decimal(46,4) NOT NULL,
              PRIMARY KEY (avg_us)
            ) ENGINE=InnoDB;
            SET v_sql = CONCAT('INSERT INTO tmp_digest_95th_percentile_by_avg_us
SELECT s2.avg_us avg_us,
       IFNULL(SUM(s1.cnt)/NULLIF((SELECT COUNT(*) FROM ', v_digests_table, '), 0), 0) percentile
  FROM tmp_digest_avg_latency_distribution1 AS s1
       JOIN tmp_digest_avg_latency_distribution2 AS s2 ON s1.avg_us <= s2.avg_us
 GROUP BY s2.avg_us
HAVING percentile > 0.95
 ORDER BY percentile
 LIMIT 1');
            CALL sys.execute_prepared_stmt(v_sql);
            SET v_sql =
                REPLACE(
                    REPLACE(
                        (SELECT VIEW_DEFINITION
                           FROM information_schema.VIEWS
                          WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_runtimes_in_95th_percentile'
                        ),
                        '`performance_schema`.`events_statements_summary_by_digest`',
                        v_digests_table
                    ),
                    'sys.x$ps_digest_95th_percentile_by_avg_us',
                �F.�ޏU    '`sys`.`x$ps_digest_95th_percentile_by_avg_us`'
              );
            CALL sys.execute_prepared_stmt(v_sql);
            DROP TEMPORARY TABLE tmp_digest_avg_latency_distribution1;
            DROP TEMPORARY TABLE tmp_digest_avg_latency_distribution2;
            DROP TEMPORARY TABLE tmp_digest_95th_percentile_by_avg_us;
        END IF;
        IF (FIND_IN_SET('analysis', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries Ordered by Total Latency') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statement_analysis'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_errors_or_warnings', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Errors') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_errors_or_warnings'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_full_table_scans', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Full Table Scan') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_full_table_scans'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_sorting', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Sorting') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_sorting'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_temp_tables', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Internal Temporary Tables') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_temp_tables'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('custom', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries Using Custom View') AS 'Next Output';
            IF (@sys.statement_performance_analyzer.view IS NULL) THEN
                SET @sys.statement_performance_analyzer.view = sys.sys_get_config('statement_performance_analyzer.view', NULL);
            END IF;
            IF (@sys.statement_performance_analyzer.view IS NULL) THEN
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = 'The @sys.statement_performance_analyzer.view user variable must be set with the view or query to use.';
            END IF;
            IF (NOT INSTR(@sys.statement_performance_analyzer.view, ' ')) THEN
                IF (NOT INSTR(@sys.statement_performance_analyzer.view, '.')) THEN
                    SET v_custom_db   = DATABASE(),
                        v_custom_name = @sys.statement_performance_analyzer.view;
                ELSE
                    SET v_custom_db   = SUBSTRING_INDEX(@sys.statement_performance_analyzer.view, '.', 1);
                    SET v_custom_name = SUBSTRING(@sys.statement_performance_analyzer.view, CHAR_LENGTH(v_custom_db)+2);
                END IF;
                CALL sys.table_exists(v_custom_db, v_custom_name, v_custom_view_exists);
                IF (v_custom_view_exists <> 'VIEW') THEN
                    SIGNAL SQLSTATE '45000'
                       SET MESSAGE_TEXT = 'The @sys.statement_performance_analyzer.view user variable is set but specified neither an existing view nor a query.';
                END IF;
                SET v_sql =
                    REPLACE(
                        (SELECT VIEW_DEFINITION
                           FROM information_schema.VIEWS
                          WHERE TABLE_SCHEMA = v_custom_db AND TABLE_NAME = v_custom_name
                        ),
                        '`performance_schema`.`events_statements_summary_by_digest`',
                        v_digests_table
                    );
            ELSE
                SET v_sql = REPLACE(@sys.statement_performance_analyzer.view, '`performance_schema`.`events_statements_summary_by_digest`', v_digests_table);
            END IF;
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
    END IF;
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = @log_bin;
    END IF;
END�+�RޏU
             Description
             Create a report of the statements running on the server.
             The views are calculated based on the overall and/or delta activity.
             Requires the SUPER privilege for "SET sql_log_bin = 0;".
             Parameters
             in_action (ENUM('snapshot', 'overall', 'delta', 'create_tmp', 'create_table', 'save', 'cleanup')):
               The action to take. Supported actions are:
                 * snapshot      Store a snapshot. The default is to make a snapshot of the current content of
                                 performance_schema.events_statements_summary_by_digest, but by setting in_table
                                 this can be overwritten to copy the content of the specified table.
                                 The snapshot is stored in the sys.tmp_digests temporary table.
                 * overall       Generate analyzis based on the content specified by in_table. For the overall analyzis,
                                 in_table can be NOW() to use a fresh snapshot. This will overwrite an existing snapshot.
                                 Use NULL for in_table to use the existing snapshot. If in_table IS NULL and no snapshot
                                 exists, a new will be created.
                                 See also in_views and @sys.statement_performance_analyzer.limit.
                 * delta         Generate a delta analysis. The delta will be calculated between the reference table in
                                 in_table and the snapshot. An existing snapshot must exist.
                                 The action uses the sys.tmp_digests_delta temporary table.
                                 See also in_views and @sys.statement_performance_analyzer.limit.
                 * create_table  Create a regular table suitable for storing the snapshot for later use, e.g. for
                                 calculating deltas.
                 * create_tmp    Create a temporary table suitable for storing the snapshot for later use, e.g. for
                                 calculating deltas.
                 * save          Save the snapshot in the table specified by in_table. The table must exists and have
                                 the correct structure.
                                 If no snapshot exists, a new is created.
                 * cleanup       Remove the temporary tables used for the snapshot and delta.
             in_table (VARCHAR(129)):
               The table argument used for some actions. Use the format 'db1.t1' or 't1' without using any backticks (`)
               for quoting. Periods (.) are not supported in the database and table names.
               The meaning of the table for each action supporting the argument is:
                 * snapshot      The snapshot is created based on the specified table. Set to NULL or NOW() to use
                                 the current content of performance_schema.events_statements_summary_by_digest.
                 * overall       The table with the content to create the overall analyzis for. The following values
                                 can be used:
                                   - A table name - use the content of that table.
                                   - NOW()        - create a fresh snapshot and overwrite the existing snapshot.
                                   - NULL         - use the last stored snapshot.
                 * delta         The table name is mandatory and specified the reference view to compare the currently
                                 stored snapshot against. If no snapshot exists, a new will be created.
                 * create_table  The name of the regular table to create.
                 * create_tmp    The name of the temporary table to create.
                 * save          The name of the table to save the currently stored snapshot into.
             in_views (SET ('with_runtimes_in_95th_percentile', 'analysis', 'with_errors_or_warnings',
                            'with_full_table_scans', 'with_sorting', 'with_temp_tables', 'custom'))
               Which views to include:
                 * with_runtimes_in_95th_percentile  Based on the sys.statements_with_runtimes_in_95th_percentile view
                 * analysis                          Based on the sys.statement_analysis view
                 * with_errors_or_warnings           Based on the sys.statements_with_errors_or_warnings view
                 * with_full_table_scans             Based on the sys.statements_with_full_table_scans view
                 * with_sorting                      Based on the sys.statements_with_sorting view
                 * with_temp_tables                  Based on the sys.statements_with_temp_tables view
                 * custom                            Use a custom view. This view must be specified in @sys.statement_performance_analyzer.view to an existing view or a query
             Default is to include all except 'custom'.
             Configuration Options
             sys.statement_performance_analyzer.limit
               The maximum number of rows to include for the views that does not have a built-in limit (e.g. the 95th percentile view).
               If not set the limit is 100.
             sys.statement_performance_analyzer.view
               Used together with the 'custom' view. If the value contains a space, it is considered a query, otherwise it must be
               an existing view querying the performance_schema.events_statements_summary_by_digest table. There cannot be any limit
               clause including in the query or view definition if @sys.statement_performance_analyzer.limit > 0.
               If specifying a view, use the same format as for in_table.
             sys.debug
               Whether to provide debugging output.
               Default is 'OFF'. Set to 'ON' to include.
             Example
             To create a report with the queries in the 95th percentile since last truncate of performance_schema.events_statements_summary_by_digest
             and the delta for a 1 minute period:
                1. Create a temporary table to store the initial snapshot.
                2. Create the initial snapshot.
                3. Save the initial snapshot in the temporary table.
                4. Wait one minute.
                5. Create a new snapshot.
                6. Perform analyzis based on the new snapshot.
                7. Perform analyzis based on the delta between the initial and new snapshots.
             mysql> CALL sys.statement_performance_analyzer('create_tmp', 'mydb.tmp_digests_ini', NULL);
             Query OK, 0 rows affected (0.08 sec)
             mysql> CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
             Query OK, 0 rows affected (0.02 sec)
             mysql> CALL sys.statement_performance_analyzer('save', 'mydb.tmp_digests_ini', NULL);
             Query OK, 0 rows affected (0.00 sec)
             mysql> DO SLEEP(60);
             Query OK, 0 rows affected (1 min 0.00 sec)
             mysql> CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);
             Query OK, 0 rows affected (0.02 sec)
             mysql> CALL sys.statement_performance_analyzer('overall', NULL, 'with_runtimes_in_95th_percentile');
             +-----------------------------------------+
             | Next Output                             |
             +-----------------------------------------+
             | Queries with Runtime in 95th Percentile |
             +-----------------------------------------+
             1 row in set (0.05 sec)
             ...
             mysql> CALL sys.statement_performance_analyzer('delta', 'mydb.tmp_digests_ini', 'with_runtimes_in_95th_percentile');
             +-----------------------------------------+
             | Next Output                             |
             +-----------------------------------------+
             | Queries with Runtime in 95th Percentile |
             +-----------------------------------------+
             1 row in set (0.03 sec)
       =���ޏUBEGIN
    DECLARE v_table_exists, v_tmp_digests_table_exists, v_custom_view_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY') DEFAULT '';
    DECLARE v_this_thread_enabled ENUM('YES', 'NO');
    DECLARE v_force_new_snapshot BOOLEAN DEFAULT FALSE;
    DECLARE v_digests_table VARCHAR(133);
    DECLARE v_quoted_table, v_quoted_custom_view VARCHAR(133) DEFAULT '';
    DECLARE v_table_db, v_table_name, v_custom_db, v_custom_name VARCHAR(64);
    DECLARE v_digest_table_template, v_checksum_ref, v_checksum_table text;
    DECLARE v_sql longtext;
    DECLARE v_error_msg VARCHAR(128);
    SELECT INSTRUMENTED INTO v_this_thread_enabled FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_disable_thread(CONNECTION_ID());
    END IF;
    SET @log_bin := @@sql_log_bin;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = 0;
    END IF;
    IF (@sys.statement_performance_analyzer.limit IS NULL) THEN
        SET @sys.statement_performance_analyzer.limit = sys.sys_get_config('statement_performance_analyzer.limit', '100');
    END IF;
    IF (@sys.debug IS NULL) THEN
        SET @sys.debug                                = sys.sys_get_config('debug'                               , 'OFF');
    END IF;
    IF (in_table = 'NOW()') THEN
        SET v_force_new_snapshot = TRUE,
            in_table             = NULL;
    ELSEIF (in_table IS NOT NULL) THEN
        IF (NOT INSTR(in_table, '.')) THEN
            SET v_table_db   = DATABASE(),
                v_table_name = in_table;
        ELSE
            SET v_table_db   = SUBSTRING_INDEX(in_table, '.', 1);
            SET v_table_name = SUBSTRING(in_table, CHAR_LENGTH(v_table_db)+2);
        END IF;
        SET v_quoted_table = CONCAT('`', v_table_db, '`.`', v_table_name, '`');
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('in_table is: db = ''', v_table_db, ''', table = ''', v_table_name, '''') AS 'Debug';
        END IF;
        IF (v_table_db = DATABASE() AND (v_table_name = 'tmp_digests' OR v_table_name = 'tmp_digests_delta')) THEN
            SET v_error_msg = CONCAT('Invalid value for in_table: ', v_quoted_table, ' is reserved table name.');
            SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = v_error_msg;
        END IF;
        CALL sys.table_exists(v_table_db, v_table_name, v_table_exists);
        IF (@sys.debug = 'ON') THEN
            SELECT CONCAT('v_table_exists = ', v_table_exists) AS 'Debug';
        END IF;
        IF (v_table_exists = 'BASE TABLE') THEN
            SET v_checksum_ref = (
                 SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, COLUMN_TYPE) ORDER BY ORDINAL_POSITION) AS Checksum
                   FROM information_schema.COLUMNS
                  WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'events_statements_summary_by_digest'
                ),
                v_checksum_table = (
                 SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, COLUMN_TYPE) ORDER BY ORDINAL_POSITION) AS Checksum
                   FROM information_schema.COLUMNS
                  WHERE TABLE_SCHEMA = v_table_db AND TABLE_NAME = v_table_name
                );
            IF (v_checksum_ref <> v_checksum_table) THEN
                SET v_error_msg = CONCAT('The table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 93, CONCAT('...', SUBSTRING(v_quoted_table, -90)), v_quoted_table),
                                         ' has the wrong definition.');
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        END IF;
    END IF;
    IF (in_views IS NULL OR in_views = '') THEN
        SET in_views = 'with_runtimes_in_95th_percentile,analysis,with_errors_or_warnings,with_full_table_scans,with_sorting,with_temp_tables';
    END IF;
    CALL sys.table_exists(DATABASE(), 'tmp_digests', v_tmp_digests_table_exists);
    IF (@sys.debug = 'ON') THEN
        SELECT CONCAT('v_tmp_digests_table_exists = ', v_tmp_digests_table_exists) AS 'Debug';
    END IF;
    CASE
        WHEN in_action IN ('snapshot', 'overall') THEN
            IF (in_table IS NOT NULL) THEN
                IF (NOT v_table_exists IN ('TEMPORARY', 'BASE TABLE')) THEN
                    SET v_error_msg = CONCAT('The ', in_action, ' action requires in_table to be NULL, NOW() or specify an existing table.',
                                             ' The table ',
                                             IF(CHAR_LENGTH(v_quoted_table) > 16, CONCAT('...', SUBSTRING(v_quoted_table, -13)), v_quoted_table),
                                             ' does not exist.');
                    SIGNAL SQLSTATE '45000'
                       SET MESSAGE_TEXT = v_error_msg;
                END IF;
            END IF;
        WHEN in_action IN ('delta', 'save') THEN
            IF (v_table_exists NOT IN ('TEMPORARY', 'BASE TABLE')) THEN
                SET v_error_msg = CONCAT('The ', in_action, ' action requires in_table to be an existing table.',
                                         IF(in_table IS NOT NULL, CONCAT(' The table ',
                                             IF(CHAR_LENGTH(v_quoted_table) > 39, CONCAT('...', SUBSTRING(v_quoted_table, -36)), v_quoted_table),
                                             ' does not exist.'), ''));
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
            IF (in_action = 'delta' AND v_tmp_digests_table_exists <> 'TEMPORARY') THEN
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = 'An existing snapshot generated with the statement_performance_analyzer() must exist.';
            END IF;
        WHEN in_action = 'create_tmp' THEN
            IF (v_table_exists = 'TEMPORARY') THEN
                SET v_error_msg = CONCAT('Cannot create the table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 72, CONCAT('...', SUBSTRING(v_quoted_table, -69)), v_quoted_table),
                                         ' as it already exists.');
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        WHEN in_action = 'create_table' THEN
            IF (v_table_exists <> '') THEN
                SET v_error_msg = CONCAT('Cannot create the table ',
                                         IF(CHAR_LENGTH(v_quoted_table) > 52, CONCAT('...', SUBSTRING(v_quoted_table, -49)), v_quoted_table),
                                         ' as it already exists',
                                         IF(v_table_exists = 'TEMPORARY', ' as a temporary table.', '.'));
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = v_error_msg;
            END IF;
        WHEN in_action = 'cleanup' THEN
            DO (SELECT 1);
        ELSE
            SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = 'Unknown action. Supported actions are: cleanup, create_table, create_tmp, delta, overall, save, snapshot';
    END CASE;
    SET v_digest_table_template = 'CREATE %{TEMPORARY}TABLE %{TABLE_NAME} (\n  `SCHEMA_NAME` varchar(64) DEFAULT NULL,\n  `DIGEST` varchar(32) DEFAULT NULL,\n  `DIGEST_TEXT` longtext,\n  `COUNT_STAR` bigint(20) unsigned NOT NULL,\n  `SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,\n  `MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL,\n  `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL,\n  `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL,\n  `SUM_LOCK_TIME` bigint(20) unsigned NOT NULL,\n  `SUM_ERRORS` bigint(20) unsigned NOT NULL,\n  `SUM_WARNINGS` bigint(20) unsigned NOT NULL,\n  `SUM_ROWS_AFFECTED` bigint(20) unsigned NOT NULL,\n  `SUM_ROWS_SENT` bigint(20) unsigned NOT NULL,\n  `SUM_ROWS_EXAMINED` bigint(20) unsigned NOT NULL,\n  `SUM_CREATED_TMP_DISK_TABLES` bigint(20) unsigned NOT NULL,\n  `SUM_CREATED_TMP_TABLES` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_FULL_JOIN` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_FULL_RANGE_JOIN` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_RANGE` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_RANGE_CHECK` bigint(20) unsigned NOT NULL,\n  `SUM_SELECT_SC/��ޏUAN` bigint(20) unsigned NOT NULL,\n  `SUM_SORT_MERGE_PASSES` bigint(20) unsigned NOT NULL,\n  `SUM_SORT_RANGE` bigint(20) unsigned NOT NULL,\n  `SUM_SORT_ROWS` bigint(20) unsigned NOT NULL,\n  `SUM_SORT_SCAN` bigint(20) unsigned NOT NULL,\n  `SUM_NO_INDEX_USED` bigint(20) unsigned NOT NULL,\n  `SUM_NO_GOOD_INDEX_USED` bigint(20) unsigned NOT NULL,\n  `FIRST_SEEN` timestamp NULL DEFAULT NULL,\n  `LAST_SEEN` timestamp NULL DEFAULT NULL,\n  INDEX (SCHEMA_NAME, DIGEST)\n) DEFAULT CHARSET=utf8';
    IF (v_force_new_snapshot
           OR in_action = 'snapshot'
           OR (in_action = 'overall' AND in_table IS NULL)
           OR (in_action = 'save' AND v_tmp_digests_table_exists <> 'TEMPORARY')
       ) THEN
        IF (v_tmp_digests_table_exists = 'TEMPORARY') THEN
            IF (@sys.debug = 'ON') THEN
                SELECT 'DROP TEMPORARY TABLE IF EXISTS tmp_digests' AS 'Debug';
            END IF;
            DROP TEMPORARY TABLE IF EXISTS tmp_digests;
        END IF;
        CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', 'TEMPORARY '), '%{TABLE_NAME}', 'tmp_digests'));
        SET v_sql = CONCAT('INSERT INTO tmp_digests SELECT * FROM ',
                           IF(in_table IS NULL OR in_action = 'save', 'performance_schema.events_statements_summary_by_digest', v_quoted_table));
        CALL sys.execute_prepared_stmt(v_sql);
    END IF;
    IF (in_action IN ('create_table', 'create_tmp')) THEN
        IF (in_action = 'create_table') THEN
            CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', ''), '%{TABLE_NAME}', v_quoted_table));
        ELSE
            CALL sys.execute_prepared_stmt(REPLACE(REPLACE(v_digest_table_template, '%{TEMPORARY}', 'TEMPORARY '), '%{TABLE_NAME}', v_quoted_table));
        END IF;
    ELSEIF (in_action = 'save') THEN
        CALL sys.execute_prepared_stmt(CONCAT('DELETE FROM ', v_quoted_table));
        CALL sys.execute_prepared_stmt(CONCAT('INSERT INTO ', v_quoted_table, ' SELECT * FROM tmp_digests'));
    ELSEIF (in_action = 'cleanup') THEN
        DROP TEMPORARY TABLE IF EXISTS sys.tmp_digests;
        DROP TEMPORARY TABLE IF EXISTS sys.tmp_digests_delta;
    ELSEIF (in_action IN ('overall', 'delta')) THEN
        IF (in_action = 'overall') THEN
            IF (in_table IS NULL) THEN
                SET v_digests_table = 'tmp_digests';
            ELSE
                SET v_digests_table = v_quoted_table;
            END IF;
        ELSE
            SET v_digests_table = 'tmp_digests_delta';
            DROP TEMPORARY TABLE IF EXISTS tmp_digests_delta;
            CREATE TEMPORARY TABLE tmp_digests_delta LIKE tmp_digests;
            SET v_sql = CONCAT('INSERT INTO tmp_digests_delta\nSELECT `d_end`.`SCHEMA_NAME`,\n       `d_end`.`DIGEST`,\n       `d_end`.`DIGEST_TEXT`,\n       `d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0) AS ''COUNT_STAR'',\n       `d_end`.`SUM_TIMER_WAIT`-IFNULL(`d_start`.`SUM_TIMER_WAIT`, 0) AS ''SUM_TIMER_WAIT'',\n       `d_end`.`MIN_TIMER_WAIT` AS ''MIN_TIMER_WAIT'',\n       IFNULL((`d_end`.`SUM_TIMER_WAIT`-IFNULL(`d_start`.`SUM_TIMER_WAIT`, 0))/NULLIF(`d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0), 0), 0) AS ''AVG_TIMER_WAIT'',\n       `d_end`.`MAX_TIMER_WAIT` AS ''MAX_TIMER_WAIT'',\n       `d_end`.`SUM_LOCK_TIME`-IFNULL(`d_start`.`SUM_LOCK_TIME`, 0) AS ''SUM_LOCK_TIME'',\n       `d_end`.`SUM_ERRORS`-IFNULL(`d_start`.`SUM_ERRORS`, 0) AS ''SUM_ERRORS'',\n       `d_end`.`SUM_WARNINGS`-IFNULL(`d_start`.`SUM_WARNINGS`, 0) AS ''SUM_WARNINGS'',\n       `d_end`.`SUM_ROWS_AFFECTED`-IFNULL(`d_start`.`SUM_ROWS_AFFECTED`, 0) AS ''SUM_ROWS_AFFECTED'',\n       `d_end`.`SUM_ROWS_SENT`-IFNULL(`d_start`.`SUM_ROWS_SENT`, 0) AS ''SUM_ROWS_SENT'',\n       `d_end`.`SUM_ROWS_EXAMINED`-IFNULL(`d_start`.`SUM_ROWS_EXAMINED`, 0) AS ''SUM_ROWS_EXAMINED'',\n       `d_end`.`SUM_CREATED_TMP_DISK_TABLES`-IFNULL(`d_start`.`SUM_CREATED_TMP_DISK_TABLES`, 0) AS ''SUM_CREATED_TMP_DISK_TABLES'',\n       `d_end`.`SUM_CREATED_TMP_TABLES`-IFNULL(`d_start`.`SUM_CREATED_TMP_TABLES`, 0) AS ''SUM_CREATED_TMP_TABLES'',\n       `d_end`.`SUM_SELECT_FULL_JOIN`-IFNULL(`d_start`.`SUM_SELECT_FULL_JOIN`, 0) AS ''SUM_SELECT_FULL_JOIN'',\n       `d_end`.`SUM_SELECT_FULL_RANGE_JOIN`-IFNULL(`d_start`.`SUM_SELECT_FULL_RANGE_JOIN`, 0) AS ''SUM_SELECT_FULL_RANGE_JOIN'',\n       `d_end`.`SUM_SELECT_RANGE`-IFNULL(`d_start`.`SUM_SELECT_RANGE`, 0) AS ''SUM_SELECT_RANGE'',\n       `d_end`.`SUM_SELECT_RANGE_CHECK`-IFNULL(`d_start`.`SUM_SELECT_RANGE_CHECK`, 0) AS ''SUM_SELECT_RANGE_CHECK'',\n       `d_end`.`SUM_SELECT_SCAN`-IFNULL(`d_start`.`SUM_SELECT_SCAN`, 0) AS ''SUM_SELECT_SCAN'',\n       `d_end`.`SUM_SORT_MERGE_PASSES`-IFNULL(`d_start`.`SUM_SORT_MERGE_PASSES`, 0) AS ''SUM_SORT_MERGE_PASSES'',\n       `d_end`.`SUM_SORT_RANGE`-IFNULL(`d_start`.`SUM_SORT_RANGE`, 0) AS ''SUM_SORT_RANGE'',\n       `d_end`.`SUM_SORT_ROWS`-IFNULL(`d_start`.`SUM_SORT_ROWS`, 0) AS ''SUM_SORT_ROWS'',\n       `d_end`.`SUM_SORT_SCAN`-IFNULL(`d_start`.`SUM_SORT_SCAN`, 0) AS ''SUM_SORT_SCAN'',\n       `d_end`.`SUM_NO_INDEX_USED`-IFNULL(`d_start`.`SUM_NO_INDEX_USED`, 0) AS ''SUM_NO_INDEX_USED'',\n       `d_end`.`SUM_NO_GOOD_INDEX_USED`-IFNULL(`d_start`.`SUM_NO_GOOD_INDEX_USED`, 0) AS ''SUM_NO_GOOD_INDEX_USED'',\n       `d_end`.`FIRST_SEEN`,\n       `d_end`.`LAST_SEEN`\n  FROM tmp_digests d_end\n       LEFT OUTER JOIN ', v_quoted_table, ' d_start ON `d_start`.`DIGEST` = `d_end`.`DIGEST`\n                                                    AND (`d_start`.`SCHEMA_NAME` = `d_end`.`SCHEMA_NAME`\n                                                          OR (`d_start`.`SCHEMA_NAME` IS NULL AND `d_end`.`SCHEMA_NAME` IS NULL)\n                                                        )\n WHERE `d_end`.`COUNT_STAR`-IFNULL(`d_start`.`COUNT_STAR`, 0) > 0');
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_runtimes_in_95th_percentile', in_views)) THEN
            SELECT 'Queries with Runtime in 95th Percentile' AS 'Next Output';
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_avg_latency_distribution1;
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_avg_latency_distribution2;
            DROP TEMPORARY TABLE IF EXISTS tmp_digest_95th_percentile_by_avg_us;
            CREATE TEMPORARY TABLE tmp_digest_avg_latency_distribution1 (
              cnt bigint unsigned NOT NULL,
              avg_us decimal(21,0) NOT NULL,
              PRIMARY KEY (avg_us)
            ) ENGINE=InnoDB;
            SET v_sql = CONCAT('INSERT INTO tmp_digest_avg_latency_distribution1\nSELECT COUNT(*) cnt,\n       ROUND(avg_timer_wait/1000000) AS avg_us\n  FROM ', v_digests_table, '\n GROUP BY avg_us');
            CALL sys.execute_prepared_stmt(v_sql);
            CREATE TEMPORARY TABLE tmp_digest_avg_latency_distribution2 LIKE tmp_digest_avg_latency_distribution1;
            INSERT INTO tmp_digest_avg_latency_distribution2 SELECT * FROM tmp_digest_avg_latency_distribution1;
            CREATE TEMPORARY TABLE tmp_digest_95th_percentile_by_avg_us (
              avg_us decimal(21,0) NOT NULL,
              percentile decimal(46,4) NOT NULL,
              PRIMARY KEY (avg_us)
            ) ENGINE=InnoDB;
            SET v_sql = CONCAT('INSERT INTO tmp_digest_95th_percentile_by_avg_us\nSELECT s2.avg_us avg_us,\n       IFNULL(SUM(s1.cnt)/NULLIF((SELECT COUNT(*) FROM ', v_digests_table, '), 0), 0) percentile\n  FROM tmp_digest_avg_latency_distribution1 AS s1\n       JOIN tmp_digest_avg_latency_distribution2 AS s2 ON s1.avg_us <= s2.avg_us\n GROUP BY s2.avg_us\nHAVING percentile > 0.95\n ORDER BY percentile\n LIMIT 1');
            CALL sys.execute_prepared_stmt(v_sql);
            SET v_sql =
                REPLACE(
                    REPLACE(
                        (SELECT VIEW_DEFINITION
                           FROM information_schema.VIEWS
                          WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_runtimes_in_95th_percentile'
                        ),
                        '`performance_schema`.`events_statements_summary_by_digest`',
                        v_digests_table
                    ),
   s��LޏU                 'sys.x$ps_digest_95th_percentile_by_avg_us',
                    '`sys`.`x$ps_digest_95th_percentile_by_avg_us`'
              );
            CALL sys.execute_prepared_stmt(v_sql);
            DROP TEMPORARY TABLE tmp_digest_avg_latency_distribution1;
            DROP TEMPORARY TABLE tmp_digest_avg_latency_distribution2;
            DROP TEMPORARY TABLE tmp_digest_95th_percentile_by_avg_us;
        END IF;
        IF (FIND_IN_SET('analysis', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries Ordered by Total Latency') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statement_analysis'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_errors_or_warnings', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Errors') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_errors_or_warnings'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_full_table_scans', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Full Table Scan') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_full_table_scans'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_sorting', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Sorting') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_sorting'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('with_temp_tables', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries with Internal Temporary Tables') AS 'Next Output';
            SET v_sql =
                REPLACE(
                    (SELECT VIEW_DEFINITION
                       FROM information_schema.VIEWS
                      WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'statements_with_temp_tables'
                    ),
                    '`performance_schema`.`events_statements_summary_by_digest`',
                    v_digests_table
                );
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
        IF (FIND_IN_SET('custom', in_views)) THEN
            SELECT CONCAT('Top ', @sys.statement_performance_analyzer.limit, ' Queries Using Custom View') AS 'Next Output';
            IF (@sys.statement_performance_analyzer.view IS NULL) THEN
                SET @sys.statement_performance_analyzer.view = sys.sys_get_config('statement_performance_analyzer.view', NULL);
            END IF;
            IF (@sys.statement_performance_analyzer.view IS NULL) THEN
                SIGNAL SQLSTATE '45000'
                   SET MESSAGE_TEXT = 'The @sys.statement_performance_analyzer.view user variable must be set with the view or query to use.';
            END IF;
            IF (NOT INSTR(@sys.statement_performance_analyzer.view, ' ')) THEN
                IF (NOT INSTR(@sys.statement_performance_analyzer.view, '.')) THEN
                    SET v_custom_db   = DATABASE(),
                        v_custom_name = @sys.statement_performance_analyzer.view;
                ELSE
                    SET v_custom_db   = SUBSTRING_INDEX(@sys.statement_performance_analyzer.view, '.', 1);
                    SET v_custom_name = SUBSTRING(@sys.statement_performance_analyzer.view, CHAR_LENGTH(v_custom_db)+2);
                END IF;
                CALL sys.table_exists(v_custom_db, v_custom_name, v_custom_view_exists);
                IF (v_custom_view_exists <> 'VIEW') THEN
                    SIGNAL SQLSTATE '45000'
                       SET MESSAGE_TEXT = 'The @sys.statement_performance_analyzer.view user variable is set but specified neither an existing view nor a query.';
                END IF;
                SET v_sql =
                    REPLACE(
                        (SELECT VIEW_DEFINITION
                           FROM information_schema.VIEWS
                          WHERE TABLE_SCHEMA = v_custom_db AND TABLE_NAME = v_custom_name
                        ),
                        '`performance_schema`.`events_statements_summary_by_digest`',
                        v_digests_table
                    );
            ELSE
                SET v_sql = REPLACE(@sys.statement_performance_analyzer.view, '`performance_schema`.`events_statements_summary_by_digest`', v_digests_table);
            END IF;
            IF (@sys.statement_performance_analyzer.limit > 0) THEN
                SET v_sql = CONCAT(v_sql, ' LIMIT ', @sys.statement_performance_analyzer.limit);
            END IF;
            CALL sys.execute_prepared_stmt(v_sql);
        END IF;
    END IF;
    IF (v_this_thread_enabled = 'YES') THEN
        CALL sys.ps_setup_enable_thread(CONNECTION_ID());
    END IF;
    IF (@log_bin = 1) THEN
        SET sql_log_bin = @log_bin;
    END IF;
ENDX|�