text_image.cp

/* Program name: text_image.cp
**
** Description: Inserting text and image data using host 
** variables of types CS_TEXT and CS_IMAGE.

** Notes: This is a new feature in 11.x which allows you to use
** host variables of type CS_TEXT and CS_IMAGE in insert
** or update statements to handle text or image data. You don't
** need to use to mixed-mode client-library programming or
** dynamic sql, which had a limit of 64 k bytes.
** The size of the text or image data that can now be sent is
** limited only by memory or the maximum size allowed for
** text and image data by the Adaptive Server. However,
** the larger the data being sent this way, the slower the
** performance.
**
** Script file: text_image.sql
**
** Notes: Make sure you compile the program using the '-y'
** precompiler flag.
**
*/

#include <stdio.h>
#include "sybsqlex.h"

/* Declare the SQLCA */
EXEC SQL INCLUDE sqlca;

/*
** Forward declarations of the error and message handlers and
** other subroutines called from main().
*/
void    error_handler();
void    warning_handler();

int main()
{
int i=0;

EXEC SQL BEGIN DECLARE SECTION;
/* storage for login name and password */
	CS_CHAR        username[30], password[30];
	CS_TEXT        text_var[10000];
	CS_IMAGE        image_var[10000];
EXEC SQL END DECLARE SECTION;

	EXEC SQL WHENEVER SQLERROR CALL error_handler();
	EXEC SQL WHENEVER SQLWARNING CALL warning_handler();
	EXEC SQL WHENEVER NOT FOUND CONTINUE;

/*
** Copy the user name and password defined in sybsqlex.h to
** the variables declared for them in the declare section.
*/
strcpy(username, USER);
strcpy(password, PASSWORD);

/* Connect to the server and specify the database to use */
EXEC SQL CONNECT :username IDENTIFIED BY :password;

EXEC SQL USE tempdb;

/* Put something interesting in the variables. */
for (i=0; i< 10000; i++ )
	{
	text_var[i]  = 'a';
	image_var[i] = '@';
	}

EXEC SQL INSERT text_tab VALUES(:text_var, :image_var);
if ( sqlca.sqlcode == 0 )
	{
	printf("Row successfully inserted! \n");
	EXEC SQL COMMIT WORK ;
	}

EXEC SQL DISCONNECT ALL;
exit(0);
}

/*
** void error_handler()
**
** Displays error codes and numbers from the SQLCA and exits with
** an ERREXIT status.
*/
void error_handler()
{
fprintf(stderr, "\n** SQLCODE=(%d)", sqlca.sqlcode);

if (sqlca.sqlerrm.sqlerrml)
	{
	fprintf(stderr, "\n** Error Message: ");
	fprintf(stderr, "\n** %s", sqlca.sqlerrm.sqlerrmc);
	}

fprintf(stderr, "\n\n");

exit(ERREXIT);
}

/*
** void warning_handler()
**
** Displays warning messages.
*/
void warning_handler()
{

if (sqlca.sqlwarn[1] == 'W')
	{
	fprintf(stderr,
	"\n** Data truncated.\n");
	}

if (sqlca.sqlwarn[3] == 'W')
	{
	fprintf(stderr,
	"\n** Insufficient host variables to store results.\n");
	}
return;
}