/*******************************************************************************
*                                                                       
*N  {queryrasterattributes.c}  --  Example of how to access the data from a raster attribute. 
*This includes the pixel data from the tiles defined in the SE_RASCONSTRAINT structure.
*                                                                       
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
* How to compile:: 
*       This program provides programming examples of 
*
*
*     1. Add {ibm ICU installation directory}/lib to your library paths
*	  2. Add {ibm ICU installation directory}/include to your include paths
*     3. Add {$SDEHOME}/lib to your library paths;
*     4. Compile and link with icuuc.lib and other needed SDE libraries
*E
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*X  Legalese:
*
* Copyright � 2006-2007 ESRI
*
* All rights reserved under the copyright laws of the United States and 
* applicable international laws, treaties, and conventions.
*
* You may freely redistribute and use this sample code, with or without 
* modification, provided you include the original copyright notice and use 
* restrictions.  
*
* Disclaimer:  THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 
* WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ESRI OR 
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
* OR BUSINESS INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED 
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
* TORT ARISING IN ANY WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED 
* OF THE POSSIBILITY OF SUCH DAMAGE.
* 
* For additional information, contact:
* Environmental Systems Research Institute, Inc.
* Attn: Contracts and Legal Services Department
* 380 New York Street
* Redlands, California, 92373
* USA
* 
* email: contracts@esri.com
*
*E
****************************************************************************/


#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "sdeerno.h"
#include "sdetype.h"
#include "sderaster.h"
#include "raster_source.h"

/* Query the raster attribute. */

void main (void) {

	SE_CONNECTION connection;
	SE_ERROR error;
	SE_RASCOLINFO rascol_info;
	SE_RASTERATTR raster_attrib;
	SE_STREAM stream;
	SE_QUERYINFO queryinfo;
	CHAR table[SE_MAX_TABLE_LEN], 
	*server, 
	*instance, 
	*database, 
	*username, 
	*password,
	raster_column[] = "RASTER", 
	name_column[] = "NAME", 
	**columns, 
	**tables, 
	name[SE_MAX_COLUMN_LEN];
	LONG rc;
	SHORT name_ind, 
	raster_ind;
	UCHAR *pixel_buffer=NULL;

	/*****************/
	/* Set connection info */
	/*****************/

	server = "jolex";
	instance = "9000";
	database = NULL;
	username = "mark";
	password = "mark";

	/*****************/
	/* Set the table name */
	/*****************/

	strcpy (table,"myrastertable");

	/*******************/
	/* Connect to the server */
	/*******************/

	rc = SE_connection_create (server,instance,database,username,password,&error,&connection);
	check_error (rc,connection,NULL,"SE_connection_create");

	/***********************/
	/* Create the raster structures */
	/***********************/

	/* Create the SE_RASCOLINFO raster column info structure */ 

	rc = SE_rascolinfo_create (&rascol_info);
	check_error (rc, NULL, NULL, "SE_rascolinfo_create");

	/* Create the SE_RASTERATTR raster attribute info structure */

	rc = SE_rasterattr_create (&raster_attrib,FALSE);
	check_error (rc,NULL, NULL, "SE_rasterattr_create");

	/*******************************/
	/* Setup the stream and query structures */
	/*******************************/

	/* Set up the stream query on the raster table */

	rc = SE_stream_create (connection, &stream);
	check_error (rc, connection, NULL, "SE_stream_create");

	/* Create the queryinfo structure */

	rc = SE_queryinfo_create (&queryinfo);
	check_error (rc, connection, NULL, "SE_stream_create");

	/* Add the raster table to the query info structure */

	tables = (CHAR **)malloc(sizeof(CHAR*));
	tables[0] = malloc((strlen(table) +1) * sizeof(CHAR));
	strcpy(tables[0],table);

	rc = SE_queryinfo_set_tables (queryinfo,1,(const CHAR **)tables,NULL); 
		check_error (rc, connection, NULL, "SE_queryinfo_set_table");

	/* Add the raster table's columns to the query info structure */

	columns = (CHAR **)malloc(sizeof(CHAR*) * 2); 
	columns[0] = malloc((strlen(name_column) +1) * sizeof(CHAR));
	columns[1] = malloc((strlen(raster_column) +1) * sizeof(CHAR));

	strcpy(columns[0],name_column);
	strcpy(columns[1],raster_column);

	rc = SE_queryinfo_set_columns (queryinfo,2,(const CHAR **)columns);
	check_error (rc, connection, NULL, "SE_queryinfo_set_table");

	/* Add the queryinfo structure to the stream */

	rc = SE_stream_query_with_info (stream, queryinfo);
	check_error (rc, NULL, stream, "SE_stream_query_with_info");

	/* Free the queryinfo, done with it. */

	SE_queryinfo_free (queryinfo);

	/* Bind the output of the stream (the name column and raster column) */
	/* to the variables for display */

	rc = SE_stream_bind_output_column (stream, 1, name, &name_ind); 
	check_error (rc, NULL, stream, "SE_stream_bind_output_column");

	rc = SE_stream_bind_output_column (stream, 2, raster_attrib, &raster_ind); 
	check_error (rc, NULL, stream, "SE_stream_bind_output_column"); 

	/* Execute the query */

	rc = SE_stream_execute (stream);
	check_error (rc, NULL, stream, "SE_stream_execute");

	while (SE_SUCCESS == rc) {

		/* Fetch each record of the business table */

		rc = SE_stream_fetch (stream);

		if (SE_FINISHED != rc) { 

		check_error (rc, NULL, stream, "SE_stream_fetch");

			if (raster_ind == SE_IS_NOT_NULL_VALUE)
			print_raster_attribute (connection, stream, raster_attrib);
			else
			printf("The raster value is null\n\n");

		}  /* if( SE_FINISHED != rc)  */

	}  /* while (SE_SUCCESS == rc) */

	/* We're done. */

	rc = SE_stream_free (stream);
	check_error (rc, NULL, stream, "SE_stream_free");

	/***************************/
	/* Free raster column & band info */
	/***************************/

	SE_rasterattr_free (raster_attrib);
	SE_rascolinfo_free (rascol_info);

	/* Free the connection */

	SE_connection_free (connection);

}