Commit 04cd6a3f authored by Gavin Brown's avatar Gavin Brown

cast query variables to integers so we insert zeroes instead of NULLs (causes errors on MySQL)

document table schemas for MySQL/MariaDB
parent 4be4592a
......@@ -44,9 +44,7 @@ DSD - A Simple DNS Statistics Collecting Daemon
# DATABASE SCHEMA
The following SQL statements can be used to configure an SQLite database for
use with `dsd-db`. You will need to modify them if you want to use a different
database:
## SQLite
# the 'files' table stores a record of all the files that
# have been processed:
......@@ -66,6 +64,55 @@ database:
`rcode` VARCHAR,
`queries` INTEGER
);
CREATE INDEX `end` ON data (`end`);
CREATE INDEX `end` ON data (`end`);
CREATE INDEX `family` ON data (`family`);
CREATE INDEX `host` ON data (`host`);
CREATE INDEX `proto` ON data (`proto`);
CREATE INDEX `rcode` ON data (`rcode`);
CREATE INDEX `start` ON data (`start`);
CREATE INDEX `zone` ON data (`zone`);
## MySQL/MariaDB
# create the database
CREATE DATABASE `dsd`;
# the 'files' table stores a record of all the files that
# have been processed:
CREATE TABLE `dsd`.`files` (
`filename` VARCHAR(255) NOT NULL,
`processed` datetime NOT NULL,
PRIMARY KEY (`filename`),
UNIQUE KEY `filename` (`filename`)
);
# the 'data' table stores the actual data:
CREATE TABLE `dsd`.`data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`host` VARCHAR(255) NOT NULL,
`start` DATETIME NOT NULL,
`end` DATETIME NOT NULL,
`zone` VARCHAR(255) DEFAULT NULL,
`family` TINYINT(1) UNSIGNED DEFAULT NULL,
`proto` ENUM('udp','tcp') DEFAULT NULL,
`rcode` VARCHAR(24) DEFAULT NULL,
`queries` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `host` (`host`),
KEY `end` (`end`),
KEY `zone` (`zone`),
KEY `family` (`family`),
KEY `proto` (`proto`),
KEY `rcode` (`rcode`)
);
## Other
If you want to use a "real" database other than the "toy" databases described
above, then I'm sure you know how to convert the SQL statements above so that
they work with your preferred RDBMS.
# SEE ALSO
......
......@@ -97,22 +97,22 @@ sub parse_json {
foreach my $zone (keys(%{$data->{'zones'}})) {
$total += $data->{'zones'}->{$zone}->{'queries'};
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, $zone, undef, undef, undef, $data->{'zones'}->{$zone}->{'queries'});
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, $zone, undef, undef, undef, int($data->{'zones'}->{$zone}->{'queries'});
foreach my $family (@families) {
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, $zone, $family, undef, undef, $data->{'zones'}->{$zone}->{'families'}->{$family});
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, $zone, $family, undef, undef, int($data->{'zones'}->{$zone}->{'families'}->{$family}));
}
foreach my $proto (@protos) {
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, $zone, undef, $proto, undef, $data->{'zones'}->{$zone}->{'protos'}->{$proto});
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, $zone, undef, $proto, undef, int($data->{'zones'}->{$zone}->{'protos'}->{$proto}));
}
foreach my $rcode (keys(%{$data->{'zones'}->{$zone}->{'rcodes'}})) {
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, $zone, undef, undef, $rcode, $data->{'zones'}->{$zone}->{'rcodes'}->{$rcode});
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, $zone, undef, undef, $rcode, int($data->{'zones'}->{$zone}->{'rcodes'}->{$rcode}));
}
}
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, undef, undef, undef, undef, $total);
$disth->execute($data->{'host'}, $data->{'start'}, $data->{'end'}, undef, undef, undef, undef, int($total));
}
__END__
......@@ -169,9 +169,7 @@ Show help.
=head1 DATABASE SCHEMA
The following SQL statements can be used to configure an SQLite database for
use with C<dsd-db>. You will need to modify them if you want to use a different
database:
=head2 SQLite
# the 'files' table stores a record of all the files that
# have been processed:
......@@ -191,6 +189,55 @@ database:
`rcode` VARCHAR,
`queries` INTEGER
);
CREATE INDEX `end` ON data (`end`);
CREATE INDEX `end` ON data (`end`);
CREATE INDEX `family` ON data (`family`);
CREATE INDEX `host` ON data (`host`);
CREATE INDEX `proto` ON data (`proto`);
CREATE INDEX `rcode` ON data (`rcode`);
CREATE INDEX `start` ON data (`start`);
CREATE INDEX `zone` ON data (`zone`);
=head2 MySQL/MariaDB
# create the database
CREATE DATABASE `dsd`;
# the 'files' table stores a record of all the files that
# have been processed:
CREATE TABLE `dsd`.`files` (
`filename` VARCHAR(255) NOT NULL,
`processed` datetime NOT NULL,
PRIMARY KEY (`filename`),
UNIQUE KEY `filename` (`filename`)
);
# the 'data' table stores the actual data:
CREATE TABLE `dsd`.`data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`host` VARCHAR(255) NOT NULL,
`start` DATETIME NOT NULL,
`end` DATETIME NOT NULL,
`zone` VARCHAR(255) DEFAULT NULL,
`family` TINYINT(1) UNSIGNED DEFAULT NULL,
`proto` ENUM('udp','tcp') DEFAULT NULL,
`rcode` VARCHAR(24) DEFAULT NULL,
`queries` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `host` (`host`),
KEY `end` (`end`),
KEY `zone` (`zone`),
KEY `family` (`family`),
KEY `proto` (`proto`),
KEY `rcode` (`rcode`)
);
=head2 Other
If you want to use a "real" database other than the "toy" databases described
above, then I'm sure you know how to convert the SQL statements above so that
they work with your preferred RDBMS.
=head1 SEE ALSO
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment