Exemple #1
0
/**
 * The purpose of this program is to load or extract the text of a stored procedure.  
 * This first cut does extract only.  
 * TODO: support loading procedures onto the server.  
 */
int
main(int argc, char *argv[])
{
	LOGINREC *login;
	DBPROCESS *dbproc;
	PROCEDURE procedure;
	RETCODE erc;
	int i, nrows;

	/* Initialize db-lib */
	erc = dbinit();	
	if (erc == FAIL) {
		fprintf(stderr, "%s:%d: dbinit() failed\n", options.appname, __LINE__);
		exit(1);
	}
	

	memset(&options, 0, sizeof(options));
	login = get_login(argc, argv, &options); /* get command-line parameters and call dblogin() */
	assert(login != NULL);

	/* Install our error and message handlers */
	dberrhandle(err_handler);
	dbmsghandle(msg_handler);

	/* 
	 * Override stdin, stdout, and stderr, as required 
	 */
	if (options.input_filename) {
		if (freopen(options.input_filename, "r", stdin) == NULL) {
			fprintf(stderr, "%s: unable to open %s: %s\n", options.appname, options.input_filename, strerror(errno));
			exit(1);
		}
	}

	if (options.output_filename) {
		if (freopen(options.output_filename, "w", stdout) == NULL) {
			fprintf(stderr, "%s: unable to open %s: %s\n", options.appname, options.output_filename, strerror(errno));
			exit(1);
		}
	}
	
	/* 
	 * Connect to the server 
	 */
	dbproc = dbopen(login, options.servername);
	assert(dbproc != NULL);
	
	/* Switch to the specified database, if any */
	if (options.database)
		dbuse(dbproc, options.database);

	/* 
	 * Read the procedure names and move their texts.  
	 */
	for (i=options.optind; i < argc; i++) {
		static const char query[] = " select	c.text"
					 " from	syscomments  as c"
					 " join 	sysobjects as o"
					 " on 		o.id = c.id"
					 " where	o.name = '%s'"
					 " and 	o.uid = user_id('%s')"
					 " and		o.type not in ('U', 'S')" /* no user or system tables */
					 " order by 	c.colid"
					;
		static const char query_table[] = " execute sp_help '%s.%s' ";

		parse_argument(argv[i], &procedure);

		erc = dbfcmd(dbproc, query, procedure.name, procedure.owner); 
	
		/* Send the query to the server (we could use dbsqlexec(), instead) */
		erc = dbsqlsend(dbproc);
		if (erc == FAIL) {
			fprintf(stderr, "%s:%d: dbsqlsend() failed\n", options.appname, __LINE__);
			exit(1);
		}
		
		/* Wait for it to execute */
		erc = dbsqlok(dbproc);
		if (erc == FAIL) {
			fprintf(stderr, "%s:%d: dbsqlok() failed\n", options.appname, __LINE__);
			exit(1);
		}

		/* Write the output */
		nrows = print_results(dbproc);
		
		if (0 == nrows) {
			erc = dbfcmd(dbproc, query_table, procedure.owner, procedure.name);
			erc = dbsqlexec(dbproc);
			if (erc == FAIL) {
				fprintf(stderr, "%s:%d: dbsqlexec() failed\n", options.appname, __LINE__);
				exit(1);
			}
			nrows = print_ddl(dbproc, &procedure);
		}

		switch (nrows) {
		case -1:
			return 1;
		case 0:
			fprintf(stderr, "%s: error: %s.%s.%s.%s not found\n", options.appname, 
					options.servername, options.database, procedure.owner, procedure.name);
			return 2;
		default:
			break;
		}
	}

	return 0;
}
/*---------------------------------------------------------------------
 * process_lcr - Print this LCR and process the data in it
 *---------------------------------------------------------------------*/
static sb4 process_lcr(myctx_t *ctxp, void *lcrp, ub1 lcrtype,
                       boolean *idkey_lcr, boolean *stmt_lcr)
{
  oratext     *src_db_name;
  ub2          src_db_namel;
  oratext     *cmd_type;
  ub2          cmd_type_len;
  oratext     *owner;
  ub2          ownerl;
  oratext     *oname;
  ub2          onamel;
  ub1         *tag;
  ub2          tagl;
  oratext     *txid;
  ub2          txidl;
  OCIDate      src_time;
  oci_t       *ocip = ctxp->outbound_ocip;
  sword        ret;
  oratext      datebuf[DATE_BUF_LEN+1];
  ub4          datelen = DATE_BUF_LEN;
  ub1         *pos;
  ub2          pos_len;
  oraub8       flag = 0;
  boolean      seqlcr = FALSE;

  *idkey_lcr = FALSE;
  *stmt_lcr = FALSE;
  
  /* Get LCR Header information */  
  ret = OCILCRHeaderGet(ocip->svcp, ocip->errp,
                        &src_db_name, &src_db_namel,       /* source db */
                        &cmd_type, &cmd_type_len,       /* command type */
                        &owner, &ownerl,                  /* owner name */
                        &oname, &onamel,                 /* object name */
                        &tag, &tagl,                         /* lcr tag */
                        &txid, &txidl, &src_time, /* txn id  & src time */
                        (ub2 *)0, (ub2 *)0,         /* OLD/NEW col cnts */
                        &pos, &pos_len,                 /* LCR position */
                        &flag, lcrp, OCI_DEFAULT);

  printf("\n ----------- %s LCR Header  -----------------\n",
         lcrtype == OCI_LCR_XDDL ? "DDL" : "ROW");
  
  if (ret != OCI_SUCCESS)
    ocierror(ocip, (char *)"OCILCRHeaderGet failed", TRUE);
  else
  {
    /* check sequence lcr */
    if (lcrtype == OCI_LCR_XROW)
    {
      seqlcr = (lbit(flag, OCI_ROWLCR_SEQ_LCR) > 0);
    }
      
    printf("OCILCRHeaderGet, flag is 0x%x\n", flag);
    
    /* See if this is an idkey LCR */
    if (lbit(flag, OCI_ROWLCR_HAS_ID_KEY_ONLY))
    {
      printf("XXX ID KEY LCR\n");
      *idkey_lcr = TRUE;
    }

    memcpy(ctxp->last_lcrpos, pos, pos_len);
    ctxp->last_lcrpos_len = pos_len;

    print_pos(ocip, pos, pos_len, (char *)"LCR ID");
    printf("src_db_name=%.*s cmd_type=%.*s txid=%.*s\n",
           src_db_namel, src_db_name, cmd_type_len, cmd_type, txidl, txid );
    ctxp->is_commit = FALSE;
    if (samecmd(cmd_type, cmd_type_len,
                OCI_LCR_ROW_CMD_COMMIT, strlen(OCI_LCR_ROW_CMD_COMMIT)))
    {
      ctxp->is_commit = TRUE;
      return OCI_SUCCESS;
    }
     
    printf("owner=%.*s oname=%.*s \n", ownerl, owner, onamel, oname);

    ctxp->ownerlen = ownerl;
    ctxp->owner    = owner;
    ctxp->onamelen = onamel;
    ctxp->oname    = oname;
    ctxp->txid     = txid;
    ctxp->txidlen  = txidl;

    if (lcrtype == OCI_LCR_XDDL)
    {
      print_ddl(ocip, lcrp);
    }
    else if (lcrtype == OCI_LCR_XROW && ctxp->longform)
    {
      boolean  loblcr = FALSE;

      loblcr = 
         (samecmd(cmd_type, cmd_type_len,
              OCI_LCR_ROW_CMD_LOB_WRITE, strlen(OCI_LCR_ROW_CMD_LOB_WRITE)) ||
          samecmd(cmd_type, cmd_type_len,
              OCI_LCR_ROW_CMD_LOB_TRIM, strlen(OCI_LCR_ROW_CMD_LOB_TRIM)) ||
          samecmd(cmd_type, cmd_type_len,
              OCI_LCR_ROW_CMD_LOB_ERASE, strlen(OCI_LCR_ROW_CMD_LOB_ERASE)));

      if (loblcr)
        print_lob_info (ctxp);

      if (samecmd(cmd_type, cmd_type_len,
                  OCI_LCR_ROW_CMD_DELETE, strlen(OCI_LCR_ROW_CMD_DELETE)) ||
          samecmd(cmd_type, cmd_type_len,
                  OCI_LCR_ROW_CMD_UPDATE, strlen(OCI_LCR_ROW_CMD_UPDATE)))
      {
        ret = process_lcr_data(ctxp, lcrp, OCI_LCR_ROW_COLVAL_OLD, seqlcr);
      }

      if (samecmd(cmd_type, cmd_type_len,
                  OCI_LCR_ROW_CMD_INSERT, strlen(OCI_LCR_ROW_CMD_INSERT)) ||
          samecmd(cmd_type, cmd_type_len,
                  OCI_LCR_ROW_CMD_UPDATE, strlen(OCI_LCR_ROW_CMD_UPDATE)) ||
          loblcr)
      {
        ret = process_lcr_data(ctxp, lcrp, OCI_LCR_ROW_COLVAL_NEW, seqlcr);
      }
    }
  } 
  return ret;
}