/******************************************************************************* * *N {xml_sample.c} -- demonstrates how to create xml column and xml index, * and how to perform xml editing and xml query * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *P Purpose: * * This sample C program demonstrates how to: * (1) create XML column; * (2) create XML tags; * (3) create XML index; * (4) edit XMLs; * (5) query with XPATH; * *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * How to compile:: * Please refer section "Build C Samples" in the SDE sample * *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Usage: * xml_sample.c {server} {instance} {database} {user} {password} {keywords} * *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *X Legalese: * * Copyright © 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 #include #include #include #include "sdetype.h" #include "sdeerno.h" /* Local defines */ #define KEYCOL "KEYCOL" #define STRCOL "STRCOL" #define XMLCOL "XMLCOL" #define ROWIDCOL "ROWIDCOL" #define DOC1 "\ \ \ \ 1000\ 4\ 5\ 18\ 2000\ \ \ " #define DOC2 "\ \ \ \ 5000\ 400\ 6000\ \ \ " #define DOC3 "\ \ \ \ 9000\ 500\ 600\ 900\ \ \ " #define UPDATEDOC1 "\ \ \ \ 5500\ 1500\ 1500\ \ \ " #define UPDATEDOC2 "\ \ \ \ 1\ 2\ 150\ \ \ " /* Function macros */ #define check_rc_return_on_failure(c,s,rc,f) \ {if(rc!= SE_SUCCESS) {check_rc_(c,s,rc,f,__LINE__,__FILE__);return SE_FAILURE; }} /* Local Function Prototypes */ void check_rc_(SE_CONNECTION Connection, SE_STREAM Stream, LONG rc, char *comment, LONG line_no, char* file_name); static LONG S_create_table(SE_CONNECTION connection,CHAR *table,CHAR *keyword); static LONG S_insert_rows(SE_CONNECTION handle,const CHAR *table); static LONG S_update_rows(SE_CONNECTION handle,const CHAR *table,int num_of_row, LONG* keyvals); static LONG S_query_table (SE_CONNECTION connection, const CHAR *table, const CHAR *where, const CHAR *xpath); LONG main(int argc, char *argv[]){ CHAR *server, *user, *passwd, *database, *instance, *keyword; SE_CONNECTION conn; SE_ERROR error; LONG result; CHAR tablename[SE_MAX_TABLE_LEN+1]; CHAR **xml_tag_name; SE_XMLTAGINFO xml_tag; CHAR xml_index_name[SE_MAX_XML_INDEX_NAME_LEN+1]; SE_XMLINDEXINFO xml_index; SE_XMLCOLUMNINFO xmlcol; LONG i,tag_types[4]; LONG updaterows,update_keyvals[10]; CHAR where[1024]; CHAR xpath[128]; if(argc<7){ printf("Usage %s ",argv[0]); exit(1); } server=argv[1]; instance=argv[2]; database=argv[3]; user=argv[4]; passwd=argv[5]; keyword=argv[6]; strcpy(tablename,"xml_samlple"); //connect to sde server result = SE_connection_create( server, instance, database, user, passwd, &error, &conn ); check_rc_return_on_failure (conn,NULL,result,"SE_connection_create"); printf("Connected to %s:%s as %s\n",server,instance,user); //Create the sample table printf("\nCreating sample table......\n"); result=S_create_table(conn,tablename,keyword); if(result!=SE_SUCCESS) return result; //Init a xmlindex object result = SE_xmlindexinfo_create (&xml_index); check_rc_return_on_failure (conn,NULL,result,"SE_xmlindexinfo_create"); result = SE_xmlindexinfo_set_type (xml_index, SE_XML_INDEX_DEFINITION); check_rc_return_on_failure (conn,NULL,result,"SE_xmlindexinfo_set_type"); //prepare xtags xml_tag_name = (CHAR **) malloc (4 * sizeof(CHAR *)); for(i=0;i<4;i++){ xml_tag_name[i]=(CHAR *) malloc (SE_MAX_XML_TAG_NAME_LEN * sizeof(CHAR)); tag_types[i]=SE_XML_INDEX_DOUBLE_TYPE; } strcpy(xml_tag_name[0],"/xmldocmetadata/Esri/gn/suptheme"); strcpy(xml_tag_name[1],"/xmldocmetadata/Esri/gn/coverage"); strcpy(xml_tag_name[2],"/xmldocmetadata/Esri/gn/featured"); strcpy(xml_tag_name[3],"/xmldocmetadata/Esri@MetaID"); result = SE_xmltaginfo_create (&xml_tag); //Add xmltags to the xmlindex object check_rc_return_on_failure (conn,NULL,result,"SE_xmltaginfo_create"); for (i = 0; i < 4; i++) { result = SE_xmltaginfo_set_name (xml_tag, xml_tag_name[i]); check_rc_return_on_failure (conn,NULL,result,"SE_xmltaginfo_set_name"); result = SE_xmltaginfo_set_data_type (xml_tag,tag_types[i]); check_rc_return_on_failure (conn,NULL,result,"SE_xmltaginfo_set_data_type"); result = SE_xmlindexinfo_add_tag (xml_index, xml_tag); check_rc_return_on_failure (conn,NULL,result,"SE_xmlindexinfo_add_tag"); } free(xml_tag_name); //Create a xml column printf("\nCreating XML column XMLCOL......\n"); result = SE_xmlcolumninfo_create(&xmlcol); check_rc_return_on_failure (conn,NULL,result,"SE_xmlcolumninfo_create"); result = SE_xmlcolumninfo_set_xml_column(xmlcol, tablename, XMLCOL); check_rc_return_on_failure (conn,NULL,result,"SE_xmlcolumninfo_set_xml_column"); result = SE_xmlcolumninfo_set_creation_keyword (xmlcol,keyword); check_rc_return_on_failure (conn,NULL,result,"SE_xmlcolumninfo_set_creation_keyword"); result = SE_xmlcolumn_create (conn, xmlcol); check_rc_return_on_failure (conn,NULL,result,"SE_xmlcolumn_create"); //Create xml index on the xml column printf("\nCreate index on XMLCOL......\n"); strcpy (xml_index_name, "xmlindex"); result = SE_xmlindexinfo_set_name (xml_index, xml_index_name); check_rc_return_on_failure (conn,NULL,result,"SE_xmlindexinfo_set_name"); result = SE_xmlcolumninfo_set_index (xmlcol, xml_index); check_rc_return_on_failure (conn,NULL,result,"SE_xmlcolumninfo_set_index"); result = SE_xmlcolumn_alter (conn, xmlcol); check_rc_return_on_failure (conn,NULL,result,"SE_xmlcolumn_alter"); //Insert 30 rows into the table printf("\nInsert 30 rows into the sample table......\n"); result=S_insert_rows(conn,tablename); if(result!=SE_SUCCESS) return result; sprintf(where,"KEYCOL > 10 AND KEYCOL< 20"); strcpy(xpath,"/xmldocmetadata/Esri/gn[featured>1000]"); //Perform query on the table without xpath printf("\nQuery the sample table without xpath.....\n"); result=S_query_table(conn,tablename,where,NULL); //Perform query on the table with xpath printf("\nQuery the sample table with xpaths.....\n"); result=S_query_table(conn,tablename,where,xpath); if(result!=SE_SUCCESS) return result; //Update the first 9 rows: row 10-19 printf("\nUpdateing rows.....\n"); updaterows=9; for(i=0;i == (SE_CONNECTION) The connection handle. * == (CHAR*) the table name * == (CHAR*) the keyword for table creation *E ***********************************************************************/ static LONG S_create_table(SE_CONNECTION connection,CHAR *table,CHAR *keyword) { SHORT number_of_columns; SE_COLUMN_DEF column_definitions[3]; LONG result; SE_REGINFO registration; /* Prepare the column definitions */ number_of_columns = 2; strcpy (column_definitions[0].column_name,KEYCOL); column_definitions[0].sde_type = SE_INT32_TYPE; column_definitions[0].size = 0; column_definitions[0].decimal_digits = 0; column_definitions[0].nulls_allowed = TRUE; strcpy(column_definitions[1].column_name,STRCOL); column_definitions[1].sde_type = SE_STRING_TYPE; column_definitions[1].size = 128; column_definitions[1].decimal_digits = -1; column_definitions[1].nulls_allowed = FALSE; //delete the table if already existed result = SE_table_delete(connection,table); if(result!=SE_TABLE_NOEXIST) check_rc_return_on_failure (connection,NULL,result,"SE_table_delete"); //create the sample table result = SE_table_create (connection,table,number_of_columns, column_definitions,keyword); check_rc_return_on_failure (connection,NULL,result,"SE_table_create"); /* Add an SDE maintained rowid to the table*/ result = SE_reginfo_create (®istration); check_rc_return_on_failure (connection,NULL,result,"SE_reginfo_create"); result = SE_registration_get_info (connection,table,registration); check_rc_return_on_failure (connection,NULL,result,"SE_registration_get_info"); result = SE_reginfo_set_creation_keyword (registration,keyword); check_rc_return_on_failure (connection,NULL,result,"SE_reginfo_set_creation_keyword"); check_rc_return_on_failure (connection,NULL,result,"SE_reginfo_set_description"); result = SE_reginfo_set_rowid_column (registration,ROWIDCOL, SE_REGISTRATION_ROW_ID_COLUMN_TYPE_SDE); check_rc_return_on_failure (connection,NULL,result,"SE_reginfo_set_rowid_column"); result = SE_registration_alter (connection,registration); check_rc_return_on_failure (connection,NULL,result,"SE_registration_alter"); SE_reginfo_free(registration); return(SE_SUCCESS); } /*********************************************************************** * *N {S_insert_rows} - insert 30 rows into the table * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * *P Purpose: * Insert rows into the sample table. * The value of the XML column will be determice by the value of KEYCOL: * KEYCOL%3==0: XML=DOC1 * KEYCOL%3==1: XML=DOC2 * KEYCOL%3==2: XML=DOC3 *E *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * *A Parameters: * == (SE_CONNECTION) The connection handle. *
== (CHAR*) the layer name *E ***********************************************************************/ static LONG S_insert_rows(SE_CONNECTION handle,const CHAR *table) { SE_STREAM stream; LONG result,row,i; SHORT number_of_columns; CHAR **base_columns; SE_INT32 key_val; CHAR str_val[128]; SE_XMLDOC xmldoc; /*Init the column names for insertion*/ number_of_columns = 3; base_columns = (CHAR **) malloc (number_of_columns * sizeof(CHAR *)); for(i=0;i == (SE_CONNECTION) The connection handle. *
== (CHAR*) the layer name * == (LONG) number of rows to be inserted * == (LONG*) specify rows to be updated *E ***********************************************************************/ static LONG S_update_rows(SE_CONNECTION handle,const CHAR *table,int num_of_row, LONG* keyvals) { SE_STREAM stream; LONG result,row,i; SHORT number_of_columns; CHAR **base_columns; CHAR str_val[128]; SE_XMLDOC xmldoc; CHAR where_clause[256]; //Prepare column names for the update number_of_columns = 2; base_columns = (CHAR **) malloc (number_of_columns * sizeof(CHAR *)); for(i=0;i == (SE_CONNECTION) The connection handle. *
== (CHAR*) the layer name * == (const CHAR*) the where clause * == (const CHAR*) the xpath *E ***********************************************************************/ static LONG S_query_table(SE_CONNECTION connection, const CHAR *table, const CHAR *where, const CHAR *xpath) { SE_XMLDOC xmldoc; LONG result,keyval,i,fetched_rows=0,rowid; CHAR **columns; SE_QUERYINFO qinfo; SE_STREAM query_stream; CHAR orderby[128]; SHORT num_of_column; CHAR str_val[128]; printf("\t%-11s: %s\n","WHERE",where); printf("\t%-11s: %s\n","XPATH",xpath==NULL?"NULL":xpath); //prepare columns to be queried num_of_column = 4; columns = (CHAR **) malloc (num_of_column * sizeof(CHAR *)); for(i=0;i