File: processing-queries.html

package info (click to toggle)
libgda2 1.2.4-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 13,724 kB
  • ctags: 6,979
  • sloc: ansic: 47,166; xml: 19,896; sh: 8,797; makefile: 863; yacc: 202; lex: 129; cpp: 73
file content (246 lines) | stat: -rw-r--r-- 12,234 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Processing queries</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2">
<link rel="start" href="index.html" title="GNOME Data Access manual">
<link rel="up" href="index.html" title="GNOME Data Access manual">
<link rel="prev" href="connections.html" title="Connecting">
<link rel="next" href="data-model.html" title="Managing data models">
<meta name="generator" content="GTK-Doc V1.10 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="chapter" href="introduction.html" title="Introduction">
<link rel="chapter" href="architecture.html" title="libgda architecture">
<link rel="chapter" href="installation.html" title="Installation">
<link rel="chapter" href="connecting.html" title="Beginning">
<link rel="chapter" href="processing-queries.html" title="Processing queries">
<link rel="chapter" href="ch06.html" title="Transactions and batch processes">
<link rel="chapter" href="managing-errors.html" title="Managing errors">
<link rel="chapter" href="main_example.html" title="Full example">
<link rel="chapter" href="migration.html" title="Some formulae for migration from old version">
<link rel="chapter" href="libgda-api.html" title="Client API Reference">
<link rel="chapter" href="libgda-providers.html" title="GDA Providers">
<link rel="chapter" href="libgda-xql.html" title="XML Queries">
<link rel="chapter" href="libgda-reports.html" title="GDA Report Engine">
<link rel="appendix" href="fdl.html" title="Appendix A. GNU Free Documentation License">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="connections.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td> </td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GNOME Data Access manual</th>
<td><a accesskey="n" href="data-model.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="chapter" lang="en">
<div class="titlepage"><div><div><h2 class="title">
<a name="processing-queries"></a>Processing queries</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="sect1"><a href="processing-queries.html#id446644">Executing sentences</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="processing-queries.html#building-commands">Building commands</a></span></dt>
<dt><span class="sect2"><a href="processing-queries.html#making-queries">Making <span class="emphasis"><em>non queries</em></span></a></span></dt>
<dt><span class="sect2"><a href="processing-queries.html#normal-queries">Making normal queries</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="data-model.html">Managing <span class="emphasis"><em>data models</em></span></a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="data-model.html#data-model-table-access">Example using direct cell access</a></span></dt>
<dt><span class="sect2"><a href="data-model.html#data-model-row-access">Example using row access</a></span></dt>
<dt><span class="sect2"><a href="data-model.html#data-model-free">Freeing data models</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="manage-gdavalue.html">Managing values</a></span></dt>
</dl></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id446644"></a>Executing sentences</h2></div></div></div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="building-commands"></a>Building commands</h3></div></div></div>
<p>
          Before invoking a query you have to build the structure containing the
          command and you can do this with <a class="link" href="libgda-gda-command.html#gda-command-new" title="gda_command_new ()">
          <span class="emphasis"><em>gda_command_new ()</em></span></a>.
        </p>
<p>
          The command type we most commonly use is <a class="link" href="libgda-gda-command.html#GdaCommandType" title="enum GdaCommandType">GDA_COMMAND_TYPE_SQL</a> because we will only
          focus on <a href="http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=SQL"><span class="acronym">SQL</span></a> queries<sup>[<a name="id446679" href="#ftn.id446679" class="footnote">3</a>]</sup>
        </p>
<div class="programlistingco">
<pre class="programlisting">
          typedef enum {
                  GDA_COMMAND_OPTION_IGNORE_ERRORS  = 1,
                  GDA_COMMAND_OPTION_STOP_ON_ERRORS = 1 &lt;&lt; 1,
                  GDA_COMMAND_OPTION_BAD_OPTION     = 1 &lt;&lt; 2
          } <a class="link" href="libgda-gda-command.html#GdaCommandOptions" title="enum GdaCommandOptions">GdaCommandOptions</a>;
          </pre>
<div class="calloutlist"><table border="0" summary="Callout list">
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/1.png" alt="1" border="0"></p></td>
<td valign="top" align="left"><p>
                        Ignores all errors and executes all sentences returning data models.
                For failed sentences, it returns an empty data model.
              </p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/2.png" alt="2" border="0"></p></td>
<td valign="top" align="left"><p>
                Stops when finding and error and doesn't return data models.
              </p></td>
</tr>
</table></div>
</div>
<p>
          Here you see an example of creating a command:
        </p>
<div class="programlistingco">
<pre class="programlisting">
          gint
          execute_sql_non_query (GdaConnection *connection, const gchar * buffer)
          {
            GdaCommand *command;
            gint number;
          
            command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
            number  = gda_connection_execute_non_query (connection, command, NULL);
          
            gda_command_free (command);
          
            return (number);
          }
          </pre>
<div class="calloutlist"><table border="0" summary="Callout list">
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/1.png" alt="1" border="0"></p></td>
<td valign="top" align="left"><p>
                Our function. You can give it several comma-separated sentences.
              </p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/2.png" alt="2" border="0"></p></td>
<td valign="top" align="left"><p>
                We will see it <a class="link" href="processing-queries.html#making-queries" title="Making non queries">later</a>.
              </p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/3.png" alt="3" border="0"></p></td>
<td valign="top" align="left"><p>
                It is a good practice to free the commands.
              </p></td>
</tr>
</table></div>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="making-queries"></a>Making <span class="emphasis"><em>non queries</em></span>
</h3></div></div></div>
<p>
          <span class="emphasis"><em>Non queries</em></span> are queries that does not return data, only the
          number of rows affected, as a DELETE or an UPDATE. We use <a class="link" href="libgda-GdaConnection.html#gda-connection-execute-non-query" title="gda_connection_execute_non_query ()"><span class="emphasis"><em>
          gda_connection_execute_non_query()</em></span></a>
        </p>
<pre class="programlisting">
        gint
        execute_sql_non_query (GdaConnection *connection, const gchar * buffer)
        {
          GdaCommand *command;
          gint number;
        
          command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
          number  = gda_connection_execute_non_query (connection, command, NULL);
        
          gda_command_free (command);
        
          return (number);
        }
        </pre>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="normal-queries"></a>Making normal queries</h3></div></div></div>
<p>
          Normal queries are queries that return data (<a class="link" href="data-model.html" title="Managing data models">data models</a>). You have two ways to do this:
        </p>
<div class="itemizedlist"><ul type="disc">
<li><p>
            <a class="link" href="libgda-GdaConnection.html#gda-connection-execute-single-command" title="gda_connection_execute_single_command ()">
              gda_data_model_execute_single_command()</a></p></li>
<li><p>
            <a class="link" href="libgda-GdaConnection.html#gda-connection-execute-command" title="gda_connection_execute_command ()">
              gda_data_model_execute_command()</a></p></li>
</ul></div>
<p>
          You can use the first way when you want to invoke only a single command.
          Second way is used to execute several comma-separated sentences. It is recommended
          to use <a class="link" href="libgda-GdaConnection.html#gda-connection-execute-single-command" title="gda_connection_execute_single_command ()">
          gda_connection_execute_single_command ()</a>. Here you see an
          example:
        </p>
<div class="programlistingco">
<pre class="programlisting">
          gboolean
          execute_sql_command (GdaConnection *connection, const gchar * buffer)
          {
            GdaCommand *command;
            GList *list;
            GList *node;
            gboolean errors=FALSE;
          
            GdaDataModel *dm;
          
          
            command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
            list = gda_connection_execute_command (connection, command, NULL);
            if (list!=NULL)
              for (node=g_list_first(list); node != NULL; node=g_list_next(node))
                {
                  dm=(GdaDataModel *) node-&gt;data;
                  if (dm == NULL)
                    {
                      errors=TRUE;
                    }
                  else
                    {
                      show_table (dm);
                      g_object_unref(dm);
                    }
                }
            else
              {
                errors=TRUE;
              }
            gda_command_free (command);
          
            return (errors);
          }
          </pre>
<div class="calloutlist"><table border="0" summary="Callout list">
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/1.png" alt="1" border="0"></p></td>
<td valign="top" align="left"><p>
                Executes the query and obtains a list of <a class="link" href="data-model.html" title="Managing data models">data models</a>
              </p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><img src="images/callouts/2.png" alt="2" border="0"></p></td>
<td valign="top" align="left"><p>
                Loop for moving through the list of data models. If you use <a class="link" href="libgda-GdaConnection.html#gda-connection-execute-single-command" title="gda_connection_execute_single_command ()">
                gda_connection_execute_single_command()</a>, you should not need to use a loop,
                because this function would return a <a class="link" href="data-model.html" title="Managing data models">data model</a>.
              </p></td>
</tr>
</table></div>
</div>
</div>
</div>
<div class="footnotes">
<br><hr width="100" align="left">
<div class="footnote"><p><sup>[<a name="ftn.id446679" href="#id446679" class="para">3</a>] </sup>There are other command types, as <a href="http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=XML"><span class="acronym">XML</span></a> and so on.</p></div>
</div>
</div>
<div class="footer">
<hr>
          Generated by GTK-Doc V1.10</div>
</body>
</html>