1010using System . Windows . Media . Imaging ;
1111using SizeInt = System . Drawing . Size ;
1212using static ZipImageViewer . TableHelper ;
13+ using System . Threading . Tasks ;
1314
1415namespace ZipImageViewer
1516{
@@ -60,10 +61,13 @@ internal static void CheckThumbsDB() {
6061 using ( var r = cmd . ExecuteReader ( ) ) {
6162 while ( r . Read ( ) ) {
6263 switch ( r [ "name" ] ) {
63- case nameof ( Column . VirtualPath ) :
64+ case nameof ( Column . BasePath ) :
6465 if ( r [ "type" ] . ToString ( ) == "TEXT" &&
65- r [ "notnull" ] . ToString ( ) == "1" &&
66- r [ "pk" ] . ToString ( ) == "1" ) goodColumns += 1 ;
66+ r [ "notnull" ] . ToString ( ) == "1" ) goodColumns += 1 ;
67+ break ;
68+ case nameof ( Column . SubPath ) :
69+ if ( r [ "type" ] . ToString ( ) == "TEXT" &&
70+ r [ "notnull" ] . ToString ( ) == "1" ) goodColumns += 1 ;
6771 break ;
6872 case nameof ( Column . DecodeWidth ) :
6973 case nameof ( Column . DecodeHeight ) :
@@ -78,7 +82,7 @@ internal static void CheckThumbsDB() {
7882 }
7983 }
8084 } ) ;
81- if ( goodColumns == 4 ) return ;
85+ if ( goodColumns == 5 ) return ;
8286
8387 //recreate thumbs table
8488 if ( File . Exists ( Tables [ Table . Thumbs ] . FullPath ) )
@@ -90,17 +94,17 @@ internal static void CheckThumbsDB() {
9094 using ( var cmd = new SQLiteCommand ( con ) ) {
9195 cmd . CommandText =
9296$@ "create table if not exists [{ table . Name } ] (
93- [{ Column . VirtualPath } ] TEXT NOT NULL,
97+ [{ Column . BasePath } ] TEXT NOT NULL,
98+ [{ Column . SubPath } ] TEXT NOT NULL,
9499[{ Column . DecodeWidth } ] INTEGER,
95100[{ Column . DecodeHeight } ] INTEGER,
96- [{ Column . ThumbData } ] BLOB,
97- PRIMARY KEY({ Column . VirtualPath } ))" ;
101+ [{ Column . ThumbData } ] BLOB)" ;
98102 return cmd . ExecuteNonQuery ( ) ;
99103 }
100104 } ) ;
101105 }
102106
103- internal static int AddToThumbDB ( ImageSource source , string path , SizeInt decodeSize ) {
107+ internal static int AddToThumbDB ( ImageSource source , string basePath , string subPath , SizeInt decodeSize ) {
104108 if ( ! ( source is BitmapSource bs ) ) throw new NotSupportedException ( ) ;
105109
106110 object [ ] affected = null ;
@@ -116,13 +120,18 @@ internal static int AddToThumbDB(ImageSource source, string path, SizeInt decode
116120 affected = Execute ( Table . Thumbs , ( table , con ) => {
117121 using ( var cmd = new SQLiteCommand ( con ) ) {
118122 //remove existing
119- cmd . CommandText = $@ "delete from { table . Name } where { Column . VirtualPath } = @path";
120- cmd . Parameters . Add ( new SQLiteParameter ( "@path" , DbType . String ) { Value = path } ) ;
123+ cmd . CommandText =
124+ $@ "delete from { table . Name } where
125+ { Column . BasePath } = @basePath and
126+ { Column . SubPath } = @subPath";
127+ cmd . Parameters . Add ( new SQLiteParameter ( "@basePath" , DbType . String ) { Value = basePath } ) ;
128+ cmd . Parameters . Add ( new SQLiteParameter ( "@subPath" , DbType . String ) { Value = subPath } ) ;
121129 cmd . ExecuteNonQuery ( ) ;
122130 //insert new
123- cmd . CommandText = $@ "insert into { table . Name }
124- ({ Column . VirtualPath } , { Column . DecodeWidth } , { Column . DecodeHeight } , { Column . ThumbData } ) values
125- (@path, { decodeSize . Width } , { decodeSize . Height } , @png)" ;
131+ cmd . CommandText =
132+ $@ "insert into { table . Name }
133+ ({ Column . BasePath } , { Column . SubPath } , { Column . DecodeWidth } , { Column . DecodeHeight } , { Column . ThumbData } ) values
134+ (@basePath, @subPath, { decodeSize . Width } , { decodeSize . Height } , @png)" ;
126135 cmd . Parameters . Add ( new SQLiteParameter ( "@png" , DbType . Binary ) { Value = png } ) ;
127136 return cmd . ExecuteNonQuery ( ) ;
128137 }
@@ -131,22 +140,35 @@ internal static int AddToThumbDB(ImageSource source, string path, SizeInt decode
131140 return ( int ) affected [ 0 ] ;
132141 }
133142
143+ /// <summary>
144+ /// Async version of <see cref="GetFromThumbDB(string, string, SizeInt)"/>
145+ /// </summary>
146+ internal static Task < Tuple < BitmapSource , string > > GetFromThumbDBAsync ( string basePath , SizeInt decodeSize , string subPath = null ) {
147+ return Task . Run ( ( ) => GetFromThumbDB ( basePath , decodeSize , subPath ) ) ;
148+ }
149+
134150 /// <summary>
135151 /// Returns null if thumb either does not exist in DB or has different size.
152+ /// If <paramref name="subPath"/> is null, the first match by <paramref name="basePath"/> will be returned.
136153 /// </summary>
137- internal static BitmapSource GetFromThumbDB ( string path , SizeInt decodeSize ) {
154+ internal static Tuple < BitmapSource , string > GetFromThumbDB ( string basePath , SizeInt decodeSize , string subPath = null ) {
138155 var png = Execute ( Table . Thumbs , ( table , con ) => {
139156 byte [ ] pngByte = null ;
140157 using ( var cmd = new SQLiteCommand ( con ) ) {
141158 cmd . CommandText =
142- $@ "select { Column . ThumbData } from { table . Name } where
143- { Column . VirtualPath } = @path and
159+ $@ "select * from { table . Name } where
160+ { Column . BasePath } = @basePath
161+ { ( subPath == null ? "" : $@ "and { Column . SubPath } = @subPath") } and
144162{ Column . DecodeWidth } = { decodeSize . Width } and
145- { Column . DecodeHeight } = { decodeSize . Height } ";
146- cmd . Parameters . Add ( new SQLiteParameter ( "@path" , DbType . String ) { Value = path } ) ;
163+ { Column . DecodeHeight } = { decodeSize . Height } limit 1";
164+ cmd . Parameters . Add ( new SQLiteParameter ( "@basePath" , DbType . String ) { Value = basePath } ) ;
165+ if ( subPath != null )
166+ cmd . Parameters . Add ( new SQLiteParameter ( "@subPath" , DbType . String ) { Value = subPath } ) ;
147167 using ( var reader = cmd . ExecuteReader ( ) ) {
148168 while ( reader . Read ( ) ) {
149- pngByte = ( byte [ ] ) reader [ 0 ] ;
169+ pngByte = ( byte [ ] ) reader [ nameof ( Column . ThumbData ) ] ;
170+ if ( subPath == null )
171+ subPath = ( string ) reader [ nameof ( Column . SubPath ) ] ;
150172 break ;
151173 }
152174 }
@@ -162,19 +184,22 @@ internal static BitmapSource GetFromThumbDB(string path, SizeInt decodeSize) {
162184 bi . StreamSource = ms ;
163185 bi . EndInit ( ) ;
164186 bi . Freeze ( ) ;
165- return bi ;
187+ return new Tuple < BitmapSource , string > ( bi , subPath ) ;
166188 }
167189 }
168190
169- internal static bool ThumbExistInDB ( string path , SizeInt decodeSize ) {
191+ internal static bool ThumbExistInDB ( string basePath , string subPath , SizeInt decodeSize ) {
170192 return ( bool ) Execute ( Table . Thumbs , ( table , con ) => {
171193 using ( var cmd = new SQLiteCommand ( con ) ) {
172194 cmd . CommandText =
173195$@ "select count({ Column . ThumbData } ) from { table . Name } where
174- { Column . VirtualPath } = @path and
196+ { Column . BasePath } = @basePath
197+ { ( subPath == null ? "" : $@ "and { Column . SubPath } = @subPath") } and
175198{ Column . DecodeWidth } = { decodeSize . Width } and
176199{ Column . DecodeHeight } = { decodeSize . Height } ";
177- cmd . Parameters . Add ( new SQLiteParameter ( "@path" , DbType . String ) { Value = path } ) ;
200+ cmd . Parameters . Add ( new SQLiteParameter ( "@basePath" , DbType . String ) { Value = basePath } ) ;
201+ if ( subPath != null )
202+ cmd . Parameters . Add ( new SQLiteParameter ( "@subPath" , DbType . String ) { Value = subPath } ) ;
178203 using ( var r = cmd . ExecuteReader ( ) ) {
179204 r . Read ( ) ;
180205 return ( long ) r [ 0 ] > 0 ;
0 commit comments