-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* all code for fetching sqlite database * add example
- Loading branch information
1 parent
4121493
commit c21be41
Showing
11 changed files
with
1,821 additions
and
3,776 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
/* scrollbar */ | ||
@import 'simplebar-react/dist/simplebar.min.css'; | ||
|
||
/* Spinner Styles */ | ||
.spinner { | ||
border: 4px solid rgba(0, 0, 0, 0.1); | ||
width: 40px; | ||
height: 40px; | ||
border-radius: 50%; | ||
border-left-color: #09f; | ||
|
||
animation: spin 1s ease infinite; | ||
position: absolute; /* Positioning the spinner */ | ||
top: 50%; /* Center vertically */ | ||
left: 50%; /* Center horizontally */ | ||
transform: translate(-50%, -50%); /* Adjust the position correctly */ | ||
} | ||
|
||
@keyframes spin { | ||
0% { | ||
transform: rotate(0deg); | ||
} | ||
100% { | ||
transform: rotate(360deg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import initSqlJs from 'sql.js' | ||
|
||
const sqlPromise = initSqlJs({ | ||
locateFile: file => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.9.0/${file}` | ||
}); | ||
|
||
const dataPromise = fetch("http://localhost:3030/netperf/dist/example.sqlite").then(res => res.arrayBuffer()); // TODO: replace this with netperf/sqlite URL. | ||
const [SQL, buf] = await Promise.all([sqlPromise, dataPromise]) | ||
const db = new SQL.Database(new Uint8Array(buf)); | ||
|
||
export default db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useState, useEffect } from 'react'; | ||
import initSqlJs from 'sql.js'; | ||
|
||
const useSQLite = (dbUrl) => { | ||
const [db, setDb] = useState(null); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
let dbInstance = null; | ||
|
||
const loadDatabase = async () => { | ||
try { | ||
const SQL = await initSqlJs({ | ||
locateFile: file => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.9.0/${file}` | ||
}); | ||
|
||
const response = await fetch(dbUrl); | ||
const buf = await response.arrayBuffer(); | ||
dbInstance = new SQL.Database(new Uint8Array(buf)); | ||
setDb(dbInstance); | ||
} catch (err) { | ||
setError(err); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
loadDatabase(); | ||
|
||
// Cleanup function to close the db when the component unmounts | ||
return () => { | ||
if (dbInstance) { | ||
dbInstance.close(); | ||
} | ||
}; | ||
}, []); | ||
|
||
return { db, loading, error }; | ||
}; | ||
|
||
export default useSQLite; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { useState } from 'react'; | ||
import { Helmet } from 'react-helmet-async'; | ||
|
||
export default function DetailedPage(props) { | ||
const { db } = props; | ||
const [query, setQuery] = useState(''); | ||
|
||
const executeQuery = (query) => { | ||
let values = db.exec(query); | ||
for (let val of values.values()) { | ||
console.log(val); | ||
} | ||
}; | ||
|
||
const handleInputChange = (event) => { | ||
setQuery(event.target.value); | ||
}; | ||
|
||
const handleQuerySubmit = () => { | ||
executeQuery(query); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Helmet> | ||
<title> Detailed Page </title> | ||
</Helmet> | ||
|
||
<div> | ||
<input | ||
type="text" | ||
value={query} | ||
onChange={handleInputChange} | ||
placeholder="Enter SQL query" | ||
/> | ||
<br></br> | ||
<button onClick={handleQuerySubmit}>Execute Query</button> | ||
<button>Download sqlite file</button> | ||
<br></br> | ||
Look in the browser console for the output. (Right click, select option 'inspect element', navigate to the console tab) | ||
<br></br> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters