Contiki-NG
result.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * Result acquisition interface for AQL queries.
33  * \author
34  * Nicolas Tsiftes <nvt@sics.se>
35  */
36 
37 #include <string.h>
38 
39 #define DEBUG DEBUG_NONE
40 #include "net/ipv6/uip-debug.h"
41 
42 #include "result.h"
43 #include "storage.h"
44 
45 /* db_get_value: Retrieve the value of the specified attribute in
46  the current tuple. */
47 db_result_t
48 db_get_value(attribute_value_t *value, db_handle_t *handle, unsigned col)
49 {
50  attribute_t *attr;
51  unsigned char *buf;
52 
53  if(col >= handle->ncolumns) {
54  PRINTF("DB: Requested value (%d) is out of bounds; max = (%d)\n",
55  col, handle->ncolumns);
56  return DB_LIMIT_ERROR;
57  }
58 
59  buf = handle->tuple;
60 
61  for(attr = list_head(handle->result_rel->attributes); attr != NULL; attr = attr->next) {
62  if(attr->flags & ATTRIBUTE_FLAG_NO_STORE) {
63  /* This attribute was used for processing only. */
64  continue;
65  }
66  PRINTF("Found attribute %s in the result. The element size is %d\n",
67  attr->name, attr->element_size);
68  if(col == 0) {
69  break;
70  }
71  --col;
72  buf += attr->element_size;
73  }
74 
75  if(attr == NULL) {
76  return DB_NAME_ERROR;
77  }
78 
79  return db_phy_to_value(value, attr, buf);
80 }
81 
82 /* db_phy_to_value: Convert a value from the physical storage
83  representation to the internal RAM representation. */
84 db_result_t
85 db_phy_to_value(attribute_value_t *value, attribute_t *attr,
86  unsigned char *ptr)
87 {
88  int int_value;
89  long long_value;
90 
91  value->domain = attr->domain;
92 
93  switch(attr->domain) {
94  case DOMAIN_STRING:
95  ptr[attr->element_size - 1] = '\0';
96  VALUE_STRING(value) = ptr;
97  PRINTF("DB: %s = %s\n", attr->name, ptr);
98  break;
99  case DOMAIN_INT:
100  int_value = (ptr[0] << 8) | ((unsigned)ptr[1] & 0xff);
101  VALUE_INT(value) = int_value;
102  PRINTF("DB: %s = %d\n", attr->name, int_value);
103  break;
104  case DOMAIN_LONG:
105  long_value = (long)ptr[0] << 24 | (long)ptr[1] << 16 |
106  (long)ptr[2] << 8 | (long)ptr[3];
107  VALUE_LONG(value) = long_value;
108  PRINTF("DB: %s = %ld\n", attr->name, long_value);
109  break;
110  default:
111  return DB_TYPE_ERROR;
112  }
113 
114  return DB_OK;
115 }
116 
117 /* db_value_to_phy: Convert a value from the internal RAM representation
118  to the physical storage representation. */
119 db_result_t
120 db_value_to_phy(unsigned char *ptr, attribute_t *attr,
121  attribute_value_t *value)
122 {
123  int int_value;
124  long long_value;
125 
126  switch(attr->domain) {
127  case DOMAIN_STRING:
128  memcpy(ptr, VALUE_STRING(value), attr->element_size);
129  ptr[attr->element_size - 1] = '\0';
130  break;
131  case DOMAIN_INT:
132  int_value = VALUE_INT(value);
133  ptr[0] = int_value >> 8;
134  ptr[1] = int_value & 0xff;
135  break;
136  case DOMAIN_LONG:
137  long_value = VALUE_LONG(value);
138  ptr[0] = long_value >> 24;
139  ptr[1] = long_value >> 16;
140  ptr[2] = long_value >> 8;
141  ptr[3] = long_value & 0xff;
142  break;
143  default:
144  return DB_TYPE_ERROR;
145  }
146 
147  return DB_OK;
148 }
149 
150 /* db_value_to_long: Convert an attribute value
151  to a value of the C long type. */
152 long
153 db_value_to_long(attribute_value_t *value)
154 {
155  switch(value->domain) {
156  case DOMAIN_INT:
157  return (long)VALUE_INT(value);
158  case DOMAIN_LONG:
159  return (long)VALUE_LONG(value);
160  default:
161  return 0;
162  }
163 }
164 
165 /* db_free: Free all the resources that are referenced in a DB handle. */
166 db_result_t
167 db_free(db_handle_t *handle)
168 {
169  if(handle->rel != NULL) {
170  relation_release(handle->rel);
171  }
172  if(handle->result_rel != NULL) {
173  relation_release(handle->result_rel);
174  }
175  if(handle->left_rel != NULL) {
176  relation_release(handle->left_rel);
177  }
178  if(handle->right_rel != NULL) {
179  relation_release(handle->right_rel);
180  }
181 
182  handle->flags = 0;
183 
184  return DB_OK;
185 }
A set of debugging macros for the IP stack
Declarations for the result acquisition API.
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
The storage interface used by the database.