📄 newobj.c
字号:
/*****************************************************************
* *
* Copyright (c) 2001-2007 McObject LLC. All Right Reserved. *
* *
*****************************************************************/
#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "samples.h"
/* This function writes a new object of SimpleClass
* into the database.
*/
int newobj(mco_db_h db, int id0, uint4 vh)
{
MCO_RET rc;
mco_trans_h t;
/*
* SimpleClass and simple_id typedefs are generated by the DDL compiler and
* could be found in simple.h
*/
SimpleClass hClass;
simple_oid id;
char src[64];
int donetrn = 0;
uint2 va = (uint2)(rand() &0x3FF);
uint2 vb = (uint2)(rand() &0x3FFF);
sprintf(src, "abc_%d", id0);
id.seq = id0;
/*
* open a transaction and obtain a transaction handle. This transaction is a
* read_write transaction, ran at the FOREGROUND priority level.
*/
mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
/*
* Allocate a new class and return a class handle. Two input parameters must be
* passed to the new interface: the transaction handle t and class id. If
* successful the output parameter hClass is a pointer to the newly allocated class.
*/
rc = SimpleClass_new(t, &id, &hClass);
if (rc)
{
goto Err;
}
/* assign values, two integers and a string. By this time, we must
* know the string size already. Note that return value is not checked
* with every call. In case of an error, runtime sets the transaction into
* set "error state" and will return the error indication at commit
*/
SimpleClass_a_put(&hClass, va);
SimpleClass_b_put(&hClass, vb);
SimpleClass_c_put(&hClass, src, (uint2)strlen(src));
SimpleClass_h_put(&hClass, vh);
/*
* commit the transaction unless there is a problem and return 1, otherwise
* rollback and return 0
*/
rc = mco_trans_commit(t);
donetrn = 1;
/*
* Important! After the transaction was committed, the class handle is no longer
* valid. Any attempt to reference the created object would result in error
* condition.
*/
if (rc)
{
goto Err;
}
printf("\n\tobject with OID=%d, a=%d, b=%d, c=%s\tinserted okay", id.seq, va, vb, src);
return 1;
Err: printf("\n\terror (%d) inserting object with OID %d", rc, id.seq);
if (!donetrn)
{
mco_trans_rollback(t);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -