diff options
author | Eudyptula <eitan@mosenkis.net> | 2009-06-24 13:46:55 -0400 |
---|---|---|
committer | Eudyptula <eitan@mosenkis.net> | 2009-06-24 13:46:55 -0400 |
commit | fd16c4ce52767634818c423c0ec7a3795d5ed31c (patch) | |
tree | a1783eef6519b93730c7b2857da9e657becfed64 /shared/classes | |
parent | Major improvements to logging in frontend and backend; moved log_command func... (diff) | |
download | ingenue-fd16c4ce52767634818c423c0ec7a3795d5ed31c.tar.gz ingenue-fd16c4ce52767634818c423c0ec7a3795d5ed31c.tar.bz2 ingenue-fd16c4ce52767634818c423c0ec7a3795d5ed31c.zip |
Updates to sql_row_obj class; moved newclass.php to update_sql_classes.php and updated it
Diffstat (limited to 'shared/classes')
-rw-r--r-- | shared/classes/0sql_row_obj.php | 120 | ||||
-rw-r--r-- | shared/classes/buildlog.php | 31 | ||||
-rw-r--r-- | shared/classes/commandlog_entry.php (renamed from shared/classes/buildlog_entry.php) | 9 | ||||
-rw-r--r-- | shared/classes/profile.php | 2 | ||||
-rw-r--r-- | shared/classes/session.php | 2 | ||||
-rw-r--r-- | shared/classes/task.php | 6 | ||||
-rw-r--r-- | shared/classes/user.php | 2 |
7 files changed, 105 insertions, 67 deletions
diff --git a/shared/classes/0sql_row_obj.php b/shared/classes/0sql_row_obj.php index a31403c..f2f815f 100644 --- a/shared/classes/0sql_row_obj.php +++ b/shared/classes/0sql_row_obj.php @@ -28,10 +28,9 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up // This line is the static cache so each class is only initialized once per page-load (maybe this should really be once per class edit, saved in a file somewhere...) private static $cache=array(), $table_cache=array(); // These are input by the source class (auto-filled by cache after init) - // $ids - set this to use multi_key for cases when more than one column is necessary to find a particular row - protected $table, $columns, $ids, $primary_key; + protected $table, $columns, $primary_key; // TODO $unique_keys // These are loaded from the static cache - private $auto_increment, $num_key, $misc_key, $multi_key; + private $auto_increment, $num_key, $misc_key; // These are run-time variables private $db_values=array(), $values, $ref_cache; // Sets the PDO object to use @@ -48,7 +47,7 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up // Makes an SQL query using $sql and returns the resulting object private static function sql_query($q) { self::check_pdo_obj(); -// echo $q."\n"; + // echo $q."\n"; return self::$pdo->query($q); } public static function sql_quote_string($s) { @@ -74,7 +73,8 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up } } private function init_constructors() { - // Sets the primary key as a constructor if it exists + // Primary key is its own key now, we don't care if others are available +/* // Sets the primary key as a constructor if it exists if (isset($this->primary_key)) { $pk=$this->columns[$this->primary_key]; if ($this->columns[$this->primary_key]->is_numeric()) { @@ -82,8 +82,9 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up } else { $this->misc_key=$this->primary_key; } - } + }*/ // Fills num and misc constructors with other unique columns + // TODO check where this is called and see if we really need this first check if (!isset($this->num_key) || !isset($this->misc_key)) { foreach ($this->columns as $name => $col) { if ($col->unique) { @@ -105,11 +106,6 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up } } } - // Fills multi-constructor if manually set (no way to automatically guess this one) - if (isset($this->ids)) { - $this->multi_key=explode(' ', $this->ids); - unset($this->ids); - } } // Inserts generated data to the cache so we won't have to do this again private function cache_me() { @@ -119,7 +115,6 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up self::$cache['auto_increment'][get_class($this)]=&$this->auto_increment; self::$cache['num_key'][get_class($this)]=&$this->num_key; self::$cache['misc_key'][get_class($this)]=&$this->misc_key; - self::$cache['multi_key'][get_class($this)]=&$this->multi_key; // Inserts this class into a lookup-table if necessary so we know which tables are serviced by which classes if (!isset(self::$table_cache[$this->table])) { self::$table_cache[$this->table]=get_class($this); @@ -142,10 +137,16 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up $this->columns[$name]=new sql_col($line); } else { if (strpos($line, 'PRIMARY KEY') === 0) { - $line=explode(' ', $line, 3); - $line=trim($line[2]); - $this->primary_key=substr($line, 2, strlen($line)-4); + $line=substr($line, strpos($line, '(')+1); + $line=substr($line, 0, strrpos($line, ')')); + $line=explode(',', $line); + foreach ($line as $col) { + $col=trim($col); + $col=trim($col, '`'); + $this->primary_key[]=$col; + } } elseif (strpos($line, 'UNIQUE KEY') === 0) { + // TODO process multi-column unique keys, multiple unique keys correctly (steal the text processing side from PRIMARY KEY above) $line=explode(' ', $line, 3); $line=trim($line[2]); $line=substr($line, strrpos($line, '(')); @@ -168,7 +169,6 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up $this->auto_increment=&self::$cache['auto_increment'][get_class($this)]; $this->num_key=&self::$cache['num_key'][get_class($this)]; $this->misc_key=&self::$cache['misc_key'][get_class($this)]; - $this->multi_key=&self::$cache['multi_key'][get_class($this)]; } else { if (isset($this->columns)) { $this->init_from_array(); @@ -189,50 +189,59 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up } // One argument - this may be one of four things: // 1. An array directly from an sql result fetch() (in associative form - TODO accept numeric index), - // 2. An integer intended to be used to fetch the object from the DB by unique column - // 3. A non-integer intended to be used to fetch the object from the DB by unique column - // 4. Just the value to be put into the first column (handled like any arbitrary number of values) + // 2. The primary key if the primary key has one column + // 3. An integer intended to be used to fetch the object from the DB by unique numeric column + // 4. A string intended to be used to fetch the object from the DB by unique column + // 5. Just the value to be put into the first column (handled like any arbitrary number of values) if (func_num_args() == 1) { if (is_array(func_get_arg(0))) { // We're assuming that the array comes from sql_result->fetch() - assoc form, so we do from_array(from_db=true), but this may not be wise $this->from_array(func_get_arg(0), true); self::debug(get_class($this), 'array constructor'); return; - } else { - $arg=func_get_arg(0); - if (is_numeric($arg)) { - if (isset($this->num_key)) { - self::debug(get_class($this), 'numeric constructor'); - $r=self::sql_query('SELECT * FROM `'.$this->table.'` WHERE `'.$this->num_key.'`='.$this->columns[$this->num_key]->sql_value($arg)); - if ($r->rowCount() == 0) { - throw new Exception(get_class($this).' object constructed with single numeric value ('.$arg.') but found no rows with this in the `'.$this->num_key.'` column.'); - } else { - $this->from_array($r->fetch(), true); - return; - } - } - } elseif (isset($this->misc_key)) { - self::debug(get_class($this), 'misc constructor'); - $r=self::sql_query('SELECT * FROM `'.$this->table.'` WHERE `'.$this->misc_key.'`='.$this->columns[$this->misc_key]->sql_value($arg)); + } + $arg=func_get_arg(0); + if (isset($this->primary_key) && count($this->primary_key) == 1) { + if (is_numeric($arg) && $this->columns[$this->primary_key[0]]->is_numeric() || !$this->columns[$this->primary_key[0]]->is_numeric()) { + self::debug(get_class($this), 'primary key constructor ('.$this->primary_key[0].')'); + $r=self::sql_query('SELECT * FROM `'.$this->table.'` WHERE `'.$this->primary_key[0].'`='.$this->columns[$this->primary_key[0]]->sql_value($arg)); if ($r->rowCount() == 0) { - throw new Exception(get_class($this).' object constructed with single non-numeric value ('.$arg.') but found no rows with this in the `'.$this->misc_key.'` column.'); + throw new Exception(get_class($this).' object constructed with single argument ('.$arg.') but found no rows with this in the `'.$this->primary_key[0].'` column (PRIMARY_KEY).'); } else { $this->from_array($r->fetch(), true); return; } } } + if (is_numeric($arg) && isset($this->num_key)) { + self::debug(get_class($this), 'numeric constructor'); + $r=self::sql_query('SELECT * FROM `'.$this->table.'` WHERE `'.$this->num_key.'`='.$this->columns[$this->num_key]->sql_value($arg)); + if ($r->rowCount() == 0) { + throw new Exception(get_class($this).' object constructed with single numeric argument ('.$arg.') but found no rows with this in the `'.$this->num_key.'` column (UNIQUE numeric).'); + } else { + $this->from_array($r->fetch(), true); + return; + } + } elseif (isset($this->misc_key)) { + self::debug(get_class($this), 'misc constructor'); + $r=self::sql_query('SELECT * FROM `'.$this->table.'` WHERE `'.$this->misc_key.'`='.$this->columns[$this->misc_key]->sql_value($arg)); + if ($r->rowCount() == 0) { + throw new Exception(get_class($this).' object constructed with single value ('.$arg.') but found no rows with this in the `'.$this->misc_key.'` column (UNIQUE non-numeric).'); + } else { + $this->from_array($r->fetch(), true); + return; + } + } // We have a table that requires multiple columns to identify a given row // and we have the right number of arguments to expect that's what's happening - } elseif (isset($this->multi_key) && func_num_args() == count($this->multi_key)) { - self::debug(get_class($this), 'multi constructor'); + } elseif (isset($this->primary_key) && func_num_args() == count($this->primary_key)) { + self::debug(get_class($this), 'primary key constructor ('.implode(', ', $this->primary_key).')'); for ($i=0; $i<func_num_args(); $i++) { - $this->db_values[$this->multi_key[$i]]=func_get_arg($i); + $this->db_values[$this->primary_key[$i]]=func_get_arg($i); } $r=self::sql_query('SELECT * FROM `'.$this->table.'` WHERE '.$this->sql_id()); if ($r->rowCount() == 0) { - $e=get_class($this).' object constructed with multiple values ('; - throw new Exception(get_class($this).' object constructed with multiple values '.$this->sql_id().' but no rows were found.'); + throw new Exception(get_class($this).' object constructed with '.func_num_args().' values '.$this->sql_id().' but no rows were found (PRIMARY KEY).'); } else { $this->from_array($r->fetch(), true); return; @@ -331,25 +340,22 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up // 4. Multi key function sql_id() { if (isset($this->primary_key)) { - return '`'.$this->primary_key.'`='.$this->columns[$this->primary_key]->sql_value($this->db_values[$this->primary_key]); - } elseif (isset($this->num_key) && $this->db_values[$this->num_key] !== null) { - return '`'.$this->num_key.'`='.$this->columns[$this->num_key]->sql_value($this->db_values[$this->num_key]); - } elseif (isset($this->misc_key) && $this->db_values[$this->misc_key] !== null) { - return '`'.$this->misc_key.'`='.$this->columns[$this->misc_key]->sql_value($this->db_values[$this->misc_key]); - } elseif (isset($this->multi_key)) { - // TODO Doesn't check for null (maybe null is okay here? prob. not) - $id='('; + $id=count($this->primary_key)>1?'(':''; $i=0; - foreach ($this->multi_key as $name) { + foreach ($this->primary_key as $name) { if (++$i > 1) { $id.=' AND '; } $id.='`'.$name.'`='.$this->columns[$name]->sql_value($this->db_values[$name]); } - $id.=')'; + $id.=count($this->primary_key)>1?')':''; return $id; + } elseif (isset($this->num_key) && $this->db_values[$this->num_key] !== null) { + return '`'.$this->num_key.'`='.$this->columns[$this->num_key]->sql_value($this->db_values[$this->num_key]); + } elseif (isset($this->misc_key) && $this->db_values[$this->misc_key] !== null) { + return '`'.$this->misc_key.'`='.$this->columns[$this->misc_key]->sql_value($this->db_values[$this->misc_key]); } else { - throw new Exception('Tried to generate SQL to select unique '.get_class($this).' object, but there were no available unique indicators (primary key, numeric, misc, or multi).'); + throw new Exception('Tried to generate SQL to select unique '.get_class($this).' object, but there were no available unique indicators (primary key, numeric, misc).'); } } // If no argument is given, returns whether this object represents a row that is currently in the database. @@ -388,7 +394,7 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up $rows[]="\t".'`'.$name.'` '.$col->describe(); } if (isset($this->primary_key)) { - $rows[]="\t".'PRIMARY KEY (`'.$this->primary_key.'`)'; + $rows[]="\t".'PRIMARY KEY (`'.implode('`, `', $this->primary_key).'`)'; } foreach ($this->columns as $name => $col) { if ($col->unique && is_string($col->unique)) { @@ -402,7 +408,7 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up function to_php() { $r="class ".get_class($this)." extends sql_row_obj {\n\tprotected \$table='".$this->table."', "; if (isset($this->primary_key)) { - $r.="\$primary_key='".$this->primary_key."', "; + $r.='$primary_key=array(\''.implode('\', \'', $this->primary_key).'\'), '; } $r.="\$columns=array(\n"; $i=0; @@ -570,7 +576,7 @@ class sql_col { if ($this->is_numeric() && !$this->auto_increment) { $this->default=0; } elseif ($this->type == 'ENUM') { -// $this->default=$this->length; // TODO finish this + // $this->default=$this->length; // TODO finish this } elseif ($this->is_string()) { $this->default=''; } @@ -704,9 +710,9 @@ class sql_col { case 'DOUBLE PRECISION': case 'DECIMAL': case 'DEC': - return '7,3'; + return '7,3'; default: - return null; + return null; } } } diff --git a/shared/classes/buildlog.php b/shared/classes/buildlog.php new file mode 100644 index 0000000..cfb9f3b --- /dev/null +++ b/shared/classes/buildlog.php @@ -0,0 +1,31 @@ +<?php +class sql_buildlog extends sql_row_obj { + protected $table='buildlogs', $primary_key=array('build', 'order'), $columns=array( + 'build' => array ( + 'type' => 'CHAR', + 'length' => 6, + 'not_null' => true + ), + 'order' => array ( + 'type' => 'TINYINT', + 'length' => 3, + 'unsigned' => true, + 'not_null' => true + ), + 'type' => array ( + 'type' => 'ENUM', + 'length' => '\'command\',\'system\',\'msg\'', + 'not_null' => true + ), + 'id' => array ( + 'type' => 'INT', + 'length' => 10, + 'unsigned' => true + ), + 'msg' => array ( + 'type' => 'TEXT' + ) + + ); +} +?> diff --git a/shared/classes/buildlog_entry.php b/shared/classes/commandlog_entry.php index 69d689f..5f78aab 100644 --- a/shared/classes/buildlog_entry.php +++ b/shared/classes/commandlog_entry.php @@ -1,12 +1,12 @@ <?php -class sql_buildlog_entry extends sql_row_obj { - protected $table='buildlogs', $columns=array( +class sql_commandlog_entry extends sql_row_obj { + protected $table='commandlogs', $columns=array( 'task' => array ( 'type' => 'INT', 'length' => 10, 'unsigned' => true, 'not_null' => true, - 'refers to' => 'tasks.id' + 'default' => 0 ), 'order' => array ( 'type' => 'INT', @@ -19,7 +19,8 @@ class sql_buildlog_entry extends sql_row_obj { 'type' => 'INT', 'length' => 10, 'unsigned' => true, - 'not_null' => true + 'not_null' => true, + 'default' => 0 ), 'stream' => array ( 'type' => 'ENUM', diff --git a/shared/classes/profile.php b/shared/classes/profile.php index f45106b..9694352 100644 --- a/shared/classes/profile.php +++ b/shared/classes/profile.php @@ -1,6 +1,6 @@ <?php class sql_profile extends sql_row_obj { - protected $table='profiles', $primary_key='pkgdir', $columns=array( + protected $table='profiles', $primary_key=array('pkgdir'), $columns=array( 'pkgdir' => array ( 'type' => 'VARCHAR', 'length' => 255, diff --git a/shared/classes/session.php b/shared/classes/session.php index 2cab1cb..57804ed 100644 --- a/shared/classes/session.php +++ b/shared/classes/session.php @@ -1,6 +1,6 @@ <?php class sql_session extends sql_row_obj { - protected $table='sessions', $primary_key='id', $columns=array( + protected $table='sessions', $primary_key=array('id'), $columns=array( 'id' => array ( 'type' => 'VARCHAR', 'length' => 30, diff --git a/shared/classes/task.php b/shared/classes/task.php index 7a02719..30adb73 100644 --- a/shared/classes/task.php +++ b/shared/classes/task.php @@ -1,6 +1,6 @@ <?php class sql_task extends sql_row_obj { - protected $table='tasks', $primary_key='id', $columns=array( + protected $table='tasks', $primary_key=array('id'), $columns=array( 'id' => array ( 'type' => 'INT', 'length' => 10, @@ -80,13 +80,13 @@ class sql_task extends sql_row_obj { $c=stream_get_contents($pipes[2]); if ($c) { // STDERR - $entry=new sql_buildlog_entry($this->id, $msg++, time(), 'stderr', $c); + $entry=new sql_commandlog_entry($this->id, $msg++, time(), 'stderr', $c); $entry->write(); } $c=stream_get_contents($pipes[1]); if ($c) { // STDOUT - $entry=new sql_buildlog_entry($this->id, $msg++, time(), 'stdout', $c); + $entry=new sql_commandlog_entry($this->id, $msg++, time(), 'stdout', $c); $entry->write(); } } diff --git a/shared/classes/user.php b/shared/classes/user.php index 2f604b5..aa09ae3 100644 --- a/shared/classes/user.php +++ b/shared/classes/user.php @@ -1,6 +1,6 @@ <?php class sql_user extends sql_row_obj { - protected $table='users', $primary_key='id', $columns=array( + protected $table='users', $primary_key=array('id'), $columns=array( 'id' => array ( 'type' => 'INT', 'length' => 10, |