Re: Re: Re: bashSQL: SQL API for BaSH

From NAT, 6 Years ago, written in Bash, viewed 710 times. This paste is a reply to Re: Re: bashSQL: SQL API for BaSH from NAT - go back
URL https://code.nat.moe/view/1fc0786e/diff Embed
Viewing differences between Re: Re: bashSQL: SQL API for BaSH and Re: Re: Re: bashSQL: SQL API for BaSH
#!/bin/bash
#
# bashSQL: A BaSH SQL lib
# usage:
#
# sql.conn -h <host> -P <port> -u <user> <pass>
-p<pass>
# create a new sql session. Return session ID.
#
# sql.disconn <session>
# disconnect a sql session
#
# sql.query <session>
# do SQL query. Read SQL from stdin.
#
# sample usage:
#
# % sql.conn localhost 3306 root toor
# 1003
# % echo 'show databases;' | sql.query 1003
# information_schema
# mysql
# ...
sql.disconn 1003

function sql.conn {
        local host=$1
        local port=$2
        local user=$3
        local pass=$4
        local id=$RANDOM
        mkfifo /tmp/bashsql.$id
        cat > /tmp/bashsql.$id &; echo $! > /tmp/bashsql.$id.pid
        cat /tmp/bashsql.$id | mysql -h$host --port=$port -p$pass -u$user & 
        echo $! >> /tmp/bashsql.$id.pid
$* &
        echo $id
}

function sql.disconn {
        [[ ! -e /tmp/bashsql.$1 ]] && return 1
        echo 'quit' | sql.query $1
        local kills="$(cat for tar in $(cat /tmp/bashsql.$1.pid)"
        IFS='\n'
        for tar in $kills
pid)
        do
                kill $tar
$tar 2> /dev/null > /dev/null
        done
        rm /tmp/bashsql.$1 /tmp/bashsql.$1.*
        echo "killed $1"
}

function sql.query {
        [[ ! -e /tmp/bashsql.$1 ]] && return 1
        cat > /tmp/bashsql.$1
}

Reply to "Re: Re: Re: bashSQL: SQL API for BaSH"

Here you can reply to the paste above

captcha