@@ -16,6 +16,7 @@ public partial class DbViewerPanel : UserControl
1616{
1717 private const int DefaultPageSize = 200 ;
1818 private string _path ;
19+ private string _sqlitePassword ;
1920 private DatabaseType _databaseType ;
2021 private string _currentObjectName ;
2122 private int _totalCount ;
@@ -29,10 +30,14 @@ public DbViewerPanel()
2930 pagination . PageSize = DefaultPageSize ;
3031 }
3132
32- public void LoadDatabase ( string path )
33+ public void LoadDatabase ( string path , string password = null )
3334 {
3435 _path = path ;
35- _databaseType = DetectDatabaseType ( path ) ;
36+ _sqlitePassword = password ;
37+ // When a password is supplied the caller already verified it is SQLite.
38+ _databaseType = password != null
39+ ? DatabaseType . SQLite
40+ : Plugin . DetectDatabaseType ( path ) ;
3641 _hasLoadedData = false ;
3742
3843 if ( _databaseType == DatabaseType . Unknown )
@@ -155,41 +160,19 @@ private void LoadCurrentPage()
155160 pageInfoTextBlock . Text = $ "{ _totalCount } 行 / 每页 { DefaultPageSize } 行 / 第 { pagination . CurrentPage } 页,共 { pagination . PageCount } 页";
156161 }
157162
158- private static DatabaseType DetectDatabaseType ( string path )
163+ private SqliteConnection OpenSqliteConnection ( string path )
159164 {
160- try
161- {
162- using var stream = File . OpenRead ( path ) ;
163- var header = new byte [ 59 ] ; // need 32+27 bytes for LiteDB magic
164- var count = stream . Read ( header , 0 , header . Length ) ;
165-
166- if ( count >= 16 )
167- {
168- var text = System . Text . Encoding . ASCII . GetString ( header , 0 , 16 ) ;
169- if ( text . StartsWith ( "SQLite format 3" , StringComparison . Ordinal ) )
170- return DatabaseType . SQLite ;
171- }
172-
173- // LiteDB 5.x: magic string "** This is a LiteDB file **" at offset 32
174- if ( count >= 59 )
175- {
176- var liteDbMagic = System . Text . Encoding . ASCII . GetString ( header , 32 , 27 ) ;
177- if ( liteDbMagic == "** This is a LiteDB file **" )
178- return DatabaseType . LiteDb ;
179- }
180- }
181- catch
182- {
183- }
184-
185- return DatabaseType . Unknown ;
165+ var csb = new SqliteConnectionStringBuilder { DataSource = path } ;
166+ if ( ! string . IsNullOrEmpty ( _sqlitePassword ) )
167+ csb . Password = _sqlitePassword ;
168+ return new SqliteConnection ( csb . ToString ( ) ) ;
186169 }
187170
188- private static string [ ] LoadSqliteNames ( string path )
171+ private string [ ] LoadSqliteNames ( string path )
189172 {
190173 var items = new List < string > ( ) ;
191174
192- using var connection = new SqliteConnection ( $ "Data Source= { path } " ) ;
175+ using var connection = OpenSqliteConnection ( path ) ;
193176 connection . Open ( ) ;
194177
195178 using var command = connection . CreateCommand ( ) ;
@@ -201,18 +184,18 @@ private static string[] LoadSqliteNames(string path)
201184 items . Add ( reader . GetString ( 0 ) ) ;
202185 }
203186
204- return items . ToArray ( ) ;
187+ return [ .. items ] ;
205188 }
206189
207190 private static string [ ] LoadLiteDbNames ( string path )
208191 {
209192 using var db = new LiteDatabase ( path ) ;
210- return db . GetCollectionNames ( ) . OrderBy ( name => name ) . ToArray ( ) ;
193+ return [ .. db . GetCollectionNames ( ) . OrderBy ( name => name ) ] ;
211194 }
212195
213- private static int GetSqliteRowCount ( string path , string tableName )
196+ private int GetSqliteRowCount ( string path , string tableName )
214197 {
215- using var connection = new SqliteConnection ( $ "Data Source= { path } " ) ;
198+ using var connection = OpenSqliteConnection ( path ) ;
216199 connection . Open ( ) ;
217200
218201 using var command = connection . CreateCommand ( ) ;
@@ -224,12 +207,12 @@ private static int GetLiteDbRowCount(string path, string collectionName)
224207 {
225208 using var db = new LiteDatabase ( path ) ;
226209 var collection = db . GetCollection ( collectionName ) ;
227- return ( int ) collection . Count ( ) ;
210+ return collection . Count ( ) ;
228211 }
229212
230- private static DataTable LoadSqlitePage ( string path , string tableName , int page , int pageSize )
213+ private DataTable LoadSqlitePage ( string path , string tableName , int page , int pageSize )
231214 {
232- using var connection = new SqliteConnection ( $ "Data Source= { path } " ) ;
215+ using var connection = OpenSqliteConnection ( path ) ;
233216 connection . Open ( ) ;
234217
235218 using var command = connection . CreateCommand ( ) ;
@@ -250,9 +233,9 @@ private static DataTable LoadLiteDbPage(string path, string collectionName, int
250233 return ConvertBsonDocumentsToTable ( docs ) ;
251234 }
252235
253- private static DataTable LoadSqliteTable ( string path , string tableName )
236+ private DataTable LoadSqliteTable ( string path , string tableName )
254237 {
255- using var connection = new SqliteConnection ( $ "Data Source= { path } " ) ;
238+ using var connection = OpenSqliteConnection ( path ) ;
256239 connection . Open ( ) ;
257240
258241 using var command = connection . CreateCommand ( ) ;
@@ -311,11 +294,4 @@ private static object ConvertBsonValue(BsonValue value)
311294
312295 return value . RawValue ? . ToString ( ) ?? value . ToString ( ) ?? string . Empty ;
313296 }
314-
315- private enum DatabaseType
316- {
317- Unknown ,
318- SQLite ,
319- LiteDb ,
320- }
321297}
0 commit comments