Oracle8 Image Cartridge User's Guide Release 8.0.4 A55713-02 |
Library | Product | Contents | Index

# B
Installation and Demo

Oracle8 Image Cartridge is installed under the database user ORDSYS. This user is created during installation and is subject to change in future beta or production releases.

If you created database objects during the Image Cartridge installation, the password for the ORDSYS user may have been written to the log file. This will happen if you attempt to re-install the data cartridge database objects more than one time. Please check your installation log files.

## B.1 Installation Procedure

This section describes the generic steps required to install Image Cartridge. See your platform-specific documentation for details.

### B.1.1 Pre-Installation Decisions

During the installation process you are prompted to set the password for the ORDSYS user. This user ID is the standard Oracle database account with special privileges for data cartridges. You must decide on a password for the ORDSYS user.

### B.1.2 Pre-Installation Steps

Perform the following tasks prior to installing Oracle data cartridges. For instructions, please see the Oracle8 installation and configuration guide for your operating system.

1. Install the Oracle8 RDBMS, including the PL/SQL option.
2. Create the database.
3. Start up the database.

### B.1.3 Installation Steps

Perform the following mandatory installation steps. If you are using the Oracle Installer with the Create New Database Objects option, then steps 2 through 6 will be done for you automatically.

1. Install the non-database components for Image Cartridge using the Oracle Installer.
2. Create the ORDSYS account.

   Create the ORDSYS account. Use the password you gave in Section B.2.1.

       SVRMGRL> create user ORDSYS identified by <ORDSYS password>;

3. Grant privileges to the ORDSYS account.

   Grant the following privileges to the ORDSYS account:

       SVRMGRL> grant connect,resource,create library to ORDSYS;

4. Create the image library where <ORACLE_HOME> is the ORACLE_HOME directory. .

       SVRMGRL> connect ORDSYS/<ORDSYS password>
       SVRMGRL> create or replace library ORDImgLibS as <ORACLE_HOME>/lib/
       libordimg.so';

   The exact name of this library is operating system dependent. See the installation and configuration guide for your operating system for the exact file name.

5. Create the cartridge where <ORACLE_HOME> is the ORACLE_HOME directory.

       SVRMGRL> connect ORDSYS/<ORDSYS password>
       SVRMGRL> @<ORACLE_HOME>/ord/img/admin/ordispec.sql
       SVRMGRL> @<ORACLE_HOME>/ord/img/admin/ordibody.plb

6. Grant privileges to others on the Image Cartridge data types.

       % svrmgrl
       SVRMGRL> connect ORDSYS/<ORDSYS password>
       SVRMGRL> grant execute on ORDSYS.ORDImgB to PUBLIC;
       SVRMGRL> grant execute on ORDSYS.ORDImgF to PUBLIC;

## B.2 Demo Program

Once you have installed Image Cartridge, you may choose to install the Image Cartridge demonstration program. The demo can be used as a test to confirm successful installation.

This section contains the steps required to install the Image Cartridge demo.

The image demo files are located in <ORACLE_HOME>/ord/img/demo, where <ORACLE_HOME> is the ORACLE_HOME directory.

### B.2.1 Demo Installation Steps

1. The image cartridge demo uses the SCOTT/TIGER database user. If this user does not exist, you must create it:

       % svrmgrl
       SVRMGRL> connect internal;
       SVRMGRL> create user SCOTT identified by tiger;
       SVRMGRL> grant connect,resource to SCOTT;

2. Create the image demo directory where <ORACLE_HOME> is the ORACLE_HOME directory.

       % svrmgrl
       SVRMGRL> connect internal;
       SVRMGRL> create or replace directory imgdemodir as `<ORACLE_HOME>/ord/img
       demo';

3. Grant privileges on the directory to PUBLIC.

       SVRMGRL> grant read on directory imgdemodir to public with grant option;

4. If needed, make the imgdemo program.

       % cd <ORACLE_HOME>/ord/img/demo
       % make -f demo_ordimg.mk imgdemo

   The make operation is operating system specific. See the installation and configuration guide for your operating system for details on how to make this program on your operating system.

### B.2.2 Running the Demo

The file imgdemo is a sample program that shows how Image Cartridge can be used from within a program. The demo is written in C and uses OCI, Oracle CallInterfaceTM, to access the database and exercise the cartridge.

The program operates on imgdemo.dat, which is a bitmap (BMP) image in the local directory. Optionally, you can supply an image file name on the command line, provided that the file resides in the same directory as the demo. In either case, once the image has been manipulated by the cartridge, the resulting image is written to the file imgdemo.out and can then be viewed with common rendering tools that you supply.

When the demo is run, it drops and re-creates a table named IMGDEMOTAB in the SCOTT/TIGER account of the default database. This table is used to hold the demo data. Once the table is created, a reference to the image file is inserted into the table. The data is then loaded into the table using the copyContent( ) method of the ORDImgF cartridge type.

The image properties are extracted within the database using the setProperties( ) method. An UPDATE command is issued after the setProperties( ) invocation. This is required because the setProperties( ) invocation has only updated a local copy of the type attributes.

Next, the Image Cartridge process( ) method is used to cut and scale the image within the database. This is followed by an update that commits the change. The program cut a portion of the image 100 pixels wide by 100 pixels high starting from pixel location (100,100). This subimage is scaled to twice its original size and the resulting image is written out to the file system in a file named imgdemo.out.

Upon completion, the demo program leaves the imgdemo.out file in the current directory. It also leaves the table IMGDEMOTAB in the SCOTT/TIGER account of the database.

#### Example B-1

Execute the demo by typing imgdemo on the command line. Optionally, a different image can be used in the demo by first copying the file to the directory in which the demo resides and then specifying its file name on the command line as an argument to imgdemo.

Use the following command:

       $ imgdemo <optional-image-filename>

The demo displays a number of messages describing its progress, along with any errors encountered in the event that something was not set up correctly. Expect to see the following messages:

    Dropping table IMGDEMOTAB...
    Creating and populating table IMGDEMOTAB...
    Loading data into the cartridge...
    Modifying image characteristics...
    Writing image to file imddemo.out...
    Disconnecting from database...
    Logged off and detached from server.
    Demo completed successfully.

If the program encounters any errors, it is likely that either the Image Cartridge software has not been installed correctly or the database has not been started. If the program completes successfully, the original image and the resultant image, which has undergone the cutting and scaling described earlier, can be viewed with common image rendering tools.

## B.3 Image Demo Code Example

The following code example represents the demonstration program shipped with this cartridge. This example was designed to work on UNIX systems. A different version may be available on your platform.

    #ifdef RCSID
    static char *RCSid =
       "$Header: imgdemo.c 18-apr-97.10:26:57 svivian Exp $ ";
    #endif /* RCSID */

    /* Copyright (c) Oracle Corporation 1997. All Rights Reserved. */
    /*
       NAME
         imgdemo.c - out-of-the-box demo using the image data cartridge

       DESCRIPTION
          This program demonstrates how to use the image data cartidge to
          load an image into the database, alter its attributes, and then
          write the data to a file for viewing.  The demo will use an
          image data file provided with the cartridge named imgdemo.dat
          as its input so no parameter is required.

          The process method will be used to scale and crop the original
          image so expect the resultant image in imgdemo.out to reflect
          this change.

          Sample usage:    imgdemo <optional-image-filename>

          imgdemo.dat    --  image to be manipulated
          imgdemo.out    --  resultant image filename

          Optionally, a user provided image file can be specified on
          the command line as shown above, provided that it resides
          in the IMGDEMODIR directory (see assumptions below).

       ENVIRONMENT:

          The following assumptions are made:

           1) Image data cartridge has been installed and PUBLIC
              has EXECUTE privilege on it.

           2) Install script has been run and thus created IMGDEMODIR
              directory and granted public read access in order that
    	          the image datafile can be read into the database.

           3) imgdemo.dat is a valid image file that resides in the
              IMGDEMODIR directory and the user has read/write access
    	          to the directory.

           4) User SCOTT has the default password

       PUBLIC FUNCTION(S)

       PRIVATE FUNCTION(S)

       RETURNS

       NOTES
          After demo has been executed, use a rendering tool such as xv
          to view both the original and the modified images.  The table
          IMGDEMOTAB is left in the SCOTT account in the default database
          for the user's viewing as well.  The IMGDEMODIR directory will
          likewise be left in the system account.

       MODIFIED   (MM/DD/YY)
       svivian     05/19/97 - call setProperties after modifying image
       svivian     05/15/97 - NT portability - open file for binary write
       svivian     04/24/97 - change select to use table alias
       svivian     04/18/97 - Creation

    */

     #include <stdio.h>

    #ifndef OCI_ORACLE
    #include <oci.h>
    #endif

    /* local routines */
    static sb4 init_handles();
    static sb4 attach_server();
    static sb4 log_on();
    static void logout();
    static sb4 create_table();
    static void drop_table();
    static sb4 fetch_blob();
    static sb4 load_cart();
    static sb4 mod_img();
    static sb4 blob_to_file();
    static void report_error();

    #define TRUE       1
    #define FALSE      0

    #define MAXBUFLEN  16384
    #define STMTLEN    512
    #define FNAMELEN   80

    static OCIEnv        *envhp;
    static OCIServer     *srvhp;
    static OCISvcCtx     *svchp;
    static OCIError      *errhp;
    static OCISession    *authp;
    static OCIStmt       *stmthp;
    static OCILobLocator *blob, *bfile;
    static OCIDefine     *defnp1, *defnp2;
    static OCIBind       *bndhp;

    static char *fname = "imgdemo.out";
    static text *user = (text *)"SCOTT";
    static text *upwd = (text *)"TIGER";

    int main(int argc, char *argv[])
    {
      char iname[FNAMELEN];
      if (argc == 2)
      {
         strcpy(iname, argv[1]);
      }
      else
      {
         strcpy(iname, "imgdemo.dat");
      }

      if (init_handles())
      {
        (void) fprintf(stdout,"FAILED: init_handles()\n");
        return OCI_ERROR;
      }

      if (attach_server())
      {
        (void)fprintf (stdout, "FAILED: attach_server()\n");
        logout();
        return OCI_ERROR;
      }

      if (log_on(user,upwd))
      {
        (void) fprintf(stdout,"FAILED: log_on()\n");
        logout();
        return OCI_ERROR;
      }

      if (create_table(iname))
      {
        (void) fprintf(stdout,"FAILED: create_table()\n");
        logout();
        return OCI_ERROR;
      }

      (void)fprintf (stdout, "\nLoading data into cartridge...\n");
      if (load_cart())
      {
        (void) fprintf(stdout,"FAILED: load_cart()\n");
        logout();
        return OCI_ERROR;
      }

      (void)fprintf (stdout, "\nModifying image characteristics...\n");
      if (mod_img())
      {
        (void)fprintf (stdout, "\nFAILED: mod_img()\n");
        logout();
        return OCI_ERROR;
      }

      (void)fprintf (stdout, "\nWriting image to file %s...\n",fname);
      if (blob_to_file())
      {
        (void)fprintf (stdout, "\nFAILED: blob_to_file()\n");
        logout();
        return OCI_ERROR;
      }

      (void)fprintf (stdout, "\nDisconnecting from database...\n");
      logout();

      (void)fprintf (stdout, "\nDemo completed successfully.\n");
      return OCI_SUCCESS;

    }  /* end main */

    /*
     * -----------------------------------------------------------------
     * init_handles - initialize environment, allocate handles, etc.
     * -----------------------------------------------------------------
     */

    sb4 init_handles()
    {
      sword status;

      if (OCIInitialize((ub4) OCI_DEFAULT,
                        (dvoid *)0,
                        (dvoid * (*)(dvoid *, size_t)) 0,
                        (dvoid * (*)(dvoid *, dvoid *, size_t))0,
                        (void (*)(dvoid *, dvoid *)) 0 ))
      {
        (void) fprintf(stdout,"FAILED: OCIInitialize()\n");
        return OCI_ERROR;
      }

      /* initialize environment handle */
      if (OCIEnvInit((OCIEnv **) &envhp,
                     (ub4) OCI_DEFAULT,
                     (size_t) 0,
                     (dvoid **) 0 ))
      {
        (void) fprintf(stdout,"FAILED: OCIEnvInit()\n");
        return OCI_ERROR;
      }

      if (OCIHandleAlloc((dvoid *) envhp,
                         (dvoid **) &svchp,
                         (ub4) OCI_HTYPE_SVCCTX,
                         (size_t) 0,
                         (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIHandleAlloc()\n");
        return OCI_ERROR;
      }

      if (OCIHandleAlloc((dvoid *) envhp,
                         (dvoid **) &errhp,
                         (ub4) OCI_HTYPE_ERROR,
                         (size_t) 0,
                         (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIHandleAlloc()\n");
        return OCI_ERROR;
      }

      if (OCIHandleAlloc((dvoid *) envhp,
                         (dvoid **) &stmthp,
                         (ub4) OCI_HTYPE_STMT,
                         (size_t) 0,
                         (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIHandleAlloc()\n");
        return OCI_ERROR;
      }

      if (OCIHandleAlloc((dvoid *) envhp,
                         (dvoid **) &srvhp,
                         (ub4) OCI_HTYPE_SERVER,
                         (size_t) 0,
                         (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIHandleAlloc()\n");
        return OCI_ERROR;
      }

      if (OCIHandleAlloc((dvoid *) envhp,
                         (dvoid **) &authp,
                         (ub4) OCI_HTYPE_SESSION,
                         (size_t) 0,
                         (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIHandleAlloc()\n");
        return OCI_ERROR;
      }

      /* allocate the lob locator variables */
      if (OCIDescriptorAlloc((dvoid *) envhp,
                             (dvoid **) &blob,
                             (ub4)OCI_DTYPE_LOB,
                             (size_t) 0,
                             (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIDescriptorAlloc(blob)\n");
        return OCI_ERROR;
      }

      /* allocate the lob locator variables - will change to OCI_DTYPE_FILE */
      if (OCIDescriptorAlloc((dvoid *) envhp,
                             (dvoid **) &bfile,
                             (ub4)OCI_DTYPE_LOB,
                             (size_t) 0,
                             (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIDescriptorAlloc(bfile)\n");
        return OCI_ERROR;
      }

      /* allocate the define handles */
      if (OCIHandleAlloc((dvoid *) stmthp,
                         (dvoid **) &defnp1,
                         (ub4) OCI_HTYPE_DEFINE,
                         (size_t) 0,
                         (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIHandleAlloc()\n");
        return OCI_ERROR;
      }

      if (OCIHandleAlloc((dvoid *) stmthp,
                         (dvoid **) &defnp2,
                         (ub4) OCI_HTYPE_DEFINE,
                         (size_t) 0,
                         (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIHandleAlloc()\n");
        return OCI_ERROR;
      }

      /* allocate the bind handle */
      if (OCIHandleAlloc((dvoid *) stmthp,
                         (dvoid **) &bndhp,
                         (ub4) OCI_HTYPE_BIND,
                         (size_t) 0,
                         (dvoid **) 0))
      {
        (void) fprintf(stdout,"FAILED: OCIHandleAlloc()\n");
        return OCI_ERROR;
      }

      return OCI_SUCCESS;

    }  /* end init_handles */

    /*
     * -----------------------------------------------------------------
     * attach_server - attach to default server
     * -----------------------------------------------------------------
     */
    sb4 attach_server()
    {
      /* attach to the server - use default host */
      if (OCIServerAttach(srvhp,
                          errhp,
                          (text *) NULL,
                          0,
                          (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIServerAttach()\n");
        return OCI_ERROR;
      }

      /* set the server attribute in the service context */
      if (OCIAttrSet((dvoid *) svchp,
                     (ub4) OCI_HTYPE_SVCCTX,
                     (dvoid *) srvhp,
                     (ub4) 0,
                     (ub4) OCI_ATTR_SERVER,
                     errhp))
      {
        (void) fprintf(stdout,"FAILED: OCIAttrSet()\n");
        return OCI_ERROR;
      }

      return (OCI_SUCCESS);

    }  /* end attach_server */

    /*
     * -----------------------------------------------------------------
     * log_on - log on to server
     * -----------------------------------------------------------------
     */
    sb4 log_on(
      text *uid,
      text *pwd
    )
    {
      if (OCIAttrSet((dvoid *) authp,
                     (ub4) OCI_HTYPE_SESSION,
                     (dvoid *) uid,
                     (ub4) strlen((char *)uid),
                     (ub4) OCI_ATTR_USERNAME,
                     errhp))
      {
        (void) fprintf(stdout,"FAILED: OCIAttrSet()\n");
        return OCI_ERROR;
      }

      if (OCIAttrSet((dvoid *) authp,
                     (ub4) OCI_HTYPE_SESSION,
                     (dvoid *) pwd,
                     (ub4) strlen((char *)pwd),
                     (ub4) OCI_ATTR_PASSWORD,
                     errhp))
      {
        (void) fprintf(stdout,"FAILED: OCIAttrSet()\n");
        return OCI_ERROR;
      }

      /* log on */
      if (OCISessionBegin(svchp,
                          errhp,
                          authp,
                          (ub4) OCI_CRED_RDBMS,
                          (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCISessionBegin()\n");
        return OCI_ERROR;
      }

      /* set the session attribute in the service context */
      if (OCIAttrSet((dvoid *) svchp,
                     (ub4) OCI_HTYPE_SVCCTX,
                     (dvoid *) authp,
                     (ub4) 0,
                     (ub4) OCI_ATTR_SESSION, errhp))
      {
        (void) fprintf(stdout,"FAILED: OCIAttrSet()\n");
        return OCI_ERROR;
      }

      return OCI_SUCCESS;

    }  /* end log_on */

    /*
     * -----------------------------------------------------------------
     * create_table - Create table IMGDEMOTAB and insert one row.
     * -----------------------------------------------------------------
     */

    sb4 create_table(char *iname)
    {
      text *crtstmt = (text *)
    "CREATE TABLE IMGDEMOTAB (C1 INT, C2 ORDSYS.ORDImgF, C3 ORDSYS.ORDImgB)";
      text insstmt[STMTLEN];

     sprintf ((char*)insstmt,"INSERT INTO IMGDEMOTAB VALUES
    (1,ORDSYS.ORDImgF(bfilename('IMGDEMODIR','%s')
    NULL,NULL,NULL,NULL,NULL,NULL),ORDSYS.ORDImgB(empty_blob(),NULL,NULL,
    NULL,NULL,NULL,NULL))",iname);

      /*
       * drop table first if it exists and then re-create.
       */
      drop_table();

      /*
       * create table
       */
      if (OCIStmtPrepare(stmthp,
                         errhp,
                         crtstmt,
                         (ub4) strlen((char *) crtstmt),
                         (ub4) OCI_NTV_SYNTAX,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtPrepare() crtstmt\n");
        report_error();
        return OCI_ERROR;
      }

      (void)fprintf (stdout, "\nCreating and populating table IMGDEMOTAB...\n");
      if (OCIStmtExecute(svchp,
                         stmthp,
                         errhp,
                         (ub4) 1,
                         (ub4) 0,
                         (CONST OCISnapshot *) 0,
                         (OCISnapshot *) 0,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: creating table\n");
        report_error();
        return OCI_ERROR;
      }

      /*
       * populate table with simple insert
       */
      if (OCIStmtPrepare(stmthp,
                         errhp,
                         insstmt,
                         (ub4) strlen((char *) insstmt),
                         (ub4) OCI_NTV_SYNTAX,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtPrepare() insstmt\n");
        report_error();
        return OCI_ERROR;
      }

      if (OCIStmtExecute(svchp,
                         stmthp,
                         errhp,
                         (ub4) 1,
                         (ub4) 0,
                         (CONST OCISnapshot *) 0,
                         (OCISnapshot *) 0,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtExecute() insstmt\n");
        report_error();
        return OCI_ERROR;
      }

      (void) OCITransCommit(svchp, errhp, (ub4)0);

      return OCI_SUCCESS;

    }  /* end create_table */

    /*
     *---------------------------------------------------------------------
     * fetch_blob - fetch the blob locator
     *---------------------------------------------------------------------
     */

    sb4 fetch_blob()
    {
      text  *sqlstmt = (text *)"SELECT A.C3.content FROM IMGDEMOTAB A";

      if (OCIStmtPrepare(stmthp,
                         errhp,
                         sqlstmt,
                         (ub4) strlen((char *)sqlstmt),
                         (ub4) OCI_NTV_SYNTAX,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtPrepare() sqlstmt\n");
        report_error();
        return OCI_ERROR;
      }

      /*
       * define the output blob variable by position
       */
      if (ocidefn(stmthp,
                  defnp1,
                  errhp,
                  (ub4) 1,
                  (dvoid *) &blob,
                  (sb4) -1,
                  (ub2) SQLT_BLOB,
                  (dvoid *) 0,
                  (ub2 *) 0,
                  (ub2 *) 0,
                  (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: ocidefn()\n");
        report_error();
        return OCI_ERROR;
      }

      /* execute the select and fetch one row */
      if (OCIStmtExecute(svchp,
                         stmthp,
                         errhp,
                         (ub4) 1,
                         (ub4) 0,
                         (CONST OCISnapshot*) 0,
                         (OCISnapshot*) 0,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtExecute() sqlstmt\n");
        report_error();
        return OCI_ERROR;
      }

      return OCI_SUCCESS;

    }  /* fetch_blob */

    /*
     *-------------------------------------------------------------------
     * drop_table - Drop table IMGDEMOTAB
     *-------------------------------------------------------------------
     */

    void drop_table()
    {
      text  *sqlstmt = (text *) "DROP TABLE IMGDEMOTAB";
      ub1   ebuf[256];
      text  *sqlstate=0;
      ub4   errcodep;

      (void)fprintf(stdout,"\nDropping table IMGDEMOTAB...\n");
      if (OCIStmtPrepare(stmthp,
                         errhp,
                         sqlstmt,
                         (ub4) strlen((char *) sqlstmt),
                         (ub4) OCI_NTV_SYNTAX,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: drop table\n");
        return;
      }

      (void)OCIStmtExecute(svchp,
                           stmthp,
                           errhp,
                           (ub4) 1,
                           (ub4) 0,
                           (CONST OCISnapshot *) 0,
                           (OCISnapshot *) 0,
                           (ub4) OCI_DEFAULT);
      return;

    }  /* end drop_table */

    /*
     * -------------------------------------------------------------------
     * logout - Logoff and disconnect from the server.  Free handles.
     * -------------------------------------------------------------------
     */

    void logout()
    {
      (void) OCISessionEnd(svchp, errhp, authp, (ub4) 0);
      (void) OCIServerDetach(srvhp, errhp, (ub4) OCI_DEFAULT);

      (void) fprintf(stdout,"\nLogged off and detached from server.\n");

      (void) OCIHandleFree((dvoid *) srvhp, (ub4) OCI_HTYPE_SERVER);
      (void) OCIHandleFree((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX);
      (void) OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR);
      (void) OCIHandleFree((dvoid *) authp, (ub4) OCI_HTYPE_SESSION);
      (void) OCIDescriptorFree((dvoid *) blob, (ub4) OCI_DTYPE_LOB);
      (void) OCIDescriptorFree((dvoid *) bfile, (ub4) OCI_DTYPE_LOB);
      (void) OCIHandleFree((dvoid *) defnp1, (ub4) OCI_HTYPE_DEFINE);
      (void) OCIHandleFree((dvoid *) defnp2, (ub4) OCI_HTYPE_DEFINE);
      (void) OCIHandleFree((dvoid *) bndhp, (ub4) OCI_HTYPE_BIND);
      (void) OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);

      return;

    }  /* end logout */

    /*
     * -----------------------------------------------------------------
     * report_error - retrieve error message and print it out.
     * -----------------------------------------------------------------
     */
    void report_error()
    {
      text  msgbuf[1024];
      sb4   errcode;

      (void) OCIErrorGet((dvoid *) errhp,
                         (ub4) 1,
                         (text *) NULL,
                         &errcode,
                         msgbuf,
                         (ub4) sizeof(msgbuf),
                         (ub4) OCI_HTYPE_ERROR);
      (void) fprintf(stdout,"ERROR CODE = %d\n", errcode);
      (void) fprintf(stdout,"%s\n", msgbuf);
      return;

    }  /* end report_error */

    /*
     * -----------------------------------------------------------------
     * load_cart - load data into the image data cartridge
     *
     *   Populate the cartidge columns based on the contents of the bfile.
     *   The sequence of steps is to select the types and then use the
     *   contents to set the properties and perform the copy from the
     *   bfile to the blob.
     * -----------------------------------------------------------------
     */
    sb4 load_cart()
    {
    text *sqlstmt = (text *)
    "DECLARE \
      A ORDSYS.ORDImgF;\
      B ORDSYS.ORDImgB;\
    BEGIN\
     SELECT C2, C3 INTO A,B FROM IMGDEMOTAB FOR UPDATE;\
    \
     A.setProperties;\
     A.copyContent(B.content);\
     B.setProperties;\
     UPDATE IMGDEMOTAB SET C2 = A;\
     UPDATE IMGDEMOTAB SET C3 = B;\
     COMMIT;\
    END;";

      if (OCIStmtPrepare(stmthp,
                         errhp,
                         sqlstmt,
                         (ub4) strlen((char *)sqlstmt),
                         (ub4) OCI_NTV_SYNTAX,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtPrepare() sqlstmt\n");
        report_error();
        return OCI_ERROR;
      }

      /* execute the select and fetch one row */
      if (OCIStmtExecute(svchp,
                         stmthp,
                         errhp,
                         (ub4) 1,
                         (ub4) 0,
                         (CONST OCISnapshot*) 0,
                         (OCISnapshot*) 0,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtExecute() sqlstmt\n");
        report_error();
        return OCI_ERROR;
      }

      return OCI_SUCCESS;

    }  /* end load_cart */

    /*
     * -----------------------------------------------------------------
     * mod_img - modify some characteristics of the image
     *
     * This is accomplished by selecting the cartridge type and then
     * invoking the process method with a command.  The command specified
     * below scales the image by a factor of 2 and cuts a 100x100 pixel
     * piece from the original image.
     * -----------------------------------------------------------------
     */
    sb4 mod_img()
    {
    text *sqlstmt = (text *)
    "DECLARE \
      B ORDSYS.ORDImgB;\
      command VARCHAR2(400);\
    BEGIN\
     SELECT C3 INTO B FROM IMGDEMOTAB FOR UPDATE;\
    \
     command := 'scale=2 cut=100 100 100 100';\
     B.process(command);\
     B.setProprties;\
     UPDATE IMGDEMOTAB SET C3 = B;\
     COMMIT;\
    END;";

      if (OCIStmtPrepare(stmthp,
                         errhp,
                         sqlstmt,
                         (ub4) strlen((char *)sqlstmt),
                         (ub4) OCI_NTV_SYNTAX,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtPrepare() sqlstmt\n");
        report_error();
        return OCI_ERROR;
      }

      /* execute the select and fetch one row */
      if (OCIStmtExecute(svchp,
                         stmthp,
                         errhp, (ub4) 1,
                         (ub4) 0,
                         (CONST OCISnapshot*) 0,
                         (OCISnapshot*) 0,
                         (ub4) OCI_DEFAULT))
      {
        (void) fprintf(stdout,"FAILED: OCIStmtExecute() sqlstmt\n");
        report_error();
        return OCI_ERROR;
      }

      return OCI_SUCCESS;

    }  /* end mod_img */

    /*
     * -----------------------------------------------------------------
     * blob_to_file - copy the contents of a blob to a file
     *
     * Read blob using stream mode into local buffers and then write
     * data to operating system file.
     * -----------------------------------------------------------------
     */
    sb4 blob_to_file()
    {
      ub4   offset = 1;
      ub4   loblen = 0;
      ub1   bufp[MAXBUFLEN];
      ub4   amtp = 0;
      sword retval;
      ub4   piece = 0;
      ub4   remainder;
      FILE  *fp;

      /*
       * fetch the blob from which data will be read
       */
      if (fetch_blob())
      {
         (void)fprintf(stdout, "FAILED: fetch_blob()\n");
         return (OCI_ERROR);
      }

      fp = fopen((char *)fname, (const char *) "wb");

      (void) OCILobGetLength(svchp, errhp, blob, &loblen);
      amtp = loblen;

      memset(bufp, `\0', MAXBUFLEN);

      retval = OCILobRead(svchp,
                          errhp,
                          blob,
                          &amtp,
                          offset,
                          (dvoid *) bufp,
                          (loblen < MAXBUFLEN ? loblen : MAXBUFLEN),
                          (dvoid *)0,
                          (sb4 (*)(dvoid *, const dvoid *, ub4, ub1)) 0,
                          (ub2) 0,
                          (ub1) SQLCS_IMPLICIT);

      switch (retval)
      {
        case OCI_SUCCESS:             /* only one piece */
          (void) fwrite(bufp, loblen, 1, fp);
          break;
        case OCI_ERROR:
          report_error();
          break;
        case OCI_NEED_DATA:           /* there are 2 or more pieces */

          remainder = loblen;

          (void) fwrite(bufp, MAXBUFLEN, 1, fp); /* a full buffer to write */
          do
          {
            memset(bufp, `\0', MAXBUFLEN);
            amtp = 0;

            remainder -= MAXBUFLEN;

            retval = OCILobRead(svchp,
                                errhp,
                                blob,
                                &amtp,
                                offset,
                                (dvoid *) bufp,
                                (ub4) MAXBUFLEN,
                                (dvoid *)0,
                                (sb4 (*)(dvoid *, dvoid *, ub4, ub1)) 0,
                                (ub2) 0,
                                (ub1) SQLCS_IMPLICIT);

            /* the amount read returned is undefined for FIRST, NEXT pieces */

            if (remainder < MAXBUFLEN)     /* last piece not a full buffer piece */
               (void) fwrite(bufp, remainder, 1, fp);
            else
               (void) fwrite(bufp, MAXBUFLEN, 1, fp);

          } while (retval == OCI_NEED_DATA);
          break;
        default:
          (void) fprintf(stdout,"Unexpected ERROR: OCILobRead() LOB.\n");
          break;
      }
      if (ftell(fp) != loblen)
      {
         (void)fprintf(
            stdout,
            "ERROR: resultant files size of %d differs from source lob size
            of &d\n",
            ftell(fp),loblen);
         status = OCI_ERROR;
       }

      if (fclose(fp))
         return (OCI_ERROR);
      else
         return (status);

    }  /* end blob_to_file */

    /* end of file imgdemo.c */

Prev | Next
| Copyright © 1997 Oracle Corporation. All Rights Reserved. |
Library | Product | Contents | Index