Skip to content

Commit

Permalink
link ios sqlite DB
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoungbinkim committed Feb 28, 2023
1 parent b345dd0 commit c59f80f
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 8 deletions.
Empty file added dataTrade.db
Empty file.
12 changes: 4 additions & 8 deletions ios/dataTradeApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.mm */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
3B5FFCEC29AD8EC2006D1A1D /* www in Resources */ = {isa = PBXBuildFile; fileRef = 3B5FFCEB29AD8EC2006D1A1D /* www */; };
3B6E8A29298CB9B700BB4A23 /* libcrypto.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B6E8A27298CB9AF00BB4A23 /* libcrypto.a */; };
3B6E8A2A298CB9B800BB4A23 /* libgmp.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B6E8A25298CB9AF00BB4A23 /* libgmp.a */; };
3B6E8A2C298CB9B800BB4A23 /* libomp.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B6E8A24298CB9AF00BB4A23 /* libomp.a */; };
Expand Down Expand Up @@ -64,6 +65,7 @@
13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = dataTradeApp/main.m; sourceTree = "<group>"; };
19F6CBCC0A4E27FBF8BF4A61 /* libPods-dataTradeApp-dataTradeAppTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-dataTradeApp-dataTradeAppTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3B4392A12AC88292D35C810B /* Pods-dataTradeApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-dataTradeApp.debug.xcconfig"; path = "Target Support Files/Pods-dataTradeApp/Pods-dataTradeApp.debug.xcconfig"; sourceTree = "<group>"; };
3B5FFCEB29AD8EC2006D1A1D /* www */ = {isa = PBXFileReference; lastKnownFileType = folder; name = www; path = dataTradeApp/www; sourceTree = "<group>"; };
3B6E8A21298CB77B00BB4A23 /* libSnark.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libSnark.a; path = native_libs/simulator_release/libSnark.a; sourceTree = "<group>"; };
3B6E8A24298CB9AF00BB4A23 /* libomp.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libomp.a; path = native_libs/iphonesimulator_depend_libs/libomp.a; sourceTree = "<group>"; };
3B6E8A25298CB9AF00BB4A23 /* libgmp.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libgmp.a; path = native_libs/iphonesimulator_depend_libs/libgmp.a; sourceTree = "<group>"; };
Expand Down Expand Up @@ -136,7 +138,7 @@
13B07FAE1A68108700A75B9A /* dataTradeApp */ = {
isa = PBXGroup;
children = (
3BAB48EF29AC6AF60067FF4F /* www */,
3B5FFCEB29AD8EC2006D1A1D /* www */,
3BE366722988AEE900A702E3 /* LibsnarkModule.h */,
3BE3666A29882DD200A702E3 /* LibsnarkModule.m */,
3BE3666829882DA700A702E3 /* LibsnarkModule.swift */,
Expand Down Expand Up @@ -170,13 +172,6 @@
name = Frameworks;
sourceTree = "<group>";
};
3BAB48EF29AC6AF60067FF4F /* www */ = {
isa = PBXGroup;
children = (
);
path = www;
sourceTree = "<group>";
};
3BAB48F129AC6E920067FF4F /* Products */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -365,6 +360,7 @@
3BAB490829ACBAE90067FF4F /* Config.xcconfig in Resources */,
3B84D11929922AD4005283B5 /* crs in Resources */,
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */,
3B5FFCEC29AD8EC2006D1A1D /* www in Resources */,
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions react-native.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
dependencies: {
"react-native-sqlite-storage": {
platforms: {
android: {
sourceDir: "../node_modules/react-native-sqlite-storage/platforms/android-native",
packageImportPath: "import io.liteglue.SQLitePluginPackage;",
packageInstance: "new SQLitePluginPackage()"
}
}
}
}
};
1 change: 1 addition & 0 deletions scripts/dataTrade.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE IF NOT EXISTS yb_test(id_num INTEGER PRIMARY KEY, name TEXT, age INTEGER)
10 changes: 10 additions & 0 deletions src/components/init.page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Text, Icon, Button } from 'react-native-elements';
import CustomChipButton from '../elements/chipButton';

import testSnark,{ testOrder, initLibSnark } from '../core/snark/test';
import { DBtest, dropTableTEST, createTableTEST } from '../db/test';

const InitWalletPage = ({ navigation }) => {

Expand Down Expand Up @@ -42,6 +43,15 @@ const InitWalletPage = ({ navigation }) => {
await testOrder();
}}
/>
<CustomChipButton
title={'DB Test'}
containerStyle={[styles.containerBt]}
onPress={async () => {
// await createTableTEST();
// await dropTableTEST();
await DBtest();
}}
/>

</View>
)
Expand Down
205 changes: 205 additions & 0 deletions src/db/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import SQLite from 'react-native-sqlite-storage';


const db = SQLite.openDatabase(
{
name: 'dataTrade',
location: 'default',
},
() => {
console.log('Open Database Successed')
},
error => {
console.log('ERROR: ' + error);
}
)

export class SQLiteManager {
constructor() {
this.db = db;
SQLite.DEBUG(true);
SQLite.enablePromise(true);
}

executeQuery = (sql, params = []) => new Promise((resolve, reject) => {
this.db.transaction((trans) => {
trans.executeSql(sql, params, (trans, results) => {
resolve(results);
},
(error) => {
reject(error);
});
});
});

/**
*
* @param {string} tableName Table Name
* @param {Array[Object]} columns The array of column
* @param {Object} columns[i] The 'Object' type
* @param {Object} columns[i].name Row(field) name
* @param {Object} columns[i].dataType SQL data type
* @param {Object} columns[i].isNotNull boolean, The 'NOT NULL' constraint enforces a clumn to NOT accept NULL values
* @param {Object} columns[i].options Any option, e.g., PRIMARY KEYS/ AUTOINCREMENT, and so on.
*
*/
async createTable(tableName, columns) {
let query = '';
for (let i = 0; i < columns.length; i++) {
if (i === columns.length - 1) {
query += '"' + columns[i].name + '" ' + columns[i].dataType + ' ' + ((columns[i].isNotNull) ? 'NOT NULL ' : '') + ((columns[i].options) ? columns[i].options : '')
} else {
query += '"' + columns[i].name + '" ' + columns[i].dataType + ' ' + ((columns[i].isNotNull) ? 'NOT NULL ' : '') + ((columns[i].options) ? columns[i].options : '') + ','
}
}


query = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + query + ')';
// console.log(query);
return this.executeQuery(query);
// console.log(table);
}

/**
*
* @param {string} tableName Table Name
* @param {Object} data Object type, the values to be inserted { [name] : [value] }
*
*/
async insert(tableName, data) {
let keys = Object.keys(data);
let values = keys.map((key) => data[key]);

console.log(keys, values)

let query = 'INSERT INTO ' + tableName + ' (';
for (let i = 0; i < keys.length; i++) {
query += keys[i] + ',';
}
query = query.slice(0, query.length - 1);
query += ') VALUES ';

query += '(';
for (let i = 0; i < values.length; i++) {
query += '?,';
}
query = query.slice(0, query.length - 1);
query += ')';

console.log('insert :',query, values);
return this.executeQuery(query, values);
}

/**
*
* @param {string} tableName Table Name
* @param {Object} data Object type, the values to updated
* @param {Object} where The condition of value to be updated
*/
async update(tableName, data, where) {
let keys = Object.keys(data);
let values = keys.map((key) => data[key]);

let whereKey = Object.keys(where);
let whereValue = whereKey.map((key) => where[key]);

let query = 'UPDATE ' + tableName + ' SET ';
for (let i = 0; i < keys.length; i++) {
query += keys[i] + ' = ?,';
}
query = query.slice(0, query.length - 1);
query += ' WHERE '

for (let i = 0; i < whereKey.length; i++) {
query += whereKey[i] + ' = ?,';
}
query = query.slice(0, query.length - 1);
values.push(...whereValue);

// console.log(query, values);
return await this.executeQuery(query, values);
// console.log(updateQuery);
}

/**
*
* @param {string} tableName Table Name
* @param {array} columns If you want to select a specific column, input the column string array.
* @param {Object} where The condition of data to be selected
* @param {string} order The criteria for sorting when selecting
* @returns {Array}
*/
async select(tableName, columns = false, where = false, order = false) {
let query = 'SELECT ';
columns = (columns === false) ? '*' : columns.join();
query += columns + ' FROM ' + tableName;
if (where) {
let whereKey = Object.keys(where);
query += ' WHERE ';
for (let i = 0; i < whereKey.length; i++) {
query += whereKey[i] + ' = ?,';
}
query = query.slice(0, query.length - 1);
query = query.replace(/,/g, ' AND ');
}

if (order) {
query += ' ORDER BY ' + order;
}

let selectQuery;
if (where) {
let whereKey = Object.keys(where);
let whereValue = whereKey.map((key) => where[key]);
selectQuery = await this.executeQuery(query, whereValue);
} else {
// console.log(query);
selectQuery = await this.executeQuery(query);
// console.log(selectQuery);
}

let rows = selectQuery.rows;
let res = [];
for (let i = 0; i < rows.length; i++) {
res.push(rows.item(i));
}
// DEBUG
// console.log(res);
return res;
}

/**
*
* @param {string} tableName The table name to be droped.
*/
async drop(tableName) {
let query = 'DROP TABLE IF EXISTS ' + tableName;
// console.log(query);
await Promise(this.executeQuery(query));
}

/**
*
* @param {string} tableName The table name
* @param {Object} where The condition of the row, which is to be deleted.
*/
async delete(tableName, where) {
let whereKey = Object.keys(where);
let whereValue = whereKey.map((key) => where[key]);
let query = 'DELETE FROM ' + tableName;
if (where && whereValue) {
query += ' WHERE '
for (let i = 0; i < whereKey.length; i++) {
query += whereKey[i] + ' = ?,';
}
query = query.slice(0, query.length - 1);
// console.log(query, whereValue);
return this.executeQuery(query, whereValue);
// console.log(deleteQuery);
} else {
// console.log('ERROR: delete condition is nothing');
}
}
}

export default SQLiteManager;
13 changes: 13 additions & 0 deletions src/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import dataModel from "./model/data";

export const DataTradeDB = new dataModel();

export const createTable = async () => {
await Promise(DataTradeDB.createTable());
};

export const dropTable = async () => {
await DataTradeDB.dropTable();
}

export default DataTradeDB;
68 changes: 68 additions & 0 deletions src/db/model/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import SQLiteManager from "../db";

export class dataModel extends SQLiteManager {

async createTable(){
try {
await super.createTable('data',[
{
name : 'idx',
dataType : 'integer',
isNotNull : true,
options: 'PRIMARY KEY AUTOINCREMENT'
},
{
name : 'owner',
dataType : 'string',
isNotNull : true,
},
{
name : 'title',
dataType : 'string',
isNotNull : true
},
{
name : 'data',
dataType : 'string',
isNotNull : true
}
])
} catch (error) {
console.log(error);
throw error;
}
}

async insertData(data) {
try {
// console.log(data);
await super.insert('data', data);
} catch (error) {
console.log(error);
throw error;
}
}

async dropTable() {
try {
await super.drop('data');
}
catch (error) {
console.error('data : ',error);
throw error;
}

}

async searchFromTitle(title){
try {
console.log(title)
return (await super.select('data', ['title'], {'title' : title},false));
} catch (error) {
console.log(error);
throw error
}
}
}

export default dataModel;
Loading

0 comments on commit c59f80f

Please sign in to comment.