CUBRID Engine  latest
query_cl.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * query_cl.c - Query processor main interface
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <stdlib.h>
31 
32 #include "query_cl.h"
33 
34 #include "compile_context.h"
35 #include "optimizer.h"
36 #include "network_interface_cl.h"
37 #include "transaction_cl.h"
38 #include "xasl.h"
39 
40 /*
41  * prepare_query () - Prepares a query for later (and repetitive)
42  * execution
43  * return : Error code
44  * context (in) : query string; used for hash key of the XASL cache
45  * stream (in/out) : XASL stream, size, xasl_id & xasl_header;
46  * set to NULL if you want to look up the XASL cache
47  *
48  * NOTE: If stream->xasl_header is not NULL, also XASL node header will be
49  * requested from server.
50  */
51 int
53 {
54  int ret = NO_ERROR;
55 
56  assert (context->sql_hash_text);
57 
58  /* if QO_PARAM_LEVEL indicate no execution, just return */
60  {
61  return NO_ERROR;
62  }
63 
64  /* allocate XASL_ID, the caller is responsible to free this */
65  stream->xasl_id = (XASL_ID *) malloc (sizeof (XASL_ID));
66  if (stream->xasl_id == NULL)
67  {
70  }
71 
72  /* send XASL stream to the server and get XASL_ID */
73  ret = qmgr_prepare_query (context, stream);
74  if (ret != NO_ERROR)
75  {
76  free_and_init (stream->xasl_id);
77  ASSERT_ERROR ();
78  return ret;
79  }
80 
81  /* if the query is not found in the cache */
82  if (stream->buffer == NULL && stream->xasl_id && XASL_ID_IS_NULL (stream->xasl_id))
83  {
84  free_and_init (stream->xasl_id);
85  }
86 
87  assert (ret == NO_ERROR);
88 
89  return ret;
90 }
91 
92 /*
93  * execute_query () - Execute a prepared query
94  * return: Error code
95  * xasl_id(in) : XASL file id that was a result of prepare_query()
96  * query_idp(out) : query id to be used for getting results
97  * var_cnt(in) : number of host variables
98  * varptr(in) : array of host variables (query input parameters)
99  * list_idp(out) : query result file id (QFILE_LIST_ID)
100  * flag(in) : flag
101  * clt_cache_time(in) :
102  * srv_cache_time(in) :
103  */
104 int
105 execute_query (const XASL_ID * xasl_id, QUERY_ID * query_idp, int var_cnt, const DB_VALUE * varptr,
106  QFILE_LIST_ID ** list_idp, QUERY_FLAG flag, CACHE_TIME * clt_cache_time, CACHE_TIME * srv_cache_time)
107 {
108  int query_timeout;
109  int ret = NO_ERROR;
110 
111  *list_idp = NULL;
112 
113  /* if QO_PARAM_LEVEL indicate no execution, just return */
114  if (qo_need_skip_execution ())
115  {
116  return NO_ERROR;
117  }
118 
119  query_timeout = tran_get_query_timeout ();
120  /* send XASL file id and host variables to the server and get QFILE_LIST_ID */
121  *list_idp =
122  qmgr_execute_query (xasl_id, query_idp, var_cnt, varptr, flag, clt_cache_time, srv_cache_time, query_timeout);
123 
124  if (*list_idp == NULL)
125  {
126  return ((ret = er_errid ()) == NO_ERROR) ? ER_FAILED : ret;
127  }
128 
129  assert (ret == NO_ERROR);
130 
131  return ret;
132 }
133 
134 /*
135  * prepare_and_execute_query () -
136  * return:
137  * stream(in) : packed XASL tree
138  * stream_size(in) : size of stream
139  * query_id(in) :
140  * var_cnt(in) : number of input values for positional variables
141  * varptr(in) : pointer to the array of input values
142  * result(out): pointer to result list id pointer
143  * flag(in) : flag
144  *
145  * Note: Prepares and executes a query, and the result is returned
146  * through a list id (actually the list file).
147  * For csql, var_cnt must be 0 and varptr be NULL.
148  * It is the caller's responsibility to free result QFILE_LIST_ID by
149  * calling regu_free_listid.
150  */
151 int
152 prepare_and_execute_query (char *stream, int stream_size, QUERY_ID * query_id, int var_cnt, DB_VALUE * varptr,
153  QFILE_LIST_ID ** result, QUERY_FLAG flag)
154 {
155  QFILE_LIST_ID *list_idptr;
156  int query_timeout;
157  int ret = NO_ERROR;
158 
159  if (qo_need_skip_execution ())
160  {
161  *result = NULL;
162 
163  return NO_ERROR;
164  }
165 
166  query_timeout = tran_get_query_timeout ();
167  list_idptr = qmgr_prepare_and_execute_query (stream, stream_size, query_id, var_cnt, varptr, flag, query_timeout);
168  if (list_idptr == NULL)
169  {
170  return ((ret = er_errid ()) == NO_ERROR) ? ER_FAILED : ret;
171  }
172 
173  *result = list_idptr;
174 
175  assert (*result != NULL);
176  assert (ret == NO_ERROR);
177 
178  return ret;
179 }
#define NO_ERROR
Definition: error_code.h:46
int tran_get_query_timeout(void)
#define ASSERT_ERROR()
int execute_query(const XASL_ID *xasl_id, QUERY_ID *query_idp, int var_cnt, const DB_VALUE *varptr, QFILE_LIST_ID **list_idp, QUERY_FLAG flag, CACHE_TIME *clt_cache_time, CACHE_TIME *srv_cache_time)
Definition: query_cl.c:105
#define ER_FAILED
Definition: error_code.h:47
int er_errid(void)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
#define assert(x)
int qmgr_prepare_query(COMPILE_CONTEXT *context, xasl_stream *stream)
#define ER_OUT_OF_VIRTUAL_MEMORY
Definition: error_code.h:50
int prepare_query(COMPILE_CONTEXT *context, XASL_STREAM *stream)
Definition: query_cl.c:52
#define NULL
Definition: freelistheap.h:34
bool qo_need_skip_execution(void)
Definition: query_graph.c:297
int prepare_and_execute_query(char *stream, int stream_size, QUERY_ID *query_id, int var_cnt, DB_VALUE *varptr, QFILE_LIST_ID **result, QUERY_FLAG flag)
Definition: query_cl.c:152
#define ARG_FILE_LINE
Definition: error_manager.h:44
#define free_and_init(ptr)
Definition: memory_alloc.h:147
QFILE_LIST_ID * qmgr_prepare_and_execute_query(char *xasl_stream, int xasl_stream_size, QUERY_ID *query_idp, int dbval_cnt, DB_VALUE *dbval_ptr, QUERY_FLAG flag, int query_timeout)
int query_timeout
Definition: cas.c:160
XASL_ID * xasl_id
Definition: xasl.h:610
char * buffer
Definition: xasl.h:613
#define XASL_ID_IS_NULL(X)
Definition: xasl.h:560
int QUERY_FLAG
Definition: query_list.h:585
QFILE_LIST_ID * qmgr_execute_query(const XASL_ID *xasl_id, QUERY_ID *query_idp, int dbval_cnt, const DB_VALUE *dbvals, QUERY_FLAG flag, CACHE_TIME *clt_cache_time, CACHE_TIME *srv_cache_time, int query_timeout)