[Home] [Download] [Install] [Usage] [Example] [FAQ]

Usage of Pgbash

Pgbash executes 'SQL;' as a shell command. By using the exec_sql command, it is also possible to execute SQL with various options.
Start/End method of Pgbash
  1. Sub shell

    The Sub shell is the shell started from a log-in shell. If you log in, and type '/usr/local/bin/pgbash', then Pgbash is started as a sub shell by reading ~/.bashrc and ~/.pgbashrc, and Pgbash displays the following Welcome messages.
      prompt> /usr/local/bin/pgbash
      Welcome to the PGBASH version 2.X (bash-2.0X)
    
        Type '[time] SQL; [pipeline][redirection][&]' to execute SQL.
        Type 'exec_sql [option] ["SQL"]' to execute SQL with options.
        Type '?' for HELP. (This help is defined with ~/.pgbashrc)
    
      pgbash> exit
    
    Type 'exit', then Pgbash is finished and you return to a log-in shell.

    If you changed ~/.bashrc or ~/.pgbashrc while Pgbash is being used, you must update a environments using the 'source' command. For example, type 'source ~/.pgbashrc'.

  2. Shell script

    "#!/usr/local/bin/pgbash" must be described at the head of the shell script.
    #!/usr/local/bin/pgbash
    source ~/.pgbashrc
    #
    connect to db2@dbserc.xxx.co.jp as db2 user postgres;
    m
    If you want to use environments of ~/.pgbashrc, you must describe the 'source' command. The above "m" is the display-database-connection-status command that is defined in ~/.pgbashrc.
Description method of the SQL statement
    You can execute SQL only typing 'SQL;'. It is possible to execute SQL with TIMESPEC, PIPELINE, REDIRECTION and BACKGROUND_JOB. The description method is next.

    [timespec] SQL; [pipeline][redirection][&]

    It is possible to freely write the SQL statement in the multiple line. The '>' prompt is displayed when line was feeded. SQL statement can be inputted continuously until the semicolon appears.

    example)
    pgbash> select * from test;
    pgbash> time select * from test; | more > /tmp/sel.dat &
    pgbash> insert into test values(111,
    > 'aaa','bbb');

    'SQL;' can not be written after a 'if/while,..etc.' control word. In this case, the part of 'SQL;' must be written like 'exec_sql "SQL"'.

    It is possible to use shell variables in the 'SQL;'. However, in the FETCH INTO statement, you must describe ':' and a shell variable as a host variable.

    example)
    insert into test values($_AA, '$_BB', '$_CC');
    fetch in cur into :_AA, :_BB, :_CC;
The exec_sql execution method and options
    If you want to execute SQL with various options, you must describe the exec_sql command. The exec_sql command format is next.

    exec_sql [option] ["SQL"]

  1. Description method

    The SQL statement must be described by surrounding double quart. The ';' may not be added at the end of SQL. It is possible to freely write the SQL statement in the multiple line.
      example) exec_sql "select aa,bb,cc from test_table
                              where a>'123' and dd<'999'"            
    
  2. exec_sql command options
        -i        set CGI mode and set DATA received from WWW server
        -h item   help item: select,insert,.. or next command:
                 (HElp,ALl,OPtion,ERrno,EXample) <2 char are judged>
        -v        display VERSION of pgbash(exec_sql)
        -s        display SQLCA(SQLCODE,etc..) (after SQL execution)
    
        -d conn   set current CONNect_name defined in CONNECT_SQL
        -e        turn on  printing of QUERY sent to the backend
        -q        turn off printing of SYSTEM/SQL error messages
    
        -P        turn on PLAIN output(-P is default in normal mode)
        -H        turn on HTML  output(-H is default in CGI    mode)
        -X        set EXPANDED  type  (ex. -PX or -HX)
        -C cap    set CAPTION of HTML/PLAIN table (ex. -C 'TITLE')
        -O opt    set OPTION  of HTML table (ex. -O 'BORDER=0')
        -L        turn on  OUTER_FRAME of PLAIN table
        -T        turn off HEADINGS    of PLAIN/HTML table
        -B        turn off ROW_COUNT   of PLAIN/HTML table
        -A        turn off ALIGNMENT   of PLAIN table (ex. -PXA)
        -S sep    set SEPARATOR of PLAIN table field(default is '|')
        -N null   set NULL value string (ex. -N 'NULL')
        -Z zero   set ZERO value string (ex. -Z '-0-' )
        
        -m        list all the connected databases
        -l obj    list databases objects:       <2 char are judged>
                  (DAtabases, TAbles,    INdices, SEquences, GRant)
                  (FUnctions, OPerators, TYpes,   SYstem_tables   )
        -p tbl    list tables objects (tbl: table_name      or '*')
        -r des    list description:             <2 char are judged>
                  (TAble, FIeld, TYpe, FUnction, OPerator   or '*')
    
    Help and database infomation

      The exec_sql command has help options as follows. The help options are judged by only 2 characters of the head, and the 2 characters have no distinction of upper/lower character.
        exec_sql -h select         --- help SELECT
        exec_sql -h 'create table' --- help CRAETE TABLE
        exec_sql -h HELP           --- help SQL commands list
        exec_sql -h ALL            --- help all the SQL details
        exec_sql -h OPTION         --- help exec_sql options
        exec_sql -h ERRNO          --- help SQL errno
        exec_sql -h EXAMPLE        --- help SQL examples
      
      Also, the exec_sql command has the display function of the database information as follows. It is possible to describe 'exec_sql -l database -l table -l index -p test1'. The options such as a 'database' or 'table' are judged by only 2 characters of the head, and the 2 characters have no distinction of upper/lower character.
          exec_sql -l da    --- list all the database names
          exec_sql -l ta    --- list only table names
          exec_sql -l in    --- list only index names
          exec_sql -l se    --- list only sequence tables
          exec_sql -l gr    --- list grant/revoke permissions
          exec_sql -l fu    --- list functions
          exec_sql -l op    --- list operators
          exec_sql -l ty    --- list types
          exec_sql -l sy    --- list system tables
          exec_sql -p name  --- list table's colomn name, index name,..etc.
          exec_sql -p '*'   --- list all the table's colomn name, index name,..etc.
      
      The ~/.pgbashrc has described some shorten commands by using a exec_sql help functions and the database information display functions. When '?' was typed at prompt line, The shorten commands list is displayed. If you want to know the content of shorten commands, please refer to the ~/.pgbashrc file.
        h  [SQL]: help <SQL> syntax or all the SQL commands
        o       : help OPTIONs of 'exec_sql' function
        v       : print PGBASH VERSION
        s       : print STATUS after SQL execution
        u       : list USERs
        l       : list DATABASEs
        m       : list CONNECTIONs
        d  [TBL]: list tables,indices,columns in <TaBLe>, or all the tables
        dt      : list TABLEs
        di      : list INDEXes
        ds      : list SEQUENCEs
        dg      : list GRANT/REVOKE permissions
        dA      : list AGGREGATEs
        dD [obj]: list DESCRIPTIONs for <TAble,FIeld,TYp,FU,OP>, or all
        dF      : list PosgreSQL FUNCTIONs
        dO      : list OPERATORs
        dS      : list SYSTEM tables and indexes
        dT      : list TYPEs
      

    Connect to database

      Pgbash has implemented CONNECT, DISCONNECT and SET CONNECTION statements. SET CONNECTION changes the current database connection name. If SET CONNECTION is not defined, then the last CONNECT will be the current connection name. And DISCONNECT disconnects database. Each syntax are next.
        ECONNECT TO dbname[@server[:port]] | DEFAULT  
                           [AS connect_name] [USER user_name [password]];
        EDISCONNECT connect_name|CURRENT|DEFAULT|ALL;
        ESET CONNECTION connect_name|DEFAULT;
      
       ex.) connect to postgres1 as db1 user postgres xxxxxxxx;
            connect to postgres2 as db2 ;   ....login user name is used.
            set connection db1;
            select * from test1;
            set connection db2;
            select * from test2;
            disconenct all;
      
      If you execute SELECT without CONNECT statement, CONNECT TO DEFAULT is automatically issued. This is the same thing when you execute psql without database name.

      If you describe USER and not describe PASSWORD, password: prompt is displayed, and it becomes a condition waiting for password-input. If the password is not defined, you describe NULL as a password like "CONNECT TO dbname USER username NULL". In the case that the login username is equal to the database user name, it is not necessary to describe USER clause.

      It is possible to disconnect all the databases using "DISCOONECT ALL". Still, when Login shell(or Sub shell) finish, all the connections are automatically disconnected.

    Display the connection status of the database

      The connection status of the database is displayed, when the 'exec_sql -m' is executed. If you want to add the outer frame on the output, you must add -L option. If three connections were done, 'exec_sql -mL' displays as follows.
          connect to default;
          connect to postgres@www2.psn.ne.jp:5432  as db1;
          connect to admin@xxx.psn.ne.jp as db2 user admin;
          exec_sql -mL ( interactive mode: m )
          # Connected Databases List (C: current database is '*')
          +---+--------------+-----------+------------------------------+
          | C | connect_name | user_name | target_name                  |
          +---+--------------+-----------+------------------------------+
          |   | _DEFAULT_    | postgres  | postgres:5432                |
          |   | db1          | postgres  | postgres@www.psn.ne.jp:5432  |
          | * | db2          | admin     | admin@xxx.psn.ne.jp:5432     |
          +---+--------------+-----------+------------------------------+
          (3 rows)
      
    Data manipulator of the multiple databases

      It is possible to access some databases by using "SET CONNECTION"/ "-d Option". The example is as follows.
       ex.) connect to postgres1 as db1;
            connect to postgres2 as db2;
            connect to postgres1 as db3;
            set connection db1;    <-------------- current DB is db1
            exec_sql -d db2 "select * from test2" <-- db2 connection
            exec_sql        "select * from test1" <-- db1 connection
            exec_sql -d db3 "select * from test2" <-- db3 connection
            select * from test;                   <-- db1 connection
      
      SET CONNECTION changes the current connection name. SQL is executed for the current connection, if the -d option is not designated in the exec_sql. The -d option change the connection name temporary, but it doesn't change the current connection name continual.

      By using CONNECT statement, it is possible to connect in multiple connection names for one database. It is possible to easily make the testing environment which the multiuser is connecting with one database.

    Substitution of the retrieval result to the shell variable

      Pgbash can substitute the retrieval result for the shell variable using FETCH INTO statement. Syntax of ETCH INTO statement is as follows.
      FETCH [FORWARD|BACKWARD|RELATIVE] [number|ALL|NEXT|PRIOR] [IN|FROM] cursorname
            [INTO :host_var1 [[INDICATOR] :ind_var1], ...
                  :host_varN [[INDICATOR] :ind_varN]]; 
      
      It is not possible to describe number/ALL, when INTO clause is used. However, the case in which the retrieval result is one tuple, INTO clause becomes effective, even if number/ALL is designated.

      (ex.)
      begin; 
      declare cur cursor for select * from test;
      declares -i x ; let x=0;  while ((x < 10))  
      do
         fetch in cur into :CODE indicator :IND_C,
         			:NAME:IND_N, :ADDRESS :IND_A;
          if (( SQLCODE == SQL_OK )); then
              if ((IND_N != SQL_NULL || IND_A != SQL_NULL)); then
                  echo "$CODE, $NAME, $ADDRESS"
                  let x=x+1
              fi
          else
              if(( SQLCODE == SQL_NOT_FOUND )); then
                      let x=11
              else
                      echo "$SQLERRMC"
                      let x=x+1
              fi
          fi
      done
      end;
      

      Here, "SQLCODE" is the SQL error code, "$SQLERRMC" is SQL error message. And, SQL_NULL/SQL_NOT_FOUND is the value which pgbash has set early. (Refer to SQL error code.)
      # In the (( ))operation style, it is possible to omit head '$ ' of the integer type shell variable

    Output form

    1. Output of standard mode

      It is possible to output the database information and the retrieval result in the same format. Generally, the plain output (-P option) is a standard state, but if "exec_sql -i" was executed, HTML output(-H option) becomes standard state

    2. Output of a title

      It is possible to designate ( or, replace) the title in the -C option even if it is a plain output or a HTML output.

    3. Output a outer frame in a plain text output

      In the initial state, there is no outer frame, but it is displayed the outer frame, when the -L option is attached.
      $ exec_sql -L "select * from test"
      +------+------------+----------+
      | code | name       | address  |
      +------+------------+----------+
      | 111  | sakaida    | kobe     |
      | 222  | haruhiko   | hirosima |
      | 333  | nobu       |          |
      |      | youko      | oosaka   |
      +------+------------+----------+
      (4 rows)
      
    4. Stop the display of header and tail(row_count)

      It is possible to stop the display of table header and line number by the -T/-B option
      $ exec_sql -L -TB "select * from test"
      +------+------------+----------+
      | 111  | sakaida    | kobe     |
      | 222  | haruhiko   | hirosima |
      | 333  | nobu       |          |
      |      | youko      | oosaka   |
      +------+------------+----------+
      
    5. Display a NULL value and bit-zero value

      The character string of NULL value and bit-zero value is designated by the -N/-Z option.
      $ exec_sql -LTB -N 'NULL' -Z '-0-' "select * from test"
      +------+------------+----------+
      | 111  | sakaida    | kobe     |
      | 222  | haruhiko   | hirosima |
      | 333  | nobu       | -0-      |
      | NULL | youko      | oosaka   |
      +------+------------+----------+
      
    COPY statement

      In PostgreSQL default functionality, only postgres super user can copy between a table and a diskfile. However, in Pgbash, not only postgres super user but also a general user can copy it .
      COPY table( [ col1[,col2..] ] ) {TO|FROM} {'fileame'|STDIN|STDOUT} 
           [USING DELIMITERS 'delim'] [WITH NULL AS 'nullstring' ];
      

      (1) COPY TO database from file

      If you execute "copy tbname(col1,col2) from '/tmp/oo'", the COPY command is changed as follows.
        begin;
        insert into tbname(col1,col2) values(file's data1);
        insert into tbname(col1,col2) values(file's data2); 
        ...
        end;
      
      If an error was occurred, then a database is rollbacked.
      (2) COPY from database TO file

      If you execute "copy tbname(col1,col2) to '/tmp/oo'", the COPY command is changed as follows.
        begin;
        declare copy_cur00 cursor for select col1,col2 from tbname;
        fetch 300 in copy_cur00; >> /tmp/oo
        fetch 300 in copy_cur00; >> /tmp/oo
        end;
      

    Setting of CGI mode and reading of the data

      If "exec_sql -i" is executed, then HTML_OUTPUT<TABLE BORDER=1> becomes a default. And some datas are read by a GET/POST method and by HTTP_COOKIE from WWW server.

    1. Description example of the CGI script

      For example, here is a HomePage.
      <HTML>
      <FORM METHOD=GET ACTION="pgbash-test.sh">
      <INPUT TYPE=password NAME=passwd >
      <INPUT TYPE=text NAME="MYNAME" VALUE="sakaida">
      <INPUT TYPE=text NAME="ADDRESS" VALUE="Osaka">
      <INPUT TYPE=submit VALUE=submit>
      </HTML>

      And here is a "pgbash-test.sh" CGI script.
      #!/usr/local/bin/pgbash
      exec 2>&1 ............ (1)
      echo "Content-type: text/html" ........................ (2)
      echo ""
      exec_sql -i ......(3)
      #
      connect to dbname as db1 user nopbody $passwd;
      insert into test values( '$MYNAME','$ADDRESS');
      select * from test order by name limit 100;

      (1) "exec 2>&1" is a method for making a standard error output to be a standard output. By the way, it is possible to display outputting error message in a home page.
      (2) echo "Content-type: text/html" and echo "" are indispensable
      (3) It is necessary to execute exec_sql -i. "exec_sql -i" sets this shell script at the CGI mode, and the data input in the home page is set as a shell variable.

    2. Reading of the data by a GET/POST method

      It is possible to handle a data name ( in the above: passwd, MYNAME, ADDRESS) as a shell variable, when "exec_sql -i" was executed.

    3. Reading of the HTTP_COOKIE data

      When the HTTP_COOKIE has been set, By decomposing the value, it is substituted for next shell variable.
        $HTTP_NCOOKIE        : Number of the HTTP_COOKIE.
        ${HTTP_COOKIEKEY[i]} : HTTP_COOKIE key name
        ${HTTP_COOKIEVAL[i]} : HTTP_COOKIE value   
        (i = 0 to HTTP_NCOOKIE-1) 
      

    Shell variable which shows the SQL execution condition

      Pgbash sets a SQL result after the SQL execution to the shell variable. This shell variable must copy to the different shell variable in the case of retaining this value, because it is renewed at every SQL execution. Shell variables of SQL results are as follows.
        $SQLOID     :(int type) OID of recent insert
        $SQLCODE    :(int type) SQL error code (9.refer to error code)
                            0    --- normal end 
                            100  --- EOF(Eond Of File)  
                            minus--- SQL error
        $SQLERRMC   :(char type) SQL error message(max 70)
        $SQLERRML   :(int type) length of SQL error message(<70)
        $SQLERRD2   :(int type) number of tuples(PQntuples())
        $SQLERRD3   :(int type) number of fields(PQnfields())
        $SQLNTUPLE  :(int type) equal to SQLERRD2.
        $SQLNFIELD  :(int type) equal to SQLERRD3
      
        ${SQLFIELDNAME[i]} : filed name list(i = 0 to SQLNFIELD-1 )
      
    Error code

      Pgbash sets SQL error codes to next shell variables. The shell variables are diaplayed by using 'exec_sql -h errno' (interactive mode: 'h errno').
          SQL_OK                 :    0 : normal end. 
          SQL_NOT_FOUND          :  100 : EOF(End Of File). 
          SQL_SYSTEM_ERROR       : -200 : system error. 
          SQL_TOO_MANY_ARGUMENTS : -201 : too many arguments in fetch_stmt. 
          SQL_TOO_FEW_ARGUMENTS  : -202 : too few  arguments in fetch_stmt. 
          SQL_CONNECT_ERROR      : -203 : Database connection error. 
          SQL_INVALID_STMT       : -230 : invalid statements. 
          SQL_READONLY_SHELLVAR  : -231 : can not set read-only shell variable. 
          SQL_DB_NOT_OPEN        : -232 : DB not open. 
          SQL_CNAME_NOT_FOUND    : -233 : connect-name not found. 
          SQL_CNAME_ALREADY_USE  : -234 : connect-name already exist. 
          SQL_INVALID_COMMAND    : -235 : invalid command. 
          SQL_BAD_RESPONSE       : -400 : bad response(backend maybe died)." 
          SQL_EMPTY_QUERY        : -401 : empty query (backend lost query). 
          SQL_CONNECTION_BAD     : -402 : connection bad(disconnect backend)" 
          SQL_FATAL_ERROR        : -403 : query fatal error   (SQL error on backend) 
          SQL_NONFATAL_ERROR     : -404 : query nonfatal error(SQL error on backend) 
          SQL_NULL               :   -1 : indicator is NULL. 
      
      The error code can be used using the (( )) operation style in the shell script as follows.
        ex)  exec_sql "insert into test values(111,'aaa','bbb')"
             if(( SQLCODE < SQL_OK ))
             then
                echo $SQLERRMC
             fi
      
      It is possible to display error codes, etc.. using the -s option.
       ex) # SQL status (shell variable)                           
             SQLCODE   = -403   (SQL error code)
             SQLNTUPLES= 0      (number of tuples)
             SQLNFIELDS= 0      (number of fields)
             SQLERRML  = 38     (length of SQLERRMC)     
             SQLERRMC  = ERROR:  testxxx: Table does not exist.
      
    Restriction

    • Bash commands name

      The "declare/set/select" bash commands are equal to SQL words. In pgbash, since SQL statement is prior handled, the bash commands name have been changed as follows.
        bash's declare command ==> declares
        bash's set command     ==> sets
        bash's select command  ==> selects
      
    • Position of 'SQL;'

      'SQL;' must be located in the head of statement or 'SQL;' must be located in the back side of TIME spec. 'SQL;' must not be located in the back side of IF/WHILE stetment.

    • Shell variables after SQL execution

      If SQL was executed using PIPELINE/BACKGROUND_JOB, then a SQLCODE shell variable can not be used, because a PIPELINE/BACKGROUND_JOB is executed as the different process from bash process. Next example is a mistake.

      (1) exec_sql -s ( interactive mode: s ) after a PIPELINE process

      prompt> select * from test; | more
      prompt> s ...SQLCODE is always zero

      (2) fetch into on BACKGROUND_JOB

      fetch in cur into :aa, :bb; & ... can not use $aa,$bb


[Home] [Download] [Install] [Usage] [Example] [FAQ]

Last Modified at 2000/05/10