change to oop

This commit is contained in:
Yurun Chen
2024-12-02 14:14:32 +11:00
parent 8fe2e9236c
commit 32fd2b341e
3 changed files with 65 additions and 63 deletions

0
db.js Normal file
View File

View File

@@ -8,14 +8,29 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head> </head>
<body> <body>
<script src="https://12sen.dchen4.au/sqlite-js/sqlite3.js"></script>
<script>
window.sqlite3InitModule().then((_sqlite3)=>{
sqlite3 = _sqlite3;
console.warn("Installing sqlite3 module globally", sqlite3);
globalThis.sqlite3 = sqlite3;
const db = new sqlite3.oo1.DB();
db.exec([
"create table locations (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, subdiv VARCHAR(255) NOT NULL);",
"INSERT INTO LOCATIONS (subdiv) VALUES('thususnutluf');"
]);
init(db);
}).catch(e=>{
console.error("sqlite init failed:",e);
});</script>
<!-- bootstrap--> <!-- bootstrap-->
<script defer src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js" integrity="sha384-0pUGZvbkm6XF6gxjEnlmuGrJXVbNuzT9qBBavbLwCsOGabYfZo0T0to5eqruptLy" crossorigin="anonymous"></script> <script defer src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js" integrity="sha384-0pUGZvbkm6XF6gxjEnlmuGrJXVbNuzT9qBBavbLwCsOGabYfZo0T0to5eqruptLy" crossorigin="anonymous"></script>
<!-- vue stuff--> <!-- vue stuff-->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<!-- SQL --> <!-- SQL -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.2.1/dist/sql-wasm.js"></script>
<!--nav--> <!--nav-->
<nav class="navbar navbar-expand-lg navbar-light bg-warning"> <nav class="navbar navbar-expand-lg navbar-light bg-warning">
@@ -48,9 +63,8 @@
<!-- page columns --> <!-- page columns -->
<div class="container text-center " id="body"> <div class="container text-center " id="body">
<div class="row"> <div class="row"> <!-- now to vue-->
<!-- now to vue--> <div class="col-2 border border-primary text-left" id="filters" >
<div class="col-2 border border-primary text-left" id="filters">
<ul class=""> <ul class="">
<li class="list-group-item text-left" v-for="item in filters"> <li class="list-group-item text-left" v-for="item in filters">
<span v-html="item.html"></span> <span v-html="item.html"></span>
@@ -61,6 +75,7 @@
</div> </div>
</div> </div>
</div> </div>
<!-- my stuff--> <!-- my stuff-->
<script src="index.js"></script> <script src="index.js"></script>
</body> </body>

View File

@@ -1,60 +1,47 @@
const initSqlJs = window.initSqlJs class db_handler {
const SQL = await initSqlJs({ constructor (sqlite3){
// Required to load the wasm binary asynchronously. Of course, you can host it wherever you want this.db = sqlite3;
// You can omit locateFile completely when running in node
locateFile: file => `https://sql.js.org/dist/${file}`
});
// Create a database
const db = new SQL.Database();
const { createApp, ref } = Vue
//filters module
createApp({
props: ['dbConnObj'],
data(){
return {
filters : [{"html": a}]
} }
}, select_all(table){
var stmt = ("SELECT * FROM "+table+";");
methods:{ console.log(stmt);
sqlStatements(opt, val = 0){ return this.db.exec(stmt, {"returnValue":"resultRows"})
var stmt; }
switch(opt){
case 0: select(column, condition, table){
stmt = "SELECT * FROM index"; console.log(column,condition);
break; var stmt = this.db.prepare("SELECT * FROM "+table+" WHERE $column = $condition").bind([column,condition]);
case 1: return stmt.finalize();
stmt = "SELECT DISTINCT lga FROM index"; }
break; }
case 2:
stmt = "SELECT DISTINCT crime_category FROM index"; class component {
break; constructor(dbhandler){
case 3: this.handler = dbhandler;
stmt = "SELECT record_id FROM index"; }
case 4: }
stmt = db.prepare("SELECT * FROM records WHERE record_id=:id");
stmt = stmt. class filter extends component{
default: constructor(dbhandler){
break; super(dbhandler);
this.init();
} }
return stmt;
} ,
init(){ init(){
db.exec(sqlStatements(0)) var all_location = this.handler.select_all("locations");
} console.log(all_location);
}, var some_location = this.handler.select("id","1","locations");
console.log(some_location);
beforeMount(){
this.init;
} }
}, {"dbConnObj": db}).mount('#filters');
}
function init(sqlite3){
var global_component = new filter(new db_handler(sqlite3));
}
function filtergen(){
console.log(this.sqlite3);
}