1 /*
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 2013 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 
8 /*
9  * Db_sql is a utility program that translates a schema description
10  * written in a SQL Data Definition Language dialect into C code that
11  * implements the schema using Berkeley DB.
12  *
13  * This include file declares the data structures used to model the
14  * schema; along with macro definitions and function prototypes used
15  * throughout the program.
16  */
17 
18 #include "sqlite/sqliteInt.h"
19 
20 /*
21  * Forward structure declarations and typedefs.
22  */
23 struct __db_schema;        typedef struct __db_schema DB_SCHEMA;
24 struct __parse_progress;   typedef struct __parse_progress PARSE_PROGRESS;
25 struct __environment_info; typedef struct __environment_info ENVIRONMENT_INFO;
26 struct __db_entity;        typedef struct __db_entity ENTITY;
27 struct __db_attribute;     typedef struct __db_attribute ATTRIBUTE;
28 struct __db_attr_type;     typedef struct __db_attr_type ATTR_TYPE;
29 struct __db_index;         typedef struct __db_index DB_INDEX;
30 
31 /*
32  * Information about the BDB environment comes from a CREATE
33  * DATABASE statement in the input DDL.
34  */
35 struct __environment_info {
36 	char   *name;
37 	char   *uppercase_name;        /* A copy of the name in all caps. */
38 	int    line_number;
39 	unsigned long int cache_size;
40 	int 	transactional;
41 };
42 
43 /*
44  * DDL statements are parsed into a tree of data structures
45  * rooted in the global instance of this structure.
46  */
47 struct __db_schema {
48         ENVIRONMENT_INFO environment;
49 	ENTITY	        *entities_head; /* head of the list of Entities */
50 	ENTITY	        *entities_tail; /* the tail of that list */
51 	DB_INDEX        *indexes_head;  /* head of the list of DBIndexes */
52 	DB_INDEX        *indexes_tail;  /* tail of the list of DBIndexes */
53 };
54 
55 /*
56  * An ENTITY describes a table from the DDL.
57  */
58 struct __db_entity {
59 	ENTITY	        *next;
60 	char		*name; /* the name of this Entity */
61 	ATTRIBUTE	*attributes_head;
62 	ATTRIBUTE	*attributes_tail;
63 	ATTRIBUTE	*primary_key;
64 	char            *serialized_length_name; /* filled in when needed */
65 	int             line_number; /* line where this entity is declared */
66 	char            *dbtype;
67 	int		transactional;
68 };
69 
70 /*
71  * ATTRIBUTE is a fancy name for a column description
72  */
73 struct __db_attribute {
74 	ATTRIBUTE	*next;
75 	char		*name; /* the name of this Attribute */
76 	ATTR_TYPE	*type; /* the given type, simply a copy of the tokens */
77 	int		is_autoincrement;
78 	char            *array_dim_name; /* filled in when needed */
79 	char            *decl_name; /* name with array dimension, when needed */
80 };
81 
82 /*
83  * ATTR_TYPE describes the type of an attribute
84  */
85 struct __db_attr_type {
86 	char		*sql_token; /* the original type string from the sql */
87 	char		*c_type;    /* the mapped c-language type */
88 	int		array_dim;  /* The array size, if there is one */
89 	int             is_array;   /* boolean, tells if the c type is array */
90         int             is_string;  /* boolean, tells if it's a char* string */
91 };
92 
93 
94 /*
95  * DBIndex is what you get from CREATE_INDEX, it describes a secondary database
96  */
97 struct __db_index {
98 	DB_INDEX        *next;
99 	char            *name;      /* the name of the index/secondary */
100 	ENTITY          *primary;   /* primary database this index refers to */
101 	ATTRIBUTE       *attribute; /* the indexed field */
102 	int             line_number; /* line where this index is declared */
103 	char            *dbtype;
104 };
105 
106 /*
107  * PARSE_PROGRESS is just a little housekeeping info about what was the
108  * last thing parsed.
109  */
110 enum parse_event {
111 	PE_NONE = 0, PE_ENVIRONMENT =1, PE_ENTITY = 2,
112 	PE_ATTRIBUTE = 3, PE_INDEX = 4
113 };
114 
115 struct __parse_progress {
116 	enum parse_event last_event;
117 	ENTITY           *last_entity;
118         ATTRIBUTE        *last_attribute;
119         DB_INDEX         *last_index;
120 };
121 
122 extern DB_SCHEMA the_schema;
123 extern PARSE_PROGRESS the_parse_progress;
124 extern int line_number;
125 extern int debug;
126 extern int txnflag;
127 extern char *me;
128 
129 #define MAX_SQL_LENGTH 1000000000
130 
131 #define KILO (1024)
132 #define MEGA (KILO * KILO)
133 #define GIGA (MEGA * KILO)
134 #define TERA (GIGA * KILO)
135 
136 #if defined (_WIN32) && !defined(__MINGW_H)
137 #define snprintf sprintf_s
138 #define strdup _strdup
139 #define strconcat(dest, size, src) strcat_s(dest, size, src)
140 #define strnconcat(dest, size, src, count) strncat_s(dest, size, src, count)
141 #else
142 #define strconcat(dest, size, src) strcat(dest, src)
143 #define strnconcat(dest, size, src, count) strncat(dest, src, count)
144 #endif
145 
146 /* Windows lacks strcasecmp; how convenient that sqlite has its own */
147 #define strcasecmp sqlite3StrICmp
148 
149 extern void generate(FILE *, FILE *, FILE *, FILE *, char *);
150 extern void generate_test(FILE *, char *);
151 extern int do_parse(const char *, char **);
152 extern void parse_hint_comment(Token *);
153 extern void setString(char **, ...);
154 extern void preparser(void *, int,Token, Parse *);
155 extern void bdb_create_database(Token *, Parse *);
156 
157 /*
158  * To avoid warnings on unused functionarguments and variables, we
159  * write and then read the variable.
160  */
161 #define	COMPQUIET(n, v)	do {					        \
162 	(n) = (v);						        \
163 	(n) = (n);						        \
164 } while (0)
165