Cacti (home)ForumsRepositoryDocumentation
Cacti: offical forums and support  

 FAQFAQ   SearchSearch   MemberlistMemberlist    RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in    


Fix for KEY bug in lib/plugins.php

 
Post new topic   Reply to topic    Cacti Forum Index -> Plugin Architecture
Author Message
niobe
Cacti User


Joined: 10 Mar 2008
Posts: 125
Location: Australia

PostPosted: Fri May 02, 2008 2:13 am    Post subject: Fix for KEY bug in lib/plugins.php Reply with quote

I found the function to create other KEYs besides the primary key is broken in PA2.1.

Here is the fix, just change the appropriate section in api_plugin_db_table_create function. Replace this:

Code:
                foreach ($data['keys'] as $key) {
                        if (isset($key['name'])) {
                                $sql .= ",\n KEY `" . $key['name'] . '` (`' . $key['columns'] . '`)';
                        }
                }


with this:

Code:

                foreach ($data['keys'] as $key) {

                        while (list($ky, $value) = each($key)) {
                                //echo "Key: $ky; Value: $value<br />\n";
                                $sql .= ",\n KEY `" . $ky . '` (`' . $value . '`)';
                        }
                }


Now you can create KEYs/INDEXes for faster mysql in your setup.php
Code:

function <plugin_name>_setup_table () {
        <snip>
        $data['keys'][] = array('some_col' => 'some_col', 'another_col' => 'another_col');
        <snip>


cheers,

N
Back to top
gandalf
Developer


Joined: 02 Dec 2004
Posts: 12295
Location: Muenster, Germany

PostPosted: Sat Jun 21, 2008 7:20 pm    Post subject: Reply with quote

Thanks for the pointers. I extended your approach to multi-column keys (primary, normal and unique keys) and to more than one (normal, unique) key per table by using
Code:

      /* primary keys, multi-key columns are allowed */
      if (isset($data['primary'])) {
         $sql .= ",\n PRIMARY KEY (`";
         /* remove blanks */
         $no_blanks = str_replace(" ", "", $data['primary']);
         /* add tics to columns names */
         $sql .=  str_replace(",", "`, `", $no_blanks) . '`)';
      }

      /* "normal" keys, multi-key columns are allowed, multiple keys per run are allowed as well */
      if (isset($data['keys'])) {
         foreach ($data['keys'] as $key) {
            if (isset($key['name'])) {
               $sql .= ",\n KEY `" . $key['name'] . '` (`';
               if (isset($key['columns'])) {
                  /* remove blanks */
                  $no_blanks = str_replace(" ", "", $key['columns']);
                  /* add tics to columns names */
                  $sql .=  str_replace(",", "`, `", $no_blanks) . '`)';
               }
            }
         }
      }

      /* "unique" keys, multi-key columns are allowed, multiple keys per run are allowed as well */
      if (isset($data['unique'])) {
         foreach ($data['unique'] as $unique) {
            if (isset($unique['name'])) {
               $sql .= ",\n UNIQUE KEY `" . $unique['name'] . '` (`';
               if (isset($unique['columns'])) {
                  /* remove blanks */
                  $no_blanks = str_replace(" ", "", $unique['columns']);
                  /* add tics to columns names */
                  $sql .=  str_replace(",", "`, `", $no_blanks) . '`)';
               }
            }
         }
      }

This will be available with cacti 088 as part of the integrated plugin architecture
Reinhard
Back to top
niobe
Cacti User


Joined: 10 Mar 2008
Posts: 125
Location: Australia

PostPosted: Sun Jun 22, 2008 8:49 am    Post subject: Reply with quote

Sweet, thanks for incorporating the fix.
Back to top
gandalf
Developer


Joined: 02 Dec 2004
Posts: 12295
Location: Muenster, Germany

PostPosted: Sun Jun 22, 2008 9:49 am    Post subject: Reply with quote

de rien
Reinhard
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Cacti Forum Index -> Plugin Architecture All times are GMT - 5 Hours
Page 1 of 1

 



Powered by phpBB © 2001, 2005 phpBB Group