Mum Psp SQL

Embed Size (px)

Citation preview

  • 8/7/2019 Mum Psp SQL

    1/24

    MumpswithPostgreSQL

    by

    Kevin C. Kane

    1

  • 8/7/2019 Mum Psp SQL

    2/24

  • 8/7/2019 Mum Psp SQL

    3/24

    Table of Contents

    1 PostgreSQL Access.............................................4

    1.1 Configuring Mumps to work with PostgreSQL ........................4

    1.2 Basic Mumps SQL commands: ..............................................91.2.1 Connect to server.......................................................................91.2.2 Disconnect from server..............................................................91.2.3 Format/prepare global array table............................................101.2.4 Execute SQL command.............................................................101.2.5 Execute SQL command with file results....................................101.2.6 Re-direct Mumps global array table..........................................111.2.7 SQL related builtin functions....................................................111.2.8 Examples .................................................................................12

    1.2.8.1 Database definition............................................................121.2.8.2 Accessing a relational database from Mumps: ...................121.2.8.3 With a web server .............................................................131.2.8.4 Using PostgreSQL to store the global arrays ......................151.2.8.5 More examples ..................................................................161.2.8.6 Timing comparisons. .........................................................191.2.8.7 SQL Commands generated by Mumps statements ............20

    Index of Figures

    Figure 1 PostgreSQL configuration.........5Figure 2 PostgreSQL connections...........9Figure 3 - Accessing the RDBMS...........11Figure 4 - Data base definition.............12Figure 5 - SQL from Mumps..................13Figure 6 - Web server SQL access........15Figure 7 - Browser display....................15Figure 8 - Accessing RDBMS.................16

    Figure 9 - Browser display....................16Figure 10 - name.mps...........................17Figure 11 - rsltprint.mps.......................18Figure 12 - labrpt.mps..........................19Figure 13 - Initial screen HTML ............19Figure 14 - Native vs. PostgreSQL........20Figure 15 - Mumps to SQL....................23

    3

  • 8/7/2019 Mum Psp SQL

    4/24

  • 8/7/2019 Mum Psp SQL

    5/24

    server may contain other Mumps global arrays. These arecompletely separate from one another.

    When using a PostgreSQL server, it is possible to construct viewsof non-Mumps RDBMS tables which can be directly accessed byMumps as though they were global arrays. An example of this isgiven below.

    When accessing data in global arrays on a PostgreSQL server,Mumps does so by generating SQL commands that are sent to andprocessed by the PostgreSQL server. Normally, PostgreSQL willcheckpoint each of transaction which modifies the database unlessyou specify otherwise.

    However, to improve performance, it may be desirable to initiatethis set of transactions with the SQL BEGIN command beforeglobal array assignments ultimately followed by a SQL COMMIT

    command when the assignments are done. This will permit theMumps global array inserts/updates to run faster. However, shouldthere be a server or client failure before the COMMIT, the data willbe lost and need to be re-assigned. An example is given below.The BEGIN/COMMIT mechanism can also be used to rollback failedtransactions.

    In order to use PostgreSQL for the global array backend server,use the following configuration and make commands for Ubuntu(you must be rootwhen doing these):

    configure prefix=/usr --with-pgdb=/usr/include/postgresqlmakemake install

    Figure 1 PostgreSQL configuration

    If you are using 64 bit Ubuntu, add the

    --with-cpu64

    option to the configure command line. If you are using a systemdifferent than Ubuntu, the PostgreSQL libraries may be located in adifferent place (/usr/include/postrgresql in Ubuntu). You will needto locate the PostgreSQL include libraries and add the option:

    --with-pgdb=/usr/include/postgresql

    to reflect the correct location (insert the correct location for yoursystem in the above). For example, in Cygwin this would be:

    --with-pgdb=/usr/include

    If you want to run Mumps scripts on a different machine than theone running PostgreSQL, you need to enable TCP/IP connections tothe PostgreSQL on the machine running the server. You can dothis by modifying the listen_addresses option in the file/etc/postgresql/8.4/main/postgresql.conf. Note: 8.4 is the current

    5

  • 8/7/2019 Mum Psp SQL

    6/24

    POstgreSQL version number as of this writing. The number may bedefferent on your system. Modify listen_addresses to include aquoted comma separated list of authorized IP number. An '*' willenable all (probably not a good idea). Example:

    listen_addresses = '127.0.0.1,localhost' # what IP address(es) to# listen on;

    The above enables the local machine only.

    Note: the default install of PostgreSQL was configured to listenon port 5433 whereas the client software was configured tooperate on port 5432. The port is set in this file. You may need tochange the port setting to 5432 on your system (re-start required).You can discover which port PostgreSQL is listening on with thecommand:

    ls /var/run/postgresql/ a

    If you see files named: .s.PGSQL.5433 and .s.PGSQL.5433.lock,your PostgreSQL is listening on port 5433 and you should changethe configuration port and re-start.

    The connecting clients IP numbers should be in the filepg_hba.conffound in the same directory. This file is used in clientauthentication. For localhost, the following should be adequate:

    # IPv4 local connections:

    host all all 127.0.0.1/32 trust# IPv6 local connections:

    host all all ::1/128 trust

    Note: to enable a group of IP numbers, your IPV4 address shouldlook like:

    host all all 134.161.0.0/16 trust

    Where the high order 16 bits must match (134.161) but theremaining 16 bits can be any value. The above enables any

    machine on the UNI campus (134.161.....). See the PostgreSQLdocumentation for further details.

    After you have modified the configuration settings you need torestart the server. As root, execute the following:

    /etc/init.d/postgresql 8.4 stop/etc/init.d/postgresql 8.4 start

    Note: systems other than Ubuntu may place the commands in adifferent location. Also not the version number which may bedifferent on your machine.

    Next, you need to create a user or users. By default, the UbuntuPostgreSQL install will add your primary login account as a user. Itwill also create a user namedpostgres. You can add additionalusers by adding their Ubuntu login id. To do this, become

    6

  • 8/7/2019 Mum Psp SQL

    7/24

    superuser then become userpostgres and then create the user.You will be prompted for details:

    sudo su postgres (system password will be requested)createuser demoShall the new role be a superuser? (y/n) nShall the new role be allowed to create databases? (y/n) nShall the new role be allowed to create more new roles? (y/n) n

    Answer the questions as appropriate for you case. You candelete a user with the dropusercommand. Note: there are otherways to add users and configure their privileges. The user idshould be the same as the user's Linux login user id.

    To continue you need to create a database. As mentioned above,PostgreSQL consists of a collection of one or more databases. Ineach, are the relational tables, etc. One of them will be namedmumps and will contain the Mumps global arrays. The actual nameof you database(s) will be determined by their use but for purposesof example, we create a database named medical. One way tocreate a database is:

    createdb medical

    To test a connection, try something like:

    psql h host1.abc.com d medical

    where the -h option specifies the remote host to connect to andthe -d medical specifies the name of the database to which you areconnecting. Omit the -h host1.abc.com if the server is running onyour local machine. If all works well, you should get a prompt fromthepsql command line interface. You may exit it by typing \q.

    Some other handy command forpsql are:

    1. \h a complete list of commands to the server for whichhelp is available (type q to end the listing);

    2. \? - a complete list of commands to psql ;

    3. \dt to show the tables in this data base.

    Finally, you need to start Mumps and initialize the global arraytable. As noted above, when you are connected to PostgreSQL, youare connected to a database. Each database has, among otherthings, tables. The Mumps global arrays are stored in a tablenamed mumps. For each database in which you store global array

    variables, you must have a mumps table. Each mumps table mustbe initialized before it is used.

    By default, Mumps will attempt to connect to the databasenamed mumps and access the table named mumps unless youspecify a different database in the connection command (sql/d).

    7

  • 8/7/2019 Mum Psp SQL

    8/24

    Thus, you should to create the default database and initialize it atthis point. To do so, enter the following commands:

    createdb mumpsmumpssql/fsql/chalt

    The first creates the default mumps database. The secondstarts the Mumps interpreter. The third, processed by the Mumpsinterpreter, initializes the mumps table in the mumps database.You may receive a Notice message from PostgreSQL when youtype this. You may ignore the message. It is merely advising thatno previous instances of the mumps table exist. The sql/ccommand disconnects from the database.

    Now you can run mumps programs and all global array activitywill default to the database named mumps. When you run theMumps interpreter, you will not need to connect to the server andspecify a particular database. The interpreter will automaticallyconnect to the mumps database.

    However, you may want your mumps global arraysto reside inanother database. In this case, you need to create the databaseand initialize a mumps tablein it. Subsequently, you will need toconnect to the database each time you want to use it (sql/d).

    First, you need to create the database:

    createdb medical;

    Then you initialize a mumps table in it. To do this, you need toconnect to the database and then run the initialization commandsql/f. The initialization command will create the mumps table (anddelete any pre-existing table and all its contents). The commandsto do this are:

    mumpssql/d dbname=medicalsql/fsql/c

    The first command establishes the connection with the database,in this case medical running on the local host (see below foradditional connection parameters). The second command formatsthe mumps table. The third command disconnects from thedatabase. Now the database is ready for use by Mumps. Note: you

    must re-connect to the database each time you want to storeglobal arrays in it and you should always disconnect when theconnection is no longer needed.

    8

  • 8/7/2019 Mum Psp SQL

    9/24

    1.2 Basic Mumps SQL commands:

    1.2.1 Connect to server

    sql/d connect-string

    where connect-string is either text or &~exp~ such that expevaluates to a string of text.

    The string is the information needed to connect to thePostgreSQL server. At a minimum, it should include the name ofthe database to which you are connecting:

    dbname=medical

    Other options include the host, host address, user, password,

    etc. See http://www.postgresql.org/docs/8.4/static/libpq connect.html

    for a complete list (this is the documentation of the C functionPQconnectdb() used to connect to the database but has all thepossible options listed).

    Options you don't include default. For a local server, youprobably only need the dbname= parameter and be sure that (1)you are running as a known PostgreSQL user and (2) that youhave read/write privs in the directory you are running in.

    Examples:

    1 sql/d dbname=medical2 sql/d host=abc.def.xyz.edu dbname=medical3 sql/d hostaddr=123.321.432.321 dbname=medical4 sql/d user=joe password=abc123 dbname=medical host=abc.def.com5 set x="dbname=medical"6 sql/d &~x~

    Figure 2 PostgreSQL connections

    Line 1 shows a connection being made to a server on the localmachine by a Mumps program being run by a user who is anauthorized user of the server. Line 2 shows access to a remoteserver by a user on a machine authorized to access the server.Line 3 uses the IP number style rather than a URL. Line 4 shows alogin to a remote server with user name and password. Lines 5and 6 show command line substitution where the value ofx isinserted onto the connection line.

    1.2.2 Disconnect from server

    sql/c

    9

    http://www.postgresql.org/docs/8.3/static/libpq-connect.htmlhttp://www.postgresql.org/docs/8.3/static/libpq-connect.html
  • 8/7/2019 Mum Psp SQL

    10/24

    Disconnect from the database. No parameters, no othercommands on the line. No further commands may be sent to thedatabase server until a new connection is established.

    1.2.3 Format/prepare global array table

    sql/f

    Clear and prepare a mumps database table in PostgreSQL. Thiscommand deletes and removes any previous mumps databasetable from the PostgreSQL server and defines a new, empty tableof the same name. You should do this the first time you storeMumps global arrays in PostgreSQL. Subsequently, do this only ifyou want to delete the global array database and start over.

    This command (no other line options, no other commands

    permitted on the same line) creates a table named mumps inthe current database. Thus, you must be connected with a serverand a database before you execute it. The mumps database haseleven columns the first of which is named 'gbl' and thesubsequent ones are named a1, a2, ... a10 and it may be queriedby SELECT statements but this is probably not desirable.

    Note: this only clears the mumps table in the current database(dbname=...). The contents ofmumps tables in other databases

    are unchanged.1.2.4 Execute SQL command

    sql string

    Where string can be text or &~exp~ where exp evaluates toa string. Passes a command to the PostgreSQL server. $test willbe true (1) if no error is reported.

    The builtin Mumps variable $zsql will contain any messages or'ok' if there were none. Do not use this command for SELECTqueries.

    1.2.5 Execute SQL command with file results

    sql/o=fileName string

    where fileName must evaluate to a valid filename in which will

    be stored the results of the command which follows.string may be a text string or &~exp~ where exp evaluates to

    valid SQL command text.

    The tuple output of the command will be stored in the file withTAB characters delimiting the values of the columns.

    10

  • 8/7/2019 Mum Psp SQL

    11/24

  • 8/7/2019 Mum Psp SQL

    12/24

    iii. $zsqlOpen Returns true if a connection to the SQLserver is open, false otherwise.

    1.2.8 Examples

    1.2.8.1 Database definition

    The following examples use a simplistic relational databasecontaining tables related to medical data. The following gives thedatabase definition:

    create table demographic (ptid text, DOB text, Gender text,Race text, Marital text, PriPhys text,PriPhysTel text, PriPhysAddr text, EContact text,ERelat text, EAddr text, ETel text, PriPharm text,PriPharmAddr text, PriPharmTel text);

    create table temperature (ptid text, date text, time text,

    temperature text);

    create table bp (ptid text, date text, time text, sys text,dia text, pulse text);

    create table problems (ptid text, icd text, problem text,onset text, resolved text, dxphys text);

    create table financial (ptid text, insur text, insnbr text,insname text, emp text,empaddr text, emptel text);

    create table ptname (ptid text, namefirst text, namelast text,namemiddle text, nameprefix text, namesuffix text);

    create table address (ptid text, street text, city text,state text, zip text, telephone text, date text);

    create table labs (ptid text, date text, time text, test text,result text);

    create table meds (ptid text, date text, time text,med text, init text, finish text, dose text,freq text, phys text);

    Figure 4 - Data base definition

    1.2.8.2 Accessing a relational database from Mumps:

    1 #!/usr/bin/mumps23 # establish connection to server45 sql/d dbname=medical67 # check connection89 if '$test write "Connection error: ",$zsql,! halt1011 # build a command1213 set cmd="select namefirst,namelast,problem from ptname,problems"14 set cmd=cmd_" where ptname.ptid=problems.ptid;"15

    12

  • 8/7/2019 Mum Psp SQL

    13/24

    16 # send command to server17 # results will be in xxx.tmp where xxx is the unique process id18 # of this progrogarm1920 sql/o=$job_".tmp" &~cmd~2122 # check for error2324 if '$test write "Command error: ",$zsql,! halt2526 # open the results file for input2728 open 1:$job_".tmp,old"2930 if '$test write "file not found",! halt3132 # read input & print. values separated by TAB's - $char(9)3334 for do35 . use 136 . read line37 . if '$test break38 . set first=$piece(line,$char(9),1)39 . set last=$piece(line,$char(9),2)40 . set prob=$piece(line,$char(9),3)41 . use 542 . write last,", ",first,":",?25,prob,!4344 close 145 use 54647 # disconnect from server

    4849 sql/c50 halt

    with the results (all data fictitious):

    Jones, John: ACUTE TONSILLITISJones, John: CARDIOMYOPATHYJones, John: INFLAMMATION OF EYELIDSJones, John: EPILEPSYJones, John: DEPRESSIVE DISORDER NECJones, John: COAGULATION DEFECTS

    Jones, John: THIAMINE/NIACIN DEFICJones, John: ACUTE TONSILLITISSmith, Charles: ACUTE TONSILLITISSmith, Charles: CARDIOMYOPATHYSmith, Charles: INFLAMMATION OF EYELIDSSmith, Charles: EPILEPSYSmith, Charles: DEPRESSIVE DISORDER NECSmith, Charles: COAGULATION DEFECTSSmith, Charles: THIAMINE/NIACIN DEFICSmith, Charles: ACUTE TONSILLITIS

    Figure 5 - SQL from Mumps

    1.2.8.3 With a web server

    1 #!/usr/bin/mumps23 html Content-type: text/html &!&!4 html 5

    13

  • 8/7/2019 Mum Psp SQL

    14/24

    6 set x="dbname=medical"78 # Open the connection.9 # The &~exp~ causes 'exp' to be inserted into the line1011 sql/d &~x~1213 # $test will be 1 and $zsql will be 'ok' if it worked1415 if $test html Connection to database open
    16 else do17 . html Connection to database failed 18 . halt1920 # Flush/delete/create a mumps table in the database.2122 sql/f2324 if $test html Mumps tables initialized
    25 else html Mumps table initialization failed
    2627 # prepare a query and run it. Output will go to xxx.file28 # where 'xxx' is the process id of this program2930 set x="select * from ptname;"3132 html Sending query: &~x~
    3334 sql/o="/tmp/"_$job_".file" &~x~3536 set ptname=$zsqlCols // gets column names TAB separated37

    38 if $test html Query successfully processed

    39 else do40 . html Query failed. Message=&~$zsql~
    41 . halt4243 # Open the file or results and process same.4445 open 1:"/tmp/"_$job_".file,old"4647 if '$test do48 . html Results file error
    49 . halt

    5051 # Each line consists of columns separated by TAB characters52 # $char(9) is a TAB. sepearte the lines. ptname is:5354 html 5556 for i=1:1:6 do57 . html &~$piece(ptname,$char(9),i)~ 5859 html 6061 for do

    62 . use 1 // unit 1 to be used for I/O63 . read line64 . if '$test break65 . use 5 // unit 5 now used for I/O66 . html 67 . for i=1:1 do68 .. set col=$piece(line,$char(9),i)69 .. if col="" break

    14

  • 8/7/2019 Mum Psp SQL

    15/24

    70 .. html &~col~ 71 . html 7273 use 574 html 7576 html 77 html 7879 shell/g rm &~"/tmp/"_$job_".file"~80 halt

    Figure 6 - Web server SQL access

    Yields the following web browser display:

    1.2.8.4 Using PostgreSQL to store the global arrays

    1 # name.mps23 #!/usr/bin/mumps45 html Content-type: text/html &!&!6 html 78 set x="dbname=medical"9

    10 # Open the connection.11 # &~exp~ causes result of 'exp' to be inserted in line1213 sql/d &~x~1415 # $test will be 1 and $zsql will be 'ok' if it worked1617 if $test html Connection to database open

    15

    Figure 7 - Browser display

  • 8/7/2019 Mum Psp SQL

    16/24

  • 8/7/2019 Mum Psp SQL

    17/24

    In these examples, the HTML page, when the SUBMIT button isclicked, invokes the labrpt.mps program. This program, in turn,invokes name.mps which gets data from the ptname table.name.mps invokes rsltprint.mps which formats the results tablefrom the results file xxx.file (where xxx is the unique process id).rsltprint.mps returns to name.mps which returns to labrpt.mpswhich then queries the labs table then calls the rsltprint.mpsprogram a second time. Note, the connection to the server isestablished at the beginning and remains open until labrpt.mpsfinishes.

    1 # name.mps23 #!/usr/bin/mumps45 # this module assumes that the connection is open6

    7 # prepare a query and run it. Output will go to xxx.file8 # where 'xxx' is the process id of this program910 set x="select * from ptname where ptid='"_ptid_"';"1112 sql/o="/tmp/"_$job_".file" &~x~1314 set cols=$zsqlCols1516 if '$test do17 . html Query failed.
    18 . halt

    1920 # Open the file or results and process same.2122 do rsltprint.mps

    Figure 10 - name.mps

    1 #!/usr/bin/mumps23 # rsltprint.mps45 # this module assumes that the results file exists

    67 open 1:"/tmp/"_$job_".file,old"89 if '$test do10 . html Results file error
    11 . halt1213 # Each line consists of columns separated by TAB characters14 # $char(9) is a TAB. separate the lines.1516 html 17

    18 for i=1:1 do19 . set x=$piece(cols,$char(9),i)20 . if x="" break21 . html &~x~ 22 html 2324 for do25 . use 1 // unit 1 to be used for I/O

    17

  • 8/7/2019 Mum Psp SQL

    18/24

    26 . read line27 . if '$test break28 . use 5 // unit 5 now used for I/O29 . html 30 . for i=1:1 do31 .. set col=$piece(line,$char(9),i)32 .. if col="" break33 .. html &~col~ 34 . html 3536 use 537 html 3839 html 40 html 4142 close 14344 # delete the file4546 shell/g rm &~"/tmp/"_$job_".file"~47 halt

    Figure 11 - rsltprint.mps

    1 # labrpt.mps23 #!/usr/bin/mumps45 html Content-type: text/html &!&!6 html

    78 set x="user=okane host=192.168.2.4 dbname=medical"910 # Open the connection.11 # The &~exp~ causes 'exp' result inserted into the line1213 sql/d &~x~1415 # $test will be 1 and $zsql will be 'ok' if it worked1617 if '$test do18 . html Connection to database failed

    19 . halt2021 if '$data(ptid) do22 . html Error: no value specified for Patient ID (ptid).23 . html 2425 # call a mumps routine to print the name of person ptid2627 do ^name.mps2829 # prepare a query and run it. Output will go to xxx.file30 # where 'xxx' is the process id of this program

    3132 set x="select * from labs where ptid='"_ptid_"';"3334 sql/o="/tmp/"_$job_".file" &~x~3536 set cols=$zsqlCols3738 if '$test do

    18

  • 8/7/2019 Mum Psp SQL

    19/24

    39 . html Query failed. Message=&~$zsql~
    40 . halt4142 # print results4344 do rsltprint.mps4546 html 47 html 48 halt

    Figure 12 - labrpt.mps

    1 2 3 4 Worst General Hospital Lab Reports5

    6 7

    8 Enter Patient Id:9 10 11 12 13 14

    Figure 13 - Initial screen HTML

    1.2.8.6 Timing comparisons.

    Using Postgresql versus the native B-tree results inapproximately a 12 to 1 degradation. The following test programwas run several times. It randomly inserts 100,000 items into thedatabase.

    1 #!/usr/bin/mumps23 sql/d dbname=medical4 sql/f56 kill a

    78 do $zsrand(999) // see random number generator910 sql begin;11 set t1=$zd1 // start time12 for i=1:1:100000 set ^a($r(1000000)+1,i)=i13 sql commit;1415 set i=""16 set k=017 for do18 . set i=$o(^a(i))

    19 . if i="" break20 . set j=""21 . for do22 .. set j=$o(^a(i,j))23 .. if j="" break24 .. set k=k+12526 write $zd1-t1," ",k,!

    19

  • 8/7/2019 Mum Psp SQL

    20/24

    Figure 14 - Native vs. PostgreSQL

    The average time of execution using the native B-tree wasapproximately 4 seconds. For the same program with the --with-pgdb option enabled was 48 seconds. Note: the SQL commandsare ignored if the --with-pgdb option was not specified when the

    Mumps system was configured.1.2.8.7 SQL Commands generated by Mumps statements

    The following are examples of the SQL commands generated bycommon Mumps global array manipulations. The Mumpsinterpreter has an option which displays to stdout the SQLcommands being executed. These appear in the output thatfollows the example program:

    1 #!/usr/bin/mumps23 write "*** establish database connection",!!45 sql/d dbname=medical67 write !,"*** flush/initialize the mumps table",!!89 sql/f1011 write !,"*** kill any values in the table",!!1213 kill ^a1415 write !,"*** execute an SQL command",!!1617 sql begin;1819 write !,"*** insert values into the database",!!2021 set ^a(1)=122 set ^a(2)=1223 set ^a(1,2)=224 set ^a(1,3)=23

    25 set ^a(1,2,3)=326 set ^a(1,2,3,4)=427 set ^a(1,2,3,4,5)=528 set ^a(1,2,3,4,5,6)=629 set ^a(1,2,3,4,5,6,7)=730 set ^a(1,2,3,4,5,6,7,8)=831 set ^a(1,2,3,4,5,6,7,8,9)=932 set ^a(1,2,3,4,5,6,7,8,9,10)=103334 write !,"*** extracting from the data base",!!3536 set i=^a(1)

    37 write "*** expect 1 ",i,!38 set i=^a(1,2)39 write "*** expect 2 ",i,!40 set i=^a(1,2,3)41 write "*** expect 3 ",i,!42 set i=^a(1,2,3,4)43 write "*** expect 4 ",i,!44 set i=^a(1,2,3,4,5)

    20

  • 8/7/2019 Mum Psp SQL

    21/24

    45 write "*** expect 5 ",i,!46 set i=^a(1,2,3,4,5,6)47 write "*** expect 6 ",i,!48 set i=^a(1,2,3,4,5,6,7)49 write "*** expect 7 ",i,!50 set i=^a(1,2,3,4,5,6,7,8)51 write "*** expect 8 ",i,!52 set i=^a(1,2,3,4,5,6,7,8,9)53 write "*** expect 9 ",i,!54 set i=^a(1,2,3,4,5,6,7,8,9,10)55 write "*** expect 10 ",i,!5657 write !,"*** execute an SQL command",!!5859 sql commit;6061 write !,"*** $order() examples",!!6263 set i=$order(^a(1))64 write "*** $o(^a(1)) expect 2 ",i,!65 set i=$order(^a(1,2))66 write "*** $o(^a(1,2)) expect 3 ",i,!6768 write !,"*** $data() examples",!!6970 set i=$data(^a)71 write "*** $d(^a) expect 10 ",i,!!7273 set i=$data(^a(1))74 write "*** $d(^a(1)) expect 11 ",i,!!7576 set i=$data(^a(1,2))

    77 write "*** $d(^a(1,2)) expect 11 ",i,!!7879 set i=$data(^a(1,2,3,4,5,6,7,8,9))80 write "*** $d(^a(1,2...9)) expect 11 ",i,!!8182 set i=$data(^a(1,2,3,4,5,6,7,8,9,10))83 write "*** $d(^a(1,2,...10)) expect 1 ",i,!!8485 write !,"*** $query() example",!!8687 set x="^a"88 for do

    89 . set x=$query(x)90 . if x="" break91 . write "*** returned: ",x,!9293 write !,"*** kill example",!!94 kill ^a(1,2)9596 write !,"*** dump the entire mumps table",!!9798 sql/o="tmp" select * from mumps;

    output:

    *** establish database connection

    *** flush/initialize the mumps table

    cmd=drop table if exists mumps;

    21

  • 8/7/2019 Mum Psp SQL

    22/24

    cmd=create table mumps (gbl text, a1 text, a2 text, a3 text, a4 text,a5 text, a6 text, a7 text, a8 text, a9 text, a10 text, a11text);

    cmd=create index m1 on mumps (gbl, a1, a2);

    *** kill any values in the table

    cmd04=delete from mumps where gbl='a' ;

    *** execute an SQL command

    *** insert values into the database

    cmd=delete from mumps where gbl='a' and a1='1' ;cmd02=insert into mumps values ('a', '1', '', '', '', '', '', '', '',

    '', '', '1' );cmd=delete from mumps where gbl='a' and a1='2' ;cmd02=insert into mumps values ('a', '2', '', '', '', '', '', '', '',

    '', '', '12' );cmd=delete from mumps where gbl='a' and a1='1' and a2='2' ;cmd02=insert into mumps values ('a', '1', '2', '', '', '', '', '', '',

    '', '', '2' );...cmd=delete from mumps where gbl='a' and a1='1' and a2='2' and a3='3'

    and a4='4' and a5='5' and a6='6' and a7='7' and a8='8' anda9='9' and a10='10' ;

    cmd02=insert into mumps values ('a', '1', '2', '3', '4', '5', '6','7', '8', '9', '10', '10' );

    *** extracting from the data base

    cmd01=select a11 from mumps where gbl='a' and a1='1' and a2='' and

    a3='' and a4='' and a5='' and a6='' and a7='' and a8='' anda9='' and a10='' limit 1;*** expect 1 1cmd01=select a11 from mumps where gbl='a' and a1='1' and a2='2' and

    a3='' and a4='' and a5='' and a6='' and a7='' and a8='' anda9='' and a10='' limit 1;

    *** expect 2 2...cmd01=select a11 from mumps where gbl='a' and a1='1' and a2='2' and

    a3='3' and a4='4' and a5='5' and a6='6' and a7='7' and a8='8'and a9='9' and a10='10' limit 1;

    *** expect 10 10

    *** execute an SQL command

    *** $order() examples

    cmd05=select a1 from mumps where gbl='a' and a1>'1' order by a1 limit1;*** $o(^a(1)) expect 2 2cmd05=select a2 from mumps where gbl='a' and a1='1' and a2>'2' order

    by a2 limit 1;*** $o(^a(1,2)) expect 3 3

    *** $data() examples

    cmd01=select a11 from mumps where gbl='a' and a1='' and a2='' anda3='' and a4='' and a5='' and a6='' and a7='' and a8='' anda9='' and a10='' limit 1;

    cmd05=select a1 from mumps where gbl='a' and a1>'' order by a1 limit1;

    *** $d(^a) expect 10 10

    22

  • 8/7/2019 Mum Psp SQL

    23/24

    cmd01=select a11 from mumps where gbl='a' and a1='1' and a2='' anda3='' and a4='' and a5='' and a6='' and a7='' and a8='' anda9='' and a10='' limit 1;

    cmd05=select a2 from mumps where gbl='a' and a1='1' and a2>'' orderby a2 limit 1;

    *** $d(^a(1)) expect 11 11

    cmd01=select a11 from mumps where gbl='a' and a1='1' and a2='2' anda3='' and a4='' and a5='' and a6='' and a7='' and a8='' anda9='' and a10='' limit 1;

    cmd05=select a3 from mumps where gbl='a' and a1='1' and a2='2' anda3>'' order by a3 limit 1;

    *** $d(^a(1,2)) expect 11 11

    cmd01=select a11 from mumps where gbl='a' and a1='1' and a2='2' anda3='3' and a4='4' and a5='5' and a6='6' and a7='7' and a8='8'and a9='9' and a10='' limit 1;

    cmd05=select a10 from mumps where gbl='a' and a1='1' and a2='2' anda3='3' and a4='4' and a5='5' and a6='6' and a7='7' and a8='8'and a9='9' and a10>'' order by a10 limit 1;

    *** $d(^a(1,2...9)) expect 11 11

    cmd01=select a11 from mumps where gbl='a' and a1='1' and a2='2' anda3='3' and a4='4' and a5='5' and a6='6' and a7='7' and a8='8'and a9='9' and a10='10' limit 1;

    *** $d(^a(1,2,...10)) expect 1 1

    *** $query() example

    cmd03=select gbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10 from mumps where

    (gbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) >('a','','','','','','','','','','') order bygbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10 limit 1;

    *** returned: ^a("1")...cmd03=select gbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10 from mumps where

    (gbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) >('a','1','2','3','4','5','6','7','8','9','') order bygbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10 limit 1;

    *** returned: ^a("1","2","3","4","5","6","7","8","9","10")...cmd03=select gbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10 from mumps where

    (gbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) >('a','1','3','','','','','','','','') order bygbl,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10 limit 1;

    *** returned: ^a("2")

    *** kill example

    cmd04=delete from mumps where gbl='a' and a1='1' and a2='2';

    *** dump the entire mumps table

    Figure 15 - Mumps to SQL

    23

  • 8/7/2019 Mum Psp SQL

    24/24

    Alphabetical IndexB-tree......................................................4configure.................................................5

    Connect to server...................................9cpu64......................................................5create table..........................................12Disconnect from server..........................9Execute SQL command.........................10Execute SQL command with file results..............................................................10Format/prepare global array table.......10pg_hba.conf ...........................................6psql.........................................................7Re-direct Mumps global array table.....11SELECT..................................................10

    sql.........................................................10

    SQL BEGIN..............................................5SQL COMMIT...........................................5

    sql/c........................................................9sql/d........................................................9sql/f.......................................................10sql/o......................................................10sql/t.......................................................11SUBMIT button......................................17

    TCP/IP......................................................5Ubuntu....................................................5--with-pgdb...........................................20$zsql..................................................10p.$zsqlCols...............................................11$zsqlOpen.............................................12