⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 group__g__usertypes.html

📁 oci的源码,可以在任何平台上编译,相当方便实用
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>OCILIB (C Driver for Oracle): Oracle Named Types (Oracle OBJECTs)</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.4 --><div class="tabs">  <ul>    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>    <li><a href="modules.html"><span>Modules</span></a></li>    <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>    <li><a href="files.html"><span>Files</span></a></li>  </ul></div><h1>Oracle Named Types (Oracle OBJECTs)</h1><hr><a name="_details"></a><h2>Detailed Description</h2>OCILIB implements Oracle Named types (user types and built-in types) through the <a class="el" href="struct_o_c_i___object.html" title="Oracle Named types representation.">OCI_Object</a> type.<p>OTT and C structures are not required to use objects in OCILIB.<p>In order to manipulate objects attributes, OCILIB proposes a set of functions to get/set properties for various supported types.<p>Objects can be:<ul><li>Created as standalone instances (transient objects)</li><li>Used for binding (persistent / transient objects)</li><li>Retrieved from select statements (persistent / embedded objects)</li></ul><p>References (Oracle type REF) are identifiers (smart pointers) to objects and are implemented in OCILIB with the type <a class="el" href="struct_o_c_i___ref.html" title="Oracle REF type representation.">OCI_Ref</a>.<p>OCILIB implements Oracle REFs as strong typed Reference (underlying OCI REFs are weaker in terms of typing). It means it's mandatory to provide type information to:<ul><li>create a local <a class="el" href="struct_o_c_i___ref.html" title="Oracle REF type representation.">OCI_Ref</a> handle.</li><li>register an <a class="el" href="struct_o_c_i___ref.html" title="Oracle REF type representation.">OCI_Ref</a> handle for a "return into" clause.</li></ul><p><dl class="note" compact><dt><b>Note:</b></dt><dd>See Oracle Database SQL Language Reference for more details about REF datatype</dd></dl><dl class="warning" compact><dt><b>Warning:</b></dt><dd>There is a known bug in Oracle OCI when setting an object attribute if OCI is initialized in Unicode mode (UTF16). This bug has been marked as fixed for in the current Oracle 12g development status. So, DO NOT try to set Objects attributes in Unicode builds with versions &lt;= 11g because OCI will overwrite internal buffers and later calls to object attributes handles will lead to an OCI crash.</dd></dl><dl class="user" compact><dt><b>Example : Inserting a local object into a table</b></dt><dd><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "ocilib.h"</span><span class="comment">/* </span><span class="comment">    DML for the test </span><span class="comment"></span><span class="comment">    create type t_vendor as object</span><span class="comment">    ( </span><span class="comment">        code  number, </span><span class="comment">        name  varchar2(30)</span><span class="comment">    ); </span><span class="comment"></span><span class="comment">    create type t_sale as object </span><span class="comment">    ( </span><span class="comment">        code  number, </span><span class="comment">        price  float, </span><span class="comment">        name  varchar2(30),</span><span class="comment">        ref  varchar2(30), </span><span class="comment">        date_sale date, </span><span class="comment">        vendor  t_vendor</span><span class="comment">    ); </span><span class="comment"></span><span class="comment">    create table sales(item t_sale);</span><span class="comment"></span><span class="comment">*/</span><span class="keywordtype">int</span> main(<span class="keywordtype">void</span>){    <a class="code" href="struct_o_c_i___connection.html" title="Oracle physical connection.">OCI_Connection</a> *cn;    <a class="code" href="struct_o_c_i___statement.html" title="Oracle SQL or PL/SQL statement.">OCI_Statement</a> *st;    <a class="code" href="struct_o_c_i___object.html" title="Oracle Named types representation.">OCI_Object</a> *obj, *obj2;    <a class="code" href="struct_o_c_i___date.html" title="Oracle internal date representation.">OCI_Date</a> *date;    <span class="keywordflow">if</span> (!<a class="code" href="group__g__init.html#gcdb642d75f7c8478e083634144bc813c" title="Initializes the library.">OCI_Initialize</a>(err_handler, NULL, OCI_ENV_DEFAULT))        <span class="keywordflow">return</span> EXIT_FAILURE;    cn = <a class="code" href="group__g__connect.html#gdb6e2cdf759587dd81c2fda7c5f44338" title="Create a physical connection to an Oracle database server.">OCI_ConnectionCreate</a>(<span class="stringliteral">"db"</span>, <span class="stringliteral">"usr"</span>, <span class="stringliteral">"pwd"</span>, OCI_SESSION_DEFAULT);    st = <a class="code" href="group__g__exec.html#g335822f983af0fb5c529431f06a9a17b" title="Create a statement object and return its handle.">OCI_StatementCreate</a>(cn);    obj  = <a class="code" href="group__g__usertypes.html#g5e8f1db675e2b4fe74c82bdc79be8294" title="Create a local object instance.">OCI_ObjectCreate</a>(cn, OCI_SchemaGet(cn, <span class="stringliteral">"t_sale"</span>, OCI_SCHEMA_TYPE));    <a class="code" href="group__g__usertypes.html#g147c7a4197929f725aabe4bfb4e42a52" title="Set an object attribute of type int.">OCI_ObjectSetInt</a>(obj, <span class="stringliteral">"CODE"</span>, 1);    <a class="code" href="group__g__usertypes.html#gbdd2fa4bd5d4b019087d8a9aa9e21a2a" title="Set an object attribute of type double.">OCI_ObjectSetDouble</a>(obj, <span class="stringliteral">"PRICE"</span>, 12.99);    <a class="code" href="group__g__usertypes.html#g93eb03a281c7a798bc804eb4a3496799" title="Set an object attribute of type string.">OCI_ObjectSetString</a>(obj, <span class="stringliteral">"NAME"</span>, <span class="stringliteral">"USB KEY 2go"</span>);    <a class="code" href="group__g__usertypes.html#g93eb03a281c7a798bc804eb4a3496799" title="Set an object attribute of type string.">OCI_ObjectSetString</a>(obj, <span class="stringliteral">"REF"</span>, <span class="stringliteral">"A56547WSAA"</span>);    date = <a class="code" href="group__g__usertypes.html#g482b6b2454951874790451be6bc810f7" title="Return the date value of the given object attribute.">OCI_ObjectGetDate</a>(obj, <span class="stringliteral">"DATE_SALE"</span>);    <a class="code" href="group__g__date.html#gd7c5c65d88b52e6ecb46b3e9ba8843dc" title="Return the current system date/time into the date handle.">OCI_DateSysDate</a>(date);    obj2 = <a class="code" href="group__g__usertypes.html#g89378768a57c72154e464a99a20ac5ae" title="Return the object value of the given object attribute.">OCI_ObjectGetObject</a>(obj, <span class="stringliteral">"VENDOR"</span>);    <a class="code" href="group__g__usertypes.html#g147c7a4197929f725aabe4bfb4e42a52" title="Set an object attribute of type int.">OCI_ObjectSetInt</a>(obj2, <span class="stringliteral">"CODE"</span>, 134);    <a class="code" href="group__g__usertypes.html#g93eb03a281c7a798bc804eb4a3496799" title="Set an object attribute of type string.">OCI_ObjectSetString</a>(obj2, <span class="stringliteral">"NAME"</span>, <span class="stringliteral">"JOHN SMITH"</span>);    <a class="code" href="group__g__exec.html#gd5a7fcffe7d24da001e44636cfc06578" title="Prepare a SQL statement or PL/SQL block.">OCI_Prepare</a>(st, <span class="stringliteral">"insert into sales values(:obj)"</span>);    <a class="code" href="group__g__bind.html#g91d068da1fbd27e81160588e08062940" title="Bind an object (named type) variable.">OCI_BindObject</a>(st, <span class="stringliteral">":obj"</span>, obj);    <a class="code" href="group__g__exec.html#g7189aa353845909aaedc8d5956429450" title="Execute a prepared SQL statement or PL/SQL block.">OCI_Execute</a>(st);    printf(<span class="stringliteral">"\n%d row(s) inserted\n"</span>, <a class="code" href="group__g__exec.html#g1cf932261960da80cd36d650a08565c3" title="Return the number of rows affected by the SQL statement.">OCI_GetAffectedRows</a>(st));    <a class="code" href="group__g__transac.html#gee1ba614ed2dc5bd83bf788ca08f3e71" title="Commit current pending changes.">OCI_Commit</a>(cn);    <a class="code" href="group__g__usertypes.html#g3fd2c9dfdcab2c56400c95c31cc0851e" title="Free a local object.">OCI_ObjectFree</a>(obj);    <a class="code" href="group__g__init.html#g639706aa8e9689c7ebffc018fac6d3ae" title="Clean up all resources allocated by the library.">OCI_Cleanup</a>();    <span class="keywordflow">return</span> EXIT_SUCCESS;}</pre></div></dd></dl><dl class="user" compact><dt><b>Example : Using Object References</b></dt><dd><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "ocilib.h"</span><span class="preprocessor">#define SIZE_STR 100</span><span class="preprocessor"></span><span class="keywordtype">void</span> dump_ref(<a class="code" href="struct_o_c_i___ref.html" title="Oracle REF type representation.">OCI_Ref</a> *ref){    <a class="code" href="struct_o_c_i___object.html" title="Oracle Named types representation.">OCI_Object</a> *obj;    dtext data[SIZE_STR + 1];    <span class="comment">/* print ref hexadecimal value */</span>    <a class="code" href="group__g__usertypes.html#g4634e5657c97065fb952a7e67b5447e8" title="Converts a Ref handle value to a hexadecimal string.">OCI_RefToText</a>(ref, SIZE_STR, data);    printf(<span class="stringliteral">"...Ref Hex value : %s\n"</span>, data);    <span class="comment">/* get object from ref */</span>    obj = <a class="code" href="group__g__usertypes.html#ge101916e55785052645366228f3a8751" title="Returns the object pointed by the Ref handle.">OCI_RefGetObject</a>(ref);    <span class="comment">/* print object values */</span>    printf(<span class="stringliteral">"...%i - %s\n"</span>, <a class="code" href="group__g__usertypes.html#g05bf984246cbaaa6d554af9cfb8d99e9" title="Return the integer value of the given object attribute.">OCI_ObjectGetInt</a>(obj, <span class="stringliteral">"ID"</span>),                            <a class="code" href="group__g__usertypes.html#g95ba08a32cf97b7bed6235cc48bd2300" title="Return the string value of the given object attribute.">OCI_ObjectGetString</a>(obj, <span class="stringliteral">"NAME"</span>));}<span class="keywordtype">int</span> main(<span class="keywordtype">void</span>){    <a class="code" href="struct_o_c_i___connection.html" title="Oracle physical connection.">OCI_Connection</a> *cn;    <a class="code" href="struct_o_c_i___statement.html" title="Oracle SQL or PL/SQL statement.">OCI_Statement</a>  *st;    <a class="code" href="struct_o_c_i___resultset.html" title="Collection of output columns from a select statement.">OCI_Resultset</a>  *rs;    <a class="code" href="struct_o_c_i___ref.html" title="Oracle REF type representation.">OCI_Ref</a>       *ref;    <span class="keywordflow">if</span> (!<a class="code" href="group__g__init.html#gcdb642d75f7c8478e083634144bc813c" title="Initializes the library.">OCI_Initialize</a>(NULL, NULL, OCI_ENV_DEFAULT))        <span class="keywordflow">return</span> EXIT_FAILURE;    cn  = <a class="code" href="group__g__connect.html#gdb6e2cdf759587dd81c2fda7c5f44338" title="Create a physical connection to an Oracle database server.">OCI_ConnectionCreate</a>(<span class="stringliteral">"db"</span>, <span class="stringliteral">"usr"</span>, <span class="stringliteral">"pwd"</span>, OCI_SESSION_DEFAULT);    st  = <a class="code" href="group__g__exec.html#g335822f983af0fb5c529431f06a9a17b" title="Create a statement object and return its handle.">OCI_StatementCreate</a>(cn);    <a class="code" href="group__g__exec.html#gf2163ec44e644cc734c393188707985b" title="Parse and execute a SQL statement or PL/SQL block.">OCI_ExecuteStmt</a>(st, <span class="stringliteral">"select ref(e) from table_obj e"</span>);    rs = <a class="code" href="group__g__fetch.html#gf2a9e28b66a9538ba0ffb62bffb87c16" title="Retrieve the resultset handle from an executed statement.">OCI_GetResultset</a>(st);    printf(<span class="stringliteral">"\n\n=&gt; fetch refs from object table\n\n"</span>);    <span class="keywordflow">while</span> (<a class="code" href="group__g__fetch.html#g36ac26dcea78f6074421781e401f97ba" title="Fetch the next row of the resultset.">OCI_FetchNext</a>(rs))    {        dump_ref(<a class="code" href="group__g__fetch.html#ga9376300d7b7a383e98cacb809677a5d" title="Return the current Ref value of the column at the given index in the resultset.">OCI_GetRef</a>(rs, 1));    }    printf(<span class="stringliteral">"\n\n=&gt; bind a local ref object to a PL/SQL statement\n\n"</span>);    ref = <a class="code" href="group__g__usertypes.html#gdd765dc3c5ebe2f3472c4fc2760e9b29" title="Create a local Ref instance.">OCI_RefCreate</a>(cn, OCI_SchemaGet(cn, <span class="stringliteral">"ARTICLE_T"</span>, OCI_SCHEMA_TYPE));    <a class="code" href="group__g__exec.html#gd5a7fcffe7d24da001e44636cfc06578" title="Prepare a SQL statement or PL/SQL block.">OCI_Prepare</a>(st, <span class="stringliteral">"begin "</span>                    <span class="stringliteral">"  select ref(e) into :r from table_obj e where e.id = 1; "</span>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -