/[drupal]/contributions/modules/tw/tw.module
ViewVC logotype

Diff of /contributions/modules/tw/tw.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.25, Tue Apr 14 21:04:33 2009 UTC revision 1.1.2.26, Fri May 1 01:44:13 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: tw.module,v 1.1.2.24 2009/04/14 20:36:59 mikeryan Exp $  // $Id: tw.module,v 1.1.2.25 2009/04/14 21:04:33 mikeryan Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 14  Line 14 
14   * Add tables to be managed by Table Wizard   * Add tables to be managed by Table Wizard
15   *   *
16   * @param $tablelist The name of a table or an array of table names. If a period is present,   * @param $tablelist The name of a table or an array of table names. If a period is present,
17   *                   the form connection.tablename is assumed (where connection is an   *                   the form connection.tablename is assumed (where connection is an
18   *                   alternate database in $db_url). The tablename must be the logical,   *                   alternate database in $db_url). The tablename must be the logical,
19   *                   not physical, name of the table. That is, if you have a table prefix   *                   not physical, name of the table. That is, if you have a table prefix
20   *                   of pf_ defined, pass in 'node' instead of 'pf_node'.   *                   of pf_ defined, pass in 'node' instead of 'pf_node'.
# Line 30  function tw_add_tables($tablelist) { Line 30  function tw_add_tables($tablelist) {
30      if (isset($split[1])) {      if (isset($split[1])) {
31        $connection = $split[0];        $connection = $split[0];
32        $tablename = $split[1];        $tablename = $split[1];
33      } else {      }
34        else {
35        $connection = 'default';        $connection = 'default';
36      }      }
37    
38      // See if we're already managing this table      // See if we're already managing this table
39      $sql = "SELECT twtid FROM {tw_tables}      $sql = "SELECT twtid FROM {tw_tables}
40              WHERE connection='%s' AND tablename='%s'";              WHERE connection='%s' AND tablename='%s'";
41      $twtid = db_result(db_query($sql, $connection, $tablename));      $twtid = db_result(db_query($sql, $connection, $tablename));
42      if (!$twtid) {      if (!$twtid) {
# Line 43  function tw_add_tables($tablelist) { Line 44  function tw_add_tables($tablelist) {
44                  (connection, tablename)                  (connection, tablename)
45                  VALUES('%s', '%s')",                  VALUES('%s', '%s')",
46                 $connection, $tablename);                 $connection, $tablename);
47      }      }
48    
49      // Analysis populates the tw_columns table      // Analysis populates the tw_columns table
50      tw_perform_analysis($tablename, $connection);      tw_perform_analysis($tablename, $connection);
51    }    }
# Line 58  function tw_add_tables($tablelist) { Line 59  function tw_add_tables($tablelist) {
59   * Remove tables from the Table Wizard   * Remove tables from the Table Wizard
60   *   *
61   * @param $tablelist The name of a table or an array of table names. If a period is present,   * @param $tablelist The name of a table or an array of table names. If a period is present,
62   *                   the form connection.tablename is assumed (where connection is an   *                   the form connection.tablename is assumed (where connection is an
63   *                   alternate database in $db_url). The tablename must be the logical,   *                   alternate database in $db_url). The tablename must be the logical,
64   *                   not physical, name of the table. That is, if you have a table prefix   *                   not physical, name of the table. That is, if you have a table prefix
65   *                   of pf_ defined, pass in 'node' instead of 'pf_node'.   *                   of pf_ defined, pass in 'node' instead of 'pf_node'.
# Line 72  function tw_remove_tables($tablelist) { Line 73  function tw_remove_tables($tablelist) {
73      if (isset($split[1])) {      if (isset($split[1])) {
74        $connection = $split[0];        $connection = $split[0];
75        $tablename = $split[1];        $tablename = $split[1];
76      } else {      }
77        else {
78        $connection = 'default';        $connection = 'default';
79      }      }
80      $sql = "SELECT twtid FROM {tw_tables}      $sql = "SELECT twtid FROM {tw_tables}
81              WHERE connection='%s' AND tablename='%s'";              WHERE connection='%s' AND tablename='%s'";
82      $twtid = db_result(db_query($sql, $connection, $tablename));      $twtid = db_result(db_query($sql, $connection, $tablename));
83      if ($twtid) {      if ($twtid) {
# Line 108  function tw_add_fk($tablename, $colname) Line 110  function tw_add_fk($tablename, $colname)
110    if (isset($split[1])) {    if (isset($split[1])) {
111      $connection = $split[0];      $connection = $split[0];
112      $tablename = $split[1];      $tablename = $split[1];
113    } else {    }
114      else {
115      $connection = 'default';      $connection = 'default';
116    }    }
117    $sql = "SELECT twtid FROM {tw_tables}    $sql = "SELECT twtid FROM {tw_tables}
118            WHERE connection='%s' AND tablename='%s'";            WHERE connection='%s' AND tablename='%s'";
119    $twtid = db_result(db_query($sql, $connection, $tablename));    $twtid = db_result(db_query($sql, $connection, $tablename));
120    if ($twtid) {    if ($twtid) {
# Line 135  function tw_add_fk($tablename, $colname) Line 138  function tw_add_fk($tablename, $colname)
138  function tw_add_relationship($leftcol, $rightcol) {  function tw_add_relationship($leftcol, $rightcol) {
139    if (is_numeric($leftcol)) {    if (is_numeric($leftcol)) {
140      $leftcolid = $leftcol;      $leftcolid = $leftcol;
141    } else {    }
142      else {
143      $split = explode('.', $leftcol);      $split = explode('.', $leftcol);
144      if (count($split) == 3) {      if (count($split) == 3) {
145        $leftcolid = tw_get_column_id($split[0], $split[1], $split[2]);        $leftcolid = tw_get_column_id($split[0], $split[1], $split[2]);
146      } else {      }
147        else {
148        $leftcolid = tw_get_column_id('default', $split[0], $split[1]);        $leftcolid = tw_get_column_id('default', $split[0], $split[1]);
149      }      }
150    }    }
151    if (is_numeric($rightcol)) {    if (is_numeric($rightcol)) {
152      $rightcolid = $rightcol;      $rightcolid = $rightcol;
153    } else {    }
154      else {
155      $split = explode('.', $rightcol);      $split = explode('.', $rightcol);
156      $rightcolid = tw_get_column_id($split[0], $split[1]);      $rightcolid = tw_get_column_id($split[0], $split[1]);
157    }    }
# Line 156  function tw_add_relationship($leftcol, $ Line 162  function tw_add_relationship($leftcol, $
162               $leftcolid, $rightcolid);               $leftcolid, $rightcolid);
163      // Make sure Views hears about the newly available relationship      // Make sure Views hears about the newly available relationship
164      drupal_flush_all_caches();      drupal_flush_all_caches();
165    }    }
166  }  }
167    
168  /**  /**
# Line 191  function tw_get_column_id($connection, $ Line 197  function tw_get_column_id($connection, $
197  }  }
198    
199  /**  /**
200   * Analyze a table - determine value ranges, empty fields, etc., and   * Analyze a table - determine value ranges, empty fields, etc., and
201   * populate {tw_columns}   * populate {tw_columns}
202   *   *
203   * @param $tablename   * @param $tablename
# Line 212  function tw_perform_analysis($tablename, Line 218  function tw_perform_analysis($tablename,
218    if (!$schema) {    if (!$schema) {
219      drupal_set_message(t('Could not analyze !tablename (connection !conn)',      drupal_set_message(t('Could not analyze !tablename (connection !conn)',
220        array('!tablename' => $tablename, '!conn' => $connection)), 'error');        array('!tablename' => $tablename, '!conn' => $connection)), 'error');
221    } else {    }
222      else {
223      if (isset($schema['primary key'])) {      if (isset($schema['primary key'])) {
224        $pks = $schema['primary key'];        $pks = $schema['primary key'];
225      } else {      }
226        else {
227        $pks = array();        $pks = array();
228      }      }
229      $weight = 0;      $weight = 0;
# Line 223  function tw_perform_analysis($tablename, Line 231  function tw_perform_analysis($tablename,
231      if (!is_array($schema)) {      if (!is_array($schema)) {
232        drupal_set_message(t('No fields found for !tablename (connection !conn)',        drupal_set_message(t('No fields found for !tablename (connection !conn)',
233          array('!tablename' => $tablename, '!conn' => $connection)));          array('!tablename' => $tablename, '!conn' => $connection)));
234      } else {      }
235        else {
236        foreach ($schema['fields'] as $colname => $coldef) {        foreach ($schema['fields'] as $colname => $coldef) {
237          db_set_active($connection);          db_set_active($connection);
238          $coltype = $coldef['type'];          $coltype = $coldef['type'];
239          // Analyze the values in the table          // Analyze the values in the table
240          $sql = 'SELECT MIN('.tw_quote_column($colname).') FROM {' . $tablename . '}';          $sql = 'SELECT MIN(' . tw_quote_column($colname) . ') FROM {' . $tablename . '}';
241          $minvalue = db_result(db_query($sql));          $minvalue = db_result(db_query($sql));
242          $sql = 'SELECT MAX('.tw_quote_column($colname).') FROM {' . $tablename . '}';          $sql = 'SELECT MAX(' . tw_quote_column($colname) . ') FROM {' . $tablename . '}';
243          $maxvalue = db_result(db_query($sql));          $maxvalue = db_result(db_query($sql));
244          if (tw_column_type($coltype) == 'text') {          if (tw_column_type($coltype) == 'text') {
245            $sql = 'SELECT MIN(CHAR_LENGTH('.tw_quote_column($colname).')) FROM {' . $tablename . '}';            $sql = 'SELECT MIN(CHAR_LENGTH(' . tw_quote_column($colname) . ')) FROM {' . $tablename . '}';
246            $minlength = db_result(db_query($sql));            $minlength = db_result(db_query($sql));
247            if (!isset($minlength)) {            if (!isset($minlength)) {
248              $minlength = 0;              $minlength = 0;
249            }            }
250            $sql = 'SELECT MAX(CHAR_LENGTH('.tw_quote_column($colname).')) FROM {' . $tablename . '}';            $sql = 'SELECT MAX(CHAR_LENGTH(' . tw_quote_column($colname) . ')) FROM {' . $tablename . '}';
251            $maxlength = db_result(db_query($sql));            $maxlength = db_result(db_query($sql));
252            if (!isset($maxlength)) {            if (!isset($maxlength)) {
253              $maxlength = 0;              $maxlength = 0;
254            }            }
255            if ($maxlength == 0) {            if ($maxlength == 0) {
256              $isempty = TRUE;              $isempty = TRUE;
257            } else {            }
258              else {
259              $isempty = FALSE;              $isempty = FALSE;
260            }            }
261          } else {          }
262            else {
263            if ($minvalue || $maxvalue) {            if ($minvalue || $maxvalue) {
264              $isempty = FALSE;              $isempty = FALSE;
265            } else {            }
266              else {
267              $isempty = TRUE;              $isempty = TRUE;
268            }            }
269          }          }
# Line 284  function tw_perform_analysis($tablename, Line 296  function tw_perform_analysis($tablename,
296            $colrecord->maxstring = $maxvalue;            $colrecord->maxstring = $maxvalue;
297            unset($colrecord->minvalue);            unset($colrecord->minvalue);
298            unset($colrecord->maxvalue);            unset($colrecord->maxvalue);
299          } else {          }
300            else {
301            if (!$minvalue) {            if (!$minvalue) {
302              $minvalue = 0;              $minvalue = 0;
303            }            }
# Line 302  function tw_perform_analysis($tablename, Line 315  function tw_perform_analysis($tablename,
315            $colrecord->primarykey = (int)TRUE;            $colrecord->primarykey = (int)TRUE;
316            // Assume PKs are always available as FKs            // Assume PKs are always available as FKs
317            $colrecord->availablefk = (int)TRUE;            $colrecord->availablefk = (int)TRUE;
318          } else {          }
319            else {
320            $colrecord->primarykey = (int)FALSE;            $colrecord->primarykey = (int)FALSE;
321            // Leave previous availablefk setting alone            // Leave previous availablefk setting alone
322          }          }
323    
324          if (isset($colrecord->twcid)) {          if (isset($colrecord->twcid)) {
325            drupal_write_record('tw_columns', $colrecord, 'twcid');            drupal_write_record('tw_columns', $colrecord, 'twcid');
326          } else {          }
327            else {
328            drupal_write_record('tw_columns', $colrecord);            drupal_write_record('tw_columns', $colrecord);
329          }          }
330        }        }
# Line 338  function tw_perform_analysis($tablename, Line 353  function tw_perform_analysis($tablename,
353   *   *
354   * @param $schematype   * @param $schematype
355   *   The schema type to identify   *   The schema type to identify
356   * @return   * @return
357   *   Generic column type (text, numeric, or datetime) - unknown if unrecognized   *   Generic column type (text, numeric, or datetime) - unknown if unrecognized
358   */   */
359  function tw_column_type($schematype) {  function tw_column_type($schematype) {
# Line 374  function tw_quote_column($colname) { Line 389  function tw_quote_column($colname) {
389    global $db_type;    global $db_type;
390    if ($db_type == 'mysql' || $db_type == 'mysqli') {    if ($db_type == 'mysql' || $db_type == 'mysqli') {
391      $quote = '`';      $quote = '`';
392    } elseif ($db_type == 'pgsql') {    }
393      elseif ($db_type == 'pgsql') {
394      $quote = '"';      $quote = '"';
395    } else {    }
396      else {
397      drupal_set_message(t('Unrecognized database type %dbtype', array('%dbtype' => $db_type)));      drupal_set_message(t('Unrecognized database type %dbtype', array('%dbtype' => $db_type)));
398    }    }
399    return $quote.$colname.$quote;    return $quote . $colname . $quote;
400  }  }
401    
402  //////////////////////////////////////////////////////////  //////////////////////////////////////////////////////////
# Line 388  function tw_quote_column($colname) { Line 405  function tw_quote_column($colname) {
405  define('TW_ACCESS', 'table wizard administration');  define('TW_ACCESS', 'table wizard administration');
406    
407  /**  /**
408   * Implementation of hook_menu()   * Implementation of hook_menu().
409   */   */
410  function tw_menu() {  function tw_menu() {
411    $items = array();    $items = array();

Legend:
Removed from v.1.1.2.25  
changed lines
  Added in v.1.1.2.26

  ViewVC Help
Powered by ViewVC 1.1.2