api_plugin_db_table_create() method in cacti plugins

Discussions on developing plugins for the Cacti Plugin Architecture

Moderators: Developers, Moderators

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

api_plugin_db_table_create() method in cacti plugins

Post by kdas »

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: 6022
Joined: Mon Nov 17, 2003 6:35 pm
Location: Michigan, USA
Contact:

Re: api_plugin_db_table_create() method in cacti plugins

Post by rony »

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: 17007
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Re: api_plugin_db_table_create() method in cacti plugins

Post by TheWitness »

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 dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest