SCA

SDO Data API examples

test_getInstanceProperties.c
Instance properties
test_setList_manyvalued.c
Using appendStringList() which is used in the official SDO examples but missing from the API spec; (if it weren't supported, there would be an asymmetry in the SDO API for the case of a sequence of elements of simple types, for which, according to the API spec, a dataobject can't be obtained but just a primitive value)
test_setList_nonmanyvalued.c
Attempt to getList() a non-many-valued property fails
test_setList_opencontent.c
Using open content properties (XML Schema wildcards)
test_setCString_replace.c
Schema-driven replacement of an already existing (non-multi-valued) property
test_setCString_append.c
Schema-driven append of a multi-valued property
test_setCString_insert.c
Insertion before another element supposed to come after it in the type's content model
test_setCString_attribute.c
Schema-driven setting of an attribute property
test_setCString_replaceattribute.c
Schema-driven replacement of an attribute property

test_getInstanceProperties.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Instance properties
void test_getInstanceProperties() {
	SDOFACTORY df = dfAlloc("getInstanceProperties");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"testInstanceProperties\">\n"
		"    <xsd:sequence>\n"
		"      <xsd:element name=\"declaredProp\" type=\"xsd:string\"/>\n"
		"      <xsd:any/>\n"
		"    </xsd:sequence>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df,
		"http://www.example.com", "testInstanceProperties");

	setCStringByName(d, "instanceProp", "whatever");

	SDOLIST l = getInstanceProperties(d);

	assert(getCountList(l));

	for (int i = 0; i < getCountList(l); i++) {
		SDOPROPERTY p = getPropertyList(l, i);

		if (i == 0) {
			assert(!strcmp("declaredProp", getPropertyName(p)));
		}
		else if (i == 1) {
			assert(!strcmp("instanceProp", getPropertyName(p)));
		}
		else {
			assert(0);
		}
	}
}

test_setList_manyvalued.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Using appendStringList() which is used in the
// official SDO examples but missing from the API spec;
// (if it weren't supported, there would be an asymmetry
// in the SDO API for the case of a sequence of
// elements of simple types, for which,
// according to the API spec, a dataobject can't be
// obtained but just a primitive value)
void test_setList_manyvalued() {
	SDOFACTORY df = dfAlloc("setList_manyvalued");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns:t=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"setListManyvalued\">\n"
		"    <xsd:sequence>\n"
		"      <xsd:element name=\"prop\" type=\"xsd:string\" maxOccurs=\"unbounded\"/>\n"
		"    </xsd:sequence>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df, "http://www.example.com", "setListManyvalued");
	SDOPROPERTY p = getTypePropertyByName(getType(d), "prop");
	SDOLIST l = getList(d, p);
	appendStringList(l, "first value");
	appendStringList(l, "second value");
	setList(d, p, l);

	SDOLIST l2 = getList(d, p);
	assert(getCountList(l2) == 2);
	for (int i = 0; i < getCountList(l2); i++) {
		char *s = getStringList(l2, i);

		if (i == 0) {
			assert(!strcmp("first value", s));
		}
		else if (i == 1) {
			assert(!strcmp("second value", s));
		}
		else {
			assert(0);
		}
	}
}

test_setList_nonmanyvalued.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Attempt to getList() a non-many-valued property fails
void test_setList_nonmanyvalued() {
	SDOFACTORY df = dfAlloc("setList_nonmanyvalued");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns:t=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"setListNonmanyvalued\">\n"
		"    <xsd:sequence>\n"
		"      <xsd:element name=\"prop\" type=\"xsd:string\"/>\n"
		"    </xsd:sequence>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df, "http://www.example.com", "setListNonmanyvalued");
	SDOPROPERTY p = getTypePropertyByName(getType(d), "prop");
	assert(!isMany(d, p));
	SDOLIST l = getList(d, p);
	assert(!l);
}

test_setList_opencontent.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Using open content properties (XML Schema wildcards)
void test_setList_opencontent() {
	SDOFACTORY df = dfAlloc("setList_opencontent");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns:t=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"setListOpencontent\">\n"
		"    <xsd:sequence>\n"
		"      <xsd:any maxOccurs=\"unbounded\"/>\n"
		"    </xsd:sequence>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df, "http://www.example.com", "setListOpencontent");
	SDOPROPERTY p = defineOpenContentProperty(df, 0, "prop", 0);
	SDOLIST l = getList(d, p);
	appendStringList(l, "first value");
	appendStringList(l, "second value");
	setList(d, p, l);

	SDOLIST l2 = getList(d, p);
	assert(getCountList(l2) == 2);
	for (int i = 0; i < getCountList(l2); i++) {
		char *s = getStringList(l2, i);

		if (i == 0) {
			assert(!strcmp("first value", s));
		}
		else if (i == 1) {
			assert(!strcmp("second value", s));
		}
		else {
			assert(0);
		}
	}
}

test_setCString_replace.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Schema-driven replacement of an already existing (non-multi-valued)
// property 
void test_setCString_replace() {
	SDOFACTORY df = dfAlloc("setCString_replace");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns:t=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"testSetCStringReplace\">\n"
		"    <xsd:sequence>\n"
		"      <xsd:element name=\"prop\"/>\n"
		"    </xsd:sequence>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df,
		"http://www.example.com", "testSetCStringReplace");

	setCStringByName(d, "prop", "whatever");
	// supposed to override value just set:
	setCStringByName(d, "prop", "something else");

	SDOPROPERTY p = getTypePropertyByName(getType(d), "prop");
	char * s = getCString(d, p);

	assert(!strcmp("something else", s));
}

test_setCString_append.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Schema-driven append of a multi-valued property
void test_setCString_append() {
	SDOFACTORY df = dfAlloc("setCString_append");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns:t=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"testSetCStringAppend\">\n"
		"    <xsd:sequence>\n"
		"      <xsd:element name=\"prop\" maxOccurs=\"unbounded\"/>\n"
		"    </xsd:sequence>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df,
		"http://www.example.com", "testSetCStringAppend");

	setCStringByName(d, "prop", "whatever");
	// supposed to get appended after value just set:
	setCStringByName(d, "prop", "something else");

	SDOPROPERTY p = getTypePropertyByName(getType(d), "prop");
	SDOLIST l = getList(d, p);

	assert(2 == getCountList(l));
	assert(!strcmp("whatever", getStringList(l, 0)));
	assert(!strcmp("something else", getStringList(l, 1)));
}

test_setCString_insert.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Insertion before another element supposed to come after it 
// in the type's content model
void test_setCString_insert() {
	SDOFACTORY df = dfAlloc("setCString_insert");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns:t=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"testSetCStringInsert\">\n"
		"    <xsd:sequence>\n"
		"      <xsd:element name=\"prop\"/>\n"
		"      <xsd:element name=\"otherelmt\"/>\n"
		"    </xsd:sequence>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df,
		"http://www.example.com", "testSetCStringInsert");

	setCStringByName(d, "otherelmt", "somevalue");
	setCStringByName(d, "prop", "whatever");
	
	assert(!strcmp("whatever", getCStringByName(d, "prop")));

	// serialize node to string then check relative
	// positions of "prop>" vs "otherelmt>"
	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, d, 0, "testSetCStringInsert");
	char buffer[512];
	sdoSave(doc, buffer, 0, 0);
	char *prop_pos = strstr(buffer, "prop>");
	char *otherelmt_pos = strstr(buffer, "otherelmt>");
	assert(prop_pos && otherelmt_pos && prop_pos < otherelmt_pos);
}

test_setCString_attribute.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Schema-driven setting of an attribute property 
void test_setCString_attribute() {
	SDOFACTORY df = dfAlloc("setCString_attribute");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns:t=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"testSetCStringAttribute\">\n"
		"    <xsd:attribute name=\"prop\" type=\"xsd:string\" use=\"optional\"/>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df,
		"http://www.example.com", "testSetCStringAttribute");

	setCStringByName(d, "prop", "whatever");

	SDOPROPERTY p = getTypePropertyByName(getType(d), "prop");
	char * s = getCString(d, p);

	assert(!strcmp("whatever", s));
}

test_setCString_replaceattribute.c

#include "SDO.h"
#include "string.h"
#include "assert.h"


// Schema-driven replacement of an attribute property 
void test_setCString_replaceattribute() {
	SDOFACTORY df = dfAlloc("setCString_replaceattribute");

	stypeDefine(df,
		"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                "            targetNamespace=\"http://www.example.com\"\n"
                "            xmlns:t=\"http://www.example.com\">\n"
		"  <xsd:complexType name=\"testSetCStringReplaceAttribute\">\n"
		"    <xsd:attribute name=\"prop\" type=\"xsd:string\" use=\"optional\"/>\n"
		"  </xsd:complexType>\n"
		"</xsd:schema>\n");

	DATAOBJECT d = doAlloc(df,
		"http://www.example.com", "testSetCStringReplaceAttribute");

	setCStringByName(d, "prop", "whatever");
	// supposed to override value just set:
	setCStringByName(d, "prop", "something else");

	SDOPROPERTY p = getTypePropertyByName(getType(d), "prop");
	char * s = getCString(d, p);

	assert(!strcmp("something else", s));
}