forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcxoObjectAttr.c
151 lines (128 loc) · 5.04 KB
/
cxoObjectAttr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//-----------------------------------------------------------------------------
// Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// cxoObjectAttr.c
// Defines the routines for handling attributes of Oracle types.
//-----------------------------------------------------------------------------
#include "cxoModule.h"
//-----------------------------------------------------------------------------
// cxoObjectAttr_initialize()
// Initialize the new object attribute.
//-----------------------------------------------------------------------------
static int cxoObjectAttr_initialize(cxoObjectAttr *attr,
cxoConnection *connection)
{
dpiObjectAttrInfo info;
if (dpiObjectAttr_getInfo(attr->handle, &info) < 0)
return cxoError_raiseAndReturnInt();
attr->transformNum = cxoTransform_getNumFromDataTypeInfo(&info.typeInfo);
attr->dbType = cxoDbType_fromTransformNum(attr->transformNum);
if (!attr->dbType)
return -1;
Py_INCREF(attr->dbType);
attr->oracleTypeNum = info.typeInfo.oracleTypeNum;
attr->name = PyUnicode_Decode(info.name, info.nameLength,
connection->encodingInfo.encoding, NULL);
if (!attr->name)
return -1;
if (info.typeInfo.objectType) {
attr->objectType = cxoObjectType_new(connection,
info.typeInfo.objectType);
if (!attr->objectType)
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoObjectAttr_new()
// Allocate a new object attribute.
//-----------------------------------------------------------------------------
cxoObjectAttr *cxoObjectAttr_new(cxoConnection *connection,
dpiObjectAttr *handle)
{
cxoObjectAttr *attr;
attr = (cxoObjectAttr*)
cxoPyTypeObjectAttr.tp_alloc(&cxoPyTypeObjectAttr, 0);
if (!attr) {
dpiObjectAttr_release(handle);
return NULL;
}
attr->handle = handle;
if (cxoObjectAttr_initialize(attr, connection) < 0) {
Py_DECREF(attr);
return NULL;
}
return attr;
}
//-----------------------------------------------------------------------------
// cxoObjectAttr_free()
// Free the memory associated with an object attribute.
//-----------------------------------------------------------------------------
static void cxoObjectAttr_free(cxoObjectAttr *attr)
{
if (attr->handle) {
dpiObjectAttr_release(attr->handle);
attr->handle = NULL;
}
Py_CLEAR(attr->name);
Py_CLEAR(attr->objectType);
Py_CLEAR(attr->dbType);
Py_TYPE(attr)->tp_free((PyObject*) attr);
}
//-----------------------------------------------------------------------------
// cxoObjectAttr_getType()
// Return the type associated with the attribute. This is either an object
// type or one of the database type constants.
//-----------------------------------------------------------------------------
static PyObject *cxoObjectAttr_getType(cxoObjectAttr *attr, void *unused)
{
if (attr->objectType) {
Py_INCREF(attr->objectType);
return (PyObject*) attr->objectType;
}
Py_INCREF(attr->dbType);
return (PyObject*) attr->dbType;
}
//-----------------------------------------------------------------------------
// cxoObjectAttr_repr()
// Return a string representation of the object attribute.
//-----------------------------------------------------------------------------
static PyObject *cxoObjectAttr_repr(cxoObjectAttr *attr)
{
PyObject *module, *name, *result;
if (cxoUtils_getModuleAndName(Py_TYPE(attr), &module, &name) < 0)
return NULL;
result = cxoUtils_formatString("<%s.%s %s>",
PyTuple_Pack(3, module, name, attr->name));
Py_DECREF(module);
Py_DECREF(name);
return result;
}
//-----------------------------------------------------------------------------
// declaration of members
//-----------------------------------------------------------------------------
static PyMemberDef cxoMembers[] = {
{ "name", T_OBJECT, offsetof(cxoObjectAttr, name), READONLY },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of calculated members
//-----------------------------------------------------------------------------
static PyGetSetDef cxoCalcMembers[] = {
{ "type", (getter) cxoObjectAttr_getType, 0, 0, 0 },
{ NULL }
};
//-----------------------------------------------------------------------------
// Python type declaration
//-----------------------------------------------------------------------------
PyTypeObject cxoPyTypeObjectAttr = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "cx_Oracle.ObjectAttribute",
.tp_basicsize = sizeof(cxoObjectAttr),
.tp_dealloc = (destructor) cxoObjectAttr_free,
.tp_repr = (reprfunc) cxoObjectAttr_repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_members = cxoMembers,
.tp_getset = cxoCalcMembers
};