mirror of https://github.com/iSoron/uhabits.git
parent
96c1a046d4
commit
b96385c4a7
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.database;
|
||||
|
||||
import org.isoron.uhabits.core.db.*;
|
||||
|
||||
public class AndroidCursor implements Cursor
|
||||
{
|
||||
private android.database.Cursor cursor;
|
||||
|
||||
public AndroidCursor(android.database.Cursor cursor)
|
||||
{
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean moveToNext()
|
||||
{
|
||||
return cursor.moveToNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInt(int index)
|
||||
{
|
||||
return cursor.getInt(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLong(int index)
|
||||
{
|
||||
return cursor.getLong(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(int index)
|
||||
{
|
||||
return cursor.getDouble(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(int index)
|
||||
{
|
||||
return cursor.getString(index);
|
||||
}
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.database;
|
||||
|
||||
import android.content.*;
|
||||
import android.database.sqlite.*;
|
||||
|
||||
import org.isoron.uhabits.core.db.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class AndroidSQLiteDatabase implements Database
|
||||
{
|
||||
private final SQLiteDatabase db;
|
||||
|
||||
public AndroidSQLiteDatabase(SQLiteDatabase db)
|
||||
{
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor select(String query, String... params)
|
||||
{
|
||||
return new AndroidCursor(db.rawQuery(query, params));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execSQL(String query, Object... params)
|
||||
{
|
||||
db.execSQL(query, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beginTransaction()
|
||||
{
|
||||
db.beginTransaction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTransactionSuccessful()
|
||||
{
|
||||
db.setTransactionSuccessful();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endTransaction()
|
||||
{
|
||||
db.endTransaction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int update(String tableName,
|
||||
Map<String, Object> map,
|
||||
String where,
|
||||
String... params)
|
||||
{
|
||||
ContentValues values = mapToContentValues(map);
|
||||
return db.update(tableName, values, where, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long insert(String tableName, Map<String, Object> map)
|
||||
{
|
||||
ContentValues values = mapToContentValues(map);
|
||||
return db.insert(tableName, null, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String tableName, String where, String... params)
|
||||
{
|
||||
db.delete(tableName, where, params);
|
||||
}
|
||||
|
||||
private ContentValues mapToContentValues(Map<String, Object> map)
|
||||
{
|
||||
ContentValues values = new ContentValues();
|
||||
for (Map.Entry<String, Object> entry : map.entrySet())
|
||||
{
|
||||
if (entry.getValue() == null) continue;
|
||||
if (entry.getValue() instanceof Integer)
|
||||
values.put(entry.getKey(), (Integer) entry.getValue());
|
||||
else if (entry.getValue() instanceof Long)
|
||||
values.put(entry.getKey(), (Long) entry.getValue());
|
||||
else if (entry.getValue() instanceof Double)
|
||||
values.put(entry.getKey(), (Double) entry.getValue());
|
||||
else if (entry.getValue() instanceof String)
|
||||
values.put(entry.getKey(), (String) entry.getValue());
|
||||
else throw new IllegalStateException(
|
||||
"unsupported type: " + entry.getValue());
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
@ -1,20 +1,25 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Markus Pfeiffer
|
||||
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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
|
||||
* 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/>.
|
||||
*
|
||||
* 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.androidbase.storage;
|
||||
package org.isoron.uhabits.database;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.util.*;
|
||||
|
||||
public interface Database
|
||||
{
|
||||
Cursor select(String query, String... params);
|
||||
|
||||
int update(String tableName,
|
||||
Map<String, Object> values,
|
||||
String where,
|
||||
String... params);
|
||||
|
||||
Long insert(String tableName, Map<String, Object> values);
|
||||
|
||||
void delete(String tableName, String where, String... params);
|
||||
|
||||
void execSQL(String query, Object... params);
|
||||
|
||||
void beginTransaction();
|
||||
|
||||
void setTransactionSuccessful();
|
||||
|
||||
void endTransaction();
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.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.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
|
||||
public class RepetitionRecordTest extends BaseUnitTest
|
||||
{
|
||||
@Test
|
||||
public void testRecord() throws Exception
|
||||
{
|
||||
Repetition rep = new Repetition(2000L, 50);
|
||||
RepetitionRecord record = new RepetitionRecord();
|
||||
record.copyFrom(rep);
|
||||
assertThat(rep, equalTo(record.toRepetition()));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue