mysql._pike
Go to the documentation of this file.
1 /*0*/
2 inherit "handle";
3 #include <macros.h>
4 #include <database.h>
5 #include <classes.h>
6 #include <attributes.h>
7 class mysql : public handle{
8 public:
9 
10 
11 string describe() { return "MySQLHandle"; }
12 
13 void check_tables()
14 {
15  string query;
16 
17  array tableinfo = oHandle->list_fields("ob_class", "obkeywords");
18  // keywords update
19  if ( sizeof(tableinfo) == 0 ) {
20  query = "alter table ob_class add (obkeywords text, obname text, obdescription text, obmimetype text, obversionof text)";
21  mixed err = catch(oHandle->big_query(query));
22  if ( err != 0 ) {
23  throw(err);
24  }
25  }
26  tableinfo = oHandle->list_fields("ob_class", "obdecoration");
27  if ( sizeof(tableinfo)==0 ) {
28  query = "alter table ob_class add (obdecoration text)";
29  oHandle->big_query(query);
30  MESSAGE("Modified Table ob_class with decoration column!");
31  }
32 }
33 
34 int enable_decorations()
35 {
36  array tableinfo = oHandle->list_fields("ob_class", "obdecoration");
37  if ( sizeof(tableinfo)==0 )
38  return 0;
39  return 1;
40 }
41 
42 
43 mapping check_updates(object dbupdates, function update_classtableobject)
44 {
45  mixed err;
46  mapping resultmap = ([ ]);
47 
48  object update = dbupdates->get_object_byname("classIndex");
49  if ( !objectp(update) ) {
50  string query = "create index class on ob_class (ob_class)";
51  catch(oHandle->big_query(query));
52  query = "create fulltext index obkeywords on ob_class (obkeywords)";
53  err=catch(oHandle->big_query(query));
54  resultmap->classIndex = "Successfully created index class on ob_class !\n";
55 
56  if ( err != 0 ) {
57  FATAL("Database: Class-Update Failed:\n%O\n%O", err[0], err[1]);
58  resultmap->classIndex = sprintf("Failed:\n%O\n%O", err[0], err[1]);
59  }
60 
61  }
62  update = dbupdates->get_object_byname("classKeywords");
63  if ( !objectp(update) ) {
64  int ts = time();
65  object res = oHandle->big_query("select ob_id from ob_class where ob_class !='-'");
66  int sz = res->num_rows();
67  MESSAGE("Updating Objects in class table .... "+
68  "(might take a while: %d objects, ~%d minutes).....:\n ",
69  sz,
70  sz/4800);
71  for ( int i = 0; i < sz; i++ ) {
72  int oid = (int)res->fetch_row()[0];
73  update_classtableobject(oid);
74  if ( i%1000 == 0 )
75  write("#");
76  }
77  MESSAGE("** %d Objects updated in %d Seconds !", sz, time()-ts);
78  resultmap->classKeywords =
79  "Successfully installed keywords on ob_class !\n";
80  }
81  update = dbupdates->get_object_byname("binaryContent");
82  if ( !objectp(update) ) {
83  object res = oHandle->big_query("select ob_id from ob_class where ob_class !='-'");
84  int sz = res->num_rows();
85 
86  MESSAGE("Updating doc_data table - setting binary content blobs, ~%d minutes",
87  sz/3600);
88  int t = time();
89  res = oHandle->big_query("alter table doc_data modify rec_data blob");
90  resultmap->binaryContent = "Successfully updated CONTENT !\n";
91  MESSAGE("Installed binary-content Update in database in %d seconds",
92  time() - t);
93  }
94 
95  object keywordUpdate = dbupdates->get_object_byname("keywords");
96  if ( !objectp(keywordUpdate) ) {
97  object attr = get_factory(CLASS_OBJECT)->describe_attribute(OBJ_KEYWORDS);
98  attr->set_acquire(0);
99  get_factory(CLASS_OBJECT)->register_attribute(attr);
100  array tables = oHandle->list_tables();
101  catch(oHandle->big_query("drop index obkeywords on ob_class"));
102  if ( search(tables, "mi_keyword_index") != -1 ) {
103  object res = oHandle->big_query("select k,v from mi_keyword_index");
104  object tagmod = get_module("tagging");
105 
106  for ( int i = 0; i < res->num_rows(); i++ ) {
107  int oid;
108  object obj;
109  string keyword;
110 
111  mixed erg = res->fetch_row();
112  if ( sscanf(erg[1], "%%%d", oid) != 1 )
113  continue;
114  if ( sscanf(erg[0], "\"%s\"", keyword) != 1 )
115  keyword = erg[0];
116 
117  obj = find_object(oid);
118  if ( !objectp(obj) )
119  continue;
120  if ( objectp(tagmod) )
121  tagmod->tag_object(obj, keyword);
122  }
123  catch(oHandle->big_query("drop table mi_keyword_index"));
124 
125  MESSAGE("Converted " + res->num_rows() + " keywords");
126  }
127  mixed err = catch(oHandle->big_query("create fulltext index obkeywords on ob_class (obkeywords)"));
128 
129  keywordUpdate = get_factory(CLASS_DOCUMENT)->execute((["name":"keywords"]));
130  keywordUpdate->set_content("Updated documents and acquire for OBJ_KEYWORDS!"+
131  (err!=0?sprintf("%O\n%O\n",err[0],err[1]):""));
132  keywordUpdate->move(dbupdates);
133  }
134  // userlookup index on login
135  object userLookupUpdate = dbupdates->get_object_byname("userLookup");
136  if ( !objectp(userLookupUpdate) ) {
137  catch(oHandle->big_query("create index loginlookup on i_userlookup (login)"));
138  catch(oHandle->big_query("create index emaillookup on i_userlookup (email)"));
139  catch(oHandle->big_query("create index firstnamelookup on i_userlookup (firstname)"));
140  catch(oHandle->big_query("create index lastnamelookup on i_userlookup (lastname)"));
141 
142  userLookupUpdate = get_factory(CLASS_DOCUMENT)->execute((["name":"userLookup"]));
143  userLookupUpdate->set_content("Created indices on i_userlookup !");
144  userLookupUpdate->move(dbupdates);
145 
146  }
147  object securityCache = dbupdates->get_object_byname("securityCache");
148  if ( !objectp(securityCache) ) {
149  catch(oHandle->big_query("create index securityklookup on i_security_cache \
150 (k)"));
151  catch(oHandle->big_query("create index securityvlookup on i_security_cache \
152 (v)"));
153  securityCache = get_factory(CLASS_DOCUMENT)->execute((["name":"securityCach\
154 e"]));
155  securityCache->set_content("Created index on security cache !");
156  securityCache->move(dbupdates);
157  }
158 
159  object obDataIndex = dbupdates->get_object_byname("obDataIndex");
160  if ( !objectp(obDataIndex) ) {
161  MESSAGE("Creating index on ob_data - could take a while depending on the size of your database!");
162  catch(oHandle->big_query("create index obdatalookup on ob_data (ob_data(40))"));
163  obDataIndex = get_factory(CLASS_DOCUMENT)->execute((["name":"obDataIndex"]));
164  obDataIndex->set_content("Created index on ob_data");
165  obDataIndex->move(dbupdates);
166  }
167 
168  object binaryAttr = dbupdates->get_object_byname("Binary_Attributes");
169  if (!objectp(binaryAttr)) {
170  MESSAGE("Updating database (changing ob_data to use varchar(128) binary for ob_attr) - could take a while depending on the size of your database!");
171  mixed err=catch(oHandle->big_query("alter table ob_data modify ob_attr varchar(128) binary"));
172  if (err)
173  MESSAGE("Error: %O\n%O", err[0], err[1]);
174  else {
175  binaryAttr = get_factory(CLASS_DOCUMENT)->execute((["name":"Binary_Attributes",]));
176  binaryAttr->set_content("Collation on ob_data set to utf8_bin");
177  binaryAttr->move(dbupdates);
178  }
179  }
180 
181  return resultmap;
182 }
183 
184 void create_tables()
185 {
186  MESSAGE("creating table \"doc_data\" ");
187  oHandle->big_query("create table doc_data ("+
188  " rec_data text, "+
189  " doc_id int, "+
190  " rec_order int, "+
191  " primary key (doc_id, rec_order)"+
192  ") AVG_ROW_LENGTH=65535 MAX_ROWS=4294967296");
193  //FIXME: postgres does not support (and probably not even need)
194  //AVG_ROW_LENGTH and MAX_ROWS in this place
195 
196 
197  MESSAGE("creating table \"ob_class\" ");
198  oHandle->big_query("create table ob_class ("+
199  " ob_id int primary key, "+
200  " ob_class char(128) "+
201  ")");
202 
203  MESSAGE("creating table \"ob_data\" ");
204  oHandle->big_query("create table ob_data ("+
205  " ob_id int, "+
206  " ob_ident char(15),"+
207  " ob_attr varchar(128) binary, "+
208  " ob_data mediumtext,"+
209  " unique(ob_id, ob_ident, ob_attr)"+
210  ")");
211  MESSAGE("creating table \"variables\" ");
212  oHandle->big_query("create table variables ("+
213  " var char(100) primary key, "+
214  " value int"+
215  ")");
216 }
217 
218 
219 
220 };