mirror of
https://codeberg.org/scip/note.git
synced 2025-12-17 20:51:02 +01:00
-
This commit is contained in:
7
note-1.0.7/NOTEDB/README
Normal file
7
note-1.0.7/NOTEDB/README
Normal file
@@ -0,0 +1,7 @@
|
||||
perl modules for note used as database backends.
|
||||
the install.sh script will install both of them,
|
||||
although you may only need one backend. Perhaps
|
||||
other users on your system have oter ideas in mind...
|
||||
|
||||
Therefore, please ignore these file. There is nothing
|
||||
to edit or to do. Simply leave this directory :-)
|
||||
388
note-1.0.7/NOTEDB/binary.pm
Normal file
388
note-1.0.7/NOTEDB/binary.pm
Normal file
@@ -0,0 +1,388 @@
|
||||
#!/usr/bin/perl
|
||||
# $Id: binary.pm,v 1.6 2000/06/25 19:48:00 scip Exp scip $
|
||||
# Perl module for note
|
||||
# binary database backend. see docu: perldoc NOTEDB::binary
|
||||
#
|
||||
use strict;
|
||||
use Data::Dumper;
|
||||
use IO::Seekable;
|
||||
|
||||
package NOTEDB;
|
||||
use Fcntl qw(LOCK_EX LOCK_UN);
|
||||
BEGIN {
|
||||
# make sure, it works, although encryption
|
||||
# not supported on this system!
|
||||
eval { require Crypt::CBC; };
|
||||
if($@) {
|
||||
$NOTEDB::crypt_supported = 0;
|
||||
}
|
||||
else {
|
||||
$NOTEDB::crypt_supported = 1;
|
||||
}
|
||||
}
|
||||
|
||||
# Globals:
|
||||
my ($NOTEDB, $sizeof, $typedef,$version);
|
||||
my ($cipher);
|
||||
|
||||
$version = "(NOTEDB::binary, 1.6)";
|
||||
|
||||
|
||||
sub new
|
||||
{
|
||||
my($this, $dbdriver, $dbname, $MAX_NOTE, $MAX_TIME) = @_;
|
||||
|
||||
my $class = ref($this) || $this;
|
||||
my $self = {};
|
||||
bless($self,$class);
|
||||
$NOTEDB = $dbname;
|
||||
|
||||
if(! -e $NOTEDB)
|
||||
{
|
||||
open(TT,">$NOTEDB") or die "Could not create $NOTEDB: $!\n";
|
||||
close (TT);
|
||||
}
|
||||
elsif(! -w $NOTEDB)
|
||||
{
|
||||
print "$NOTEDB is not writable!\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
my $TYPEDEF = "i a$MAX_NOTE a$MAX_TIME";
|
||||
my $SIZEOF = length pack($TYPEDEF, () );
|
||||
|
||||
$sizeof = $SIZEOF;
|
||||
$typedef = $TYPEDEF;
|
||||
return $self;
|
||||
}
|
||||
|
||||
|
||||
sub DESTROY
|
||||
{
|
||||
# clean the desk!
|
||||
}
|
||||
|
||||
sub version {
|
||||
return $version;
|
||||
}
|
||||
|
||||
sub no_crypt {
|
||||
$NOTEDB::crypt_supported = 0;
|
||||
}
|
||||
|
||||
sub use_crypt {
|
||||
my($this,$key,$method) = @_;
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$cipher = new Crypt::CBC($key, $method);
|
||||
};
|
||||
if($@) {
|
||||
$NOTEDB::crypt_supported == 0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
print "warning: Crypt::CBC not supported by system!\n";
|
||||
}
|
||||
}
|
||||
|
||||
sub set_del_all
|
||||
{
|
||||
unlink $NOTEDB;
|
||||
open(TT,">$NOTEDB") or die "Could not create $NOTEDB: $!\n";
|
||||
close (TT);
|
||||
}
|
||||
|
||||
|
||||
sub get_single
|
||||
{
|
||||
my($this, $num) = @_;
|
||||
my($address, $note, $date, $buffer, $n, $t, $buffer, );
|
||||
|
||||
open NOTE, "+<$NOTEDB" or die "could not open $NOTEDB\n";
|
||||
flock NOTE, LOCK_EX;
|
||||
|
||||
$address = ($num-1) * $sizeof;
|
||||
seek(NOTE, $address, IO::Seekable::SEEK_SET);
|
||||
read(NOTE, $buffer, $sizeof);
|
||||
($num, $n, $t) = unpack($typedef, $buffer);
|
||||
|
||||
$note = ude($n);
|
||||
$date = ude($t);
|
||||
|
||||
flock NOTE, LOCK_UN;
|
||||
close NOTE;
|
||||
|
||||
return $note, $date;
|
||||
}
|
||||
|
||||
|
||||
sub get_all
|
||||
{
|
||||
my($this, $num, $note, $date, %res);
|
||||
|
||||
open NOTE, "+<$NOTEDB" or die "could not open $NOTEDB\n";
|
||||
flock NOTE, LOCK_EX;
|
||||
my($buffer, $t, $n);
|
||||
seek(NOTE, 0, 0); # START FROM BEGINNING
|
||||
while(read(NOTE, $buffer, $sizeof)) {
|
||||
($num, $note, $date) = unpack($typedef, $buffer);
|
||||
$t = ude($date);
|
||||
$n = ude($note);
|
||||
$res{$num}->{'note'} = $n;
|
||||
$res{$num}->{'date'} = $t;
|
||||
}
|
||||
flock NOTE, LOCK_UN;
|
||||
close NOTE;
|
||||
|
||||
return %res;
|
||||
}
|
||||
|
||||
|
||||
sub get_nextnum
|
||||
{
|
||||
my($this, $num, $te, $me, $buffer);
|
||||
|
||||
open NOTE, "+<$NOTEDB" or die "could not open $NOTEDB\n";
|
||||
flock NOTE, LOCK_EX;
|
||||
|
||||
seek(NOTE, 0, 0); # START FROM BEGINNING
|
||||
while(read(NOTE, $buffer, $sizeof)) {
|
||||
($num, $te, $me) = unpack($typedef, $buffer);
|
||||
}
|
||||
$num += 1;
|
||||
flock NOTE, LOCK_UN;
|
||||
close NOTE;
|
||||
|
||||
return $num;
|
||||
}
|
||||
|
||||
sub get_search
|
||||
{
|
||||
my($this, $searchstring) = @_;
|
||||
my($buffer, $num, $note, $date, %res, $t, $n);
|
||||
|
||||
open NOTE, "+<$NOTEDB" or die "could not open $NOTEDB\n";
|
||||
flock NOTE, LOCK_EX;
|
||||
|
||||
seek(NOTE, 0, 0); # START FROM BEGINNING
|
||||
while(read(NOTE, $buffer, $sizeof))
|
||||
{
|
||||
($num, $note, $date) = unpack($typedef, $buffer);
|
||||
$n = ude($note);
|
||||
$t = ude($date);
|
||||
if($n =~ /\Q$searchstring\E/i)
|
||||
{
|
||||
$res{$num}->{'note'} = $n;
|
||||
$res{$num}->{'date'} = $t;
|
||||
}
|
||||
}
|
||||
flock NOTE, LOCK_UN;
|
||||
close NOTE;
|
||||
|
||||
return %res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
sub set_edit
|
||||
{
|
||||
my($this, $num, $note, $date) = @_;
|
||||
my $address = ($num -1 ) * $sizeof;
|
||||
|
||||
open NOTE, "+<$NOTEDB" or die "could not open $NOTEDB\n";
|
||||
flock NOTE, LOCK_EX;
|
||||
|
||||
seek(NOTE, $address, IO::Seekable::SEEK_SET);
|
||||
my $n = uen($note);
|
||||
my $t = uen($date);
|
||||
|
||||
my $buffer = pack($typedef, $num, $n, $t);
|
||||
print NOTE $buffer;
|
||||
|
||||
flock NOTE, LOCK_UN;
|
||||
close NOTE;
|
||||
}
|
||||
|
||||
|
||||
sub set_new
|
||||
{
|
||||
my($this, $num, $note, $date) = @_;
|
||||
open NOTE, "+<$NOTEDB" or die "could not open $NOTEDB\n";
|
||||
flock NOTE, LOCK_EX;
|
||||
|
||||
seek(NOTE, 0, IO::Seekable::SEEK_END); # APPEND
|
||||
my $n = uen($note);
|
||||
my $t = uen($date);
|
||||
my $buffer = pack($typedef, $num, $n, $t);
|
||||
print NOTE $buffer;
|
||||
|
||||
flock NOTE, LOCK_UN;
|
||||
close NOTE;
|
||||
}
|
||||
|
||||
|
||||
sub set_del
|
||||
{
|
||||
my($this, $num) = @_;
|
||||
my(%orig, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
|
||||
|
||||
$setnum = 1;
|
||||
|
||||
%orig = $this->get_all();
|
||||
return "ERROR" if (! exists $orig{$num});
|
||||
|
||||
delete $orig{$num};
|
||||
|
||||
# overwrite, but keep number!
|
||||
open NOTE, ">$NOTEDB" or die "could not open $NOTEDB\n";
|
||||
flock NOTE, LOCK_EX;
|
||||
seek(NOTE, 0, 0); # START FROM BEGINNING
|
||||
foreach $N (keys %orig) {
|
||||
$n = uen($orig{$N}->{'note'});
|
||||
$t = uen($orig{$N}->{'date'});
|
||||
$buffer = pack( $typedef, $N, $n, $t); # keep orig number, note have to call recount!
|
||||
print NOTE $buffer;
|
||||
seek(NOTE, 0, IO::Seekable::SEEK_END);
|
||||
$setnum++;
|
||||
}
|
||||
flock NOTE, LOCK_UN;
|
||||
close NOTE;
|
||||
return;
|
||||
}
|
||||
|
||||
sub set_recountnums
|
||||
{
|
||||
my($this) = @_;
|
||||
my(%orig, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
|
||||
|
||||
$setnum = 1;
|
||||
%orig = $this->get_all();
|
||||
|
||||
open NOTE, ">$NOTEDB" or die "could not open $NOTEDB\n";
|
||||
flock NOTE, LOCK_EX;
|
||||
seek(NOTE, 0, 0); # START FROM BEGINNING
|
||||
|
||||
foreach $N (sort {$a <=> $b} keys %orig) {
|
||||
$n = uen($orig{$N}->{'note'});
|
||||
$t = uen($orig{$N}->{'date'});
|
||||
$buffer = pack( $typedef, $setnum, $n, $t);
|
||||
print NOTE $buffer;
|
||||
seek(NOTE, 0, IO::Seekable::SEEK_END);
|
||||
$setnum++;
|
||||
}
|
||||
flock NOTE, LOCK_UN;
|
||||
close NOTE;
|
||||
return;
|
||||
}
|
||||
|
||||
sub uen
|
||||
{
|
||||
my($T);
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$T = pack("u", $cipher->encrypt($_[0]));
|
||||
};
|
||||
}
|
||||
else {
|
||||
$T = pack("u", $_[0]);
|
||||
}
|
||||
chomp $T;
|
||||
return $T;
|
||||
}
|
||||
|
||||
sub ude
|
||||
{
|
||||
my($T);
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$T = $cipher->decrypt(unpack("u",$_[0]));
|
||||
};
|
||||
}
|
||||
else {
|
||||
$T = unpack("u", $_[0]);
|
||||
}
|
||||
return $T;
|
||||
}
|
||||
|
||||
1; # keep this!
|
||||
|
||||
__END__
|
||||
|
||||
=head1 NAME
|
||||
|
||||
NOTEDB::binary - module lib for accessing a notedb from perl
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
# include the module
|
||||
use NOTEDB;
|
||||
|
||||
# create a new NOTEDB object
|
||||
$db = new NOTEDB("binary", "/home/tom/.notedb", 4096, 24);
|
||||
|
||||
# decide to use encryption
|
||||
# $key is the cipher to use for encryption
|
||||
# $method must be either Crypt::IDEA or Crypt::DES
|
||||
# you need Crypt::CBC, Crypt::IDEA and Crypt::DES to have installed.
|
||||
$db->use_crypt($key,$method);
|
||||
|
||||
# do not use encryption
|
||||
# this is the default
|
||||
$db->no_crypt;
|
||||
|
||||
# get a single note
|
||||
($note, $date) = $db->get_single(1);
|
||||
|
||||
# search for a certain note
|
||||
%matching_notes = $db->get_search("somewhat");
|
||||
# format of returned hash:
|
||||
#$matching_notes{$numberofnote}->{'note' => 'something', 'date' => '23.12.2000 10:33:02'}
|
||||
|
||||
# get all existing notes
|
||||
%all_notes = $db->get_all();
|
||||
# format of returnes hash like the one from get_search above
|
||||
|
||||
# get the next noteid available
|
||||
$next_num = $db->get_nextnum();
|
||||
|
||||
# modify a certain note
|
||||
$db->set_edit(1, "any text", "23.12.2000 10:33:02");
|
||||
|
||||
# create a new note
|
||||
$db->set_new(5, "any new text", "23.12.2000 10:33:02");
|
||||
|
||||
# delete a certain note
|
||||
$db->set_del(5);
|
||||
|
||||
# turn on encryption. CryptMethod must be IDEA, DES or BLOWFISH
|
||||
$db->use_crypt("passphrase", "CryptMethod");
|
||||
|
||||
# turn off encryption. This is the default.
|
||||
$db->no_crypt();
|
||||
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
You can use this module for accessing a note database. There are currently
|
||||
two versions of this module, one version for a SQL database and one for a
|
||||
binary file (note's own database-format).
|
||||
However, both versions provides identical interfaces, which means, you do
|
||||
not need to change your code, if you want to switch to another database format.
|
||||
|
||||
Currently, NOTEDB module is only used by note itself. But feel free to use it
|
||||
within your own project! Perhaps someone want to implement a webinterface to
|
||||
note...
|
||||
|
||||
=head1 USAGE
|
||||
|
||||
please see the section SYNOPSIS, it says it all.
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Thomas Linden <tom@daemon.de>.
|
||||
|
||||
|
||||
|
||||
=cut
|
||||
261
note-1.0.7/NOTEDB/dbm.pm
Normal file
261
note-1.0.7/NOTEDB/dbm.pm
Normal file
@@ -0,0 +1,261 @@
|
||||
#!/usr/bin/perl
|
||||
# $Id: dbm.pm,v 1.2 2000/06/25 19:51:11 scip Exp scip $
|
||||
# Perl module for note
|
||||
# DBM database backend. see docu: perldoc NOTEDB::dbm
|
||||
#
|
||||
|
||||
use DB_File;
|
||||
#use Data::Dumper;
|
||||
use strict;
|
||||
package NOTEDB;
|
||||
|
||||
BEGIN {
|
||||
# make sure, it works, although encryption
|
||||
# not supported on this system!
|
||||
eval { require Crypt::CBC; };
|
||||
if($@) {
|
||||
$NOTEDB::crypt_supported = 0;
|
||||
}
|
||||
else {
|
||||
$NOTEDB::crypt_supported = 1;
|
||||
}
|
||||
}
|
||||
|
||||
# Globals:
|
||||
my ($dbm_dir, $notefile, $timefile, $version, $cipher, %note, %date);
|
||||
$notefile = "note.dbm";
|
||||
$timefile = "date.dbm";
|
||||
|
||||
$version = "(NOTEDB::dbm, 1.1)";
|
||||
|
||||
sub new
|
||||
{
|
||||
my($this, $dbdriver, $dbm_dir) = @_;
|
||||
my $class = ref($this) || $this;
|
||||
my $self = {};
|
||||
bless($self,$class);
|
||||
|
||||
tie %note, "DB_File", "$dbm_dir/$notefile" || die $!;
|
||||
tie %date, "DB_File", "$dbm_dir/$timefile" || die $!;
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
|
||||
sub DESTROY
|
||||
{
|
||||
# clean the desk!
|
||||
untie %note, %date;
|
||||
}
|
||||
|
||||
sub version {
|
||||
return $version;
|
||||
}
|
||||
|
||||
sub no_crypt {
|
||||
$NOTEDB::crypt_supported = 0;
|
||||
}
|
||||
|
||||
sub use_crypt {
|
||||
my($this, $key, $method) = @_;
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$cipher = new Crypt::CBC($key, $method);
|
||||
};
|
||||
if($@) {
|
||||
$NOTEDB::crypt_supported == 0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
print "warning: Crypt::CBC not supported by system!\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sub get_single
|
||||
{
|
||||
my($this, $num) = @_;
|
||||
my($note, $date);
|
||||
return ude ($note{$num}), ude($date{$num});
|
||||
}
|
||||
|
||||
|
||||
sub get_all
|
||||
{
|
||||
my($this, $num, $note, $date, %res, $real);
|
||||
foreach $num (sort {$a <=> $b} keys %date) {
|
||||
$res{$num}->{'note'} = ude($note{$num});
|
||||
$res{$num}->{'date'} = ude($date{$num});
|
||||
}
|
||||
return %res;
|
||||
}
|
||||
|
||||
|
||||
sub get_nextnum
|
||||
{
|
||||
my($this, $num);
|
||||
foreach (sort {$a <=> $b} keys %date) {
|
||||
$num = $_;
|
||||
}
|
||||
$num++;
|
||||
return $num;
|
||||
}
|
||||
|
||||
sub get_search
|
||||
{
|
||||
my($this, $searchstring) = @_;
|
||||
my($num, $note, $date, %res);
|
||||
|
||||
foreach $num (sort {$a <=> $b} keys %date) {
|
||||
if (ude($note{$num}) =~ /\Q$searchstring\E/i) {
|
||||
$res{$num}->{'note'} = ude($note{$num});
|
||||
$res{$num}->{'date'} = ude($date{$num});
|
||||
}
|
||||
}
|
||||
|
||||
return %res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
sub set_recountnums
|
||||
{
|
||||
my $this = shift;
|
||||
my(%Note, %Date, $num, $setnum);
|
||||
$setnum = 1;
|
||||
foreach $num (sort {$a <=> $b} keys %note) {
|
||||
$Note{$setnum} = $note{$num};
|
||||
$Date{$setnum} = $date{$num};
|
||||
$setnum++;
|
||||
}
|
||||
%note = %Note;
|
||||
%date = %Date;
|
||||
}
|
||||
|
||||
|
||||
|
||||
sub set_edit
|
||||
{
|
||||
my($this, $num, $note, $date) = @_;
|
||||
$note{$num} = uen($note);
|
||||
$date{$num} = uen($date);
|
||||
}
|
||||
|
||||
|
||||
sub set_new
|
||||
{
|
||||
my($this, $num, $note, $date) = @_;
|
||||
$this->set_edit($num, $note, $date); # just the same thing
|
||||
}
|
||||
|
||||
|
||||
sub set_del
|
||||
{
|
||||
my($this, $num) = @_;
|
||||
my($note, $date, $T);
|
||||
($note, $date) = $this->get_single($num);
|
||||
return "ERROR" if ($date !~ /^\d/);
|
||||
delete $note{$num};
|
||||
delete $date{$num};
|
||||
}
|
||||
|
||||
sub set_del_all
|
||||
{
|
||||
my($this) = @_;
|
||||
%note = ();
|
||||
%date = ();
|
||||
return;
|
||||
}
|
||||
|
||||
sub uen
|
||||
{
|
||||
my($T);
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$T = pack("u", $cipher->encrypt($_[0]));
|
||||
};
|
||||
}
|
||||
else {
|
||||
$T = $_[0];
|
||||
}
|
||||
chomp $T;
|
||||
return $T;
|
||||
}
|
||||
|
||||
sub ude
|
||||
{
|
||||
my($T);
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$T = $cipher->decrypt(unpack("u",$_[0]))
|
||||
};
|
||||
return $T;
|
||||
}
|
||||
else {
|
||||
return $_[0];
|
||||
}
|
||||
}
|
||||
|
||||
1; # keep this!
|
||||
|
||||
__END__
|
||||
|
||||
=head1 NAME
|
||||
|
||||
NOTEDB::dbm - module lib for accessing a notedb from perl
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
# include the module
|
||||
use NOTEDB;
|
||||
|
||||
# create a new NOTEDB object (the last 4 params are db table/field names)
|
||||
$db = new NOTEDB("mysql","note","/home/user/.notedb/");
|
||||
|
||||
# get a single note
|
||||
($note, $date) = $db->get_single(1);
|
||||
|
||||
# search for a certain note
|
||||
%matching_notes = $db->get_search("somewhat");
|
||||
# format of returned hash:
|
||||
#$matching_notes{$numberofnote}->{'note' => 'something', 'date' => '23.12.2000 10:33:02'}
|
||||
|
||||
# get all existing notes
|
||||
%all_notes = $db->get_all();
|
||||
# format of returnes hash like the one from get_search above
|
||||
|
||||
# get the next noteid available
|
||||
$next_num = $db->get_nextnum();
|
||||
|
||||
# recount all noteids starting by 1 (usefull after deleting one!)
|
||||
$db->set_recountnums();
|
||||
|
||||
# modify a certain note
|
||||
$db->set_edit(1, "any text", "23.12.2000 10:33:02");
|
||||
|
||||
# create a new note
|
||||
$db->set_new(5, "any new text", "23.12.2000 10:33:02");
|
||||
|
||||
# delete a certain note
|
||||
$db->set_del(5);
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
You can use this module for accessing a note database. This is the dbm module.
|
||||
It uses the DB_FILE module to store it's data and it uses DBM files for tis purpose.
|
||||
|
||||
Currently, NOTEDB module is only used by note itself. But feel free to use it
|
||||
within your own project! Perhaps someone want to implement a webinterface to
|
||||
note...
|
||||
|
||||
=head1 USAGE
|
||||
|
||||
please see the section SYNOPSIS, it says it all.
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Thomas Linden <tom@daemon.de>.
|
||||
|
||||
|
||||
|
||||
=cut
|
||||
349
note-1.0.7/NOTEDB/mysql.pm
Normal file
349
note-1.0.7/NOTEDB/mysql.pm
Normal file
@@ -0,0 +1,349 @@
|
||||
#!/usr/bin/perl
|
||||
# $Id: mysql.pm,v 1.5 2000/06/25 19:50:43 scip Exp scip $
|
||||
# Perl module for note
|
||||
# mysql database backend. see docu: perldoc NOTEDB::binary
|
||||
#
|
||||
|
||||
use DBI;
|
||||
use strict;
|
||||
use Data::Dumper;
|
||||
|
||||
package NOTEDB;
|
||||
|
||||
BEGIN {
|
||||
# make sure, it works, although encryption
|
||||
# not supported on this system!
|
||||
eval { require Crypt::CBC; };
|
||||
if($@) {
|
||||
$NOTEDB::crypt_supported = 0;
|
||||
}
|
||||
else {
|
||||
$NOTEDB::crypt_supported = 1;
|
||||
}
|
||||
}
|
||||
|
||||
# Globals:
|
||||
my ($DB, $table, $fnum, $fnote, $fdate, $version, $cipher);
|
||||
$table = "note";
|
||||
$fnum = "number";
|
||||
$fnote = "note";
|
||||
$fdate = "date";
|
||||
$version = "(NOTEDB::mysql, 1.4)";
|
||||
|
||||
# prepare some std statements... #####################################################################
|
||||
my $sql_getsingle = "SELECT $fnote,$fdate FROM $table WHERE $fnum = ?";
|
||||
my $sql_all = "SELECT $fnum,$fnote,$fdate FROM $table";
|
||||
my $sql_nextnum = "SELECT max($fnum) FROM $table";
|
||||
my $sql_incrnum = "SELECT $fnum FROM $table ORDER BY $fnum";
|
||||
my $sql_search = "SELECT DISTINCT $fnum,$fnote,$fdate FROM $table WHERE $fnote LIKE ?";
|
||||
|
||||
my $sql_setnum = "UPDATE $table SET $fnum = ? WHERE $fnum = ?";
|
||||
my $sql_edit = "UPDATE $table SET $fnote = ?, $fdate = ? WHERE $fnum = ?";
|
||||
|
||||
my $sql_insertnew = "INSERT INTO $table VALUES (?, ?, ?)";
|
||||
|
||||
my $sql_del = "DELETE FROM $table WHERE $fnum = ?";
|
||||
my $sql_del_all = "DELETE FROM $table";
|
||||
######################################################################################################
|
||||
|
||||
sub new
|
||||
{
|
||||
# no prototype, because of the bin-version, which takes only a filename!
|
||||
my($this, $dbdriver, $dbname, $dbhost, $dbuser, $dbpasswd) = @_;
|
||||
|
||||
my $class = ref($this) || $this;
|
||||
my $self = {};
|
||||
bless($self,$class);
|
||||
my $database = "DBI:$dbdriver:$dbname;host=$dbhost";
|
||||
|
||||
$DB = DBI->connect($database, $dbuser, $dbpasswd) || die DBI->errstr();
|
||||
|
||||
# LOCK the database!
|
||||
my $lock = $DB->prepare("LOCK TABLES $table WRITE") || die $DB->errstr();
|
||||
$lock->execute() || die $DB->errstr();
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
|
||||
sub DESTROY
|
||||
{
|
||||
# clean the desk!
|
||||
my $unlock = $DB->prepare("UNLOCK TABLES") || die $DB->errstr;
|
||||
$unlock->execute() || die $DB->errstr();
|
||||
$DB->disconnect || die $DB->errstr;
|
||||
}
|
||||
|
||||
sub version {
|
||||
return $version;
|
||||
}
|
||||
|
||||
sub no_crypt {
|
||||
$NOTEDB::crypt_supported = 0;
|
||||
}
|
||||
|
||||
sub use_crypt {
|
||||
my($this, $key, $method) = @_;
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$cipher = new Crypt::CBC($key, $method);
|
||||
};
|
||||
if($@) {
|
||||
$NOTEDB::crypt_supported == 0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
print "warning: Crypt::CBC not supported by system!\n";
|
||||
}
|
||||
}
|
||||
|
||||
sub get_single
|
||||
{
|
||||
my($this, $num) = @_;
|
||||
my($note, $date);
|
||||
my $statement = $DB->prepare($sql_getsingle) || die $DB->errstr();
|
||||
|
||||
$statement->execute($num) || die $DB->errstr();
|
||||
$statement->bind_columns(undef, \($note, $date)) || die $DB->errstr();
|
||||
|
||||
while($statement->fetch) {
|
||||
return ude($note), ude($date);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sub get_all
|
||||
{
|
||||
my($this, $num, $note, $date, %res);
|
||||
my $statement = $DB->prepare($sql_all) || die $DB->errstr();
|
||||
|
||||
$statement->execute || die $DB->errstr();
|
||||
$statement->bind_columns(undef, \($num, $note, $date)) || die $DB->errstr();
|
||||
|
||||
while($statement->fetch) {
|
||||
$res{$num}->{'note'} = ude($note);
|
||||
$res{$num}->{'date'} = ude($date);
|
||||
}
|
||||
return %res;
|
||||
}
|
||||
|
||||
|
||||
sub get_nextnum
|
||||
{
|
||||
my($this, $num);
|
||||
my $statement = $DB->prepare($sql_nextnum) || die $DB->errstr();
|
||||
|
||||
$statement->execute || die $DB->errstr();
|
||||
$statement->bind_columns(undef, \($num)) || die $DB->errstr();
|
||||
|
||||
while($statement->fetch) {
|
||||
return $num+1;
|
||||
}
|
||||
}
|
||||
|
||||
sub get_search
|
||||
{
|
||||
my($this, $searchstring) = @_;
|
||||
my($num, $note, $date, %res);
|
||||
if($NOTEDB::crypt_supported != 1) {
|
||||
$searchstring = "\%$searchstring\%";
|
||||
my $statement = $DB->prepare($sql_search) || die $DB->errstr();
|
||||
$statement->execute($searchstring) || die $DB->errstr();
|
||||
$statement->bind_columns(undef, \($num, $note, $date))
|
||||
|| die $DB->errstr();
|
||||
while($statement->fetch) {
|
||||
$res{$num}->{'note'} = $note;
|
||||
$res{$num}->{'date'} = $date;
|
||||
}
|
||||
}
|
||||
else {
|
||||
my %res = $this->get_all();
|
||||
foreach $num (sort { $a <=> $b } keys %res) {
|
||||
$note = ude($res{$num}->{'note'});
|
||||
$date = ude($res{$num}->{'date'});
|
||||
if($note =~ /\Q$searchstring\E/i)
|
||||
{
|
||||
$res{$num}->{'note'} = $note;
|
||||
$res{$num}->{'date'} = $date;
|
||||
}
|
||||
}
|
||||
}
|
||||
return %res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
sub set_edit
|
||||
{
|
||||
my($this, $num, $note, $date) = @_;
|
||||
|
||||
my $statement = $DB->prepare($sql_edit) || die $DB->errstr();
|
||||
|
||||
$note =~ s/'/\'/g;
|
||||
$note =~ s/\\/\\\\/g;
|
||||
$statement->execute(uen($note), uen($date), $num) || die $DB->errstr();
|
||||
}
|
||||
|
||||
|
||||
sub set_new
|
||||
{
|
||||
my($this, $num, $note, $date) = @_;
|
||||
|
||||
my $statement = $DB->prepare($sql_insertnew) || die $DB->errstr();
|
||||
|
||||
$note =~ s/'/\'/g;
|
||||
$note =~ s/\\/\\\\/g;
|
||||
$statement->execute($num, uen($note), uen($date)) || die $DB->errstr();
|
||||
}
|
||||
|
||||
|
||||
sub set_del
|
||||
{
|
||||
my($this, $num) = @_;
|
||||
my($note, $date, $T);
|
||||
|
||||
($note, $date) = $this->get_single($num);
|
||||
|
||||
return "ERROR" if ($date !~ /^\d/);
|
||||
|
||||
# delete record!
|
||||
my $statement = $DB->prepare($sql_del) || die $DB->errstr();
|
||||
$statement->execute($num) || die $DB->errstr();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
sub set_del_all
|
||||
{
|
||||
my($this) = @_;
|
||||
my $statement = $DB->prepare($sql_del_all) || die $DB->errstr();
|
||||
$statement->execute() || die $DB->errstr();
|
||||
return;
|
||||
}
|
||||
|
||||
sub set_recountnums
|
||||
{
|
||||
my $this = shift;
|
||||
my(@count, $i, $num, $setnum, $pos);
|
||||
$setnum = 1;
|
||||
$pos=0; $i=0; @count = ();
|
||||
|
||||
my $statement = $DB->prepare($sql_incrnum) || die $DB->errstr();
|
||||
$statement->execute || die $DB->errstr();
|
||||
$statement->bind_columns(undef, \($num)) || die $DB->errstr();
|
||||
# store real id's in an array!
|
||||
while($statement->fetch) {
|
||||
$count[$i] = $num;
|
||||
$i++;
|
||||
}
|
||||
|
||||
# now recount them!
|
||||
my $sub_statement = $DB->prepare($sql_setnum) || die $DB->errstr();
|
||||
for($pos=0;$pos<$i;$pos++) {
|
||||
$setnum = $pos +1;
|
||||
$sub_statement->execute($setnum,$count[$pos]) || die $DB->errstr();
|
||||
}
|
||||
}
|
||||
|
||||
sub uen
|
||||
{
|
||||
my($T);
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$T = pack("u", $cipher->encrypt($_[0]));
|
||||
};
|
||||
}
|
||||
else {
|
||||
$T = $_[0];
|
||||
}
|
||||
chomp $T;
|
||||
return $T;
|
||||
}
|
||||
|
||||
sub ude
|
||||
{
|
||||
my($T);
|
||||
if($NOTEDB::crypt_supported == 1) {
|
||||
eval {
|
||||
$T = $cipher->decrypt(unpack("u",$_[0]))
|
||||
};
|
||||
return $T;
|
||||
}
|
||||
else {
|
||||
return $_[0];
|
||||
}
|
||||
}
|
||||
|
||||
1; # keep this!
|
||||
|
||||
__END__
|
||||
|
||||
=head1 NAME
|
||||
|
||||
NOTEDB::mysql - module lib for accessing a notedb from perl
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
# include the module
|
||||
use NOTEDB;
|
||||
|
||||
# create a new NOTEDB object (the last 4 params are db table/field names)
|
||||
$db = new NOTEDB("mysql","note","localhost","username","password","note","number","note","date");
|
||||
|
||||
# get a single note
|
||||
($note, $date) = $db->get_single(1);
|
||||
|
||||
# search for a certain note
|
||||
%matching_notes = $db->get_search("somewhat");
|
||||
# format of returned hash:
|
||||
#$matching_notes{$numberofnote}->{'note' => 'something', 'date' => '23.12.2000 10:33:02'}
|
||||
|
||||
# get all existing notes
|
||||
%all_notes = $db->get_all();
|
||||
# format of returnes hash like the one from get_search above
|
||||
|
||||
# get the next noteid available
|
||||
$next_num = $db->get_nextnum();
|
||||
|
||||
# recount all noteids starting by 1 (usefull after deleting one!)
|
||||
$db->set_recountnums();
|
||||
|
||||
# modify a certain note
|
||||
$db->set_edit(1, "any text", "23.12.2000 10:33:02");
|
||||
|
||||
# create a new note
|
||||
$db->set_new(5, "any new text", "23.12.2000 10:33:02");
|
||||
|
||||
# delete a certain note
|
||||
$db->set_del(5);
|
||||
|
||||
# turn on encryption. CryptMethod must be IDEA, DES or BLOWFISH
|
||||
$db->use_crypt("passphrase", "CryptMethod");
|
||||
|
||||
# turn off encryption. This is the default.
|
||||
$db->no_crypt();
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
You can use this module for accessing a note database. There are currently
|
||||
two versions of this module, one version for a SQL database and one for a
|
||||
binary file (note's own database-format).
|
||||
However, both versions provides identical interfaces, which means, you do
|
||||
not need to change your code, if you want to switch to another database format.
|
||||
|
||||
Currently, NOTEDB module is only used by note itself. But feel free to use it
|
||||
within your own project! Perhaps someone want to implement a webinterface to
|
||||
note...
|
||||
|
||||
=head1 USAGE
|
||||
|
||||
please see the section SYNOPSIS, it says it all.
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Thomas Linden <tom@daemon.de>.
|
||||
|
||||
|
||||
|
||||
=cut
|
||||
Reference in New Issue
Block a user