aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/grum_plugins_classes-2/tables.class.inc.php
blob: f29092caa932dca51718402f4a05e2db0f6363c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php 

/* -----------------------------------------------------------------------------
  class name: manage_tables
  class version: 1.3
  date: 2007-12-02
  ------------------------------------------------------------------------------
  author: grum at grum.dnsalias.com
  << May the Little SpaceFrog be with you >>
  ------------------------------------------------------------------------------
  
  this class provides base functions to manage tables while plugin installation
    - constructor manage_tables($tables)
    - (public) function create_tables($tables_def)
    - (public) function update_tables_fields($tables_alteration)
    - (public) function drop_tables()
    - (public) function rename_tables($list)  -v1.1
    - (public) function tables_exists()  -v1.1
    - (public) function export($filename, $options, $tables, $infos)  -v1.3
    - (public) function multi_queries($queries)  -v1.3
    - (public) function import($filename)  -v1.3

  ------------------------------------------------------------------------------
  v1.1 + add rename_tables($list) function
       + add tables_exists() function
  v1.2 + add export($filename, $options, $tables) function
  v1.3 + modify export($filename, $options, $tables, $infos, $resultboolean) function
          + new parameters '$infos' allows to add some information on the
            exported file
          + add 'delete' and 'colnames' options
          + $resultbooelan option for return
       + add multi_queries($queries) function
       + add import($filename) function 

   -------------------------------------------------------------------------- */
class manage_tables
{
  var $tables;    //array of tables names
  var $version = "1.3";

  function manage_tables($tables)
  {
    $this->tables = $tables;
  }
  
  /* 
      create tables
      $tables_def is an array of SQL CREATE queries

      return true if everything is ok, otherwise tablename 
  */
  function create_tables($tables_def)
  {
    //deleting tables if exists
    $this->drop_tables();

    for($i=0;$i<count($tables_def);$i++)
    {
      $result=pwg_query($tables_def[$i]);
      if(!$result)
      {
        //if an error occurs, deleting created tables
        $this->drop_tables();
        return(false);
      }
    }
    return(true);
  }

  /* update tables definition 
     $tables_alteration : array of arrays
      example :
      $tables_alteration['table1']=array(
        "attribute1" => " ADD COLUMN `attribute1` text null default ''",
        "attribute2" => " ADD COLUMN `attribute2` text null default ''"));
      $tables_alteration['table2']=array(
        "attribute1" => " ADD COLUMN `attribute1` text null default ''",
        "attribute2" => " ADD COLUMN `attribute2` text null default ''"));

      return true if no error, otherwise return table.fields of error 
  */
  function update_tables_fields($tables_alteration)
  {
    if(!is_array($tables_alteration))
    {
      return('');
    }

    reset($tables_alteration);
    while (list($key, $val) = each($tables_alteration))
    {
      $sql="SHOW COLUMNS FROM $key";
      $result=pwg_query($sql);
      if($result)
      {
        $columns=array();
        while($row=mysql_fetch_assoc($result))
        { 
          array_push($columns, $row['Field']); 
        }

        reset($val);
        while (list($attname, $sql) = each($val))
        {
          if(!in_array($attname, $columns))
          {
            $result=pwg_query("ALTER TABLE `$key` ".$sql);
            if(!$result)
            {
              return($key.".".$attname);
            }
          }
        }
      }
    }
    return(true);
  }


  /* 
      delete tables listed in $this->tables_list
  */
  function drop_tables()
  {
    foreach($this->tables as $key => $table_name)
    {
      $sql="DROP TABLE IF EXISTS ".$table_name;
      $result=pwg_query($sql);
    }
  }

  /*
      rename tables name of list
        $list is an array('old_name' => 'new_name')
      return true if ok, else old table name
  */
  function rename_tables($list)
  {
    $tmplist=array_flip($this->tables);
    foreach($list as $key => $val)
    {
      if(isset($tmplist[$key]))
      {
        $this->tables[$tmplist[$key]] = $val;
        $sql="ALTER TABLE `$key` RENAME TO `$val`";
        if(!pwg_query($sql))
        {
          return($key);
        }
      }
      else
      {
        return($key);
      }
    }
    return(true);
  }

  /*
    return true if all listed tables exists
  */
  function tables_exists()
  {
    $list=array_flip($this->tables);
    $sql="SHOW TABLES";
    $result=pwg_query($sql);
    if($result)
    {
      while($row=mysql_fetch_row($result))
      {
        if(isset($list[$row[0]]))
        {
          array_splice($list, $row[0],1);
        }
      }
    }
    if(count($list)>0)
    {
      return(false);
    }
    else
    {
      return(true);
    }
  }

  /*
    export all tables as SQL in a text file

    each query end with a " -- EOQ" ; it's just a method to increase parsing for
    import function

      $filename : name of the file
      $options : array of options like
                    array(
                      'drop' => true/false,  //add DROP TABLE statements
                      'create' => true/false,  //add CREATE TABLE statements
                      'insert' => true/false,  //add INSERT statements
                      'delete' => true/false, //add delete statements
                      'colnames' => true/false, //add columns names for inserts statements
                    )
      $tables : array of tables names to export
                    array('tablename1', 'tablenamen', 'tablename3', ...)
                  if empty, assume that all tables have to be exported
      $infos : additional info written in exported file (as comment)
      $resultboolean : if true, result is true/false ; 
                       if false, if result, return a string with nfo about export
  */
  function export($filename, $options=array(), $tables=array(), $infos="", $resultboolean=true)
  {
    $defaultopt=array(
      'drop' => true,
      'create' => true,
      'insert' => true,
      'delete' => false,
      'colnames' => false
    );

    if(!isset($options['drop']))
    {
      $options['drop']=$defaultopt['drop'];
    }
    if(!isset($options['create']))
    {
      $options['create']=$defaultopt['create'];
    }
    if(!isset($options['insert']))
    {
      $options['insert']=$defaultopt['insert'];
    }
    if(!isset($options['delete']))
    {
      $options['delete']=$defaultopt['delete'];
    }
    if(!isset($options['colnames']))
    {
      $options['colnames']=$defaultopt['colnames'];
    }
    if(count($tables)==0)
    {
      $tables=$this->tables;
    }

    $resultnfo='';

    $returned=true;
    $text='
-- ***************************************************************              -- EOQ
-- * SQL export made with Grum Plugins Classes (Export tool r'.$this->version.')         -- EOQ
-- * Export date    :'.date('Y-m-d H:i:s').'                                    -- EOQ
-- * Export options :';
if($options['drop']){$text.=' [drop]';}
if($options['delete']){$text.=' [delete]';}
if($options['create']){$text.=' [create]';}
if($options['insert']){$text.=' [insert]';}
if($options['colnames']){$text.=' [colnames]';}
$text.="                            -- EOQ";
if($infos!="")
{
  $text.='
-- * '.$infos." -- EOQ";
}
$text.='
-- ***************************************************************              -- EOQ

';
    foreach($tables as $key => $val)
    {
      $countelems=0;

      $text.="

-- ***************************************************************              -- EOQ
-- * Statements for ".$this->tables[$key]." table                               -- EOQ
-- ***************************************************************              -- EOQ
";

      if($options['drop'])
      {
        $text.=sprintf("DROP TABLE `%s`; -- EOQ\n", $this->tables[$key]);
      }

      if($options['delete'])
      {
        $text.=sprintf("DELETE FROM `%s`; -- EOQ\n", $this->tables[$key]);
      }

      if($options['create'])
      {
        $sql='SHOW CREATE TABLE '.$this->tables[$key];
        $result=pwg_query($sql);
        if($result)
        {
          while($row=mysql_fetch_row($result))
          {
            $text.=sprintf("%s; -- EOQ\n", $row[1]);
          }
        }
        else
        {
          $returned=false;
        }
      }

      if($options['insert'])
      {
        $colnames="";
        if($options['colnames'])
        {
          $sql='SHOW COLUMNS FROM `'.$this->tables[$key].'`';
          $result=pwg_query($sql);
          if($result)
          {
            $tmp=array();
            while($row=mysql_fetch_row($result))
            {
              $tmp[]=$row[0];
            }
          }
          $colnames='('.implode(',', $tmp).')';
        }
    
        $sql='SELECT * FROM '.$this->tables[$key];
        $result=pwg_query($sql);
        if($result)
        {
          while($row=mysql_fetch_row($result))
          {
            foreach($row as $key2 => $val2)
            {
              $row[$key2]="'".addslashes($val2)."'";
            }
            $text.=sprintf("INSERT INTO `%s` %s VALUES(%s); -- EOQ\n", $this->tables[$key], $colnames, implode(', ', $row));
            $countelems++;
          }
        }
        else
        {
          $returned=false;
        }
        $resultnfo.=$key.':'.$countelems.'@';
      }
    }
    $fhandle=fopen($filename, 'wb');
    if($fhandle)
    {
      fwrite($fhandle, $text);
      fclose($fhandle);
    }
    else
    {
      $returned=false;
    }
    if(($resultboolean==false)&&($returned))
    {
      $returned=$resultnfo;
    }
    return($returned);
  }

  /*
    import an .sql file
      $filename : name of the file
      'errors' : -1 file don't exists
                 -2 can't open file
  */
  function import($filename)
  {
    $return = array(
      'numinsert'=>0,
      'numdelete'=>0,
      'numdrop'=>0,
      'numcreate'=>0,
      'errors'=>array(),
      'total_ok'=>0
    );

    if(file_exists($filename))
    {
      $fhandle=fopen($filename, 'r');
      if($fhandle)
      {
        $queries=fread($fhandle, filesize($filename));
        fclose($fhandle);
        $return=$this->multi_queries($queries);
      }
      else
      {
        $return['errors']=-2;
      }
    }
    else
    {
      $return['errors']=-1;
    }
    return($return);
  }

  /*
    execute multiple query
      each query have to be separated by a "-- EOQ\n"
      
      $queries : sql queries
  */
  function multi_queries($queries)
  {
    $queries_list=preg_split(
      '/\s*;?\s*--\s+EOQ[\r\n]{1}/i', $queries, -1, PREG_SPLIT_NO_EMPTY);

    $return = array(
      'numinsert'=>0,
      'numdelete'=>0,
      'numdrop'=>0,
      'numcreate'=>0,
      'errors'=>array(),
      'total_ok'=>0
    );

    $i=0;
    foreach($queries_list as $key => $sql)
    {
      $i++;
      @$result=pwg_query($sql);
      if($result)
      {
        $return['total_ok']++;
        if(preg_match('/\b[\s]*insert[\s]+/i', $sql)>0)
        {$return['numinsert']++;}
        elseif(preg_match('/\b[\s]*drop[\s]+/i', $sql)>0)
        {$return['numdrop']++;}
        elseif(preg_match('/\b[\s]*delete[\s]+/i', $sql)>0)
        {$return['numdelete']++;}
        elseif(preg_match('/\b[\s]*create[\s]+/i',$sql)>0)
        {$return['numcreate']++;}
      }
      else
      {
        array_push($return['errors'], '['.$i.'] '.$sql);
      }
    }
    return($return);
  }

} //class


?>