/****************************************************************************
** (c) Copyright IBM Corp. 2007 All rights reserved.
** 
** The following sample of source code ("Sample") is owned by International 
** Business Machines Corporation or one of its subsidiaries ("IBM") and is 
** copyrighted and licensed, not sold. You may use, copy, modify, and 
** distribute the Sample in any form without payment to IBM, for the purpose of 
** assisting you in the development of your applications.
** 
** The Sample code is provided to you on an "AS IS" basis, without warranty of 
** any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR 
** IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do 
** not allow for the exclusion or limitation of implied warranties, so the above 
** limitations or exclusions may not apply to you. IBM shall not be liable for 
** any damages you suffer as a result of using, copying, modifying or 
** distributing the Sample, even if IBM has been advised of the possibility of 
** such damages.
*****************************************************************************
**
** SOURCE FILE NAME: tbcompress.sqc 
**    
** SAMPLE: How to create tables with null and default value compression 
**         option. 
**
** SQL STATEMENTS USED:
**         CREATE TABLE 
**         ALTER TABLE
**         DROP TABLE
**
**                           
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing embedded SQL applications see the Developing Embedded SQL Applications book.
**
** For information on using SQL statements, see the SQL Reference.
**
** For the latest information on programming, building, and running DB2 
** applications, visit the DB2 Information Center: 
**     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include <sqlutil.h>
#include "utilemb.h"

int TbCreate(void);
int TbCompress(void);
int TbDrop(void);

EXEC SQL INCLUDE SQLCA;

int main(int argc, char *argv[])
{
  int rc = 0;
  char dbAlias[SQL_ALIAS_SZ + 1];
  char user[USERID_SZ + 1];
  char pswd[PSWD_SZ + 1];

  /* check the command line arguments */
  rc = CmdLineArgsCheck1(argc, argv, dbAlias, user, pswd);
  if (rc != 0)
  {
    return rc;
  }

  printf("\nTHIS SAMPLE SHOWS HOW TO USE NULL AND DEFAULT VALUE\n");
  printf("COMPRESSION OPTION AT TABLE LEVEL AND COLUMN LEVEL.\n");

  /* connect to database */
  rc = DbConn(dbAlias, user, pswd);
  if (rc != 0)
  {
    return rc;
  }

  rc = TbCreate();
  rc = TbCompress();
  rc = TbDrop();

  /* disconnect from database */
  rc = DbDisconn(dbAlias);
  if (rc != 0)
  {
    return rc;
  }

  return 0;
} /* main */

int TbCreate(void)
{
  int rc = 0;

  printf("\n-----------------------------------------------------------");
  printf("\nCreating table COMP_TAB\n\n");
  
  /* create the table */
  printf("    CREATE TABLE COMP_TAB(Col1 INT NOT NULL WITH DEFAULT,\n");
  printf("                          Col2 CHAR(7),\n");
  printf("                          Col3 VARCHAR(7) NOT NULL,\n");
  printf("                          Col4 DOUBLE) \n");
 
  EXEC SQL CREATE TABLE COMP_TAB(col1 INT NOT NULL WITH DEFAULT,
                                 col2 CHAR(7),
                                 col3 VARCHAR(7) NOT NULL,
                                 col4 DOUBLE);
                                       
  EMB_SQL_CHECK("Table -- Create");

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("Transaction -- Commit");

  return rc;
} /* TbCreate */

int TbCompress(void)
{
  int rc=0;
    
  printf("\n-----------------------------------------------------------\n");
  printf(" To activate VALUE COMPRESSION at table level and COMPRESS \n");
  printf(" SYSTEM DEFAULT at column level \n\n");

  printf("    ALTER TABLE COMP_TAB ACTIVATE VALUE COMPRESSION \n\n");
  printf(" Rows will be formatted using the new row format on subsequent\n");
  printf(" insert,load and update operation, and NULL values will not be\n");
  printf(" taking up space if applicable.\n\n");
  
  /* If the table COMP_TAB does not have many NULL values, enabling
     compression will result in using more disk space than using 
     the old row format */

  EXEC SQL ALTER TABLE COMP_TAB ACTIVATE VALUE COMPRESSION;

  printf("\n To save more disk space on system default value for column\n");
  printf(" Col1, enter\n");
  printf("\n    ALTER TABLE COMP_TAB ALTER Col1 COMPRESS SYSTEM DEFAULT\n");
  printf("\n On subsequent insert, load, and update operations,numerical\n");
  printf(" 0 value (occupying 4 bytes of storage) for column Col1 will\n");
  printf(" not be saved on disk.\n");

  EXEC SQL ALTER TABLE COMP_TAB ALTER Col1 COMPRESS SYSTEM DEFAULT;

  printf("\n\n To switch the table to use the old format, enter\n\n");
  printf("    ALTER TABLE COMP_TAB DEACTIVATE VALUE COMPRESSION\n\n");   
  printf(" Rows inserted, loaded or updated after the ALTER statement\n");
  printf(" will have old row format.\n");

  EXEC SQL ALTER TABLE COMP_TAB DEACTIVATE VALUE COMPRESSION;

  return rc;
} /* TbCompress */

int TbDrop(void)
{
  int rc = 0;

  printf("\n-----------------------------------------------------------");
  printf("\nDropping table COMP_TAB\n\n");
  
  /* drop the table */
  printf("    DROP TABLE COMP_TAB\n");

  EXEC SQL DROP TABLE COMP_TAB;
  EMB_SQL_CHECK("Table -- Drop");

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("Transaction -- Commit");

  return rc;
} /* Tbdrop */