components_node/functions-db-update.js
/**
     * Function to update a generic record field.
     * @static
     * @async
     * @param {string} strTable
     * @param {string} strField
     * @param {string} recordValue
     * @param {array} arrSearchParameters
     */
Function to update a generic record field.
Function to update a generic record field .
static async updateRecordGeneric10(strTable, 
    strField, 
    recordValue, 
    arrSearchParameters)
    {
        let objReturn = {returnStatus: false, nRecords: 0};
        let strSQLRecordsGenericUpdate = "";
        let strSQLRecordsGenericUpdateParams = [];
        let resultsSQLRecordsGenericUpdate = null;
        let strOperator = "";
        strSQLRecordsGenericUpdate += "UPDATE " + process.env.CONFIG_SYSTEM_DB_TABLE_PREFIX + FunctionsGeneric.contentMaskWrite(strTable, "db_sanitize") + " ";
        strSQLRecordsGenericUpdate += "SET ";
        strSQLRecordsGenericUpdate += FunctionsGeneric.contentMaskWrite(strField, "db_sanitize") + " = ?";
        strSQLRecordsGenericUpdateParams.push(recordValue);
        for(let countArray = 0; countArray < arrSearchParameters.length; countArray++)
        {
            let arrSearchParametersInfo = arrSearchParameters[countArray].split(";");
            let searchParametersFieldName = arrSearchParametersInfo[0];
            let searchParametersFieldValue = arrSearchParametersInfo[1];
            let searchParametersFieldType = arrSearchParametersInfo[2];
            if(countArray == 0)
            {
                strOperator = "WHERE"
            }else{
                strOperator = "AND"
            }
            if(searchParametersFieldType == "i")
            {
                strSQLRecordsGenericUpdate += " " + strOperator + " " + FunctionsGeneric.contentMaskWrite(searchParametersFieldName, "db_sanitize") + " = ?";
                strSQLRecordsGenericUpdateParams.push(searchParametersFieldValue);
            }
            if(searchParametersFieldType == "ids")
            {
                let arrIds = searchParametersFieldValue.split(",");
                strSQLRecordsGenericUpdate += " " + strOperator + " " + FunctionsGeneric.contentMaskWrite(searchParametersFieldName, "db_sanitize") + " IN (" + "?".repeat(arrIds.length).split("").join(",") + ")";
                for(let countArrayParameters = 0; countArrayParameters < arrIds.length; countArrayParameters++)
                {
                    strSQLRecordsGenericUpdateParams.push(arrIds[countArrayParameters]);
                }
            }
        }
        return new Promise((resolve, reject) => {
            dbSystemConPool.getConnection(function(dbSystemPoolError, dbSystemConPoolGetConnection) {
                if(dbSystemPoolError)
                {
                    if(gSystemConfig.configDebug === true)
                    {
                        console.log(FunctionsGeneric.appLabelsGet(gSystemConfig.configLanguageBackend.appLabels, "statusMessage10e"));
                    }
                    throw dbSystemPoolError;
                }else{
                    dbSystemConPoolGetConnection.query(strSQLRecordsGenericUpdate, strSQLRecordsGenericUpdateParams, (dbSystemError, resultsSQLGenericTableRows) => {
                        dbSystemConPoolGetConnection.release();
                        if(dbSystemError)
                        {
                            if(gSystemConfig.configDebug === true)
                            {
                                console.log(FunctionsGeneric.appLabelsGet(gSystemConfig.configLanguageBackend.appLabels, "statusMessageError50"));
                            }
                            throw dbSystemError;
                        }else{
                            resultsSQLRecordsGenericUpdate = resultsSQLGenericTableRows;
                            objReturn.nRecords = resultsSQLRecordsGenericUpdate.affectedRows;
                            if(resultsSQLRecordsGenericUpdate === undefined)
                            {
                                if(gSystemConfig.configDebug === true)
                                {
                                    console.log(FunctionsGeneric.appLabelsGet(gSystemConfig.configLanguageBackend.appLabels, "statusMessage9"));
                                }
                                reject(objReturn);
                            }else{
                                objReturn.returnStatus = true;
                                resolve(objReturn);
                            }
                        }
                    });
                }
            });  
        });
    }
static async updateRecordGeneric10 ( strTable , strField , recordValue , arrSearchParameters ) { let objReturn = { returnStatus : false , nRecords : 0 } ; let strSQLRecordsGenericUpdate = "" ; let strSQLRecordsGenericUpdateParams = [ ] ; let resultsSQLRecordsGenericUpdate = null ; let strOperator = "" ; strSQLRecordsGenericUpdate += "UPDATE " + process . env . CONFIG_SYSTEM_DB_TABLE_PREFIX + FunctionsGeneric . contentMaskWrite ( strTable , "db_sanitize" ) + " " ; strSQLRecordsGenericUpdate += "SET " ; strSQLRecordsGenericUpdate += FunctionsGeneric . contentMaskWrite ( strField , "db_sanitize" ) + " = ?" ; strSQLRecordsGenericUpdateParams . push ( recordValue ) ; for ( let countArray = 0 ; countArray < arrSearchParameters . length ; countArray ++ ) { let arrSearchParametersInfo = arrSearchParameters [ countArray ] . split ( ";" ) ; let searchParametersFieldName = arrSearchParametersInfo [ 0 ] ; let searchParametersFieldValue = arrSearchParametersInfo [ 1 ] ; let searchParametersFieldType = arrSearchParametersInfo [ 2 ] ; if ( countArray == 0 ) { strOperator = "WHERE" } else { strOperator = "AND" } if ( searchParametersFieldType == "i" ) { strSQLRecordsGenericUpdate += " " + strOperator + " " + FunctionsGeneric . contentMaskWrite ( searchParametersFieldName , "db_sanitize" ) + " = ?" ; strSQLRecordsGenericUpdateParams . push ( searchParametersFieldValue ) ; } if ( searchParametersFieldType == "ids" ) { let arrIds = searchParametersFieldValue . split ( "," ) ; strSQLRecordsGenericUpdate += " " + strOperator + " " + FunctionsGeneric . contentMaskWrite ( searchParametersFieldName , "db_sanitize" ) + " IN (" + "?" . repeat ( arrIds . length ) . split ( "" ) . join ( "," ) + ")" ; for ( let countArrayParameters = 0 ; countArrayParameters < arrIds . length ; countArrayParameters ++ ) { strSQLRecordsGenericUpdateParams . push ( arrIds [ countArrayParameters ] ) ; } } } return new Promise ( ( resolve , reject ) => { dbSystemConPool . getConnection ( function ( dbSystemPoolError , dbSystemConPoolGetConnection ) { if ( dbSystemPoolError ) { if ( gSystemConfig . configDebug === true ) { console . log ( FunctionsGeneric . appLabelsGet ( gSystemConfig . configLanguageBackend . appLabels , "statusMessage10e" ) ) ; } throw dbSystemPoolError ; } else { dbSystemConPoolGetConnection . query ( strSQLRecordsGenericUpdate , strSQLRecordsGenericUpdateParams , ( dbSystemError , resultsSQLGenericTableRows ) => { dbSystemConPoolGetConnection . release ( ) ; if ( dbSystemError ) { if ( gSystemConfig . configDebug === true ) { console . log ( FunctionsGeneric . appLabelsGet ( gSystemConfig . configLanguageBackend . appLabels , "statusMessageError50" ) ) ; } throw dbSystemError ; } else { resultsSQLRecordsGenericUpdate = resultsSQLGenericTableRows ; objReturn . nRecords = resultsSQLRecordsGenericUpdate . affectedRows ; if ( resultsSQLRecordsGenericUpdate === undefined ) { if ( gSystemConfig . configDebug === true ) { console . log ( FunctionsGeneric . appLabelsGet ( gSystemConfig . configLanguageBackend . appLabels , "statusMessage9" ) ) ; } reject ( objReturn ) ; } else { objReturn . returnStatus = true ; resolve ( objReturn ) ; } } } ) ; } } ) ; } ) ; }
Function to update a generic record field.
Function to update a generic record field .
//arrSearchParameters: ["fieldNameSearch1;fieldValueSearch1;fieldTypeSearch1", "fieldNameSearch2;fieldValueSearch2;fieldTypeSearch2", "fieldNameSearch3;fieldValueSearch3;fieldTypeSearch3"] //typeFieldSearch1: s (string) | i (integer) | d (date) | dif (initial date and final date) | ids (id IN) //Variables. //---------------------- //let strReturn = false; //---------------------- //Query. //---------------------- //---------------------- //Parameters. //---------------------- //Variables. //Operator selection. //Integer. //ids. //strSQLRecordsGenericDelete += " " + strOperator + " " + FunctionsGeneric.contentMaskWrite(searchParametersFieldName, "db_sanitize") + " IN (?,?)"; //debug //strSQLRecordsGenericDeleteParams.push(searchParametersFieldValue); //strSQLRecordsGenericDeleteParams.push(649); //debug //strSQLRecordsGenericDeleteParams.push(650); //debug //console.log("arrIds[]=", arrIds[countArrayParameters]); //Debug. //print("arrSearchParameters=" + arrSearchParameters[countArray]); //print("searchParametersFieldName=" + searchParametersFieldName); //print("searchParametersFieldValue=" + searchParametersFieldValue); //print("searchParametersFieldType=" + searchParametersFieldType); //console.log("strSQLRecordsGenericUpdate=", strSQLRecordsGenericUpdate); //---------------------- //Execute query. //---------------------- //dbSystemCon.query(strSQLCounterSelect, strSQLCounterSelectParams, (dbSystemError, resultsSQLCounterRows) => { //Error. //return reject(dbSystemError); //Select the counter number. //console.log("resultsSQLRecordsGenericUpdate=", resultsSQLRecordsGenericUpdate); //Error. //reject(new Error(FunctionsGeneric.appLabelsGet(gSystemConfig.configLanguageBackend.appLabels, "statusMessage9"))); //reject(new Error("nCounter is undefined.")); //Success. //resolve(resultsSQLRecordsGenericDelete); //Debug. //resolve(resultsSQLCounterRows); //resolve(nCounter); //resolve(json(resultsSQLCounterRows));//working: returns [ RowDataPacket { id: 1, counter_global: 123, description: 'Node database test' } ] //---------------------- //Usage. //---------------------- /*
        let updateRecordsGeneric10Result = await SyncSystemNS.FunctionsDBUpdate.deleteRecordsGeneric10(strTable, 
            strField, 
            recordValue, 
            ["id;"+ idRecord + ";i"]);
        */ //----------------------