src/midgard_core_query_builder.c

00001 /* 
00002  * Copyright (c) 2005 Jukka Zitting <jz@yukatan.fi>
00003  * Copyright (C) 2006 Piotr Pokora <piotrek.pokora@gmail.com>
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser General Public License as published
00007  * by the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #include <config.h>
00021 #include "midgard/midgard_schema.h"
00022 #include "midgard/midgard_legacy.h"
00023 #include "midgard_core_query_builder.h"
00024 #include "midgard/query_builder.h"
00025 #include "query_constraint.h"
00026 #include "simple_constraint.h"
00027 #include "group_constraint.h"
00028 #include "query_order.h"
00029 #include "midgard/midgard_object_property.h"
00030 #include "midgard/midgard_object.h"
00031 #include "schema.h"
00032 #include "midgard_mysql.h"
00033 
00034 gchar *_midgard_core_qb_get_sql(
00035                 MidgardQueryBuilder *builder, guint mode, gchar *select)
00036 {
00037         g_assert(builder);
00038         g_assert(builder->schema->table);    
00039         
00040         /* FIXME, error reporting needed */
00041         if(builder->error != 0) {
00042             g_warning("Can not create query");
00043             return NULL;
00044         }
00045         
00046         GString *sql = g_string_new("SELECT ");
00047         g_string_append(sql, select);
00048         g_free(select);
00049         const gchar *tables = builder->schema->query->tables; /* FIXME */
00050         if(builder->tables != NULL)
00051             tables = builder->tables;
00052         
00053         g_string_append(sql, " FROM ");
00054         g_string_append(sql, tables);
00055         g_string_append(sql, " WHERE ");
00056         while (g_slist_next(builder->stack) != NULL) {
00057                 g_warning("Constraint groups not properly nested");
00058                 GSList *item = builder->stack;
00059                 builder->stack = g_slist_remove_link(builder->stack, item);
00060                 g_slist_free_1(item);
00061         }
00062         midgard_query_constraint_add_sql(
00063                 MIDGARD_QUERY_CONSTRAINT(builder->stack->data), sql);
00064 
00065         if (builder->schema->use_lang == 1) {
00066                 g_string_append(sql, " AND ");
00067                 g_string_append(sql, builder->schema->table);
00068                 g_string_append(sql, ".id=");
00069                 g_string_append(sql, builder->schema->table);
00070                 g_string_append(sql, "_i.sid");  
00071                
00072                 if(!builder->unset_lang) {
00073 
00074                         g_string_append(sql, " AND ");
00075                         g_string_append(sql, builder->schema->table);
00076                         /* Default behaviour , we get multilang records 
00077                          * with lang 0 and other lang set, thus we have 
00078                          * "full" list of objects , even some of them are 
00079                          * not translated yet.*/ 
00080                         if(builder->lang < 0) {
00081                                 g_string_append_printf(sql,
00082                                                 "_i.lang IN (%d, %d)",
00083                                                 mgd_get_default_lang(builder->mgd),
00084                                                 mgd_lang(builder->mgd));        
00085                         } else {
00086                                 /* builder->lang is set with set_lang method, so 
00087                                  * we get only those records with lang being set
00088                                  * and we ignore all others , even woth lang 0 
00089                                  */
00090                                 g_string_append_printf(sql,
00091                                                 "_i.lang = %d ",
00092                                                 builder->lang);
00093                         }
00094                 }
00095                
00096         }           
00097 
00098         if (builder->join != NULL){
00099                 gchar *tmpstr = g_string_free(builder->join, FALSE);
00100                 g_string_append(sql, tmpstr);
00101                 g_free(tmpstr);
00102                 builder->join = NULL;
00103         }
00104 
00105         if (!mgd_isroot(builder->mgd)) {
00106                 g_string_append(sql, " AND ");
00107                 g_string_append(sql, builder->schema->table);
00108                 g_string_append_printf(
00109                         sql, ".sitegroup IN (0, %u)",
00110                         builder->mgd->current_user->sitegroup);
00111         }
00112 
00113         if(!builder->include_deleted) {
00114                 g_string_append(sql, " AND ");
00115                 g_string_append_printf(sql,
00116                                 " %s.metadata_deleted = FALSE",
00117                                 builder->schema->table);
00118         }
00119         /* TODO: The following code adds the ORDER BY ordering
00120          * constraints to the SQL query. Commented out because
00121          * I'm not sure about the correct "table.field" format
00122          * to use for field references. /2005-06-16 JZ
00123          */
00124         int i;
00125         for (i = 0; mode < MQB_SELECT_COUNT  && i < builder->orders->len; i++) {
00126                 if (i > 0) {
00127                         g_string_append(sql, ", ");
00128                 } else {
00129                         g_string_append(sql, " ORDER BY ");
00130                 }
00131                 MidgardQueryOrder *order = g_array_index(
00132                         builder->orders, MidgardQueryOrder*, i);        
00133                 g_string_append(sql, midgard_query_order_get_sql(order));
00134         }
00135 
00136         if (mode < MQB_SELECT_COUNT && builder->limit != G_MAXUINT) {
00137                 g_string_append_printf(sql, " LIMIT %u", builder->limit);
00138                 if (builder->offset != 0) {
00139                         g_string_append_printf(
00140                                 sql, " OFFSET %u", builder->offset);
00141                 }
00142         }
00143 
00144         return g_string_free(sql, FALSE);
00145 }

Generated on Thu Feb 22 06:15:18 2007 for midgard-core by  doxygen 1.4.6