Monday, 20 November 2017

Mysql stored procedure to export sql insert statements

DELIMITER $$

DROP PROCEDURE IF EXISTS `InsGen` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsGen`(in_db varchar(20),in_table varchar(20),in_file varchar(100))
BEGIN

declare Whrs varchar(500);
declare Sels varchar(500);
declare Inserts varchar(2000);
declare tablename varchar(20);

set tablename=in_table;

# Comma separated column names - used for Select
select group_concat(concat('concat(\'"\',','ifnull(',column_name,','''')',',\'"\')')) INTO @Sels from information_schema.columns where table_schema=in_db and table_name=tablename;

# Comma separated column names - used for Group By
select group_concat('`',column_name,'`') INTO @Whrs from information_schema.columns where table_schema=in_db and table_name=tablename;

#Main Select Statement for fetching comma separated table values
set @Inserts=concat("select concat('insert into ", in_db,".",tablename," values(',concat_ws(',',",@Sels,"),');') from ", in_db,".",tablename," group by ",@Whrs, " INTO OUTFILE '", in_file ,"'");

PREPARE Inserts FROM @Inserts;
EXECUTE Inserts;

END $$

DELIMITER ;


Ref & Credits : http://kedar.nitty-witty.com/blog/mysql-stored-procedure-to-generate-extract-insert-statement

Friday, 9 June 2017

copy-or-transfer- files-from-Mac OS- to-Pen-Drive-or-Hard-Disk-or-USB-NTFS-filesystem

Hey there, 
Thanks to NTFS-3G for making read /write support for NTFS file system .it is an open source cross-platform implementation of the Microsoft Windows NTFS file system with read-write support.
https://github.com/osxfuse/osxfuse/wiki/NTFS-3G
Steps to make it work 

1. Install the ntfs-3g using below 
brew install ntfs-3g
2. Once installation is succesful, NTFS-3G you can manually mount NTFS volumes in read-write mode by executing the below commands in your terminal. 
Get the disks avail in your system using below command in your terminal
        diskutil list
 It will display all the avail disk partitions. so see the disk name of pen drive or USB drive you would like to copy. it can be something like below  /dev/disk1s1 ...
Replace /dev/disk1s1 with the actual NTFS partition you want to mount. You can find the partition name using diskutil list.
sudo mkdir /Volumes/NTFS
sudo /usr/local/bin/ntfs-3g   /dev/disk1s1     /Volumes/NTFS -olocal -oallow_other

You may face an issue like below

Error opening '/dev/disk3s1': Resource busy
Failed to mount '/dev/disk3s1': Resource busy
Mount is denied because the NTFS volume is already exclusively opened.
The volume may be already mounted, or another software may use it which
could be identified for example by the help of the 'fuser' command.



To resolve above please unmount it and do . to unmount disk 

  sudo umount /dev/disk1s1

Thats it . now you are able to copy/paste/delete the files from MAC OS to Pen drive, Hard disk , USb drive

Tuesday, 16 May 2017

How to call async functions in loops - javascript

Hey There i came across an issue recently while performing the Async db calls inside for loops in javascript.

ex:

for (let i=0 ; i<10 ; i++) {
   db.User.find({},function(err,res){
            console.log(i);
   });
}

Consider above code snippet. Here if you see db is a database call and it takes obviously time to execute query and hit call back but our for loop wont wait till it hit the call back. The for loop will be executed even before you receive the result from call back.

So you will not get actual result .

How I solved it

I used promises to solve it
q is a  package from NOde js and in angular js (Now Angular 2 moved to Observables)

Steps:

1. Frame all the async function calls you want to make in for loop.
2. Pass all this async calls in Q.all() function. (Q.all will get all the promise objects from the async functions you pass and club in the form of array.)
3. Now using this array you can map and reduce the results.


Ex:

asyncFunc1()
 {
setTimeout(function() {
return 'Hello' }, 3000); // consider this returns after 3 s of execution
}

asyncFunc2()
{
setTimeout(function() {
return 'Hello' }, 5000); // consider this returns after 5 s of execution
}

asyncFunc3()
{
setTimeout(function() {
return 'Hello' }, 2000); // consider this returns after 2 s of execution
}


var asyncCalls = [];
asyncCalls[0] = asyncFunc1();
asyncCalls[1] = asyncFunc2();
asyncCalls[2] = asyncFunc3();


now pass this array of function calls to Q.all like below


var Q = require('q');

Q.all(asynCalls,function(res) {

console.log(res)  // this result is nothing but array of collecting objects from asynccalls. now you can

//map and reduce the results according to you .


})