15cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughespackage SQLite;
25cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
35cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes/**
45cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * Class wrapping an SQLite backup object.
55cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes */
65cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
75cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughespublic class Backup {
85cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
95cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    /**
105cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * Internal handle for the native SQLite API.
115cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     */
125cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
135cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    protected long handle = 0;
145cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
155cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    /**
165cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * Finish a backup.
175cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     */
185cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
195cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    protected void finish() throws SQLite.Exception {
205cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	synchronized(this) {
215cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	    _finalize();
225cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	}
235cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    }
245cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
255cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    /**
265cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * Destructor for object.
275cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     */
285cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
295cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    protected void finalize() {
305cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	synchronized(this) {
315cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	    try {
325cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes		_finalize();
335cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	    } catch (SQLite.Exception e) {
345cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	    }
355cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	}
365cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    }
375cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
385cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    protected native void _finalize() throws SQLite.Exception;
395cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
405cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    /**
415cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * Perform a backup step.
425cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     *
435cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * @param n number of pages to backup
445cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * @return true when backup completed
455cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     */
465cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
475cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    public boolean step(int n) throws SQLite.Exception {
485cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	synchronized(this) {
495cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	    return _step(n);
505cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	}
515cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    }
525cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
535cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    private native boolean _step(int n) throws SQLite.Exception;
545cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
555cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    /**
565cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * Perform the backup in one step.
575cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     */
585cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
595cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    public void backup() throws SQLite.Exception {
605cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	synchronized(this) {
615cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	    _step(-1);
625cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	}
635cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    }
645cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
655cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    /**
665cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * Return number of remaining pages to be backed up.
675cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     */
685cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
695cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    public int remaining() throws SQLite.Exception {
705cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	synchronized(this) {
715cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	    return _remaining();
725cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	}
735cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    }
745cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
755cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    private native int _remaining() throws SQLite.Exception;
765cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
775cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    /**
785cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * Return the total number of pages in the backup source database.
795cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     */
805cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
815cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    public int pagecount() throws SQLite.Exception {
825cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	synchronized(this) {
835cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	    return _pagecount();
845cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	}
855cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    }
865cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
875cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    private native int _pagecount() throws SQLite.Exception;
885cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
895cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    /**
905cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     * Internal native initializer.
915cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes     */
925cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
935cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    private static native void internal_init();
945cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
955cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    static {
965cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes	internal_init();
975cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes    }
985cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes}
995cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes
100