src/query_order.c

00001 /* 
00002  * Copyright (C) 2005 Jukka Zitting <jz@yukatan.fi>
00003  *
00004  * This program is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License as published
00006  * by the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #include <config.h>
00020 #include "query_order.h"
00021 #include <mysql.h>
00022 #include <string.h>
00023 #include "midgard/midgard_legacy.h"
00024 #include "midgard/midgard_object.h"
00025 #include "midgard/midgard_metadata.h"
00026 
00027 static const gchar *dirs[] = { "ASC", "DESC", NULL };
00028 
00038 MidgardQueryOrder *midgard_query_order_new(
00039         midgard *mgd, GObjectClass *klass, const gchar *name, const gchar *dir)
00040 {
00041         MidgardQueryOrder *order = g_new(MidgardQueryOrder, 1);
00042 
00043         order->mgd = mgd;
00044         order->spec = g_object_class_find_property(klass, name);
00045         if (order->spec == NULL) {
00046                 g_warning("Invalid order constraint property: %s", name);
00047                 g_free(order);
00048                 return NULL;
00049         }
00050         /* FIXME , I need to duplicate name here.
00051          * One should use pointer wihout allocating new memory for name,
00052          * but property seems to "overwritten" in QB. Or some memory corruption
00053          * happens. I can not find it with valgrind */
00054         order->property = g_strdup(name);
00055         order->klass = (MidgardObjectClass *)klass;
00056         /* I do not understand why g_object_ref is used for GParamSpec */
00057         /* g_object_ref(G_OBJECT(order->spec)); */
00058 
00059         order->dir = NULL;
00060         guint i = 0;
00061         while (dirs[i] && strcmp(dir, dirs[i]) != 0) {
00062                 i++;
00063         }
00064         if (dirs[i]) {
00065                 order->dir = dirs[i];
00066         } else {
00067                 g_warning("Invalid order direction: %s", dir);
00068                 g_free(order);
00069                 return NULL;
00070         }
00071 
00072         order->sql = NULL;
00073 
00074         return order;
00075 }
00076 
00086 const gchar *midgard_query_order_get_sql(MidgardQueryOrder *order)
00087 {
00088         g_assert(order);
00089 
00090         if (!order->sql) {
00091                 
00092                 if(g_str_equal(order->property, "guid")){
00093                         order->sql = g_strdup_printf(
00094                                         "%s.guid %s", 
00095                                         midgard_object_class_get_table(order->klass),
00096                                         order->dir);
00097                 } else if (g_str_equal(order->property, "sitegroup")){
00098                         order->sql = g_strdup_printf(
00099                                         "%s.sitegroup %s",
00100                                         midgard_object_class_get_table(order->klass),
00101                                         order->dir);
00102                 } else if (order->ext_type == MIDGARD_TYPE_METADATA) {
00103                         MidgardObjectClass *pklass =  
00104                                 (MidgardObjectClass*) g_type_class_peek(order->parent_type);
00105                         if(pklass){
00106                                 order->sql = g_strdup_printf(
00107                                                 "%s.%s %s",
00108                                                 midgard_object_class_get_table(pklass),
00109                                                 g_param_spec_get_nick(
00110                                                         (GParamSpec *)order->spec),
00111                                                 order->dir);
00112                         }                       
00113                 } else {
00114                         
00115                         order->sql = g_strdup_printf(
00116                                         "%s %s", 
00117                                         g_param_spec_get_nick(
00118                                                 (GParamSpec *)order->spec), 
00119                                         order->dir);
00120                 }
00121         }
00122 
00123         return order->sql;
00124 }
00125 
00131 void midgard_query_order_free(MidgardQueryOrder *order) {
00132         g_assert(order);
00133         g_free(order->sql);
00134         g_free((gchar*)order->property);
00135         /* FIXME, order->spec is GParamSpec */
00136         /* g_object_unref(G_OBJECT(order->spec)); */
00137         g_free(order);
00138 }

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