api_plugin_db_table_create() method in cacti plugins

Discussions on developing plugins for the Cacti Plugin Architecture

Moderators: Moderators, Developers

Post Reply
Author
Message
kdas
Posts: 8
Joined: Fri Dec 09, 2011 1:21 am

api_plugin_db_table_create() method in cacti plugins

#1 Post by kdas » Wed Dec 21, 2011 4:27 am

Hi All,

I am trying to create a table in cacti database from my plugin.

The query is below

CREATE TABLE `kpi_param` (
`kpi_id` int(3) NOT NULL,
`param_id` int(3) NOT NULL,
PRIMARY KEY (`kpi_id`,`param_id`),
KEY `fk_kpi_param_param1` (`param_id`),
KEY `fk_kpi_param_kpi1` (`kpi_id`),
CONSTRAINT `fk_kpi_param_kpi1` FOREIGN KEY (`kpi_id`) REFERENCES `kpi` (`kpi_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_kpi_param_param1` FOREIGN KEY (`param_id`) REFERENCES `param` (`param_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


As per the api_plugin_db_table_create() there are no references of foreign keys.
How do we get the above query in a api_plugin_db_table_create() format?
especially foreign key , References information.

My implementation:
function kpi_param_table(){

global $config,$database_default;
include_once($config["library_path"]."/database.php");
$data=array();
$data['columns'][]=array('name' => 'kpi_id',
'type' => 'bigint(25)',
'unsigned' => 'unsigned',
'NULL' => false,
'auto_increment' => true
);
$data['columns'][]=array('name' => 'param_id',
'type' => 'int(3)',
'unsigned' => 'unsigned',
'NULL' => false
);
$data['primary']='kpi_id';
$data['type']='MyISAM';
$data['comment']='kpi param Data Table';
api_plugin_db_table_create ('param kpi','kpi_param',$data);

}

Aprreciate your help.

Thanks.

Regards,
Das

User avatar
rony
Developer/Forum Admin
Posts: 6016
Joined: Mon Nov 17, 2003 6:35 pm
Location: Michigan, USA
Contact:

Re: api_plugin_db_table_create() method in cacti plugins

#2 Post by rony » Wed Dec 21, 2011 4:22 pm

Well....

Mostly it doesn't support foreign keys because the default database engine in mysql does not.
[size=117][i][b]Tony Roman[/b][/i][/size]
[size=84][i]Experience is what causes a person to make new mistakes instead of old ones.[/i][/size]
[size=84][i]There are only 3 way to complete a project: Good, Fast or Cheap, pick two.[/i][/size]
[size=84][i]With age comes wisdom, what you choose to do with it determines whether or not you are wise.[/i][/size]

User avatar
TheWitness
Developer
Posts: 14834
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Re: api_plugin_db_table_create() method in cacti plugins

#3 Post by TheWitness » Wed Dec 21, 2011 5:48 pm

I suggest that you don't use the API function for setting up Fk's. You should instead use the 'create' syntax and wrap it into a function called "db_execute". So, it would go something like this:

Code: Select all

db_execute("CREATE TABLE IF NOT EXISTS...");
Then, please log an enhancement request at http://bugs.cacti.net under the plugins section to allow the specification of Fk's in the API.

TheWitness
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of MacTrack, Boost, CLog, SpikeKill, Platform RTM, DSStats, maintainer of Spine, lot's of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Central Plugin Repository
Central Templates Repository


I'm still out there people. Getting excited for Cacti 1.2. I think it will be a great release.

Post Reply