mirror of https://github.com/iSoron/uhabits.git
parent
e19339d808
commit
7cab0a39e5
@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.habits
|
||||||
|
|
||||||
|
import junit.framework.Assert.*
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
import org.junit.*
|
||||||
|
|
||||||
|
class AndroidDatabaseTest : BaseTest() {
|
||||||
|
@Test
|
||||||
|
fun testUsage() {
|
||||||
|
val dbFile = fileOpener.openUserFile("test.sqlite3")
|
||||||
|
if (dbFile.exists()) dbFile.delete()
|
||||||
|
val db = databaseOpener.open(dbFile)
|
||||||
|
|
||||||
|
var stmt = db.prepareStatement("drop table if exists demo")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("begin")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("create table if not exists demo(key int, value text)")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("insert into demo(key, value) values (?1, ?2)")
|
||||||
|
stmt.bindInt(1, 42)
|
||||||
|
stmt.bindText(2, "Hello World")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("select * from demo where key > ?1")
|
||||||
|
stmt.bindInt(1, 10)
|
||||||
|
var result = stmt.step()
|
||||||
|
assertEquals(result, StepResult.ROW)
|
||||||
|
assertEquals(stmt.getInt(0), 42)
|
||||||
|
assertEquals(stmt.getText(1), "Hello World")
|
||||||
|
result = stmt.step()
|
||||||
|
assertEquals(result, StepResult.DONE)
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("drop table demo")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("commit")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
dbFile.delete()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.habits
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
import org.junit.Assert.*
|
||||||
|
import java.io.*
|
||||||
|
|
||||||
|
class AndroidFilesTest : BaseTest() {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testUserFiles() {
|
||||||
|
val file = File(context.filesDir, "test.txt")
|
||||||
|
file.writeText("Hello world!")
|
||||||
|
|
||||||
|
val af = fileOpener.openUserFile("test.txt")
|
||||||
|
assertTrue(af.exists())
|
||||||
|
af.delete()
|
||||||
|
assertFalse(af.exists())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testResourceFiles() {
|
||||||
|
val file = fileOpener.openResourceFile("migrations/010.sql")
|
||||||
|
val lines = file.readLines()
|
||||||
|
assertEquals("delete from Score", lines[0])
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.habits
|
||||||
|
|
||||||
|
import android.support.test.*
|
||||||
|
|
||||||
|
open class BaseTest {
|
||||||
|
val context = InstrumentationRegistry.getTargetContext()
|
||||||
|
val fileOpener = AndroidFileOpener(context)
|
||||||
|
val databaseOpener = AndroidDatabaseOpener()
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
../../../core/assets/main/
|
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.habits
|
||||||
|
|
||||||
|
import org.isoron.uhabits.database.*
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
import java.sql.*
|
||||||
|
|
||||||
|
class AndroidDatabaseOpener : DatabaseOpener {
|
||||||
|
override fun open(file: UserFile): Database {
|
||||||
|
val platformFile = file as AndroidUserFile
|
||||||
|
DriverManager.registerDriver(Class.forName("org.sqldroid.SQLDroidDriver").newInstance() as Driver)
|
||||||
|
val conn = DriverManager.getConnection("jdbc:sqlite:${platformFile.file.absolutePath}")
|
||||||
|
return JavaDatabase(conn, AndroidLog())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.habits
|
||||||
|
|
||||||
|
import android.content.*
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
import java.io.*
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
|
class AndroidFileOpener(val context: Context) : FileOpener {
|
||||||
|
override fun openUserFile(filename: String): UserFile {
|
||||||
|
return AndroidUserFile(File(context.filesDir, filename))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun openResourceFile(filename: String): ResourceFile {
|
||||||
|
return AndroidResourceFile(context, filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AndroidResourceFile(val context: Context,
|
||||||
|
val filename: String) : ResourceFile {
|
||||||
|
|
||||||
|
override fun readLines(): List<String> {
|
||||||
|
val asset = context.assets.open(filename)
|
||||||
|
val reader = BufferedReader(InputStreamReader(asset))
|
||||||
|
val lines = ArrayList<String>()
|
||||||
|
while (true) {
|
||||||
|
val line = reader.readLine() ?: break
|
||||||
|
lines.add(line)
|
||||||
|
}
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AndroidUserFile(val file: File) : UserFile {
|
||||||
|
override fun delete() {
|
||||||
|
file.delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun exists(): Boolean {
|
||||||
|
return file.exists()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.habits
|
||||||
|
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
|
||||||
|
class AndroidLog : Log {
|
||||||
|
override fun debug(msg: String) {
|
||||||
|
android.util.Log.d("LOOP", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun info(msg: String) {
|
||||||
|
android.util.Log.i("LOOP", msg)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
create table Habits ( id integer primary key autoincrement, archived integer, color integer, description text, freq_den integer, freq_num integer, highlight integer, name text, position integer, reminder_hour integer, reminder_min integer )
|
||||||
|
create table Checkmarks ( id integer primary key autoincrement, habit integer references habits(id), timestamp integer, value integer )
|
||||||
|
create table Repetitions ( id integer primary key autoincrement, habit integer references habits(id), timestamp integer )
|
||||||
|
create table Streak ( id integer primary key autoincrement, end integer, habit integer references habits(id), length integer, start integer )
|
||||||
|
create table Score ( id integer primary key autoincrement, habit integer references habits(id), score integer, timestamp integer )
|
@ -0,0 +1,3 @@
|
|||||||
|
delete from Score
|
||||||
|
delete from Streak
|
||||||
|
delete from Checkmarks
|
@ -0,0 +1 @@
|
|||||||
|
alter table Habits add column reminder_days integer not null default 127
|
@ -0,0 +1,3 @@
|
|||||||
|
delete from Score
|
||||||
|
delete from Streak
|
||||||
|
delete from Checkmarks
|
@ -0,0 +1,4 @@
|
|||||||
|
create index idx_score_habit_timestamp on Score(habit, timestamp)
|
||||||
|
create index idx_checkmark_habit_timestamp on Checkmarks(habit, timestamp)
|
||||||
|
create index idx_repetitions_habit_timestamp on Repetitions(habit, timestamp)
|
||||||
|
create index idx_streak_habit_end on Streak(habit, end)
|
@ -0,0 +1,14 @@
|
|||||||
|
update habits set color=0 where color=-2937041
|
||||||
|
update habits set color=1 where color=-1684967
|
||||||
|
update habits set color=2 where color=-415707
|
||||||
|
update habits set color=3 where color=-5262293
|
||||||
|
update habits set color=4 where color=-13070788
|
||||||
|
update habits set color=5 where color=-16742021
|
||||||
|
update habits set color=6 where color=-16732991
|
||||||
|
update habits set color=7 where color=-16540699
|
||||||
|
update habits set color=8 where color=-10603087
|
||||||
|
update habits set color=9 where color=-7461718
|
||||||
|
update habits set color=10 where color=-2614432
|
||||||
|
update habits set color=11 where color=-13619152
|
||||||
|
update habits set color=12 where color=-5592406
|
||||||
|
update habits set color=0 where color<0 or color>12
|
@ -0,0 +1,3 @@
|
|||||||
|
delete from Score
|
||||||
|
delete from Streak
|
||||||
|
delete from Checkmarks
|
@ -0,0 +1,2 @@
|
|||||||
|
alter table Habits add column type integer not null default 0
|
||||||
|
alter table Repetitions add column value integer not null default 2
|
@ -0,0 +1,5 @@
|
|||||||
|
drop table Score
|
||||||
|
create table Score ( id integer primary key autoincrement, habit integer references habits(id), score real, timestamp integer)
|
||||||
|
create index idx_score_habit_timestamp on Score(habit, timestamp)
|
||||||
|
delete from streak
|
||||||
|
delete from checkmarks
|
@ -0,0 +1,3 @@
|
|||||||
|
alter table Habits add column target_type integer not null default 0
|
||||||
|
alter table Habits add column target_value real not null default 0
|
||||||
|
alter table Habits add column unit text not null default ""
|
@ -0,0 +1 @@
|
|||||||
|
create table Events ( id integer primary key autoincrement, timestamp integer, message text, server_id integer )
|
@ -0,0 +1,3 @@
|
|||||||
|
drop table checkmarks
|
||||||
|
drop table streak
|
||||||
|
drop table score
|
@ -0,0 +1,12 @@
|
|||||||
|
update habits set color=19 where color=12
|
||||||
|
update habits set color=17 where color=11
|
||||||
|
update habits set color=15 where color=10
|
||||||
|
update habits set color=14 where color=9
|
||||||
|
update habits set color=13 where color=8
|
||||||
|
update habits set color=10 where color=7
|
||||||
|
update habits set color=9 where color=6
|
||||||
|
update habits set color=8 where color=5
|
||||||
|
update habits set color=7 where color=4
|
||||||
|
update habits set color=5 where color=3
|
||||||
|
update habits set color=4 where color=2
|
||||||
|
update habits set color=0 where color<0 or color>19
|
@ -0,0 +1,11 @@
|
|||||||
|
delete from repetitions where habit not in (select id from habits)
|
||||||
|
delete from repetitions where timestamp is null
|
||||||
|
delete from repetitions where habit is null
|
||||||
|
delete from repetitions where rowid not in ( select min(rowid) from repetitions group by habit, timestamp )
|
||||||
|
alter table Repetitions rename to RepetitionsBak
|
||||||
|
create table Repetitions ( id integer primary key autoincrement, habit integer not null references habits(id), timestamp integer not null, value integer not null)
|
||||||
|
drop index if exists idx_repetitions_habit_timestamp
|
||||||
|
create unique index idx_repetitions_habit_timestamp on Repetitions( habit, timestamp)
|
||||||
|
insert into Repetitions select * from RepetitionsBak
|
||||||
|
drop table RepetitionsBak
|
||||||
|
pragma foreign_keys=ON
|
@ -0,0 +1,2 @@
|
|||||||
|
Hello World!
|
||||||
|
This is a resource.
|
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits
|
||||||
|
|
||||||
|
const val LOOP_DATABASE_VERSION = 22
|
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.models
|
||||||
|
|
||||||
|
import org.isoron.uhabits.utils.Database
|
||||||
|
import org.isoron.uhabits.utils.StepResult
|
||||||
|
|
||||||
|
class HabitList(var db: Database) {
|
||||||
|
var habits = mutableListOf<Habit>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
loadHabitsFromDatabase()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getActive(): List<Habit> {
|
||||||
|
return habits.filter { h -> !h.isArchived }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadHabitsFromDatabase() {
|
||||||
|
val stmt = db.prepareStatement(
|
||||||
|
"select id, name, description, freq_num, freq_den, color, " +
|
||||||
|
"archived, position, unit, target_value, type " +
|
||||||
|
"from habits")
|
||||||
|
|
||||||
|
while (stmt.step() == StepResult.ROW) {
|
||||||
|
habits.add(Habit(id = stmt.getInt(0),
|
||||||
|
name = stmt.getText(1),
|
||||||
|
description = stmt.getText(2),
|
||||||
|
frequency = Frequency(stmt.getInt(3),
|
||||||
|
stmt.getInt(4)),
|
||||||
|
color = Color(stmt.getInt(5)),
|
||||||
|
isArchived = (stmt.getInt(6) != 0),
|
||||||
|
position = stmt.getInt(7),
|
||||||
|
unit = stmt.getText(8),
|
||||||
|
target = 0,
|
||||||
|
type = HabitType.YES_NO_HABIT))
|
||||||
|
}
|
||||||
|
stmt.finalize()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
enum class StepResult {
|
||||||
|
ROW,
|
||||||
|
DONE
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PreparedStatement {
|
||||||
|
fun step(): StepResult
|
||||||
|
fun finalize()
|
||||||
|
fun getInt(index: Int): Int
|
||||||
|
fun getText(index: Int): String
|
||||||
|
fun bindInt(index: Int, value: Int)
|
||||||
|
fun bindText(index: Int, value: String)
|
||||||
|
fun reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Database {
|
||||||
|
fun prepareStatement(sql: String): PreparedStatement
|
||||||
|
fun close()
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DatabaseOpener {
|
||||||
|
fun open(file: UserFile): Database
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Database.execute(sql: String) {
|
||||||
|
val stmt = prepareStatement(sql)
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Database.queryInt(sql: String): Int {
|
||||||
|
val stmt = prepareStatement(sql)
|
||||||
|
stmt.step()
|
||||||
|
val result = stmt.getInt(0)
|
||||||
|
stmt.finalize()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Database.begin() = execute("begin")
|
||||||
|
fun Database.commit() = execute("commit")
|
||||||
|
fun Database.rollback() = execute("rollback")
|
||||||
|
fun Database.getVersion() = queryInt("pragma user_version")
|
||||||
|
fun Database.setVersion(v: Int) = execute("pragma user_version = $v")
|
||||||
|
|
||||||
|
fun Database.migrateTo(newVersion: Int, fileOpener: FileOpener, log: Log) {
|
||||||
|
val currentVersion = getVersion()
|
||||||
|
log.debug("Current database version: $currentVersion")
|
||||||
|
|
||||||
|
if (currentVersion == newVersion) return
|
||||||
|
log.debug("Upgrading to version: $newVersion")
|
||||||
|
|
||||||
|
if (currentVersion > newVersion)
|
||||||
|
throw RuntimeException("database produced by future version of the application")
|
||||||
|
|
||||||
|
begin()
|
||||||
|
for (v in (currentVersion + 1)..newVersion) {
|
||||||
|
log.debug("Running migration $v")
|
||||||
|
val filename = sprintf("migrations/%03d.sql", v)
|
||||||
|
val migrationFile = fileOpener.openResourceFile(filename)
|
||||||
|
for (line in migrationFile.readLines()) {
|
||||||
|
if (line.isEmpty()) continue
|
||||||
|
execute(line)
|
||||||
|
}
|
||||||
|
setVersion(v)
|
||||||
|
}
|
||||||
|
commit()
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a file that was shipped with the application, such as migration
|
||||||
|
* files or translations. These files cannot be deleted.
|
||||||
|
*/
|
||||||
|
interface ResourceFile {
|
||||||
|
fun readLines(): List<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a file that was created after the application was installed, as a
|
||||||
|
* result of some user action, such as databases and logs. These files can be
|
||||||
|
* deleted.
|
||||||
|
*/
|
||||||
|
interface UserFile {
|
||||||
|
fun delete()
|
||||||
|
fun exists(): Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FileOpener {
|
||||||
|
/**
|
||||||
|
* Opens a file which was shipped bundled with the application, such as a
|
||||||
|
* migration file.
|
||||||
|
*
|
||||||
|
* The path is relative to the assets folder. For example, to open
|
||||||
|
* assets/main/migrations/09.sql you should provide migrations/09.sql
|
||||||
|
* as the filename.
|
||||||
|
*/
|
||||||
|
fun openResourceFile(filename: String): ResourceFile
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens a file which was not shipped with the application, such as
|
||||||
|
* databases and logs.
|
||||||
|
*
|
||||||
|
* The path is relative to the user folder. For example, if the application
|
||||||
|
* stores the user data at /home/user/.loop/ and you wish to open the file
|
||||||
|
* /home/user/.loop/crash.log, you should provide crash.log as the filename.
|
||||||
|
*/
|
||||||
|
fun openUserFile(filename: String): UserFile
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
interface Log {
|
||||||
|
fun info(msg: String)
|
||||||
|
fun debug(msg: String)
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
expect fun sprintf(format: String, vararg args: Any?): String
|
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
import kotlinx.cinterop.*
|
||||||
|
|
||||||
|
actual fun sprintf(format: String, vararg args: Any?): String {
|
||||||
|
val buffer = ByteArray(1000)
|
||||||
|
buffer.usePinned { p ->
|
||||||
|
platform.posix.sprintf(p.addressOf(0), format, *args)
|
||||||
|
}
|
||||||
|
return buffer.stringFromUtf8()
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.database
|
||||||
|
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
import java.sql.Connection
|
||||||
|
import java.sql.DriverManager
|
||||||
|
import java.sql.PreparedStatement
|
||||||
|
import java.sql.ResultSet
|
||||||
|
|
||||||
|
class JavaPreparedStatement(private var stmt : PreparedStatement) : org.isoron.uhabits.utils.PreparedStatement {
|
||||||
|
|
||||||
|
private var rs: ResultSet? = null
|
||||||
|
private var hasExecuted = false
|
||||||
|
|
||||||
|
override fun step(): StepResult {
|
||||||
|
if (!hasExecuted) {
|
||||||
|
hasExecuted = true
|
||||||
|
val hasResult = stmt.execute()
|
||||||
|
if (hasResult) rs = stmt.resultSet
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rs == null || !rs!!.next()) return StepResult.DONE
|
||||||
|
return StepResult.ROW
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun finalize() {
|
||||||
|
stmt.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getInt(index: Int): Int {
|
||||||
|
return rs!!.getInt(index + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getText(index: Int): String {
|
||||||
|
return rs!!.getString(index + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun bindInt(index: Int, value: Int) {
|
||||||
|
stmt.setInt(index, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun bindText(index: Int, value: String) {
|
||||||
|
stmt.setString(index, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun reset() {
|
||||||
|
stmt.clearParameters()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JavaDatabase(private var conn: Connection,
|
||||||
|
private val log: Log) : Database {
|
||||||
|
|
||||||
|
override fun prepareStatement(sql: String): org.isoron.uhabits.utils.PreparedStatement {
|
||||||
|
log.debug("Running SQL: ${sql}")
|
||||||
|
return JavaPreparedStatement(conn.prepareStatement(sql))
|
||||||
|
}
|
||||||
|
override fun close() {
|
||||||
|
conn.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JavaDatabaseOpener(private val log: Log) : DatabaseOpener {
|
||||||
|
override fun open(file: UserFile): Database {
|
||||||
|
val platformFile = file as JavaUserFile
|
||||||
|
val conn = DriverManager.getConnection("jdbc:sqlite:${platformFile.path}")
|
||||||
|
return JavaDatabase(conn, log)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Path
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
|
class JavaResourceFile(private val path: Path) : ResourceFile {
|
||||||
|
override fun readLines(): List<String> {
|
||||||
|
return Files.readAllLines(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JavaUserFile(val path: Path) : UserFile {
|
||||||
|
override fun exists(): Boolean {
|
||||||
|
return Files.exists(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun delete() {
|
||||||
|
Files.delete(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JavaFileOpener : FileOpener {
|
||||||
|
override fun openUserFile(filename: String): UserFile {
|
||||||
|
val path = Paths.get("/tmp/$filename")
|
||||||
|
return JavaUserFile(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun openResourceFile(filename: String): ResourceFile {
|
||||||
|
val rootFolders = listOf("assets/main",
|
||||||
|
"assets/test")
|
||||||
|
for (root in rootFolders) {
|
||||||
|
val path = Paths.get("$root/$filename")
|
||||||
|
if (Files.exists(path) && Files.isReadable(path)) {
|
||||||
|
return JavaResourceFile(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw RuntimeException("file not found")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
class JavaLog : Log {
|
||||||
|
override fun info(msg: String) {
|
||||||
|
println("[I] $msg")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun debug(msg: String) {
|
||||||
|
println("[D] $msg")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
actual fun sprintf(format: String, vararg args: Any?): String {
|
||||||
|
return String.format(format, *args)
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits
|
||||||
|
|
||||||
|
import junit.framework.Assert.assertEquals
|
||||||
|
import junit.framework.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class BackendTest : BaseTest() {
|
||||||
|
@Test
|
||||||
|
fun testBackend() {
|
||||||
|
// val backend = Backend(databaseOpener, fileOpener)
|
||||||
|
// assertEquals(backend.getHabitList().size, 0)
|
||||||
|
//
|
||||||
|
// backend.createHabit("Brush teeth")
|
||||||
|
// backend.createHabit("Wake up early")
|
||||||
|
|
||||||
|
// var result = backend.getHabitList()
|
||||||
|
// assertEquals(2, result.size)
|
||||||
|
// assertEquals(result[0]["name"], "Brush teeth")
|
||||||
|
// assertEquals(result[0]["name"], "Wake up early")
|
||||||
|
//
|
||||||
|
// backend.deleteHabit(1)
|
||||||
|
// result = backend.getHabitList()
|
||||||
|
// assertEquals(result.size, 1)
|
||||||
|
//
|
||||||
|
// backend.updateHabit(2, "Wake up late")
|
||||||
|
// result = backend.getHabitList()
|
||||||
|
// assertEquals(result[2]["name"], "Wake up late")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits
|
||||||
|
|
||||||
|
import org.isoron.uhabits.utils.JavaFileOpener
|
||||||
|
import org.isoron.uhabits.database.JavaDatabaseOpener
|
||||||
|
import org.isoron.uhabits.utils.JavaLog
|
||||||
|
|
||||||
|
open class BaseTest {
|
||||||
|
val fileOpener = JavaFileOpener()
|
||||||
|
val databaseOpener = JavaDatabaseOpener(JavaLog())
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.database
|
||||||
|
|
||||||
|
import org.isoron.uhabits.BaseTest
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class JavaDatabaseTest : BaseTest() {
|
||||||
|
private lateinit var db: Database
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
val dbFile = fileOpener.openUserFile("test.sqlite3")
|
||||||
|
if (dbFile.exists()) dbFile.delete()
|
||||||
|
db = databaseOpener.open(dbFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testUsage() {
|
||||||
|
db.setVersion(0)
|
||||||
|
assertEquals(db.getVersion(), 0)
|
||||||
|
|
||||||
|
db.setVersion(23)
|
||||||
|
assertEquals(db.getVersion(), 23)
|
||||||
|
|
||||||
|
var stmt = db.prepareStatement("drop table if exists demo")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("create table if not exists demo(key int, value text)")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("insert into demo(key, value) values (?1, ?2)")
|
||||||
|
stmt.bindInt(1, 42)
|
||||||
|
stmt.bindText(2, "Hello World")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement("select * from demo where key > ?1")
|
||||||
|
stmt.bindInt(1, 10)
|
||||||
|
|
||||||
|
var result = stmt.step()
|
||||||
|
assertEquals(result, StepResult.ROW)
|
||||||
|
assertEquals(stmt.getInt(0), 42)
|
||||||
|
assertEquals(stmt.getText(1), "Hello World")
|
||||||
|
|
||||||
|
result = stmt.step()
|
||||||
|
assertEquals(result, StepResult.DONE)
|
||||||
|
|
||||||
|
stmt.finalize()
|
||||||
|
db.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMigrateTo() {
|
||||||
|
assertEquals(0, db.getVersion())
|
||||||
|
db.migrateTo(22, fileOpener)
|
||||||
|
assertEquals(22, db.getVersion())
|
||||||
|
db.execute("select * from habits")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils
|
||||||
|
|
||||||
|
import org.isoron.uhabits.BaseTest
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class JavaFilesTest : BaseTest() {
|
||||||
|
@Test
|
||||||
|
fun testReadLines() {
|
||||||
|
val hello = fileOpener.openResourceFile("hello.txt")
|
||||||
|
var lines = hello.readLines()
|
||||||
|
assertEquals("Hello World!", lines[0])
|
||||||
|
assertEquals("This is a resource.", lines[1])
|
||||||
|
|
||||||
|
val migration = fileOpener.openResourceFile("migrations/012.sql")
|
||||||
|
lines = migration.readLines()
|
||||||
|
assertEquals("delete from Score", lines[0])
|
||||||
|
}
|
||||||
|
}
|
@ -1,49 +0,0 @@
|
|||||||
import Foundation
|
|
||||||
|
|
||||||
@objc(CoreModule)
|
|
||||||
class CoreModule: RCTEventEmitter {
|
|
||||||
|
|
||||||
@objc
|
|
||||||
open override func supportedEvents() -> [String] {
|
|
||||||
return ["onHabitList"]
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc
|
|
||||||
func requestHabitList() {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
let habits = AppDelegate.backend.getHabitList()
|
|
||||||
let result = habits.map {
|
|
||||||
["key": String($0.key.intValue),
|
|
||||||
"name": $0.value["name"],
|
|
||||||
"color": $0.value["color"]]
|
|
||||||
}
|
|
||||||
self.sendEvent(withName: "onHabitList", body: result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc
|
|
||||||
func createHabit(_ name: String) {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
AppDelegate.backend.createHabit(name: name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc
|
|
||||||
func deleteHabit(_ id: Int32) {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
AppDelegate.backend.deleteHabit(id: id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc
|
|
||||||
func updateHabit(_ id: Int32, _ name: String) {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
AppDelegate.backend.updateHabit(id: id, name: name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc
|
|
||||||
override static func requiresMainQueueSetup() -> Bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
extension String: Error {}
|
||||||
|
|
||||||
|
@objc(CoreModule)
|
||||||
|
class CoreModule: RCTEventEmitter {
|
||||||
|
|
||||||
|
func convert(_ obj: Any?) -> Any? {
|
||||||
|
if obj is KotlinInt {
|
||||||
|
return (obj as! KotlinInt).intValue
|
||||||
|
}
|
||||||
|
if obj is NSString {
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
if obj is Dictionary<String, Any> {
|
||||||
|
return (obj as! Dictionary<String, Any>).mapValues{ convert($0) }
|
||||||
|
}
|
||||||
|
if obj is Array<Any> {
|
||||||
|
return (obj as! Array<Any>).map { convert($0) }
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc
|
||||||
|
open override func supportedEvents() -> [String] {
|
||||||
|
return ["onHabitList"]
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc
|
||||||
|
func requestHabitList() {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
let result = AppDelegate.backend.getHabitList()
|
||||||
|
self.sendEvent(withName: "onHabitList", body: self.convert(result))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc
|
||||||
|
func createHabit(_ name: String) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
AppDelegate.backend.createHabit(name: name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc
|
||||||
|
func deleteHabit(_ id: Int32) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
AppDelegate.backend.deleteHabit(id: id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc
|
||||||
|
func updateHabit(_ id: Int32, _ name: String) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
AppDelegate.backend.updateHabit(id: id, name: name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc
|
||||||
|
override static func requiresMainQueueSetup() -> Bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import SQLite3
|
||||||
|
|
||||||
|
internal let SQLITE_STATIC = unsafeBitCast(0, to: sqlite3_destructor_type.self)
|
||||||
|
internal let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
|
||||||
|
|
||||||
|
class IosPreparedStatement : NSObject, PreparedStatement {
|
||||||
|
|
||||||
|
var db: OpaquePointer
|
||||||
|
var statement: OpaquePointer
|
||||||
|
|
||||||
|
init(withStatement statement: OpaquePointer, withDb db: OpaquePointer) {
|
||||||
|
self.statement = statement
|
||||||
|
self.db = db
|
||||||
|
}
|
||||||
|
|
||||||
|
func step() -> StepResult {
|
||||||
|
let result = sqlite3_step(statement)
|
||||||
|
if result == SQLITE_ROW {
|
||||||
|
return StepResult.row
|
||||||
|
} else if result == SQLITE_DONE {
|
||||||
|
return StepResult.done
|
||||||
|
} else {
|
||||||
|
let errMsg = String(cString: sqlite3_errmsg(db)!)
|
||||||
|
fatalError("Database error: \(errMsg) (\(result))")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getInt(index: Int32) -> Int32 {
|
||||||
|
return sqlite3_column_int(statement, index)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getText(index: Int32) -> String {
|
||||||
|
return String(cString: sqlite3_column_text(statement, index))
|
||||||
|
}
|
||||||
|
|
||||||
|
func bindInt(index: Int32, value: Int32) {
|
||||||
|
sqlite3_bind_int(statement, index, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func bindText(index: Int32, value: String) {
|
||||||
|
sqlite3_bind_text(statement, index, value, -1, SQLITE_TRANSIENT)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reset() {
|
||||||
|
sqlite3_reset(statement)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func finalize() {
|
||||||
|
sqlite3_finalize(statement)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class IosDatabase : NSObject, Database {
|
||||||
|
var db: OpaquePointer
|
||||||
|
|
||||||
|
init(withDb db: OpaquePointer) {
|
||||||
|
self.db = db
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareStatement(sql: String) -> PreparedStatement {
|
||||||
|
if sql.isEmpty {
|
||||||
|
fatalError("Provided SQL query is empty")
|
||||||
|
}
|
||||||
|
print("Running SQL: \(sql)")
|
||||||
|
var statement : OpaquePointer?
|
||||||
|
let result = sqlite3_prepare_v2(db, sql, -1, &statement, nil)
|
||||||
|
if result == SQLITE_OK {
|
||||||
|
return IosPreparedStatement(withStatement: statement!, withDb: db)
|
||||||
|
} else {
|
||||||
|
let errMsg = String(cString: sqlite3_errmsg(db)!)
|
||||||
|
fatalError("Database error: \(errMsg)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func close() {
|
||||||
|
sqlite3_close(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class IosDatabaseOpener : NSObject, DatabaseOpener {
|
||||||
|
func open(file: UserFile) -> Database {
|
||||||
|
let dbPath = (file as! IosUserFile).path
|
||||||
|
|
||||||
|
let version = String(cString: sqlite3_libversion())
|
||||||
|
print("SQLite \(version)")
|
||||||
|
print("Opening database: \(dbPath)")
|
||||||
|
var db: OpaquePointer?
|
||||||
|
let result = sqlite3_open(dbPath, &db)
|
||||||
|
if result == SQLITE_OK {
|
||||||
|
return IosDatabase(withDb: db!)
|
||||||
|
} else {
|
||||||
|
fatalError("Error opening database (code \(result))")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class IosResourceFile : NSObject, ResourceFile {
|
||||||
|
|
||||||
|
var path: String
|
||||||
|
|
||||||
|
var fileManager = FileManager.default
|
||||||
|
|
||||||
|
init(forPath path: String) {
|
||||||
|
self.path = path
|
||||||
|
}
|
||||||
|
|
||||||
|
func readLines() -> [String] {
|
||||||
|
do {
|
||||||
|
let contents = try String(contentsOfFile: self.path, encoding: .utf8)
|
||||||
|
return contents.components(separatedBy: CharacterSet.newlines)
|
||||||
|
} catch {
|
||||||
|
return [""]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class IosUserFile : NSObject, UserFile {
|
||||||
|
|
||||||
|
var path: String
|
||||||
|
|
||||||
|
init(forPath path: String) {
|
||||||
|
self.path = path
|
||||||
|
}
|
||||||
|
|
||||||
|
func delete() {
|
||||||
|
do {
|
||||||
|
try FileManager.default.removeItem(atPath: path)
|
||||||
|
} catch {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func exists() -> Bool {
|
||||||
|
return FileManager.default.fileExists(atPath: path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class IosFileOpener : NSObject, FileOpener {
|
||||||
|
func openResourceFile(filename: String) -> ResourceFile {
|
||||||
|
let path = "\(Bundle.main.resourcePath!)/\(filename)"
|
||||||
|
return IosResourceFile(forPath: path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func openUserFile(filename: String) -> UserFile {
|
||||||
|
do {
|
||||||
|
let root = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path
|
||||||
|
return IosUserFile(forPath: "\(root)/\(filename)")
|
||||||
|
} catch {
|
||||||
|
return IosUserFile(forPath: "invalid")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class IosLog : NSObject, Log {
|
||||||
|
func info(msg: String) {
|
||||||
|
print("[I] \(msg)")
|
||||||
|
}
|
||||||
|
|
||||||
|
func debug(msg: String) {
|
||||||
|
print("[D] \(msg)")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||||
|
<key>CFBundleExecutable</key>
|
||||||
|
<string>$(EXECUTABLE_NAME)</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundleName</key>
|
||||||
|
<string>$(PRODUCT_NAME)</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>BNDL</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import XCTest
|
||||||
|
@testable import uhabits
|
||||||
|
|
||||||
|
class IosFilesTest: XCTestCase {
|
||||||
|
func testResourceFiles() {
|
||||||
|
let fileOpener = IosFileOpener()
|
||||||
|
let file = fileOpener.openResourceFile(filename: "migrations/010.sql")
|
||||||
|
let lines = file.readLines()
|
||||||
|
XCTAssertEqual(lines[0], "delete from Score")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUserFiles() throws {
|
||||||
|
let fm = FileManager.default
|
||||||
|
let root = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path
|
||||||
|
let path = "\(root)/test.txt"
|
||||||
|
fm.createFile(atPath: path, contents: "Hello world\nThis is line 2".data(using: .utf8), attributes: nil)
|
||||||
|
|
||||||
|
let fileOpener = IosFileOpener()
|
||||||
|
let file = fileOpener.openUserFile(filename: "test.txt")
|
||||||
|
XCTAssertTrue(file.exists())
|
||||||
|
|
||||||
|
file.delete()
|
||||||
|
XCTAssertFalse(file.exists())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import XCTest
|
||||||
|
@testable import uhabits
|
||||||
|
|
||||||
|
class IosDatabaseTest: XCTestCase {
|
||||||
|
func testUsage() {
|
||||||
|
let databaseOpener = IosDatabaseOpener()
|
||||||
|
let fileOpener = IosFileOpener()
|
||||||
|
|
||||||
|
let dbFile = fileOpener.openUserFile(filename: "test.sqlite3")
|
||||||
|
if dbFile.exists() {
|
||||||
|
dbFile.delete()
|
||||||
|
}
|
||||||
|
let db = databaseOpener.open(file: dbFile)
|
||||||
|
|
||||||
|
var stmt = db.prepareStatement(sql: "drop table if exists demo")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement(sql: "begin")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement(sql: "create table if not exists demo(key int, value text)")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement(sql: "insert into demo(key, value) values (?1, ?2)")
|
||||||
|
stmt.bindInt(index: 1, value: 42)
|
||||||
|
stmt.bindText(index: 2, value: "Hello World")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement(sql: "select * from demo where key > ?1")
|
||||||
|
stmt.bindInt(index: 1, value: 10)
|
||||||
|
var result = stmt.step()
|
||||||
|
XCTAssertEqual(result, StepResult.row)
|
||||||
|
XCTAssertEqual(stmt.getInt(index: 0), 42)
|
||||||
|
XCTAssertEqual(stmt.getText(index: 1), "Hello World")
|
||||||
|
result = stmt.step()
|
||||||
|
XCTAssertEqual(result, StepResult.done)
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement(sql: "drop table demo")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
stmt = db.prepareStatement(sql: "commit")
|
||||||
|
stmt.step()
|
||||||
|
stmt.finalize()
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
dbFile.delete()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue