CUBRID Engine  latest
dbtype.h
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 /*
21  * dbtype.h - Definitions related to the memory representations of database
22  * attribute values. This is an application interface file. It should contain
23  * only definitions available to CUBRID customer applications.
24  */
25 
26 #ifndef _DBTYPE_H_
27 #define _DBTYPE_H_
28 
29 #ident "$Id$"
30 
31 #include "config.h"
32 
33 #include "system_parameter.h"
34 #include "error_manager.h"
35 #include "system.h"
36 #include "db_set.h"
37 #include "db_set_function.h"
38 #include "dbtype_def.h"
39 #include "elo.h"
40 #include "object_domain.h"
41 #include "oid.h"
42 #include "language_support.h"
43 #include "intl_support.h"
44 #include "memory_alloc.h"
45 
46 #define DB_CURRENCY_DEFAULT db_get_currency_default()
47 
48 #define db_make_utime db_make_timestamp
49 
50 #define DB_VALUE_CLONE_AS_NULL(src_value, dest_value) \
51  do { \
52  if ((db_value_domain_init(dest_value, \
53  db_value_domain_type(src_value), \
54  db_value_precision(src_value), \
55  db_value_scale(src_value))) \
56  == NO_ERROR) \
57  (void)db_value_put_null(dest_value); \
58  } while (0)
59 
60 #define db_get_collection db_get_set
61 #define db_get_utime db_get_timestamp
62 
63 #define DB_IS_NULL(value) db_value_is_null(value)
64 
65 #define DB_IS_STRING(value) (db_value_type(value) == DB_TYPE_VARCHAR || \
66  db_value_type(value) == DB_TYPE_CHAR || \
67  db_value_type(value) == DB_TYPE_VARNCHAR || \
68  db_value_type(value) == DB_TYPE_NCHAR)
69 
70 #define DB_VALUE_DOMAIN_TYPE(value) db_value_domain_type(value)
71 
72 #define DB_VALUE_TYPE(value) db_value_type(value)
73 #define DB_VALUE_PRECISION(value) db_value_precision(value)
74 #define DB_VALUE_SCALE(value) db_value_scale(value)
75 
76 #define DB_SET_COMPRESSED_STRING(value, compressed_string, compressed_size, compressed_need_clear) \
77  db_set_compressed_string(value, compressed_string, compressed_size, compressed_need_clear)
78 
79 #define DB_TRIED_COMPRESSION(value) (db_get_compressed_size(value) != DB_NOT_YET_COMPRESSED)
80 
81  /* Macros from dbval.h */
82 
83 #define DB_NEED_CLEAR(v) \
84  ((!DB_IS_NULL(v) \
85  && ((v)->need_clear == true \
86  || ((DB_VALUE_DOMAIN_TYPE(v) == DB_TYPE_VARCHAR || DB_VALUE_DOMAIN_TYPE(v) == DB_TYPE_VARNCHAR) \
87  && (v)->data.ch.info.compressed_need_clear != 0))))
88 
89 #define DB_GET_COMPRESSED_STRING(v) \
90  ((DB_VALUE_DOMAIN_TYPE(v) != DB_TYPE_VARCHAR) && (DB_VALUE_DOMAIN_TYPE(v) != DB_TYPE_VARNCHAR) \
91  ? NULL : (v)->data.ch.medium.compressed_buf)
92 
93 
94 #define DB_GET_STRING_PRECISION(v) \
95  ((v)->domain.char_info.length)
96 
97 #define DB_GET_ENUMERATION(v) \
98  ((v)->data.enumeration)
99 #define DB_GET_ENUM_ELEM_SHORT(elem) \
100  ((elem)->short_val)
101 #define DB_GET_ENUM_ELEM_DBCHAR(elem) \
102  ((elem)->str_val)
103 #define DB_GET_ENUM_ELEM_STRING(elem) \
104  ((elem)->str_val.medium.buf)
105 #define DB_GET_ENUM_ELEM_STRING_SIZE(elem) \
106  ((elem)->str_val.medium.size)
107 
108 #define DB_GET_ENUM_ELEM_CODESET(elem) \
109  ((elem)->str_val.info.codeset)
110 
111 #define DB_SET_ENUM_ELEM_CODESET(elem, cs) \
112  ((elem)->str_val.info.codeset = (cs))
113 
114 #define DB_SET_ENUM_ELEM_SHORT(elem, sv) \
115  ((elem)->short_val = (sv))
116 #define DB_SET_ENUM_ELEM_STRING(elem, str) \
117  ((elem)->str_val.medium.buf = (str), \
118  (elem)->str_val.info.style = MEDIUM_STRING)
119 #define DB_SET_ENUM_ELEM_STRING_SIZE(elem, sz) \
120  ((elem)->str_val.medium.size = (sz))
121 
122 #define DB_GET_STRING_SAFE(v) \
123  ((DB_IS_NULL (v) \
124  || DB_VALUE_DOMAIN_TYPE (v) == DB_TYPE_ERROR) ? "" \
125  : ((assert (DB_VALUE_DOMAIN_TYPE(v) == DB_TYPE_VARCHAR \
126  || DB_VALUE_DOMAIN_TYPE(v) == DB_TYPE_CHAR \
127  || DB_VALUE_DOMAIN_TYPE(v) == DB_TYPE_VARNCHAR \
128  || DB_VALUE_DOMAIN_TYPE(v) == DB_TYPE_NCHAR \
129  || DB_VALUE_DOMAIN_TYPE(v) == DB_TYPE_VARBIT \
130  || DB_VALUE_DOMAIN_TYPE(v) == DB_TYPE_BIT)), \
131  (v)->data.ch.medium.buf))
132 
133 #define DB_GET_NUMERIC_PRECISION(val) \
134  ((val)->domain.numeric_info.precision)
135 
136 #define DB_GET_NUMERIC_SCALE(val) \
137  ((val)->domain.numeric_info.scale)
138 
139 #define DB_GET_STRING_PRECISION(v) \
140  ((v)->domain.char_info.length)
141 
142 #define DB_GET_BIT_PRECISION(v) \
143  ((v)->domain.char_info.length)
144 
145 #define DB_GET_JSON_SCHEMA(v) \
146  ((v)->data.json.schema_raw)
147 
148 #define db_get_json_schema(v) DB_GET_JSON_SCHEMA(v)
149 
150 #ifdef __cplusplus
151 extern "C"
152 {
153 #endif
154 
155  /********************************************************/
156  /* From db_date.h */
157 
158  extern int db_date_encode (DB_DATE * date, int month, int day, int year);
159  extern int db_time_encode (DB_TIME * timeval, int hour, int minute, int second);
160  extern void db_date_decode (const DB_DATE * date, int *monthp, int *dayp, int *yearp);
161  extern void db_time_decode (DB_TIME * timeval, int *hourp, int *minutep, int *secondp);
162  /********************************************************/
163 
164  extern DB_VALUE *db_value_create (void);
165  extern DB_VALUE *db_value_copy (DB_VALUE * value);
166  extern int db_value_clone (DB_VALUE * src, DB_VALUE * dest);
167  extern int db_value_clear (DB_VALUE * value);
168  extern int db_value_free (DB_VALUE * value);
169  extern int db_value_clear_array (DB_VALUE_ARRAY * value_array);
170  extern void db_value_print (const DB_VALUE * value);
171  extern void db_value_fprint (FILE * fp, const DB_VALUE * value);
172  extern int db_value_coerce (const DB_VALUE * src, DB_VALUE * dest, const DB_DOMAIN * desired_domain);
173 
174  extern int db_value_equal (const DB_VALUE * value1, const DB_VALUE * value2);
175  extern int db_value_compare (const DB_VALUE * value1, const DB_VALUE * value2);
176  extern int db_value_domain_init (DB_VALUE * value, DB_TYPE type, const int precision, const int scale);
177  extern void db_value_domain_init_default (DB_VALUE * value, const DB_TYPE type);
178  extern int db_value_domain_min (DB_VALUE * value, DB_TYPE type, const int precision, const int scale,
179  const int codeset, const int collation_id, const DB_ENUMERATION * enumeration);
180  extern int db_value_domain_max (DB_VALUE * value, DB_TYPE type, const int precision, const int scale,
181  const int codeset, const int collation_id, const DB_ENUMERATION * enumeration);
182  extern int db_value_domain_default (DB_VALUE * value, const DB_TYPE type, const int precision, const int scale,
183  const int codeset, const int collation_id, DB_ENUMERATION * enumeration);
184  extern int db_value_domain_zero (DB_VALUE * value, const DB_TYPE type, const int precision, const int scale);
185  extern int db_string_truncate (DB_VALUE * value, const int max_precision);
186  extern int db_value_put_null (DB_VALUE * value);
187  extern int db_value_put (DB_VALUE * value, const DB_TYPE_C c_type, void *input, const int input_length);
188  extern bool db_value_type_is_collection (const DB_VALUE * value);
189  extern int db_value_get (DB_VALUE * value, const DB_TYPE_C type, void *buf, const int buflen, int *transferlen,
190  int *outputlen);
191  extern DB_CURRENCY db_value_get_monetary_currency (const DB_VALUE * value);
192  extern double db_value_get_monetary_amount_as_double (const DB_VALUE * value);
193  extern int db_value_put_monetary_currency (DB_VALUE * value, const DB_CURRENCY type);
194  extern int db_value_put_monetary_amount_as_double (DB_VALUE * value, const double amount);
195  extern int db_value_alter_type (DB_VALUE * value, DB_TYPE type);
196 
197  extern int db_value_put_encoded_time (DB_VALUE * value, const DB_TIME * time_value);
198  extern int db_value_put_encoded_date (DB_VALUE * value, const DB_DATE * date_value);
199 
200  extern DB_CURRENCY db_get_currency_default (void);
201 
202  extern DB_DOMAIN *db_type_to_db_domain (DB_TYPE type);
203  extern const char *db_default_expression_string (DB_DEFAULT_EXPR_TYPE default_expr_type);
204 
205  extern int db_get_deep_copy_of_json (const DB_JSON * src, DB_JSON * dst);
206  extern int db_init_db_json_pointers (DB_JSON * val);
207  extern int db_convert_json_into_scalar (const DB_VALUE * src, DB_VALUE * dest);
208  extern bool db_is_json_value_type (DB_TYPE type);
209  extern bool db_is_json_doc_type (DB_TYPE type);
210  extern char *db_get_json_raw_body (const DB_VALUE * value);
211 
212  extern bool db_value_is_corrupted (const DB_VALUE * value);
213 
214  extern int db_json_val_from_str (const char *raw_str, const int str_size, DB_VALUE * json_val);
215 
216  extern DB_TYPE setobj_type (struct setobj *set);
217 
218 /* Use the inline version of the functions. */
219 #include "dbtype_function.i"
220 
221 #ifdef __cplusplus
222 
223  // todo - find a better solution
224  inline void pr_share_value (DB_VALUE * src, DB_VALUE * dst)
225  {
226  if (src == NULL || dst == NULL || src == dst)
227  {
228  // do nothing
229  return;
230  }
231 
232  memcpy (dst, src, sizeof (DB_VALUE));
233  dst->need_clear = false;
234 
235  if (DB_IS_NULL (src))
236  {
237  // avoid checking type if db_val is null
238  return;
239  }
240 
241  DB_TYPE type = db_value_domain_type (src);
242  if (type == DB_TYPE_STRING || type == DB_TYPE_VARNCHAR)
243  {
244  dst->data.ch.info.compressed_need_clear = false;
245  }
246 
247  if ((TP_IS_SET_TYPE (type) || type == DB_TYPE_VOBJ) && !DB_IS_NULL (src))
248  {
249  src->data.set->ref_count++;
250  }
251  }
252 }
253 #endif
254 
255 #endif /* _DBTYPE_H_ */
void db_date_decode(const DB_DATE *date, int *monthp, int *dayp, int *yearp)
Definition: db_date.c:338
int db_value_put(DB_VALUE *value, const DB_TYPE_C c_type, void *input, const int input_length)
Definition: db_macro.c:1256
#define TP_IS_SET_TYPE(typenum)
DB_TYPE
Definition: dbtype_def.h:670
int db_value_put_encoded_date(DB_VALUE *value, const DB_DATE *date_value)
Definition: db_macro.c:1383
int db_value_get(DB_VALUE *value, const DB_TYPE_C type, void *buf, const int buflen, int *transferlen, int *outputlen)
Definition: db_macro.c:2077
int db_value_free(DB_VALUE *value)
Definition: db_macro.c:1610
int db_value_domain_init(DB_VALUE *value, DB_TYPE type, const int precision, const int scale)
Definition: db_macro.c:153
DB_VALUE * db_value_create(void)
Definition: db_macro.c:1517
int db_value_put_monetary_currency(DB_VALUE *value, const DB_CURRENCY type)
Definition: db_macro.c:1409
DB_VALUE * db_value_copy(DB_VALUE *value)
Definition: db_macro.c:1537
int db_value_put_monetary_amount_as_double(DB_VALUE *value, const double amount)
Definition: db_macro.c:1470
int db_string_truncate(DB_VALUE *value, const int max_precision)
Definition: db_macro.c:962
int db_json_val_from_str(const char *raw_str, const int str_size, DB_VALUE *json_val)
Definition: parse_dbi.c:3578
int db_date_encode(DB_DATE *date, int month, int day, int year)
Definition: db_date.c:275
int db_init_db_json_pointers(DB_JSON *val)
Definition: db_macro.c:2042
void db_value_print(const DB_VALUE *value)
Definition: db_macro.c:1663
DB_DATA data
Definition: dbtype_def.h:1083
DB_CURRENCY
Definition: dbtype_def.h:799
int ref_count
Definition: db_set.h:47
double db_value_get_monetary_amount_as_double(const DB_VALUE *value)
Definition: db_macro.c:1505
int db_value_put_encoded_time(DB_VALUE *value, const DB_TIME *time_value)
Definition: db_macro.c:1357
static int input()
Definition: cnvlex.c:1661
DB_TYPE db_value_domain_type(const DB_VALUE *value)
struct db_char::@52 info
DB_TYPE_C
Definition: dbtype_def.h:1116
int db_value_alter_type(DB_VALUE *value, DB_TYPE type)
Definition: db_macro.c:1225
bool db_value_is_corrupted(const DB_VALUE *value)
Definition: db_macro.c:5094
int db_value_domain_default(DB_VALUE *value, const DB_TYPE type, const int precision, const int scale, const int codeset, const int collation_id, DB_ENUMERATION *enumeration)
Definition: db_macro.c:756
int db_value_domain_zero(DB_VALUE *value, const DB_TYPE type, const int precision, const int scale)
Definition: db_macro.c:903
int db_value_domain_min(DB_VALUE *value, DB_TYPE type, const int precision, const int scale, const int codeset, const int collation_id, const DB_ENUMERATION *enumeration)
Definition: db_macro.c:413
int db_value_put_null(DB_VALUE *value)
Definition: db_macro.c:122
bool db_value_type_is_collection(const DB_VALUE *value)
Definition: db_macro.c:1144
void db_value_domain_init_default(DB_VALUE *value, const DB_TYPE type)
Definition: db_macro.c:394
#define NULL
Definition: freelistheap.h:34
int db_value_compare(const DB_VALUE *value1, const DB_VALUE *value2)
Definition: db_macro.c:1855
DB_CHAR ch
Definition: dbtype_def.h:1070
void db_time_decode(DB_TIME *timeval, int *hourp, int *minutep, int *secondp)
Definition: db_date.c:432
int db_value_clone(DB_VALUE *src, DB_VALUE *dest)
Definition: db_macro.c:1564
int db_value_clear_array(DB_VALUE_ARRAY *value_array)
Definition: db_macro.c:1633
need_clear_type need_clear
Definition: dbtype_def.h:1084
DB_TYPE setobj_type(struct setobj *set)
Definition: set_object.c:6158
int db_convert_json_into_scalar(const DB_VALUE *src, DB_VALUE *dest)
Definition: db_macro.c:4981
int db_time_encode(DB_TIME *timeval, int hour, int minute, int second)
Definition: db_date.c:370
DB_DOMAIN * db_type_to_db_domain(DB_TYPE type)
Definition: db_macro.c:1710
int db_get_deep_copy_of_json(const DB_JSON *src, DB_JSON *dst)
Definition: db_macro.c:2022
DB_CURRENCY db_get_currency_default(void)
Definition: db_macro.c:1870
unsigned char compressed_need_clear
Definition: dbtype_def.h:982
const char * db_default_expression_string(DB_DEFAULT_EXPR_TYPE default_expr_type)
Definition: db_macro.c:4947
unsigned int DB_TIME
Definition: dbtype_def.h:754
unsigned int DB_DATE
Definition: dbtype_def.h:771
char * db_get_json_raw_body(const DB_VALUE *value)
Definition: db_macro.c:5082
bool db_is_json_value_type(DB_TYPE type)
Definition: db_macro.c:5043
int db_value_clear(DB_VALUE *value)
Definition: db_macro.c:1588
DB_CURRENCY db_value_get_monetary_currency(const DB_VALUE *value)
Definition: db_macro.c:1492
#define DB_IS_NULL(value)
Definition: dbtype.h:63
int db_value_domain_max(DB_VALUE *value, DB_TYPE type, const int precision, const int scale, const int codeset, const int collation_id, const DB_ENUMERATION *enumeration)
Definition: db_macro.c:581
DB_COLLECTION * set
Definition: dbtype_def.h:1063
void db_value_fprint(FILE *fp, const DB_VALUE *value)
Definition: db_macro.c:1681
int db_value_coerce(const DB_VALUE *src, DB_VALUE *dest, const DB_DOMAIN *desired_domain)
Definition: db_macro.c:1779
int db_value_equal(const DB_VALUE *value1, const DB_VALUE *value2)
Definition: db_macro.c:1803
bool db_is_json_doc_type(DB_TYPE type)
Definition: db_macro.c:5066
DB_DEFAULT_EXPR_TYPE
Definition: dbtype_def.h:1181