Implemented SQLite starting with saving the user's credentials. Next is to save the access token and then the host uri

This commit is contained in:
kdeng00
2019-10-06 23:17:06 -04:00
parent fe15ea221f
commit bb6b08cf1a
38 changed files with 981 additions and 171 deletions
+75
View File
@@ -0,0 +1,75 @@
//
// Created by brahmix on 10/6/19.
//
#include "BaseRepository.h"
#include <fstream>
#include <SQLiteCpp/SQLiteCpp.h>
namespace repository { namespace local {
bool BaseRepository::databaseExist(const std::string& appPath)
{
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
//SQLite::Database db(m_tableName, SQLite::OPEN_READONLY);
return true;
} catch (std::exception& ex) {
auto msg = ex.what();
}
return false;
}
bool BaseRepository::isTableEmpty(const std::string& appPath)
{
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
std::string queryStr("SELECT * FROM ");
queryStr.append(m_tableName);
SQLite::Statement query(db, queryStr);
return query.hasRow();
} catch (std::exception& ex) {
auto msg = ex.what();
}
return false;
}
void BaseRepository::initializedDatabase(const std::string& appPath)
{
try {
const auto dbPath = pathOfDatabase(appPath);
//std::fstream cret(dbPath, std::ios::out);
//cret.close();
SQLite::Database db(dbPath, SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE);
//SQLite::Database db(m_tableName, SQLite::OPEN_CREATE);
} catch (std::exception& ex) {
auto msg = ex.what();
}
}
std::string BaseRepository::pathOfDatabase(const std::string& appPath)
{
std::string dbPath(appPath);
auto lastChar = dbPath.at(dbPath.size()-1);
if (lastChar != '/') {
dbPath.append("/");
}
dbPath.append(m_databaseName);
return dbPath;
}
}}
+28
View File
@@ -0,0 +1,28 @@
//
// Created by brahmix on 10/6/19.
//
#ifndef MEAR_BASEREPOSITORY_H
#define MEAR_BASEREPOSITORY_H
#include <string>
#include <exception>
namespace repository { namespace local {
class BaseRepository {
public:
bool databaseExist(const std::string&);
bool isTableEmpty(const std::string&);
void initializedDatabase(const std::string&);
protected:
std::string pathOfDatabase(const std::string&);
const std::string m_databaseName = "mear.db3";
std::string m_tableName;
private:
};
} }
#endif //MEAR_BASEREPOSITORY_H
+21 -22
View File
@@ -3,7 +3,7 @@
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
cmake_minimum_required(VERSION 3.6.0)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
@@ -14,24 +14,35 @@ cmake_minimum_required(VERSION 3.4.1)
# set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_CXX11_EXTENSION_COMPILE_OPTION "-std=c++17")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O0 -std=c++17")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -std=c++17 -DNDEBUG")
set (SOURCES
BaseRepository.cpp
Demo.cpp
SongRepository.cpp
Tok.cpp
TokenRepository.cpp
UserRepository.cpp
)
set (HEADERS
BaseRepository.h
model/Song.h
model/Token.h
model/User.h
SongRepository.h
Tok.h
TokenRepository.h
UserRepository.h
)
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/json)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/3rdparty/SQLiteCpp)
add_library(native-lib SHARED
@@ -42,12 +53,6 @@ add_library(native-lib SHARED
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library(log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
@@ -59,39 +64,33 @@ set (CURL_LIBRARY
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/${ANDROID_ABI}
)
set (BASS_LIBRARY
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/bass/${ANDROID_ABI}
)
set (CURL_INCLUDE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/include/curl
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/include
)
set (BASS_INCLUDE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/bass/include
)
set(SQLITECPP_INCLUDE
${CMAKE_CURRENT_LIST_DIR}/3rdparty/SQLiteCpp/include
)
add_library(boo STATIC IMPORTED)
#add_library(bass SHARED IMPORTED)
set_target_properties(boo PROPERTIES IMPORTED_LOCATION
"${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/${ANDROID_ABI}/libcurl.a"
)
#set_target_properties(bass PROPERTIES IMPORTED_LOCATION
# "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/bass/${ANDROID_ABI}/libbass.so"
# )
include_directories(${CURL_INCLUDE_DIR}) #${BASS_INCLUDE_DIR})
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
include_directories(${CURL_INCLUDE_DIR} ${SQLITECPP_INCLUDE}) #${BASS_INCLUDE_DIR})
target_link_libraries(native-lib PRIVATE
nlohmann_json::nlohmann_json
boo
#bass
SQLiteCpp
sqlite3
#pthread
dl
${log-lib}
z
)
+50 -1
View File
@@ -3,12 +3,15 @@
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <filesystem>
#include <cstring>
#include <jni.h>
#include <string>
#include <iomanip>
#include <sstream>
#include <vector>
#include <fcntl.h>
#include <android/asset_manager_jni.h>
@@ -18,17 +21,21 @@
#include <nlohmann/json.hpp>
#include <curl/curl.h>
#include <sqlite3.h>
#include "model/Song.h"
#include "model/User.h"
#include "SongRepository.h"
#include "Tok.h"
#include "UserRepository.h"
model::Song retrieveSong(const std::string&, const std::string&);
std::string fetchToken(const std::string&, const std::string&, const std::string&);
void saveCredentials(const model::User&, const std::string&);
model::Song retrieveSong(const std::string& token, const std::string& baseUri)
{
@@ -76,6 +83,26 @@ void iterateDirectory(const std::string& path)
*/
}
void saveCredentials(const model::User& user, const std::string& appDirectory)
{
std::string filePath(appDirectory);
filePath.append("/");
repository::local::UserRepository userRepo;
if (!userRepo.databaseExist(filePath)) {
userRepo.initializedDatabase(filePath);
}
if (!userRepo.doesUserTableExist(filePath)) {
userRepo.createUserTable(filePath);
}
if (userRepo.isTableEmpty(filePath)) {
userRepo.deleteUserTable(filePath);
}
userRepo.saveUserCred(user, filePath);
}
extern "C"
JNIEXPORT jstring
@@ -143,7 +170,6 @@ Java_com_example_mear_activities_DemoStreamActivity_retrieveSong(
}
extern "C"
JNIEXPORT void
JNICALL
@@ -156,3 +182,26 @@ Java_com_example_mear_activities_DemoStreamActivity_pathIteratorDemo(
const std::string pathStr = env->GetStringUTFChars(path, NULL);
iterateDirectory(pathStr);
}
extern "C"
JNIEXPORT void
JNICALL
Java_com_example_mear_activities_DemoStreamActivity_saveUserCredentials(
JNIEnv *env,
jobject thisObj,
jstring username,
jstring password,
jstring appDirectory
) {
const std::string usernameStr = env->GetStringUTFChars(username, NULL);
const std::string passwordStr = env->GetStringUTFChars(password, NULL);
const std::string appDirectoryStr = env->GetStringUTFChars(appDirectory, NULL);
model::User user;
user.username = usernameStr;
user.password = passwordStr;
saveCredentials(user, appDirectoryStr);
}
+5
View File
@@ -0,0 +1,5 @@
//
// Created by brahmix on 10/6/19.
//
#include "TokenRepository.h"
+14
View File
@@ -0,0 +1,14 @@
//
// Created by brahmix on 10/6/19.
//
#ifndef MEAR_TOKENREPOSITORY_H
#define MEAR_TOKENREPOSITORY_H
class TokenRepository {
};
#endif //MEAR_TOKENREPOSITORY_H
+122
View File
@@ -0,0 +1,122 @@
//
// Created by brahmix on 10/6/19.
//
#include "UserRepository.h"
#include <SQLiteCpp/SQLiteCpp.h>
namespace repository { namespace local {
UserRepository::UserRepository()
{
m_tableName = userTable();
}
model::User UserRepository::retrieveUserCredentials(const std::string &appPath)
{
model::User user;
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
//SQLite::Database db(m_tableName, SQLite::OPEN_READONLY);
std::string queryString("SELECT * FROM ");
queryString.append(m_tableName);
queryString.append(" LIMIT 1");
SQLite::Statement query(db, queryString);
auto result = query.executeStep();
auto valZero = query.getColumn(1);
auto valOne = query.getColumn(2);
user.username = valZero.getString();
user.password = valOne.getString();
return user;
} catch (std::exception& ex) {
auto msg = ex.what();
}
return user;
}
bool UserRepository::doesUserTableExist(const std::string &appPath)
{
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
//SQLite::Database db(m_tableName, SQLite::OPEN_READONLY);
return db.tableExists(m_tableName);
} catch (std::exception& ex) {
auto msg = ex.what();
}
return false;
}
void UserRepository::createUserTable(const std::string &appPath)
{
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
//SQLite::Database db(m_tableName, SQLite::OPEN_READWRITE);
std::string queryString("CREATE TABLE ");
queryString.append(m_tableName);
queryString.append(" (Id INTEGER PRIMARY KEY, Username TEXT, Password TEXT)");
db.exec(queryString);
} catch (std::exception& ex) {
auto msg = ex.what();
}
}
void UserRepository::deleteUserTable(const std::string& appPath)
{
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
std::string queryStr("DELETE FROM ");
queryStr.append(m_tableName);
db.exec(queryStr);
} catch (std::exception& ex) {
auto msg = ex.what();
}
}
void UserRepository::saveUserCred(const model::User & user, const std::string& appPath)
{
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
//SQLite::Database db(m_tableName, SQLite::OPEN_READWRITE);
std::string queryString("INSERT INTO ");
queryString.append(m_tableName);
queryString.append(" (Username, Password) VALUES (?, ?)");
SQLite::Statement query(db, queryString);
query.bind(1, user.username);
query.bind(2, user.password);
query.exec();
} catch (std::exception& ex) {
auto msg = ex.what();
}
}
std::string UserRepository::userTable()
{
constexpr auto table = "User";
return table;
}
}}
+33
View File
@@ -0,0 +1,33 @@
//
// Created by brahmix on 10/6/19.
//
#ifndef MEAR_USERREPOSITORY_H
#define MEAR_USERREPOSITORY_H
#include <string>
#include "BaseRepository.h"
#include "model/User.h"
namespace repository { namespace local {
class UserRepository : public BaseRepository {
public:
UserRepository();
model::User retrieveUserCredentials(const std::string&);
bool doesUserTableExist(const std::string&);
void createUserTable(const std::string&);
void deleteUserTable(const std::string&);
void saveUserCred(const model::User&, const std::string&);
private:
std::string userTable();
};
}}
#endif //MEAR_USERREPOSITORY_H
+20
View File
@@ -0,0 +1,20 @@
//
// Created by brahmix on 10/6/19.
//
#ifndef MEAR_TOKEN_H
#define MEAR_TOKEN_H
#include <string>
namespace model {
class Token
{
public:
Token(const std::string& token) : token(token) { }
std::string token;
};
}
#endif //MEAR_TOKEN_H