コード例 #1
0
ファイル: sedna.c プロジェクト: rolftimmermans/sedna-ruby
// Iterate over all records and add them to a Ruby Array.
static VALUE sedna_get_results(SC *conn)
{
	int res, strip_n = 0;
	// Can be replaced with: rb_funcall(cSednaSet, rb_intern("new"), 0, NULL);
	VALUE set = rb_ary_new();

	while((res = SEnext(conn)) != SEDNA_RESULT_END) {
		if(res == SEDNA_ERROR) sedna_err(conn, res);
		// Set strip_n to 1 for all results except the first. This will cause
		// sedna_read() an incorrect newline that is prepended to these results.
		rb_ary_push(set, sedna_read(conn, strip_n));
		if(!strip_n) strip_n = 1;
	};

	return set;
}
コード例 #2
0
ファイル: Client.c プロジェクト: bitkeeper/sedna
int main()
{
    struct SednaConnection conn = SEDNA_CONNECTION_INITIALIZER;
    int bytes_read, res, value;
    char buf[1024];

    const char* url = "localhost";
    const char* db_name = "testdb";
    const char* login = "******";
    const char* password = "******";

    printf("Client started.\n");
    
    value = SEDNA_AUTOCOMMIT_OFF;
    res = SEsetConnectionAttr(&conn, SEDNA_ATTR_AUTOCOMMIT, (void*)&value, sizeof(int));

    //connecting to database "testdb" with login "SYSTEM", password "MANAGER"
    res = SEconnect(&conn, url, db_name, login, password);
    if(res != SEDNA_SESSION_OPEN) 
    {
        printf("Session starting failed: \n%s\n", SEgetLastErrorMsg(&conn));
		return -1;
    }

    //begin transaction
    res = SEbegin(&conn);
    if(res != SEDNA_BEGIN_TRANSACTION_SUCCEEDED) 
    {
        printf("Begin transaction failed: \n%s\n", SEgetLastErrorMsg(&conn));
        //closing session
        SEclose(&conn);
        return -1;
    }

    // load data from file "region.xml" into the document "region"
    res = SEexecute(&conn, "LOAD \"..\\data\\region.xml\" \"region\""); 
    if(res != SEDNA_BULK_LOAD_SUCCEEDED) 
    {
        printf("Bulk load failed: \n%s\n", SEgetLastErrorMsg(&conn));
        // closing session
        SEclose(&conn);
        return -1;
    }

    // execute XQuery query	
    res = SEexecute(&conn, "doc(\"region\")/*/*");
    if(res != SEDNA_QUERY_SUCCEEDED) 
    {
        printf("Query failed: \n%s\n", SEgetLastErrorMsg(&conn));
        //closing session
        SEclose(&conn);
        return -1;
    }
 
    // iterate over the result sequence and retrieve the result data
    while((res = SEnext(&conn)) != SEDNA_RESULT_END)
    {
        if (res == SEDNA_ERROR)
        {
            printf("Failed to get next result item from server: \n%s\n", SEgetLastErrorMsg(&conn));
            //closing session
            SEclose(&conn);
            return -1;
        }

        printf("\nresult item:\n");
        do
        {
            bytes_read = SEgetData(&conn, buf, 1024 - 1);
            if(bytes_read == SEDNA_ERROR)
            {
                printf("Failed to get result data from server: \n%s\n", SEgetLastErrorMsg(&conn));
                //closing session
                SEclose(&conn);
                return -1;
            }
           	buf[bytes_read] = '\0';
         	printf("%s", buf);

        }while(bytes_read > 0);
    	printf("\n");
    }

    // Drop document "region"
    res = SEexecute(&conn, "DROP DOCUMENT \"region\""); 
    if(res != SEDNA_UPDATE_SUCCEEDED) 
    {
        printf("Drop document failed: \n%s\n", SEgetLastErrorMsg(&conn));
        // closing session
        SEclose(&conn);
        return -1;
    }

    //commiting transaction
    res = SEcommit(&conn);
    if(res != SEDNA_COMMIT_TRANSACTION_SUCCEEDED) 
    {
        printf("Commit transaction failed: \n%s\n", SEgetLastErrorMsg(&conn));
        //closing session
        SEclose(&conn);
        return -1;
    }
	
    //closing session
    res = SEclose(&conn);
    if(res != SEDNA_SESSION_CLOSED) 
    {
        printf("An error occured while trying to close session: \n%s\n", SEgetLastErrorMsg(&conn));
        return -1;
    }
    
    return 0;
}