| 1 | # | ||
| 2 | # Perl module for note | ||
| 3 | # mysql database backend. see docu: perldoc NOTEDB::mysql | ||
| 4 | # | ||
| 5 | |||
| 6 | |||
| 7 | package NOTEDB::mysql; | ||
| 8 | |||
| 9 | $NOTEDB::mysql::VERSION = "1.51"; | ||
| 10 | |||
| 11 | use DBI; | ||
| 12 | use strict; | ||
| 13 | #use Data::Dumper; | ||
| 14 | use NOTEDB; | ||
| 15 | |||
| 16 | use Exporter (); | ||
| 17 | use vars qw(@ISA @EXPORT); | ||
| 18 | @ISA = qw(NOTEDB Exporter); | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | sub new { | ||
| 24 | my($this, %param) = @_; | ||
| 25 | |||
| 26 | my $class = ref($this) || $this; | ||
| 27 | my $self = {}; | ||
| 28 | bless($self,$class); | ||
| 29 | |||
| 30 | my $dbname = $param{dbname} || "note"; | ||
| 31 | my $dbhost = $param{dbhost} || "localhost"; | ||
| 32 | my $dbuser = $param{dbuser} || ""; | ||
| 33 | my $dbpasswd = $param{dbpasswd} || ""; | ||
| 34 | my $dbport = $param{dbport} || ""; | ||
| 35 | my $fnum = "number"; | ||
| 36 | my $fnote = "note"; | ||
| 37 | my $fdate = "date"; | ||
| 38 | my $ftopic = "topic"; | ||
| 39 | |||
| 40 | my $database; | ||
| 41 | if ($dbport) { | ||
| 42 | $database = "DBI:mysql:$dbname;host=$dbhost:$dbport"; | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | $database = "DBI:mysql:$dbname;host=$dbhost"; | ||
| 46 | } | ||
| 47 | |||
| 48 | $self->{table} = "note"; | ||
| 49 | |||
| 50 | $self->{sql_getsingle} = "SELECT $fnote,$fdate,$ftopic FROM $self->{table} WHERE $fnum = ?"; | ||
| 51 | $self->{sql_all} = "SELECT $fnum,$fnote,$fdate,$ftopic FROM $self->{table}"; | ||
| 52 | $self->{sql_nextnum} = "SELECT max($fnum) FROM $self->{table}"; | ||
| 53 | $self->{sql_incrnum} = "SELECT $fnum FROM $self->{table} ORDER BY $fnum"; | ||
| 54 | $self->{sql_setnum} = "UPDATE $self->{table} SET $fnum = ? WHERE $fnum = ?"; | ||
| 55 | $self->{sql_edit} = "UPDATE $self->{table} SET $fnote = ?, $fdate = ?, $ftopic = ? WHERE $fnum = ?"; | ||
| 56 | $self->{sql_insertnew} = "INSERT INTO $self->{table} VALUES (?, ?, ?, ?)"; | ||
| 57 | $self->{sql_del} = "DELETE FROM $self->{table} WHERE $fnum = ?"; | ||
| 58 | $self->{sql_del_all} = "DELETE FROM $self->{table}"; | ||
| 59 | |||
| 60 | $self->{DB} = DBI->connect($database, $dbuser, $dbpasswd) or die DBI->errstr(); | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub DESTROY | ||
| 67 | { | ||
| 68 | # clean the desk! | ||
| 69 | my $this = shift; | ||
| 70 | $this->{DB}->disconnect; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub lock { | ||
| 75 | my($this) = @_; | ||
| 76 | # LOCK the database! | ||
| 77 | my $lock = $this->{DB}->prepare("LOCK TABLES $this->{table} WRITE") | ||
| 78 | || die $this->{DB}->errstr(); | ||
| 79 | $lock->execute() || die $this->{DB}->errstr(); | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub unlock { | ||
| 84 | my($this) = @_; | ||
| 85 | my $unlock = $this->{DB}->prepare("UNLOCK TABLES") || die $this->{DB}->errstr; | ||
| 86 | $unlock->execute() || die $this->{DB}->errstr(); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub version { | ||
| 91 | my $this = shift; | ||
| 92 | return $this->{version}; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | sub get_single { | ||
| 97 | my($this, $num) = @_; | ||
| 98 | |||
| 99 | my($note, $date, $topic); | ||
| 100 | my $statement = $this->{DB}->prepare($this->{sql_getsingle}) || die $this->{DB}->errstr(); | ||
| 101 | |||
| 102 | $statement->execute($num) || die $this->{DB}->errstr(); | ||
| 103 | $statement->bind_columns(undef, \($note, $date, $topic)) || die $this->{DB}->errstr(); | ||
| 104 | |||
| 105 | while($statement->fetch) { | ||
| 106 | $note = $this->ude($note); | ||
| 107 | if ($topic) { | ||
| 108 | $note = "$topic\n" . $note; | ||
| 109 | } | ||
| 110 | return $note, $this->ude($date); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub get_all | ||
| 116 | { | ||
| 117 | my $this = shift; | ||
| 118 | my($num, $note, $date, %res, $topic); | ||
| 119 | |||
| 120 | if ($this->unchanged) { | ||
| 121 | return %{$this->{cache}}; | ||
| 122 | } | ||
| 123 | |||
| 124 | my $statement = $this->{DB}->prepare($this->{sql_all}) or die $this->{DB}->errstr(); | ||
| 125 | |||
| 126 | $statement->execute or die $this->{DB}->errstr(); | ||
| 127 | $statement->bind_columns(undef, \($num, $note, $date, $topic)) or die $this->{DB}->errstr(); | ||
| 128 | |||
| 129 | while($statement->fetch) { | ||
| 130 | $res{$num}->{'note'} = $this->ude($note); | ||
| 131 | $res{$num}->{'date'} = $this->ude($date); | ||
| 132 | if ($topic) { | ||
| 133 | $res{$num}->{'note'} = "$topic\n" . $res{$num}->{'note'}; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | $this->cache(%res); | ||
| 138 | return %res; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | sub get_nextnum | ||
| 143 | { | ||
| 144 | my $this = shift; | ||
| 145 | my($num); | ||
| 146 | if ($this->unchanged) { | ||
| 147 | $num = 1; | ||
| 148 | foreach (keys %{$this->{cache}}) { | ||
| 149 | $num++; | ||
| 150 | } | ||
| 151 | return $num; | ||
| 152 | } | ||
| 153 | |||
| 154 | my $statement = $this->{DB}->prepare($this->{sql_nextnum}) || die $this->{DB}->errstr(); | ||
| 155 | |||
| 156 | $statement->execute || die $this->{DB}->errstr(); | ||
| 157 | $statement->bind_columns(undef, \($num)) || die $this->{DB}->errstr(); | ||
| 158 | |||
| 159 | while($statement->fetch) { | ||
| 160 | return $num+1; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | sub get_search | ||
| 165 | { | ||
| 166 | my($this, $searchstring) = @_; | ||
| 167 | my($num, $note, $date, %res, $match, $use_cache, $topic); | ||
| 168 | |||
| 169 | my $regex = $this->generate_search($searchstring); | ||
| 170 | eval $regex; | ||
| 171 | if ($@) { | ||
| 172 | print "invalid expression: \"$searchstring\"!\n"; | ||
| 173 | return; | ||
| 174 | } | ||
| 175 | $match = 0; | ||
| 176 | |||
| 177 | if ($this->unchanged) { | ||
| 178 | foreach my $num (keys %{$this->{cache}}) { | ||
| 179 | $_ = $this->{cache}{$num}->{note}; | ||
| 180 | eval $regex; | ||
| 181 | if ($match) { | ||
| 182 | $res{$num}->{note} = $this->{cache}{$num}->{note}; | ||
| 183 | $res{$num}->{date} = $this->{cache}{$num}->{date} | ||
| 184 | } | ||
| 185 | $match = 0; | ||
| 186 | } | ||
| 187 | return %res; | ||
| 188 | } | ||
| 189 | |||
| 190 | my $statement = $this->{DB}->prepare($this->{sql_all}) or die $this->{DB}->errstr(); | ||
| 191 | |||
| 192 | $statement->execute or die $this->{DB}->errstr(); | ||
| 193 | $statement->bind_columns(undef, \($num, $note, $date, $topic)) or die $this->{DB}->errstr(); | ||
| 194 | |||
| 195 | while($statement->fetch) { | ||
| 196 | $note = $this->ude($note); | ||
| 197 | $date = $this->ude($date); | ||
| 198 | if ($topic) { | ||
| 199 | $note = "$topic\n" . $note; | ||
| 200 | } | ||
| 201 | $_ = $note; | ||
| 202 | eval $regex; | ||
| 203 | if($match) { | ||
| 204 | $res{$num}->{'note'} = $note; | ||
| 205 | $res{$num}->{'date'} = $date; | ||
| 206 | } | ||
| 207 | $match = 0; | ||
| 208 | } | ||
| 209 | return %res; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | sub set_edit | ||
| 216 | { | ||
| 217 | my($this, $num, $note, $date) = @_; | ||
| 218 | |||
| 219 | $this->lock; | ||
| 220 | my $statement = $this->{DB}->prepare($this->{sql_edit}) or die $this->{DB}->errstr(); | ||
| 221 | $note =~ s/'/\'/g; | ||
| 222 | $note =~ s/\\/\\\\/g; | ||
| 223 | $statement->execute($this->uen($note), $this->uen($date), $num) | ||
| 224 | or die $this->{DB}->errstr(); | ||
| 225 | $this->unlock; | ||
| 226 | $this->changed; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | sub set_new | ||
| 231 | { | ||
| 232 | my($this, $num, $note, $date) = @_; | ||
| 233 | $this->lock; | ||
| 234 | my $statement = $this->{DB}->prepare($this->{sql_insertnew}) || die $this->{DB}->errstr(); | ||
| 235 | |||
| 236 | my ($topic, $note) = $this->get_topic($note); | ||
| 237 | |||
| 238 | $note =~ s/'/\'/g; | ||
| 239 | $note =~ s/\\/\\\\/g; | ||
| 240 | $topic =~ s/\\/\\\\/g; | ||
| 241 | $statement->execute($num, $this->uen($note), $this->uen($date), $topic) || die $this->{DB}->errstr(); | ||
| 242 | $this->unlock; | ||
| 243 | $this->changed; | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | sub set_del | ||
| 248 | { | ||
| 249 | my($this, $num) = @_; | ||
| 250 | my($note, $date, $T); | ||
| 251 | |||
| 252 | $this->lock; | ||
| 253 | ($note, $date) = $this->get_single($num); | ||
| 254 | |||
| 255 | return "ERROR" if ($date !~ /^\d/); | ||
| 256 | |||
| 257 | # delete record! | ||
| 258 | my $statement = $this->{DB}->prepare($this->{sql_del}) || die $this->{DB}->errstr(); | ||
| 259 | $statement->execute($num) || die $this->{DB}->errstr(); | ||
| 260 | $this->unlock; | ||
| 261 | $this->changed; | ||
| 262 | return; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | sub set_del_all | ||
| 267 | { | ||
| 268 | my($this) = @_; | ||
| 269 | $this->lock; | ||
| 270 | my $statement = $this->{DB}->prepare($this->{sql_del_all}) || die $this->{DB}->errstr(); | ||
| 271 | $statement->execute() || die $this->{DB}->errstr(); | ||
| 272 | $this->unlock; | ||
| 273 | $this->changed; | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub set_recountnums { | ||
| 278 | my $this = shift; | ||
| 279 | |||
| 280 | $this->lock; | ||
| 281 | |||
| 282 | my(@count, $i, $num, $setnum, $pos); | ||
| 283 | $setnum = 1; | ||
| 284 | $pos=0; $i=0; @count = (); | ||
| 285 | |||
| 286 | my $statement = $this->{DB}->prepare($this->{sql_incrnum}) || die $this->{DB}->errstr(); | ||
| 287 | $statement->execute || die $this->{DB}->errstr(); | ||
| 288 | $statement->bind_columns(undef, \($num)) || die $this->{DB}->errstr(); | ||
| 289 | # store real id's in an array! | ||
| 290 | while($statement->fetch) { | ||
| 291 | $count[$i] = $num; | ||
| 292 | $i++; | ||
| 293 | } | ||
| 294 | # now recount them! | ||
| 295 | my $sub_statement = $this->{DB}->prepare($this->{sql_setnum}) || die $this->{DB}->errstr(); | ||
| 296 | for($pos=0;$pos<$i;$pos++) { | ||
| 297 | $setnum = $pos +1; | ||
| 298 | $sub_statement->execute($setnum,$count[$pos]) || die $this->{DB}->errstr(); | ||
| 299 | } | ||
| 300 | $this->unlock; | ||
| 301 | $this->changed; | ||
| 302 | } | ||
| 303 | |||
| 304 | sub import_data { | ||
| 305 | my ($this, $data) = @_; | ||
| 306 | foreach my $num (keys %{$data}) { | ||
| 307 | my $pos = $this->get_nextnum(); | ||
| 308 | $this->set_new($pos, $data->{$num}->{note}, $data->{$num}->{date}); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | sub uen | ||
| 313 | { | ||
| 314 | my $this = shift; | ||
| 315 | my($T); | ||
| 316 | if($NOTEDB::crypt_supported == 1) { | ||
| 317 | eval { | ||
| 318 | $T = pack("u", $this->{cipher}->encrypt($_[0])); | ||
| 319 | }; | ||
| 320 | } | ||
| 321 | else { | ||
| 322 | $T = $_[0]; | ||
| 323 | } | ||
| 324 | chomp $T; | ||
| 325 | return $T; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub ude | ||
| 329 | { | ||
| 330 | my $this = shift; | ||
| 331 | my($T); | ||
| 332 | if($NOTEDB::crypt_supported == 1) { | ||
| 333 | eval { | ||
| 334 | $T = $this->{cipher}->decrypt(unpack("u",$_[0])) | ||
| 335 | }; | ||
| 336 | return $T; | ||
| 337 | } | ||
| 338 | else { | ||
| 339 | return $_[0]; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | sub get_topic { | ||
| 344 | my ($this, $data) = @_; | ||
| 345 | if ($data =~ /^\//) { | ||
| 346 | my($topic, $note) = split /\n/, $data, 2; | ||
| 347 | return ($topic, $note); | ||
| 348 | } | ||
| 349 | else { | ||
| 350 | return ("", $data); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | 1; # keep this! | ||
| 355 | |||
| 356 | __END__ |