| 1 | package Fuse::TagLayer::SQLite; | ||
| 2 | |||
| 3 | use DBI; | ||
| 4 | |||
| 5 | use Exporter; | ||
| 6 | push @Fuse::TagLayer::SQLite::ISA, 'Exporter'; | ||
| 7 | our @EXPORT = ('db_init','db_sync','db_disconnect','db_tags_add','db_tags_all','db_files_add','db_files_for_tags'); | ||
| 8 | |||
| 9 | our $dbh; | ||
| 10 | our $mysql_file_insert; | ||
| 11 | our $mysql_tags_insert; | ||
| 12 | our $opts; | ||
| 13 | |||
| 14 | ## SQLite currently uses two tables: | ||
| 15 | ## tags: an index of all tags | ||
| 16 | ## files: path+basename for each file, with a tag-string for regex matching | ||
| 17 | |||
| 18 | sub db_init { | ||
| 19 | $opts = { @_ }; | ||
| 20 | |||
| 21 | my $sql = "create table if not exists `tags` ( | ||
| 22 | `tag` varchar(255) null, | ||
| 23 | `count` integer DEFAULT '0' | ||
| 24 | );"; | ||
| 25 | database()->do($sql) or die database()->errstr; | ||
| 26 | my $empty = database()->prepare("DELETE FROM `tags`; ") or die database()->errstr; # no TRUNCATE TABLE in SQLite | ||
| 27 | $empty->execute(); | ||
| 28 | |||
| 29 | $sql = "create table if not exists `file_tags` ( | ||
| 30 | `dir` varchar(255) null, | ||
| 31 | `basename` varchar(255) null, | ||
| 32 | `tags` varchar(255) null | ||
| 33 | );"; | ||
| 34 | database()->do($sql) or die database()->errstr; | ||
| 35 | my $empty2 = database()->prepare("DELETE FROM `file_tags`; ") or die database()->errstr; # no TRUNCATE TABLE in SQLite | ||
| 36 | $empty2->execute(); | ||
| 37 | |||
| 38 | database()->{AutoCommit} = 0; | ||
| 39 | |||
| 40 | ## prepare statements | ||
| 41 | our $mysql_file_insert = database()->prepare("INSERT INTO `file_tags` (`dir`,`basename`,`tags`) VALUES (?,?,?); ") or die database()->errstr; | ||
| 42 | our $mysql_tags_insert = database()->prepare("INSERT INTO `tags` (`tag`,`count`) VALUES (?,?); ") or die database()->errstr; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub database { | ||
| 46 | unless($dbh){ | ||
| 47 | $dbh = DBI->connect("dbi:SQLite::memory:", "", ""); | ||
| 48 | # $dbh = DBI->connect("dbi:SQLite:dbname=/tmp/taglayer.sqlite", "", ""); # for debug only, make sure you delete the file after each mount!! | ||
| 49 | } | ||
| 50 | return $dbh; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub db_sync { | ||
| 54 | database()->commit; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub db_disconnect { | ||
| 58 | database()->disconnect; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub db_tags_add { | ||
| 62 | my ($tag,$cnt) = @_; | ||
| 63 | print "## SQLite::db_tags_add: $tag, $cnt\n" if $opts->{debug} > 2; | ||
| 64 | $mysql_tags_insert->execute($tag, $cnt) or die database()->errstr; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub db_tags_all { | ||
| 68 | my $dirs = database()->selectcol_arrayref("SELECT `tag` FROM `tags`; "); # push first row into arrayref | ||
| 69 | return $dirs; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub db_files_add { | ||
| 73 | my ($dir,$basename,@tags) = @_; | ||
| 74 | print "## SQLite::db_files_add: dir:$dir, basename:$basename, tags:@tags\n" if $opts->{debug} > 2; | ||
| 75 | |||
| 76 | my $tagstring = join(', ', @tags); | ||
| 77 | |||
| 78 | $mysql_file_insert->execute( $dir, $basename, $tagstring ) or die database()->errstr; | ||
| 79 | } | ||
| 80 | |||
| 81 | ## returns: | ||
| 82 | ## ref to a list of files tagged with input tags, and | ||
| 83 | ## ref to a hash of subtags (all tags associated with returned files, minus the input tags) | ||
| 84 | sub db_files_for_tags { | ||
| 85 | my (@tags) = @_; | ||
| 86 | |||
| 87 | my $sql = join(" AND ", map { "`tags` REGEXP '$_'" } @tags ); | ||
| 88 | |||
| 89 | print "## SQLite::db_files_for_tags: @tags ; sql:$sql\n" if $opts->{debug}; | ||
| 90 | |||
| 91 | $entries = database()->prepare("SELECT * FROM `file_tags` WHERE $sql; "); # push first row into arrayref | ||
| 92 | $entries->execute(); | ||
| 93 | |||
| 94 | my @files_for_tags; | ||
| 95 | my %subtags; | ||
| 96 | while( my $entry = $entries->fetchrow_hashref ){ | ||
| 97 | push(@files_for_tags, $entry->{dir} .'/'. $entry->{basename}); | ||
| 98 | |||
| 99 | for( split(/,\s*/,$entry->{tags}) ){ | ||
| 100 | # already clean | ||
| 101 | # my $tag_cleaned = lc($_); | ||
| 102 | # $tag_cleaned =~ s/[^\p{L}\p{N}]//g; # matches all (Unicode) characters that are neither letters nor numbers | ||
| 103 | $subtags{$_}++; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | print "## SQLite::db_files_for_tags: ".$entries->rows." files ; ".scalar(keys(%subtags))." subtags (before minus)\n" if $opts->{debug} > 1; | ||
| 108 | |||
| 109 | for(@tags){ delete($subtags{$_}); } | ||
| 110 | |||
| 111 | return (\@files_for_tags, \%subtags); | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |