mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Implement JDBC database
This commit is contained in:
@@ -8,19 +8,24 @@ apply plugin: 'jacoco'
|
||||
dependencies {
|
||||
apt 'com.google.auto.factory:auto-factory:1.0-beta3'
|
||||
apt 'com.google.dagger:dagger:2.11-rc2'
|
||||
|
||||
compileOnly 'javax.annotation:jsr250-api:1.0'
|
||||
compileOnly 'org.jetbrains:annotations-java5:15.0'
|
||||
compileOnly 'com.google.auto.factory:auto-factory:1.0-beta3'
|
||||
compileOnly 'com.google.dagger:dagger:2.11-rc2'
|
||||
|
||||
implementation 'com.android.support:support-annotations:25.3.1'
|
||||
implementation 'com.google.code.findbugs:jsr305:3.0.2'
|
||||
implementation 'org.apache.commons:commons-lang3:3.5'
|
||||
implementation 'com.google.code.gson:gson:2.7'
|
||||
|
||||
testImplementation 'junit:junit:4+'
|
||||
testImplementation 'org.hamcrest:hamcrest-library:1.4-atlassian-1'
|
||||
testImplementation 'org.apache.commons:commons-io:1.3.2'
|
||||
testImplementation 'org.mockito:mockito-core:2.8.9'
|
||||
testImplementation 'org.json:json:20160810'
|
||||
testImplementation 'org.xerial:sqlite-jdbc:3.18.0'
|
||||
|
||||
implementation('com.opencsv:opencsv:3.9') {
|
||||
exclude group: 'commons-logging', module: 'commons-logging'
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*
|
||||
* 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.core.db;
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*
|
||||
* 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.core.db;
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*
|
||||
* 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.core.db;
|
||||
@@ -36,7 +34,7 @@ public interface Database
|
||||
|
||||
void delete(String tableName, String where, String... params);
|
||||
|
||||
void execSQL(String query, Object... params);
|
||||
void execute(String query, Object... params);
|
||||
|
||||
void beginTransaction();
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Á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.core.db;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
public class JdbcCursor implements Cursor
|
||||
{
|
||||
private ResultSet resultSet;
|
||||
|
||||
public JdbcCursor(ResultSet resultSet)
|
||||
{
|
||||
this.resultSet = resultSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
try
|
||||
{
|
||||
resultSet.close();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean moveToNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
return resultSet.next();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInt(int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
return resultSet.getInt(index);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLong(int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
return resultSet.getLong(index);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
return resultSet.getDouble(index);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
return resultSet.getString(index);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Á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.core.db;
|
||||
|
||||
import org.apache.commons.lang3.*;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
public class JdbcDatabase implements Database
|
||||
{
|
||||
private Connection connection;
|
||||
|
||||
private boolean transactionSuccessful;
|
||||
|
||||
public JdbcDatabase(Connection connection)
|
||||
{
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor select(String query, String... params)
|
||||
{
|
||||
try
|
||||
{
|
||||
PreparedStatement st = buildStatement(query, params);
|
||||
return new JdbcCursor(st.executeQuery());
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int update(String tableName,
|
||||
Map<String, Object> map,
|
||||
String where,
|
||||
String... params)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArrayList<String> fields = new ArrayList<>();
|
||||
ArrayList<String> values = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, Object> entry : map.entrySet())
|
||||
{
|
||||
if (entry.getValue() == null) continue;
|
||||
fields.add(entry.getKey() + "=?");
|
||||
values.add(entry.getValue().toString());
|
||||
}
|
||||
values.addAll(Arrays.asList(params));
|
||||
|
||||
String query = String.format("update %s set %s where %s", tableName,
|
||||
StringUtils.join(fields, ", "), where);
|
||||
System.out.println(query);
|
||||
|
||||
PreparedStatement st = buildStatement(query, values.toArray());
|
||||
return st.executeUpdate();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long insert(String tableName, Map<String, Object> map)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArrayList<String> fields = new ArrayList<>();
|
||||
ArrayList<Object> params = new ArrayList<>();
|
||||
ArrayList<String> questionMarks = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, Object> entry : map.entrySet())
|
||||
{
|
||||
if (entry.getValue() == null) continue;
|
||||
fields.add(entry.getKey());
|
||||
params.add(entry.getValue());
|
||||
questionMarks.add("?");
|
||||
}
|
||||
|
||||
String query =
|
||||
String.format("insert into %s(%s) values(%s)", tableName,
|
||||
StringUtils.join(fields, ", "),
|
||||
StringUtils.join(questionMarks, ", "));
|
||||
|
||||
PreparedStatement st = buildStatement(query, params.toArray());
|
||||
st.execute();
|
||||
|
||||
Long id = null;
|
||||
ResultSet keys = st.getGeneratedKeys();
|
||||
if (keys.next()) id = keys.getLong(1);
|
||||
return id;
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String tableName, String where, String... params)
|
||||
{
|
||||
String query =
|
||||
String.format("delete from %s where %s", tableName, where);
|
||||
execute(query, (Object[]) params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String query, Object... params)
|
||||
{
|
||||
try
|
||||
{
|
||||
buildStatement(query, params).execute();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private PreparedStatement buildStatement(String query, Object[] params)
|
||||
throws SQLException
|
||||
{
|
||||
PreparedStatement st = connection.prepareStatement(query);
|
||||
int index = 1;
|
||||
for (Object param : params) st.setString(index++, param.toString());
|
||||
return st;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void beginTransaction()
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.setAutoCommit(false);
|
||||
transactionSuccessful = false;
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void setTransactionSuccessful()
|
||||
{
|
||||
transactionSuccessful = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void endTransaction()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (transactionSuccessful) connection.commit();
|
||||
else connection.rollback();
|
||||
connection.setAutoCommit(true);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Á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.core.db;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class MigrationHelper
|
||||
{
|
||||
private final FileOpener opener;
|
||||
|
||||
private final Database db;
|
||||
|
||||
public MigrationHelper(@NonNull FileOpener opener, @NonNull Database db)
|
||||
{
|
||||
this.opener = opener;
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public void executeMigrations(int oldVersion, int newVersion)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int v = oldVersion + 1; v <= newVersion; v++)
|
||||
{
|
||||
String fname = String.format(Locale.US, "migrations/%d.sql", v);
|
||||
InputStream stream = opener.open(fname);
|
||||
for (String command : SQLParser.parse(stream))
|
||||
db.execute(command);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public interface FileOpener
|
||||
{
|
||||
InputStream open(String filename);
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,6 @@
|
||||
*
|
||||
* 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.core.db;
|
||||
@@ -57,7 +55,7 @@ public class Repository<T>
|
||||
|
||||
/**
|
||||
* Returns all records matching the given SQL query.
|
||||
*
|
||||
* <p>
|
||||
* The query should only contain the "where" part of the SQL query, and
|
||||
* optinally the "order by" part. "Group by" is not allowed. If no matching
|
||||
* records are found, returns an empty list.
|
||||
@@ -87,19 +85,19 @@ public class Repository<T>
|
||||
|
||||
/**
|
||||
* Executes the given SQL query on the repository.
|
||||
*
|
||||
* <p>
|
||||
* The query can be of any kind. For example, complex deletes and updates
|
||||
* are allowed. The repository does not perform any checks to guarantee
|
||||
* that the query is valid, however the underlying database might.
|
||||
*/
|
||||
public void execSQL(String query, Object... params)
|
||||
{
|
||||
db.execSQL(query, params);
|
||||
db.execute(query, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the given callback inside a database transaction.
|
||||
*
|
||||
* <p>
|
||||
* If the callback terminates without throwing any exceptions, the
|
||||
* transaction is considered successful. If any exceptions are thrown,
|
||||
* the transaction is aborted. Nesting transactions is not allowed.
|
||||
@@ -124,12 +122,12 @@ public class Repository<T>
|
||||
|
||||
/**
|
||||
* Saves the record on the database.
|
||||
*
|
||||
* <p>
|
||||
* If the id of the given record is null, it is assumed that the record has
|
||||
* not been inserted in the repository yet. The record will be inserted, a
|
||||
* new id will be automatically generated, and the id of the given record
|
||||
* will be updated.
|
||||
*
|
||||
* <p>
|
||||
* If the given record has a non-null id, then an update will be performed
|
||||
* instead. That is, the previous record will be overwritten by the one
|
||||
* provided.
|
||||
@@ -150,7 +148,7 @@ public class Repository<T>
|
||||
|
||||
if (id != null) affectedRows =
|
||||
db.update(getTableName(), values, getIdName() + "=?",
|
||||
new String[]{ id.toString() });
|
||||
id.toString());
|
||||
|
||||
if (id == null || affectedRows == 0)
|
||||
{
|
||||
@@ -176,8 +174,7 @@ public class Repository<T>
|
||||
Long id = (Long) getIdField().get(record);
|
||||
if (id == null) return;
|
||||
|
||||
db.delete(getTableName(), getIdName() + "=?",
|
||||
new String[]{ id.toString() });
|
||||
db.delete(getTableName(), getIdName() + "=?", id.toString());
|
||||
getIdField().set(record, null);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Markus Pfeiffer
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.core.db;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Tokenizer {
|
||||
|
||||
private final InputStream mStream;
|
||||
|
||||
private boolean mIsNext;
|
||||
private int mCurrent;
|
||||
|
||||
public Tokenizer(final InputStream in) {
|
||||
this.mStream = in;
|
||||
}
|
||||
|
||||
public boolean hasNext() throws IOException {
|
||||
|
||||
if (!this.mIsNext) {
|
||||
this.mIsNext = true;
|
||||
this.mCurrent = this.mStream.read();
|
||||
}
|
||||
return this.mCurrent != -1;
|
||||
}
|
||||
|
||||
public int next() throws IOException {
|
||||
|
||||
if (!this.mIsNext) {
|
||||
this.mCurrent = this.mStream.read();
|
||||
}
|
||||
this.mIsNext = false;
|
||||
return this.mCurrent;
|
||||
}
|
||||
|
||||
public boolean skip(final String s) throws IOException {
|
||||
|
||||
if (s == null || s.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (s.charAt(0) != this.mCurrent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final int len = s.length();
|
||||
this.mStream.mark(len - 1);
|
||||
|
||||
for (int n = 1; n < len; n++) {
|
||||
final int value = this.mStream.read();
|
||||
|
||||
if (value != s.charAt(n)) {
|
||||
this.mStream.reset();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class SQLParser {
|
||||
|
||||
public final static int STATE_NONE = 0;
|
||||
public final static int STATE_STRING = 1;
|
||||
public final static int STATE_COMMENT = 2;
|
||||
public final static int STATE_COMMENT_BLOCK = 3;
|
||||
|
||||
public static List<String> parse(final InputStream stream) throws IOException {
|
||||
|
||||
final BufferedInputStream buffer = new BufferedInputStream(stream);
|
||||
final List<String> commands = new ArrayList<String>();
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
|
||||
try {
|
||||
final Tokenizer tokenizer = new Tokenizer(buffer);
|
||||
int state = STATE_NONE;
|
||||
|
||||
while (tokenizer.hasNext()) {
|
||||
final char c = (char) tokenizer.next();
|
||||
|
||||
if (state == STATE_COMMENT_BLOCK) {
|
||||
if (tokenizer.skip("*/")) {
|
||||
state = STATE_NONE;
|
||||
}
|
||||
continue;
|
||||
|
||||
} else if (state == STATE_COMMENT) {
|
||||
if (isNewLine(c)) {
|
||||
state = STATE_NONE;
|
||||
}
|
||||
continue;
|
||||
|
||||
} else if (state == STATE_NONE && tokenizer.skip("/*")) {
|
||||
state = STATE_COMMENT_BLOCK;
|
||||
continue;
|
||||
|
||||
} else if (state == STATE_NONE && tokenizer.skip("--")) {
|
||||
state = STATE_COMMENT;
|
||||
continue;
|
||||
|
||||
} else if (state == STATE_NONE && c == ';') {
|
||||
final String command = sb.toString().trim();
|
||||
commands.add(command);
|
||||
sb.setLength(0);
|
||||
continue;
|
||||
|
||||
} else if (state == STATE_NONE && c == '\'') {
|
||||
state = STATE_STRING;
|
||||
|
||||
} else if (state == STATE_STRING && c == '\'') {
|
||||
state = STATE_NONE;
|
||||
|
||||
}
|
||||
|
||||
if (state == STATE_NONE || state == STATE_STRING) {
|
||||
if (state == STATE_NONE && isWhitespace(c)) {
|
||||
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') {
|
||||
sb.append(' ');
|
||||
}
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
buffer.close();
|
||||
}
|
||||
|
||||
if (sb.length() > 0) {
|
||||
commands.add(sb.toString().trim());
|
||||
}
|
||||
|
||||
return commands;
|
||||
}
|
||||
|
||||
private static boolean isNewLine(final char c) {
|
||||
return c == '\r' || c == '\n';
|
||||
}
|
||||
|
||||
private static boolean isWhitespace(final char c) {
|
||||
return c == '\r' || c == '\n' || c == '\t' || c == ' ';
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,6 @@
|
||||
*
|
||||
* 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.core.db;
|
||||
|
||||
3
uhabits-core/src/main/resources/migrations/10.sql
Normal file
3
uhabits-core/src/main/resources/migrations/10.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
delete from Score;
|
||||
delete from Streak;
|
||||
delete from Checkmarks;
|
||||
1
uhabits-core/src/main/resources/migrations/11.sql
Normal file
1
uhabits-core/src/main/resources/migrations/11.sql
Normal file
@@ -0,0 +1 @@
|
||||
alter table Habits add column reminder_days integer not null default 127;
|
||||
3
uhabits-core/src/main/resources/migrations/12.sql
Normal file
3
uhabits-core/src/main/resources/migrations/12.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
delete from Score;
|
||||
delete from Streak;
|
||||
delete from Checkmarks;
|
||||
4
uhabits-core/src/main/resources/migrations/13.sql
Normal file
4
uhabits-core/src/main/resources/migrations/13.sql
Normal file
@@ -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);
|
||||
14
uhabits-core/src/main/resources/migrations/14.sql
Normal file
14
uhabits-core/src/main/resources/migrations/14.sql
Normal file
@@ -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;
|
||||
3
uhabits-core/src/main/resources/migrations/15.sql
Normal file
3
uhabits-core/src/main/resources/migrations/15.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
delete from Score;
|
||||
delete from Streak;
|
||||
delete from Checkmarks;
|
||||
2
uhabits-core/src/main/resources/migrations/16.sql
Normal file
2
uhabits-core/src/main/resources/migrations/16.sql
Normal file
@@ -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;
|
||||
11
uhabits-core/src/main/resources/migrations/17.sql
Normal file
11
uhabits-core/src/main/resources/migrations/17.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
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;
|
||||
3
uhabits-core/src/main/resources/migrations/18.sql
Normal file
3
uhabits-core/src/main/resources/migrations/18.sql
Normal file
@@ -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 "";
|
||||
6
uhabits-core/src/main/resources/migrations/19.sql
Normal file
6
uhabits-core/src/main/resources/migrations/19.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
create table Events (
|
||||
id integer primary key autoincrement,
|
||||
timestamp integer,
|
||||
message text,
|
||||
server_id integer
|
||||
);
|
||||
3
uhabits-core/src/main/resources/migrations/20.sql
Normal file
3
uhabits-core/src/main/resources/migrations/20.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
drop table checkmarks;
|
||||
drop table streak;
|
||||
drop table score;
|
||||
12
uhabits-core/src/main/resources/migrations/21.sql
Normal file
12
uhabits-core/src/main/resources/migrations/21.sql
Normal file
@@ -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;
|
||||
41
uhabits-core/src/main/resources/migrations/9.sql
Normal file
41
uhabits-core/src/main/resources/migrations/9.sql
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
);
|
||||
@@ -20,6 +20,7 @@
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import org.isoron.uhabits.core.commands.*;
|
||||
import org.isoron.uhabits.core.db.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.models.memory.*;
|
||||
import org.isoron.uhabits.core.tasks.*;
|
||||
@@ -29,9 +30,11 @@ import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
import org.mockito.junit.*;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.validateMockitoUsage;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BaseUnitTest
|
||||
@@ -51,7 +54,7 @@ public class BaseUnitTest
|
||||
protected static final long FIXED_LOCAL_TIME = 1422172800000L;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME);
|
||||
|
||||
@@ -81,4 +84,16 @@ public class BaseUnitTest
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected Database buildMemoryDatabase()
|
||||
{
|
||||
try
|
||||
{
|
||||
return new JdbcDatabase(DriverManager.getConnection("jdbc:sqlite::memory:"));
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class ArchiveHabitsCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class ChangeHabitColorCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public class CommandParserTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
parser = new CommandParser(habitList, modelFactory);
|
||||
|
||||
@@ -35,7 +35,7 @@ public class CreateHabitCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class CreateRepetitionCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public class DeleteHabitsCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
selected = new LinkedList<>();
|
||||
|
||||
@@ -36,7 +36,7 @@ public class EditHabitCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public class ToggleRepetitionCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class UnarchiveHabitsCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
private Habit numericalHabit;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ public class HabitListTest extends BaseUnitTest
|
||||
private HabitList reminderHabits;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habitsArray = new ArrayList<>();
|
||||
|
||||
@@ -33,7 +33,7 @@ public class HabitTest extends BaseUnitTest
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createEmptyHabit();
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createEmptyHabit();
|
||||
|
||||
@@ -32,7 +32,7 @@ public class ScoreTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class StreakListTest extends BaseUnitTest
|
||||
private ModelObservable.Listener listener;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createLongHabit();
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Á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.core.models.sqlite;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.db.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public class SQLiteHabitListTest extends BaseUnitTest
|
||||
{
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
private SQLiteHabitList habitList;
|
||||
|
||||
private Repository<HabitRecord> repository;
|
||||
|
||||
private ModelObservable.Listener listener;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
Database db = buildMemoryDatabase();
|
||||
repository = new Repository<>(HabitRecord.class, db);
|
||||
habitList = new SQLiteHabitList(new SQLModelFactory(db));
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Habit h = modelFactory.buildHabit();
|
||||
h.setName("habit " + i);
|
||||
h.setId((long) i);
|
||||
if (i % 2 == 0) h.setArchived(true);
|
||||
|
||||
HabitRecord record = new HabitRecord();
|
||||
record.copyFrom(h);
|
||||
record.position = i;
|
||||
repository.save(record);
|
||||
}
|
||||
|
||||
habitList.reload();
|
||||
|
||||
listener = mock(ModelObservable.Listener.class);
|
||||
habitList.getObservable().addListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown()
|
||||
{
|
||||
habitList.getObservable().removeListener(listener);
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_withDuplicate()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habitList.add(habit);
|
||||
verify(listener).onModelChange();
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
habitList.add(habit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_withId()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habit.setName("Hello world with id");
|
||||
habit.setId(12300L);
|
||||
|
||||
habitList.add(habit);
|
||||
assertThat(habit.getId(), equalTo(12300L));
|
||||
|
||||
HabitRecord record = repository.find(12300L);
|
||||
assertNotNull(record);
|
||||
assertThat(record.name, equalTo(habit.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_withoutId()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habit.setName("Hello world");
|
||||
assertNull(habit.getId());
|
||||
|
||||
habitList.add(habit);
|
||||
assertNotNull(habit.getId());
|
||||
|
||||
HabitRecord record = repository.find(habit.getId());
|
||||
assertNotNull(record);
|
||||
assertThat(record.name, equalTo(habit.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize()
|
||||
{
|
||||
assertThat(habitList.size(), equalTo(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetById()
|
||||
{
|
||||
Habit h1 = habitList.getById(0);
|
||||
assertNotNull(h1);
|
||||
assertThat(h1.getName(), equalTo("habit 0"));
|
||||
|
||||
Habit h2 = habitList.getById(0);
|
||||
assertNotNull(h2);
|
||||
assertThat(h1, equalTo(h2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetById_withInvalid()
|
||||
{
|
||||
long invalidId = 9183792001L;
|
||||
Habit h1 = habitList.getById(invalidId);
|
||||
assertNull(h1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetByPosition()
|
||||
{
|
||||
Habit h = habitList.getByPosition(5);
|
||||
assertNotNull(h);
|
||||
assertThat(h.getName(), equalTo("habit 5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIndexOf()
|
||||
{
|
||||
Habit h1 = habitList.getByPosition(5);
|
||||
assertNotNull(h1);
|
||||
assertThat(habitList.indexOf(h1), equalTo(5));
|
||||
|
||||
Habit h2 = modelFactory.buildHabit();
|
||||
assertThat(habitList.indexOf(h2), equalTo(-1));
|
||||
|
||||
h2.setId(1000L);
|
||||
assertThat(habitList.indexOf(h2), equalTo(-1));
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@ package org.isoron.uhabits.core.models.sqlite.records;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
@@ -35,7 +34,7 @@ public class HabitRecordTest extends BaseUnitTest
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public class ReminderSchedulerTest extends BaseUnitTest
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createEmptyHabit();
|
||||
|
||||
@@ -35,7 +35,7 @@ public class SingleThreadTaskRunnerTest extends BaseUnitTest
|
||||
private Task task;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
runner = new SingleThreadTaskRunner();
|
||||
|
||||
@@ -39,8 +39,9 @@ public class AboutBehaviorTest extends BaseUnitTest
|
||||
private AboutBehavior.Screen screen;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
behavior = new AboutBehavior(prefs, screen);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public class HabitCardListCacheTest extends BaseUnitTest
|
||||
private HabitCardListCache.Listener listener;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habitList.removeAll();
|
||||
|
||||
@@ -46,7 +46,7 @@ public class HintListTest extends BaseUnitTest
|
||||
private long yesterday;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
today = DateUtils.getStartOfToday();
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit1 = fixtures.createShortHabit();
|
||||
|
||||
@@ -55,7 +55,7 @@ public class ListHabitsMenuBehaviorTest extends BaseUnitTest
|
||||
private ArgumentCaptor<HabitList.Order> orderCaptor;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
behavior =
|
||||
|
||||
@@ -139,7 +139,7 @@ public class ListHabitsSelectionMenuBehaviorTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public class ShowHabitMenuBehaviorTest extends BaseUnitTest
|
||||
private ShowHabitMenuBehavior menu;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
system = mock(ShowHabitMenuBehavior.System.class);
|
||||
|
||||
Reference in New Issue
Block a user