Skip to content

Commit

Permalink
Insert Query optimized, out of stack error solved
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Nov 7, 2017
1 parent f81c020 commit c2d2219
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 214 deletions.
78 changes: 78 additions & 0 deletions Code/JsStore/Business/InsertHelperLogic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module JsStore {
export module Business {
export class InsertHelper extends Base {
ValuesAffected = [];
Query: IInsert;

public onTransactionCompleted = function () {
this.OnSuccess(this.Query.Return ? this.ValuesAffected : this.RowAffected);
}

protected checkModifyInsertValues = function (table, values) {
var That = this,
ValueIndex = 0,
Value,
TableName = table.Name,
checkDatas = function () {
Value = values[ValueIndex++];
checkInternal();
},
checkInternal = function () {
if (Value) {
checkAndModifyValue();
}
else {
That.insertData(values);
}
},
checkAndModifyValue = function () {
var Index = 0,
checkAndModifyColumn = function (column) {
if (column) {
var onValidationError = function (error: ErrorType, details: any) {
That.ErrorOccured = true;
That.Error = Utils.getError(error, details);
},
CheckNotNullAndDataType = function () {
//check not null schema
if (column.NotNull && isNull(Value[column.Name])) {
onValidationError(ErrorType.NullValue, { ColumnName: column.Name });
}
//check datatype
else if (column.DataType && typeof Value[column.Name] != column.DataType) {
onValidationError(ErrorType.BadDataType, { ColumnName: column.Name });
}
checkAndModifyColumn(table.Columns[Index++]);
};
if (!That.ErrorOccured) {
//check auto increment scheme
if (column.AutoIncrement) {
KeyStore.get("JsStore_" + ActiveDataBase.Name + "_" + TableName + "_" + column.Name + "_Value", function (columnValue: number) {
Value[column.Name] = ++columnValue;
KeyStore.set("JsStore_" + ActiveDataBase.Name + "_" + TableName + "_" + column.Name + "_Value", columnValue);
CheckNotNullAndDataType();
});
}
else if (column.Default && Value[column.Name] == null) { //check Default Schema
Value[column.Name] = column.Default;
CheckNotNullAndDataType();
}
else {
CheckNotNullAndDataType();
}
}
else {
That.onErrorOccured(That.Error, true);
}
}
else {
checkDatas();
}
}
checkAndModifyColumn(table.Columns[Index++]);
}
checkDatas();
}
}
}
}
142 changes: 34 additions & 108 deletions Code/JsStore/Business/InsertLogic.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,46 @@
module JsStore {
export module Business {
export class Insert extends Base {
ValuesAffected = [];
Query: IInsert;
ValuesIndex = 0;
Table: Model.ITable;
public onTransactionCompleted = function () {
this.OnSuccess(this.Query.Return ? this.ValuesAffected : this.RowAffected);
}

private checkAndModifyValues = function (callBack) {
var That = this,
checkInternal = function (value) {
if (value) {
That.checkAndModifyValue(value, function () {
if (!That.ErrorOccured) {
checkInternal(That.Query.Values[That.ValuesIndex++]);
}
else {
That.onErrorOccured(That.Error, true);
}
});
}
else {
That.ValuesIndex = 0;
callBack();
}
};
checkInternal(this.Query.Values[this.ValuesIndex++]);
}

private insertData = function () {
export class Insert extends InsertHelper {
private insertData = function (values) {
var That = this,
ValueIndex = 0,
IsReturn = this.Query.Return,
insertDataintoTable: Function;
if (IsReturn) {
insertDataintoTable = function (value) {
if (value) {
var AddResult = That.ObjectStore.add(value);
var AddResult = ObjectStore.add(value);
AddResult.onerror = function (e) {
That.onErrorOccured(e);
}
if (IsReturn) {
AddResult.onsuccess = function (e) {
That.ValuesAffected.push(value);
}
AddResult.onsuccess = function (e) {
That.ValuesAffected.push(value);
insertDataintoTable(values[ValueIndex++]);
}
}
}
}
else {
insertDataintoTable = function (value) {
if (value) {
var AddResult = ObjectStore.add(value);
AddResult.onerror = function (e) {
That.onErrorOccured(e);
}
else {
AddResult.onsuccess = function (e) {
++That.RowAffected;
}
AddResult.onsuccess = function (e) {
++That.RowAffected;
insertDataintoTable(values[ValueIndex++]);
}
insertDataintoTable(That.Query.Values[That.ValuesIndex++]);

}
}
}
That.Transaction = DbConnection.transaction([That.Query.Into], "readwrite");
That.ObjectStore = That.Transaction.objectStore(That.Query.Into);
var ObjectStore = That.Transaction.objectStore(That.Query.Into);
That.Transaction.oncomplete = function (e) {
That.onTransactionCompleted();
}
insertDataintoTable(this.Query.Values[That.ValuesIndex++]);
insertDataintoTable(values[ValueIndex++]);
}

constructor(query: IInsert, onSuccess: Function, onError: Function) {
Expand All @@ -66,16 +49,17 @@ module JsStore {
this.Query = query;
this.OnSuccess = onSuccess;
this.OnError = onError;
var That = this;
this.Table = this.getTable(query.Into);
if (this.Table) {
var Table = this.getTable(query.Into);
if (Table) {
if (this.Query.SkipDataCheck) {
That.insertData();
this.insertData(this.Query.Values);
//remove values
this.Query.Values = undefined;
}
else {
this.checkAndModifyValues(function () {
That.insertData();
});
this.checkModifyInsertValues(Table, this.Query.Values);
//remove values
this.Query.Values = undefined;
}
}
else {
Expand All @@ -87,64 +71,6 @@ module JsStore {
this.onExceptionOccured(ex, { TableName: query.Into });
}
}

/**
* check the value based on defined schema and modify or create the value
*
* @private
* @param {any} value
* @param {string} tableName
*
* @memberof InsertLogic
*/
private checkAndModifyValue(value, callBack: Function) {
var That = this,
TableName = this.Table.Name,
Index = 0,
checkAndModifyInternal = function (column) {
if (column) {
var onValidationError = function (error: ErrorType, details: any) {
That.ErrorOccured = true;
That.Error = Utils.getError(error, details);
},
CheckNotNullAndDataType = function () {
//check not null schema
if (column.NotNull && isNull(value[column.Name])) {
onValidationError(ErrorType.NullValue, { ColumnName: column.Name });
}
//check datatype
else if (column.DataType && typeof value[column.Name] != column.DataType) {
onValidationError(ErrorType.BadDataType, { ColumnName: column.Name });
}
checkAndModifyInternal(That.Table.Columns[Index++]);
};
if (!That.ErrorOccured) {
//check auto increment scheme
if (column.AutoIncrement) {
KeyStore.get("JsStore_" + ActiveDataBase.Name + "_" + TableName + "_" + column.Name + "_Value", function (columnValue: number) {
value[column.Name] = ++columnValue;
KeyStore.set("JsStore_" + ActiveDataBase.Name + "_" + TableName + "_" + column.Name + "_Value", columnValue);
CheckNotNullAndDataType();
});
}
else if (column.Default && value[column.Name] == null) { //check Default Schema
value[column.Name] = column.Default;
CheckNotNullAndDataType();
}
else {
CheckNotNullAndDataType();
}
}
else {
callBack();
}
}
else {
callBack();
}
}
checkAndModifyInternal(That.Table.Columns[Index++]);
}
}
}
}
22 changes: 8 additions & 14 deletions Code/outputfile/JsStore-1.2.4.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,25 +495,19 @@ declare module JsStore {
}
declare module JsStore {
module Business {
class Insert extends Base {
class InsertHelper extends Base {
ValuesAffected: any[];
Query: IInsert;
ValuesIndex: number;
Table: Model.ITable;
onTransactionCompleted: () => void;
private checkAndModifyValues;
protected checkModifyInsertValues: (table: any, values: any) => void;
}
}
}
declare module JsStore {
module Business {
class Insert extends InsertHelper {
private insertData;
constructor(query: IInsert, onSuccess: Function, onError: Function);
/**
* check the value based on defined schema and modify or create the value
*
* @private
* @param {any} value
* @param {string} tableName
*
* @memberof InsertLogic
*/
private checkAndModifyValue(value, callBack);
}
}
}
Expand Down
Loading

0 comments on commit c2d2219

Please sign in to comment.