1 /*
2  * Copyright (C) 2006 Murray Cumming <murrayc@murrayc.com>
3  * Copyright (C) 2006 - 2011 Vivien Malerba <malerba@gnome-db.org>
4  * Copyright (C) 2010 David King <davidk@openismus.com>
5  * Copyright (C) 2010 Jonh Wendell <jwendell@gnome.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA  02110-1301, USA.
21  */
22 
23 #ifndef __GDA_SERVER_OPERATION_H__
24 #define __GDA_SERVER_OPERATION_H__
25 
26 #include <glib-object.h>
27 #include <libgda/gda-decl.h>
28 #include <libxml/tree.h>
29 
30 G_BEGIN_DECLS
31 
32 #define GDA_TYPE_SERVER_OPERATION            (gda_server_operation_get_type())
33 #define GDA_SERVER_OPERATION(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GDA_TYPE_SERVER_OPERATION, GdaServerOperation))
34 #define GDA_SERVER_OPERATION_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GDA_TYPE_SERVER_OPERATION, GdaServerOperationClass))
35 #define GDA_IS_SERVER_OPERATION(obj)         (G_TYPE_CHECK_INSTANCE_TYPE(obj, GDA_TYPE_SERVER_OPERATION))
36 #define GDA_IS_SERVER_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GDA_TYPE_SERVER_OPERATION))
37 
38 /*
39  * Types of identified operations
40  * all the providers don't implement them completely, though
41  */
42 typedef enum {
43 	GDA_SERVER_OPERATION_CREATE_DB,
44 	GDA_SERVER_OPERATION_DROP_DB,
45 
46 	GDA_SERVER_OPERATION_CREATE_TABLE,
47 	GDA_SERVER_OPERATION_DROP_TABLE,
48 	GDA_SERVER_OPERATION_RENAME_TABLE,
49 
50 	GDA_SERVER_OPERATION_ADD_COLUMN,
51 	GDA_SERVER_OPERATION_DROP_COLUMN,
52 
53 	GDA_SERVER_OPERATION_CREATE_INDEX,
54 	GDA_SERVER_OPERATION_DROP_INDEX,
55 
56 	GDA_SERVER_OPERATION_CREATE_VIEW,
57 	GDA_SERVER_OPERATION_DROP_VIEW,
58 
59 	GDA_SERVER_OPERATION_COMMENT_TABLE,
60 	GDA_SERVER_OPERATION_COMMENT_COLUMN,
61 
62 	GDA_SERVER_OPERATION_CREATE_USER,
63 	GDA_SERVER_OPERATION_ALTER_USER,
64 	GDA_SERVER_OPERATION_DROP_USER,
65 
66 	GDA_SERVER_OPERATION_LAST
67 } GdaServerOperationType;
68 
69 /* error reporting */
70 extern GQuark gda_server_operation_error_quark (void);
71 #define GDA_SERVER_OPERATION_ERROR gda_server_operation_error_quark ()
72 
73 typedef enum {
74 	GDA_SERVER_OPERATION_OBJECT_NAME_ERROR,
75 	GDA_SERVER_OPERATION_INCORRECT_VALUE_ERROR,
76 	GDA_SERVER_OPERATION_XML_ERROR
77 } GdaServerOperationError;
78 
79 typedef enum
80 {
81 	GDA_SERVER_OPERATION_CREATE_TABLE_NOTHING_FLAG   = 1 << 0,
82 	GDA_SERVER_OPERATION_CREATE_TABLE_PKEY_FLAG      = 1 << 1,
83 	GDA_SERVER_OPERATION_CREATE_TABLE_NOT_NULL_FLAG  = 1 << 2,
84 	GDA_SERVER_OPERATION_CREATE_TABLE_UNIQUE_FLAG    = 1 << 3,
85 	GDA_SERVER_OPERATION_CREATE_TABLE_AUTOINC_FLAG   = 1 << 4,
86 	GDA_SERVER_OPERATION_CREATE_TABLE_FKEY_FLAG      = 1 << 5,
87 	/* Flags combinations */
88 	GDA_SERVER_OPERATION_CREATE_TABLE_PKEY_AUTOINC_FLAG = GDA_SERVER_OPERATION_CREATE_TABLE_PKEY_FLAG | GDA_SERVER_OPERATION_CREATE_TABLE_AUTOINC_FLAG
89 } GdaServerOperationCreateTableFlag;
90 
91 typedef enum {
92 	GDA_SERVER_OPERATION_NODE_PARAMLIST,
93 	GDA_SERVER_OPERATION_NODE_DATA_MODEL,
94 	GDA_SERVER_OPERATION_NODE_PARAM,
95 	GDA_SERVER_OPERATION_NODE_SEQUENCE,
96 	GDA_SERVER_OPERATION_NODE_SEQUENCE_ITEM,
97 
98 	GDA_SERVER_OPERATION_NODE_DATA_MODEL_COLUMN,
99 	GDA_SERVER_OPERATION_NODE_UNKNOWN
100 } GdaServerOperationNodeType;
101 
102 typedef enum {
103 	GDA_SERVER_OPERATION_STATUS_OPTIONAL,
104 	GDA_SERVER_OPERATION_STATUS_REQUIRED,
105 	GDA_SERVER_OPERATION_STATUS_UNKNOWN
106 } GdaServerOperationNodeStatus;
107 
108 /**
109  * SECTION:gda-server-operation-sequences
110  * @short_description: Manipulating sequences
111  * @title: GdaServerOperation: sequences
112  * @stability: Stable
113  * @see_also: #GdaServerOperation
114  *
115  * The #GdaServerOperation object can contain sequences of templates. For example when creating a table,
116  * one can specify several foreign keys where for each foreign key, one must define the column(s) on which the
117  * foreign key applies, the referenced table and the corresponding columns of the referenced table (plus some
118  * additional information). In this case the foreign keys are defined as a sequence of templates (the foreign key
119  * definition): there can be zero or more foreign keys.
120  */
121 
122 /**
123  * SECTION:gda-server-operation-nodes
124  * @short_description: Getting information about parts (nodes) composing a path
125  * @title: GdaServerOperation: individual nodes
126  * @stability: Stable
127  * @see_also: #GdaServerOperation
128  *
129  * To each part of a path is associated a node (as a #GdaServerOperationNode structure). For example the
130  * "/TABLE_DEF_P/TABLE_NAME" path has two nodes, one associated to "/TABLE_DEF_P" and one to
131  * "/TABLE_DEF_P/TABLE_NAME". For more information about the path's format, see the
132  * gda_server_operation_set_value_at()'s documentation.
133  *
134  * This API is designed to get information about all the nodes present in a #GdaServerOperation object (refer to the
135  * gda_server_operation_get_root_nodes() function) and about each node of a path, and allows inspection
136  * of its contents. It is mainly reserved for database provider's implementations but can have its purpose
137  * outside of this scope.
138  */
139 
140 typedef struct _GdaServerOperationNode {
141 	GdaServerOperationNodeType    type;
142 	GdaServerOperationNodeStatus  status;
143 
144 	GdaSet                       *plist;
145 	GdaDataModel                 *model;
146 	GdaColumn                    *column;
147 	GdaHolder                    *param;
148 	gpointer                      priv;
149 } GdaServerOperationNode;
150 
151 struct _GdaServerOperation {
152 	GObject                    object;
153 	GdaServerOperationPrivate *priv;
154 };
155 
156 struct _GdaServerOperationClass {
157 	GObjectClass               parent_class;
158 
159 	/* signals */
160 	void                     (*seq_item_added) (GdaServerOperation *op, const gchar *seq_path, gint item_index);
161 	void                     (*seq_item_remove) (GdaServerOperation *op, const gchar *seq_path, gint item_index);
162 
163 	/*< private >*/
164 	/* Padding for future expansion */
165 	void (*_gda_reserved1) (void);
166 	void (*_gda_reserved2) (void);
167 	void (*_gda_reserved3) (void);
168 	void (*_gda_reserved4) (void);
169 };
170 
171 /**
172  * SECTION:gda-server-operation
173  * @short_description: Handles any DDL query in an abstract way
174  * @title: GdaServerOperation
175  * @stability: Stable
176  * @see_also:
177  *
178  * This object is basically just a data store: it can store named values, the values being
179  * organized hierarchically by their name which are similar to a Unix file path. For example a value can be read from its path
180  * using the gda_server_operation_get_value_at() method, or set using the gda_server_operation_set_value_at() method.
181  *
182  * Each #GdaServerOperation contains some structure which is usually defined by a database provider to implement
183  * a specific operation. The structure is composed of the following building blocks:
184  * <itemizedlist>
185  *   <listitem><para>Named values (internally represented as a #GdaHolder object)</para></listitem>
186  *   <listitem><para>Named values in a vector (internally represented as a #GdaSet object)</para></listitem>
187  *   <listitem><para>Values in an array (internally represented as a #GdaDataModel object)</para></listitem>
188  *   <listitem><para>Sequences of one or more of the previous blocks. A sequence can contain any number of
189  *   instances of the template block (there may be lower and upper boundaries to the number of instances)</para></listitem>
190  * </itemizedlist>
191  *
192  * <emphasis>Important note:</emphasis> #GdaServerOperation objects are usually not created
193  * manually using gda_server_operation_new(), but
194  * using a #GdaServerProvider object with gda_server_provider_create_operation().
195  * See the <link linkend="DDLIntro">global introduction about DDL</link> for more information.
196  * Alternatively one can use the <link linkend="libgda-40-Convenience-functions">Convenience functions</link>
197  * which internally manipulate #GdaServerOperation objects.
198  */
199 
200 GType                      gda_server_operation_get_type                (void) G_GNUC_CONST;
201 GdaServerOperation        *gda_server_operation_new                     (GdaServerOperationType op_type, const gchar *xml_file);
202 GdaServerOperationType     gda_server_operation_get_op_type             (GdaServerOperation *op);
203 const gchar               *gda_server_operation_op_type_to_string       (GdaServerOperationType type);
204 GdaServerOperationType     gda_server_operation_string_to_op_type       (const gchar *str);
205 GdaServerOperationNode    *gda_server_operation_get_node_info           (GdaServerOperation *op, const gchar *path_format, ...);
206 
207 const GValue              *gda_server_operation_get_value_at            (GdaServerOperation *op, const gchar *path_format, ...);
208 const GValue              *gda_server_operation_get_value_at_path       (GdaServerOperation *op, const gchar *path);
209 gchar                     *gda_server_operation_get_sql_identifier_at   (GdaServerOperation *op,
210 									 GdaConnection *cnc, GdaServerProvider *prov,
211 									 const gchar *path_format, ...);
212 gchar                     *gda_server_operation_get_sql_identifier_at_path (GdaServerOperation *op,
213 									    GdaConnection *cnc, GdaServerProvider *prov,
214 									    const gchar *path);
215 gboolean                   gda_server_operation_set_value_at            (GdaServerOperation *op, const gchar *value,
216 									 GError **error, const gchar *path_format, ...);
217 gboolean                   gda_server_operation_set_value_at_path       (GdaServerOperation *op, const gchar *value,
218 									 const gchar *path, GError **error);
219 
220 xmlNodePtr                 gda_server_operation_save_data_to_xml        (GdaServerOperation *op, GError **error);
221 gboolean                   gda_server_operation_load_data_from_xml      (GdaServerOperation *op,
222 									 xmlNodePtr node, GError **error);
223 
224 gchar**                    gda_server_operation_get_root_nodes          (GdaServerOperation *op);
225 GdaServerOperationNodeType gda_server_operation_get_node_type           (GdaServerOperation *op, const gchar *path,
226 								         GdaServerOperationNodeStatus *status);
227 gchar                     *gda_server_operation_get_node_parent         (GdaServerOperation *op, const gchar *path);
228 gchar                     *gda_server_operation_get_node_path_portion   (GdaServerOperation *op, const gchar *path);
229 
230 const gchar               *gda_server_operation_get_sequence_name       (GdaServerOperation *op, const gchar *path);
231 guint                      gda_server_operation_get_sequence_size       (GdaServerOperation *op, const gchar *path);
232 guint                      gda_server_operation_get_sequence_max_size   (GdaServerOperation *op, const gchar *path);
233 guint                      gda_server_operation_get_sequence_min_size   (GdaServerOperation *op, const gchar *path);
234 gchar                    **gda_server_operation_get_sequence_item_names (GdaServerOperation *op, const gchar *path);
235 
236 guint                      gda_server_operation_add_item_to_sequence    (GdaServerOperation *op, const gchar *seq_path);
237 gboolean                   gda_server_operation_del_item_from_sequence  (GdaServerOperation *op, const gchar *item_path);
238 
239 gboolean                   gda_server_operation_is_valid                (GdaServerOperation *op, const gchar *xml_file, GError **error);
240 
241 /*
242  * Database creation and destruction
243  */
244 GdaServerOperation *gda_server_operation_prepare_create_database       (const gchar *provider, const gchar *db_name, GError **error);
245 gboolean            gda_server_operation_perform_create_database       (GdaServerOperation *op, const gchar *provider, GError **error);
246 GdaServerOperation *gda_server_operation_prepare_drop_database         (const gchar *provider, const gchar *db_name, GError **error);
247 gboolean            gda_server_operation_perform_drop_database         (GdaServerOperation *op, const gchar *provider, GError **error);
248 
249 /*
250  * Tables creation and destruction
251  */
252 GdaServerOperation *gda_server_operation_prepare_create_table	      (GdaConnection *cnc, const gchar *table_name, GError **error, ...);
253 gboolean            gda_server_operation_perform_create_table          (GdaServerOperation *op, GError **error);
254 GdaServerOperation *gda_server_operation_prepare_drop_table            (GdaConnection *cnc, const gchar *table_name, GError **error);
255 gboolean            gda_server_operation_perform_drop_table            (GdaServerOperation *op, GError **error);
256 
257 G_END_DECLS
258 
259 #endif
260