@ -1,13 +1,14 @@
import React , { useEffect , useState } from 'react' ;
import NodeEnvironment from 'jest-environment-node' ;
export async function sqljs _async _queries ( sqljs _object , queries ) {
var t0 = performance . now ( ) ;
//var t0 = performance.now();
for ( let i = 0 ; i < ( queries . length - 1 ) ; i ++ ) {
sqljs _object . exec ( queries [ i ] ) ;
}
var r = sqljs _object . exec ( queries [ queries . length - 1 ] ) ;
console . log ( "Queries took " , ( performance . now ( ) - t0 ) , " ms." ) ;
console . log ( "Query result for " , queries [ queries . length - 1 ] , ": " , r ) ;
//console.log("Queries took ", (performance.now() - t0), " ms.");
//console.log("Query result for ", queries[queries.length - 1], ": ", r);
return r ;
}
@ -40,6 +41,55 @@ export function add_custom_functions(db) {
db . create _function ( "REGEXP" , regexp _match ) ;
}
// Digikam stores its tree of tags as individual tags,
// linked only by their parent ID. This makes searching
// difficult. Therefore we add a column in the tag table
// which holds the "full tag" (e.g. People/John for the tag John).
export async function add _full _tag _info ( db ) {
var res = db . exec ( "SELECT id, pid, name FROM Tags LEFT JOIN TagProperties ON Tags.id=TagProperties.tagid" ) ;
if ( ! Array . isArray ( res ) || res . length == 0 ) {
throw new Error ( "Couldn't get tags information." ) ;
}
var cols = res [ 0 ] . columns ;
var all _tags = res [ 0 ] . values ;
var full _tags = [ ] ; // id, full name pairs
const get _tag _by _id = ( id ) => {
for ( let i = 0 ; i < all _tags . length ; i ++ ) {
if ( all _tags [ i ] [ cols . indexOf ( "id" ) ] == id ) {
return all _tags [ i ] ;
}
}
return null ;
} ;
// Solve the full paths for each tag.
all _tags . forEach ( row => {
var name = row [ cols . indexOf ( "name" ) ] ;
var full = name ;
var pid = row [ cols . indexOf ( "pid" ) ] ;
var id = row [ cols . indexOf ( "id" ) ] ;
var parent = pid == 0 ? null : get _tag _by _id ( pid ) ;
while ( parent != null ) {
full = parent [ cols . indexOf ( "name" ) ] + "/" + full ;
pid = parent [ cols . indexOf ( "pid" ) ] ;
parent = pid == 0 ? null : get _tag _by _id ( pid ) ;
}
var new _entry = { id : id , fullname : full } ;
full _tags . push ( new _entry ) ;
} ) ;
// Now put them in the database.
db . exec ( "ALTER TABLE Tags ADD fullname TEXT;" ) ;
full _tags . forEach ( tag => {
var query = 'UPDATE Tags SET fullname="' + tag . fullname + '" WHERE id=' + tag . id + ';' ;
db . exec ( query ) ;
} ) ;
var r = db . exec ( "SELECT * FROM Tags;" ) ;
}
export function ProvideDB ( props ) {
const { children , db _url } = props ;
const [ db , setDb ] = useState ( null ) ;
@ -48,9 +98,11 @@ export function ProvideDB(props) {
useEffect ( ( ) => {
fetch _sqljs _db _from _sqlite ( db _url )
. then ( db => {
add _custom _functions ( db ) ;
setError ( false ) ;
setDb ( db ) ;
add _full _tag _info ( db ) . then ( ( ) => {
add _custom _functions ( db ) ;
setError ( false ) ;
setDb ( db ) ;
} )
} )
. catch ( error => { setError ( error ) ; } ) ;
} , [ ] )