OCILIB (C Driver for Oracle) 3.9.1
D:/Perso/dev/ocilib/ocilib/src/agent.c
00001 /*
00002     +-----------------------------------------------------------------------------------------+
00003     |                                                                                         |
00004     |                               OCILIB - C Driver for Oracle                              |
00005     |                                                                                         |
00006     |                                (C Wrapper for Oracle OCI)                               |
00007     |                                                                                         |
00008     |                              Website : http://www.ocilib.net                            |
00009     |                                                                                         |
00010     |             Copyright (c) 2007-2011 Vincent ROGIER <vince.rogier@ocilib.net>            |
00011     |                                                                                         |
00012     +-----------------------------------------------------------------------------------------+
00013     |                                                                                         |
00014     |             This library is free software; you can redistribute it and/or               |
00015     |             modify it under the terms of the GNU Lesser General Public                  |
00016     |             License as published by the Free Software Foundation; either                |
00017     |             version 2 of the License, or (at your option) any later version.            |
00018     |                                                                                         |
00019     |             This library is distributed in the hope that it will be useful,             |
00020     |             but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00021     |             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           |
00022     |             Lesser General Public License for more details.                             |
00023     |                                                                                         |
00024     |             You should have received a copy of the GNU Lesser General Public            |
00025     |             License along with this library; if not, write to the Free                  |
00026     |             Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.          |
00027     |                                                                                         |
00028     +-----------------------------------------------------------------------------------------+
00029 */
00030 
00031 /* --------------------------------------------------------------------------------------------- *
00032  * $Id: agent.c, v 3.9.1 2011-07-08 00:00 Vincent Rogier $
00033  * --------------------------------------------------------------------------------------------- */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ********************************************************************************************* *
00038  *                             PRIVATE FUNCTIONS
00039  * ********************************************************************************************* */
00040 
00041 /* --------------------------------------------------------------------------------------------- *
00042  * OCI_AgentInit
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 OCI_Agent * OCI_AgentInit
00046 (
00047     OCI_Connection *con,
00048     OCI_Agent     **pagent,
00049     OCIAQAgent     *handle,
00050     const mtext    *name,
00051     const mtext    *address
00052 )
00053 {
00054     OCI_Agent *agent = NULL;
00055     boolean res      = TRUE;
00056 
00057     OCI_CHECK(pagent == NULL, NULL);
00058 
00059     /* allocate agent structure */
00060 
00061     if (*pagent == NULL)
00062     {
00063         *pagent = (OCI_Agent *) OCI_MemAlloc(OCI_IPC_AGENT, sizeof(*agent),  (size_t) 1, TRUE);
00064     }
00065 
00066     if (*pagent != NULL)
00067     {
00068         agent = *pagent;
00069 
00070         /* reinit */
00071 
00072         OCI_FREE(agent->name);
00073         OCI_FREE(agent->address);
00074 
00075         agent->con    = con;
00076         agent->handle = handle;
00077 
00078         if (handle == NULL)
00079         {
00080             agent->hstate = OCI_OBJECT_ALLOCATED;
00081 
00082             res = (OCI_SUCCESS == OCI_DescriptorAlloc((dvoid * ) agent->con->env,
00083                                                       (dvoid **) &agent->handle,
00084                                                       OCI_DTYPE_AQAGENT,
00085                                                       (size_t) 0, (dvoid **) NULL));
00086         }
00087         else
00088         {
00089             agent->hstate = OCI_OBJECT_FETCHED_CLEAN;
00090         }
00091 
00092         /* set name attribute if provided */
00093 
00094         if ((res == TRUE) && (name != NULL) && (name[0] != 0))
00095         {
00096             res = OCI_AgentSetName(agent, name);
00097         }
00098 
00099         /* set address attribute if provided */
00100 
00101         if ((res == TRUE) && (address != NULL) && (address[0] != 0))
00102         {
00103             res = OCI_AgentSetAddress(agent, address);
00104         }
00105     }
00106     else
00107     {
00108         res = FALSE;
00109     }
00110 
00111     /* check for failure */
00112 
00113     if (res == FALSE)
00114     {
00115         OCI_AgentFree(agent);
00116         agent = NULL;
00117     }
00118 
00119     return agent;
00120 }
00121 
00122 /* ********************************************************************************************* *
00123  *                            PUBLIC FUNCTIONS
00124  * ********************************************************************************************* */
00125 
00126 /* --------------------------------------------------------------------------------------------- *
00127  * OCI_AgentCreate
00128  * --------------------------------------------------------------------------------------------- */
00129 
00130 OCI_Agent * OCI_API OCI_AgentCreate
00131 (
00132     OCI_Connection *con,
00133     const mtext    *name,
00134     const mtext    *address
00135 )
00136 {
00137     OCI_Agent *agent = NULL;
00138 
00139     OCI_CHECK_INITIALIZED(NULL);
00140 
00141     OCI_CHECK_PTR(OCI_IPC_CONNECTION, con, NULL);
00142 
00143     agent = OCI_AgentInit(con, &agent, NULL, name, address);
00144 
00145     OCI_RESULT(agent != NULL);
00146 
00147     return agent;
00148 }
00149 
00150 /* --------------------------------------------------------------------------------------------- *
00151  * OCI_AgentFree
00152  * --------------------------------------------------------------------------------------------- */
00153 
00154 boolean OCI_API OCI_AgentFree
00155 (
00156     OCI_Agent *agent
00157 )
00158 {
00159     boolean res = TRUE;
00160 
00161     OCI_CHECK_PTR(OCI_IPC_AGENT, agent, FALSE);
00162 
00163     if (agent->hstate == OCI_OBJECT_ALLOCATED)
00164     {
00165         OCI_DescriptorFree((dvoid *) agent->handle, OCI_DTYPE_AQAGENT);
00166     }
00167 
00168     OCI_FREE(agent->address);
00169     OCI_FREE(agent->name);
00170 
00171     OCI_FREE(agent);
00172 
00173     OCI_RESULT(res);
00174 
00175     return res;
00176 }
00177 
00178 /* --------------------------------------------------------------------------------------------- *
00179  * OCI_AgentGetName
00180  * --------------------------------------------------------------------------------------------- */
00181 
00182 const mtext * OCI_API OCI_AgentGetName
00183 (
00184     OCI_Agent *agent
00185 )
00186 {
00187     boolean res = TRUE;
00188 
00189     OCI_CHECK_PTR(OCI_IPC_AGENT, agent, NULL);
00190 
00191     if (agent->name == NULL)
00192     {
00193         res = OCI_StringGetFromAttrHandle(agent->con, agent->handle,  OCI_DTYPE_AQAGENT,
00194                                           OCI_ATTR_AGENT_NAME,  &agent->name);
00195     }
00196 
00197     OCI_RESULT(res);
00198 
00199     return agent->name;
00200 }
00201 
00202 /* --------------------------------------------------------------------------------------------- *
00203  * OCI_AgentSetName
00204  * --------------------------------------------------------------------------------------------- */
00205 
00206 boolean OCI_API OCI_AgentSetName
00207 (
00208     OCI_Agent   *agent,
00209     const mtext *name
00210 )
00211 {
00212     boolean res = TRUE;
00213 
00214     OCI_CHECK_PTR(OCI_IPC_AGENT, agent, FALSE);
00215 
00216     res =  OCI_StringSetToAttrHandle(agent->con, agent->handle,  OCI_DTYPE_AQAGENT,
00217                                      OCI_ATTR_AGENT_NAME, &agent->name, name);
00218 
00219     OCI_RESULT(res);
00220 
00221     return res;
00222 }
00223 
00224 /* --------------------------------------------------------------------------------------------- *
00225  * OCI_AgentGetAddress
00226  * --------------------------------------------------------------------------------------------- */
00227 
00228 const mtext * OCI_API OCI_AgentGetAddress
00229 (
00230     OCI_Agent *agent
00231 )
00232 {
00233     boolean res = TRUE;
00234 
00235     OCI_CHECK_PTR(OCI_IPC_AGENT, agent, NULL);
00236 
00237     if (agent->name == NULL)
00238     {
00239         res = OCI_StringGetFromAttrHandle(agent->con, agent->handle, OCI_DTYPE_AQAGENT,
00240                                           OCI_ATTR_AGENT_ADDRESS, &agent->address);
00241     }
00242 
00243     OCI_RESULT(res);
00244 
00245     return agent->address;
00246 }
00247 
00248 /* --------------------------------------------------------------------------------------------- *
00249  * OCI_AgentSetAddress
00250  * --------------------------------------------------------------------------------------------- */
00251 
00252 boolean OCI_API OCI_AgentSetAddress
00253 (
00254     OCI_Agent   *agent,
00255     const mtext *address
00256 )
00257 {
00258     boolean res = TRUE;
00259 
00260     OCI_CHECK_PTR(OCI_IPC_AGENT, agent, FALSE);
00261 
00262     res = OCI_StringSetToAttrHandle(agent->con, agent->handle, OCI_DTYPE_AQAGENT,
00263                                     OCI_ATTR_AGENT_ADDRESS, &agent->address, address);
00264 
00265     OCI_RESULT(res);
00266 
00267     return res;
00268 }