src/repligard.c

00001 /* $Id: repligard.c,v 1.27 2006/10/09 12:50:22 piotras Exp $
00002  *
00003  * midgard-lib: database access for Midgard clients
00004  *
00005  * Copyright (C) 2000 The Midgard Project ry
00006  * Copyright (C) 2000 Alexander Bokovoy and Aurora S.A.
00007  * Copyright (C) 2003 David Schmitter, Dataflow Solutions GmbH <schmitt@dataflow.ch>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Library General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Library General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this library; see the file COPYING.  If not, write to
00021  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00022  * Boston, MA 02111-1307, USA.
00023  */
00024 
00025 #include <config.h>
00026 #include "midgard/midgard_legacy.h"
00027 #include "defaults.h"
00028 #include "midgard/parser.h"
00029 #include "fmt_russian.h"
00030 #include <sys/types.h>
00031 #ifndef WIN32
00032 #include <unistd.h>
00033 #endif
00034 #ifdef WIN32
00035 #include <process.h>
00036 #include "win32/win95nt.h"
00037 #endif
00038 
00039 #include "midgard_mysql.h"
00040 
00041 #if HAVE_MIDGARD_VC
00042 int mgd_vc_create_repligard(midgard *mgd, const char *table, int id) {
00043         int ret;
00044         gchar *guid = midgard_guid_new(mgd);
00045         
00046         /* Do not update guid in multilang tables */
00047         if(!g_str_has_suffix(table, "_i")) {
00048                 GString *guid_sql = g_string_new("UPDATE "); 
00049                 g_string_append_printf(guid_sql,
00050                                 " %s SET guid='%s' WHERE id=%d AND sitegroup=%d", 
00051                                 table, guid, id,  mgd_sitegroup(mgd));
00052                 gchar *tmpstr = g_string_free(guid_sql, FALSE); 
00053                 mgd_exec(mgd, tmpstr, NULL); 
00054                 g_free(tmpstr);
00055         }               
00056         ret = mgd_create_repligard(mgd, table, "guid,id,changed,action,sitegroup",
00057                         "$q,$d,NULL,'create',$d",guid, id, mgd_sitegroup(mgd));
00058         mgd_cvs_dump_nowait_guid(mgd, guid);
00059         g_free(guid);
00060         return ret;
00061 }
00062 #endif /* HAVE_MIDGARD_VC */
00063 
00064 int mgd_create_repligard(midgard * mgd, const char *table,
00065                          const char *fields, const char *values, ...)
00066 {
00067         int id;
00068         va_list args;
00069         va_start(args, values);
00070         id = mgd_vcreate_repligard(mgd, table, fields, values, args);
00071         va_end(args);
00072         return id;
00073 }
00074 
00075 int mgd_vcreate_repligard(midgard * mgd, const char *table,
00076                           const char *fields, const char *values, va_list args)
00077 {
00078         const char *command;
00079         int rv;
00080         gchar *typename = NULL;
00081 
00082         /* Legacy objects ugly workaround */
00083         if(g_str_equal(table, "article"))
00084                 typename = "midgard_article";   
00085         if(g_str_equal(table, "blobs"))
00086                 typename = "midgard_attachment";
00087         if(g_str_equal(table, "element"))
00088                 typename = "midgard_element";
00089         if(g_str_equal(table, "event"))
00090                 typename = "midgard_event";
00091         if(g_str_equal(table, "eventmember"))
00092                 typename = "midgard_eventmember";
00093         if(g_str_equal(table, "grp"))
00094                 typename = "midgard_group";
00095         if(g_str_equal(table, "host"))
00096                 typename = "midgard_host";
00097         if(g_str_equal(table, "member"))
00098                 typename = "midgard_member";
00099         if(g_str_equal(table, "page"))
00100                 typename = "midgard_page";
00101         if(g_str_equal(table, "pageelement"))
00102                 typename = "midgard_pageelement";
00103         if(g_str_equal(table, "person"))
00104                 typename = "midgard_person";
00105         if(g_str_equal(table, "record_extension"))
00106                 typename = "midgard_parameter";
00107         if(g_str_equal(table, "snippet"))
00108                 typename = "midgard_snippet";
00109         if(g_str_equal(table, "snippetdir"))
00110                 typename = "midgard_snippetdir";
00111         if(g_str_equal(table, "style"))
00112                 typename = "midgard_style";
00113         if(g_str_equal(table, "topic"))
00114                 typename = "midgard_topic";
00115         
00116         if(typename == NULL)
00117                 typename = "";
00118         /* End of legacy workaround */
00119 
00120         /* format command */
00121 #if HAVE_MIDGARD_VC
00122 
00123         if (mgd->cvs_script) {
00124         command = mgd_format(mgd, mgd->tmp,
00125                                "INSERT INTO repligard (realm,locked,author,typename,$s) VALUES ($q,1,$d,$q,$s)",
00126                                fields, table, mgd->current_user->id, typename,values);
00127         } else {
00128 #endif /* HAVE_MIDGARD_VC */
00129         command = mgd_format(mgd, mgd->tmp,
00130                              "INSERT INTO repligard (realm,typename,$s) VALUES ($q,$q,$s)",
00131                              fields, table, typename,values);
00132 #if HAVE_MIDGARD_VC
00133         }
00134         
00135 #endif /* HAVE_MIDGARD_VC */
00136         if (!command)
00137                 return 0;
00138         /* execute command */
00139         rv = mgd_vexec(mgd, command, args);
00140         g_free((gchar*)command); 
00141         
00142         mgd_clear_pool(mgd->tmp);
00143         return rv ? mysql_insert_id(mgd->msql->mysql) : 0;
00144 }
00145 
00146 int mgd_update_repligard(midgard * mgd, const char *table, int id,
00147                          const char *fields, ...)
00148 {
00149         int rv;
00150         va_list args;
00151         va_start(args, fields);
00152         rv = mgd_vupdate_repligard(mgd, table, id, fields, args);
00153         va_end(args);
00154         return id;
00155 }
00156 
00157 
00158 int mgd_vupdate_repligard(midgard * mgd, const char *table, int id,
00159                           const char *fields, va_list args)
00160 {
00161         const char *command;
00162         int rv;
00163 
00164         if(g_str_has_suffix(table, "_i")) 
00165                 return 0;
00166 
00167         midgard_res *res = mgd_sitegroup_select(
00168                         mgd, "guid" , table,
00169                         "id=$d", NULL, id);
00170         if (!res || !mgd_fetch(res)) {
00171                 if (res) mgd_release(res);
00172                 return 0;
00173         }
00174         gchar *guid = (gchar *)mgd_colvalue(res, 0);
00175                 
00176         /* format command */
00177 #if HAVE_MIDGARD_VC
00178         
00179         if (mgd->cvs_script) {
00180         command = mgd_format(mgd, mgd->tmp,
00181                                "UPDATE $s SET locked=1,author=$d,$s WHERE guid=$q AND realm=$q",
00182                                "repligard", mgd->current_user->id, fields, guid, table);
00183           
00184         } else {
00185 #endif /* HAVE_MIDGARD_VC */
00186         command = mgd_format(mgd, mgd->tmp,
00187                              "UPDATE $s SET $s WHERE guid=$q AND realm=$q",
00188                              "repligard", fields, guid, table);
00189 #if HAVE_MIDGARD_VC
00190         }
00191 #endif /* HAVE_MIDGARD_VC*/
00192         mgd_release(res);
00193         if (!command)
00194                 return 0;
00195 
00196         /* execute command */
00197         rv = mgd_vexec(mgd, command, args);
00198         g_free((gchar *)command);
00199 
00200 #if HAVE_MIDGARD_VC
00201         mgd_cvs_dump_nowait(mgd,table,id);
00202 #endif /* HAVE_MIDGARD_VC */
00203 
00204         mgd_clear_pool(mgd->tmp);
00205         return rv;
00206 }
00207 
00208 int mgd_delete_repligard(midgard * mgd, const char *table, int id)
00209 {
00210         const char *command;
00211         int rv;
00212 
00213         /* format command */
00214         command =
00215            mgd_format(mgd, mgd->tmp,
00216                       "DELETE FROM repligard WHERE realm=$q AND id=$d", table,
00217                       id);
00218         if (!command)
00219                 return 0;
00220 
00221         /* execute command */
00222         rv = mgd_exec(mgd, command);
00223 
00224         mgd_clear_pool(mgd->tmp);
00225         return rv;
00226 }
00227 
00228 int mgd_delete_repligard_guid(midgard * mgd, const char *guid)
00229 {
00230         const char *command;
00231         int rv;
00232 
00233         /* format command */
00234         command =
00235            mgd_format(mgd, mgd->tmp,
00236                       "DELETE FROM repligard WHERE guid=$q", guid);
00237         if (!command)
00238                 return 0;
00239 
00240         /* execute command */
00241         rv = mgd_exec(mgd, command);
00242 
00243         mgd_clear_pool(mgd->tmp);
00244         return rv;
00245 }
00246 
00247 
00248 char *mgd_repligard_guid(midgard * mgd, midgard_pool * pool, const char *table,
00249                         int id)
00250 {
00251         midgard_res *res;
00252         char *guid;
00253         res =
00254            mgd_ungrouped_select(mgd, "guid", "repligard", "realm=$q AND id=$d",
00255                                 NULL, table, id);
00256         if (res && mgd_fetch(res)) {
00257                 guid = mgd_strdup(pool, mgd_colvalue(res, 0));
00258                 mgd_release(res);
00259                 return guid;
00260         }
00261         return NULL;
00262 }
00263 
00264 char *mgd_repligard_changed(midgard * mgd, midgard_pool * pool, const char *table, int id)
00265 {
00266         midgard_res *res;
00267         char* changed;
00268         res = mgd_ungrouped_select(mgd, "changed",
00269                                    "repligard", "realm=$q AND id=$d",
00270                                    NULL, table, id);
00271         if (res && mgd_fetch(res)) {
00272                 changed = mgd_strdup(pool, mgd_colvalue(res, 0));
00273                 mgd_release(res);
00274                 return changed;
00275         }
00276         return NULL;
00277 }
00278 
00279 char *mgd_repligard_changed_guid(midgard * mgd, midgard_pool * pool, const char *guid)
00280 {
00281         midgard_res *res;
00282         char* changed;
00283         res = mgd_ungrouped_select(mgd, "changed",
00284                                    "repligard", "guid=$q",
00285                                    NULL, guid);
00286         if (res && mgd_fetch(res)) {
00287                 changed = mgd_strdup(pool, mgd_colvalue(res, 0));
00288                 mgd_release(res);
00289                 return changed;
00290         }
00291         return NULL;
00292 }
00293 
00294 char *mgd_repligard_updated(midgard * mgd, midgard_pool * pool, const char *table, int id)
00295 {
00296         midgard_res *res;
00297         char* updated;
00298         res = mgd_ungrouped_select(mgd, "updated",
00299                                    "repligard", "realm=$q AND id=$d",
00300                                    NULL, table, id);
00301         if (res && mgd_fetch(res)) {
00302                 updated = mgd_strdup(pool, mgd_colvalue(res, 0));
00303                 mgd_release(res);
00304                 return updated;
00305         }
00306         return NULL;
00307 }
00308 
00309 char *mgd_repligard_updated_guid(midgard * mgd, midgard_pool * pool, const char *guid)
00310 {
00311         midgard_res *res;
00312         char* updated;
00313         res = mgd_ungrouped_select(mgd, "updated",
00314                                    "repligard", "guid=$q",
00315                                    NULL, guid);
00316         if (res && mgd_fetch(res)) {
00317                 updated = mgd_strdup(pool, mgd_colvalue(res, 0));
00318                 mgd_release(res);
00319                 return updated;
00320         }
00321         return NULL;
00322 }
00323 
00324 char* mgd_repligard_table(midgard * mgd, midgard_pool* pool, const char *guid)
00325 {
00326         midgard_res *res;
00327         char* table;
00328         res =
00329            mgd_ungrouped_select(mgd, "realm", "repligard", "guid=$q",
00330                                 NULL, guid);
00331         if (res && mgd_fetch(res)) {
00332                 table = mgd_strdup(pool, mgd_colvalue(res, 0));
00333                 mgd_release(res);
00334                 return table;
00335         }
00336         return NULL;
00337 }
00338 
00339 char* mgd_repligard_action(midgard * mgd, midgard_pool* pool, const char *guid)
00340 {
00341         midgard_res *res;
00342         char* action;
00343         res =
00344            mgd_ungrouped_select(mgd, "action", "repligard", "guid=$q",
00345                                 NULL, guid);
00346         if (res && mgd_fetch(res)) {
00347                 action = mgd_strdup(pool, mgd_colvalue(res, 0));
00348                 mgd_release(res);
00349                 return action;
00350         }
00351         return NULL;
00352 }
00353 
00354 int mgd_repligard_id(midgard * mgd, const char *guid)
00355 {
00356         midgard_res *res;
00357         int id;
00358         res =
00359            mgd_ungrouped_select(mgd, "id", "repligard", "guid=$q",
00360                                 NULL, guid);
00361         if (res && mgd_fetch(res)) {
00362                 id = mgd_sql2id(res, 0);
00363                 mgd_release(res);
00364                 return id;
00365         }
00366         return 0;
00367 }
00368 
00369 /*  Touches all records from table and set their timestamps in Repligard
00370     If record doesn't exist in Repligard, it will be created.
00371 */
00372 void mgd_repligard_touch(midgard * mgd, const char *table)
00373 {
00374         midgard_res *res;
00375 
00376         res = mgd_ungrouped_select(mgd, "id", table, NULL, NULL);
00377         if (res) {
00378                 while (mgd_fetch(res)) {
00379                         if (mgd_exists_bool(mgd, "repligard", "id=$d AND realm=$q",
00380                                        mgd_sql2id(res, 0), table)) {
00381                                 UPDATE_REPLIGARD(mgd, table, mgd_sql2id(res, 0))
00382                         }
00383                         else {
00384                                 CREATE_REPLIGARD(mgd, table, mgd_sql2id(res, 0))
00385                         }
00386                 }
00387                 mgd_release(res);
00388         }
00389 }
00390 

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