import { Bridge , BridgeError } from "../../bridging/" ; import { Database } from "better-sqlite3" ; import { LogService } from "matrix-bot-sdk" ; /**
 * This manages the bridge table in marco.db.
 */ export class BridgeTable { private readonly db : Database ; constructor ( db ) { this . db = db ; this . setupBridgeTable ( ) ; } param { string } id param { string } room returns { boolean } public setBridge ( id , room ) { const info = this . db . prepare ( 'INSERT INTO bridging (id, room) VALUES (?,?)' ) . run ( id , room ) ; return ( info . changes > 0 ) ; } param { string } id returns { Bridge } throws public getBridge ( id ) { const row = this . db . prepare ( 'SELECT room FROM bridging WHERE id=?' ) . get ( id ) ; const roomID = row ?. room ; if ( roomID ) { return new Bridge ( id , roomID ) ; } else { throw new BridgeError . NotBridgedError ( ) ; } } param { string } room returns { boolean } public unBridge ( room ) { const info = this . db . prepare ( 'DELETE FROM bridging WHERE room=?' ) . run ( room ) ; return ( info . changes > 0 ) ; } param { string } room returns { boolean } public isRoomBridged ( room ) { const row = this . db . prepare ( 'SELECT id FROM bridging WHERE room=?' ) . get ( room ) ; return ( row != undefined ) ; } param { string } id returns { boolean } public isBridged ( id ) { const row = this . db . prepare ( 'SELECT id FROM bridging WHERE id=?' ) . get ( id ) ; return ( row != undefined ) ; } /**
     * This sets up the bridge table
     */ private setupBridgeTable ( ) { LogService . info ( 'marco-db: bridging' , 'Setting up bridge table' ) ; this . db . prepare ( 'CREATE TABLE IF NOT EXISTS bridging (' + 'id text primary key NOT NULL,' + 'room text NOT NULL' + ')' ) . run ( ) ; } }