cvs: pear /MDB2_Schema/MDB2 Schema.php /MDB2_Schema/MDB2/Schema Parser.php

From: Date: Wed, 16 Aug 2006 19:59:05 +0000
Subject: cvs: pear /MDB2_Schema/MDB2 Schema.php /MDB2_Schema/MDB2/Schema Parser.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-41638@lists.php.net to get a copy of this message
ifeghali Wed Aug 16 19:59:05 2006 UTC Modified files: /pear/MDB2_Schema/MDB2 Schema.php /pear/MDB2_Schema/MDB2/Schema Parser.php Log: Added support for DML statements on initializeTable()

http://cvs.php.net/viewvc.cgi/pear/MDB2_Schema/MDB2/Schema.php?r1=1.80&r2=1.81&diff_format=u Index: pear/MDB2_Schema/MDB2/Schema.php diff -u pear/MDB2_Schema/MDB2/Schema.php:1.80 pear/MDB2_Schema/MDB2/Schema.php:1.81 --- pear/MDB2_Schema/MDB2/Schema.php:1.80 Mon Aug 14 17:22:35 2006 +++ pear/MDB2_Schema/MDB2/Schema.php Wed Aug 16 19:59:05 2006 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@pooteeweet.org> | // +----------------------------------------------------------------------+ // -// $Id: Schema.php,v 1.80 2006/08/14 17:22:35 lsmith Exp $ +// $Id: Schema.php,v 1.81 2006/08/16 19:59:05 ifeghali Exp $ // require_once 'MDB2.php'; @@ -690,31 +690,214 @@ */ function initializeTable($table_name, $table) { - foreach ($table['fields'] as $field_name => $field) { - $placeholders[$field_name] = ':'.$field_name; - $types[$field_name] = $field['type']; - } - $fields = implode(',', array_keys($table['fields'])); - $placeholders = implode(',', $placeholders); - $query = "INSERT INTO ".$this->quoteIdentifier($table_name, true)." ($fields) VALUES ($placeholders)"; - $stmt = $this->db->prepare($query, $types, null, true); - if (PEAR::isError($stmt)) { - return $stmt; - } + $query['i'] = 'INSERT INTO $table_name ($fields) VALUES ($values)'; + $query['u'] = 'UPDATE $table_name SET $fields_values'; + $query['uc'] = 'UPDATE $table_name SET $fields_values WHERE $where'; + $query['d'] = 'DELETE FROM $table_name WHERE $where'; + $query['last'] = ''; foreach ($table['initialization'] as $instruction) { switch ($instruction['type']) { case 'insert': - if (!empty($instruction['fields']) && is_array($instruction['fields'])) { - $result = $stmt->execute($instruction['fields']); - if (PEAR::isError($result)) { - return $result; - } + $data = $this->getInstructionFields($instruction); + if (!empty($data)) { + + $fields = array_keys($data); + $values = array_values($data); + + $fields = implode(',', $fields); + $values = implode(',', $values); + + eval('$query[\'last\'] = "'.$query['i'].'";'); + $result = $this->db->exec($query['last']); + } + break; + case 'update': + $data = $this->getInstructionFields($instruction); + $where = $this->getInstructionWhere($instruction); + if (!empty($data)) { + + array_walk($data, array($this, 'buildFieldValue')); + $fields_values = implode(',', $data); + + if (!empty($where)) + eval('$query[\'last\'] = "'.$query['uc'].'";'); + else + eval('$query[\'last\'] = "'.$query['u'].'";'); + + $result = $this->db->exec($query['last']); + } + break; + case 'delete': + $where = $this->getInstructionWhere($instruction); + if (!empty($where)) { + eval('$query[\'last\'] = "'.$query['d'].'";'); + $result = $this->db->exec($query['last']); } break; } + if (PEAR::isError($result)) { + return $result; + } } - return $stmt->free(); + return $result; + } + + // }}} + // {{{ buildFieldValue() + + /** + * Used with array_walk() to change values to key=value + * for a later UPDATE query + * + * @param array multi dimensional array that contains the + * parsed element. + * @return string + * @access public + */ + function buildFieldValue(&$element, $key) + { + $element = $key."=$element"; + } + + // }}} + // {{{ getValue() + + /** + * Translate the a parsed element to a string + * + * @param array multi dimensional array that contains the + * parsed element. + * @return string + * @access public + */ + function getValue($element) + { + $str = ""; + switch ($element['type']) + { + case 'value': + /* 2DO: Add string escape here */ + $str .= $element['data']; + break; + case 'column': + /* 2DO: Add column escape here */ + $str .= $element['data']; + break; + case 'function': + $str .= $element['data']['name']."("; + if ( + (!empty($element['data']['arguments'])) && + (is_array($element['data']['arguments'])) + ) + { + $arguments = array(); + foreach ($element['data']['arguments'] as $v) + $arguments[] = $this->getValue($v); + $str .= implode(",", $arguments); + } + $str .= ")"; + break; + case 'expression': + $str .= "("; + $str .= $this->getValue($element['data']['operant'][0]); + $str .= $this->getOperator($element['data']['operator']); + $str .= $this->getValue($element['data']['operant'][1]); + $str .= ")"; + break; + } + return $str; + } + + // }}} + // {{{ getOperator() + + /** + * Return the matching operator of the RDBMS + * (should be ported to MDB2/Driver ?) + * + * @param string parsed operator + * @return string + * @access public + */ + function getOperator($op) + { + switch ($op) + { + case 'NOT': + return 'NOT'; + break; + case 'AND': + return 'AND'; + break; + case 'OR': + return 'OR'; + break; + case 'PLUS': + return '+'; + break; + case 'MINUS': + return '-'; + break; + case 'EQUAL': + return '='; + break; + case 'LIKE': + return 'LIKE'; + break; + case 'NOT EQUAL': + return '!='; + break; + case 'LESS THAN': + return '<'; + break; + case 'GREATER THAN': + return '>'; + break; + } + } + + // }}} + // {{{ getInstructionFields() + + /** + * Return the fields of the given initialization instruction + * + * @param array multi dimensional array that contains the + * the instruction to be analised + * @param array multi dimensional array that contains the + * structure and optional data of the table + * @return array + * @access public + */ + function getInstructionFields($instruction) + { + $fields = array(); + if ((!empty($instruction['data']['field'])) && (is_array($instruction['data']['field']))) + { + foreach ($instruction['data']['field'] as $field) + $fields[$field['name']] = $this->getValue($field['group']); + } + return $fields; + } + + // }}} + // {{{ getInstructionWhere() + + /** + * Return the translated WHERE statement of the instruction + * + * @param array multi dimensional array that contains the + * the instruction to be analised + * @return string + * @access public + */ + function getInstructionWhere($instruction) + { + $where = ''; + if (!empty($instruction['data']['where'])) + $where = $this->getValue($instruction['data']['where']); + return $where; } // }}} http://cvs.php.net/viewvc.cgi/pear/MDB2_Schema/MDB2/Schema/Parser.php?r1=1.40&r2=1.41&diff_format=u Index: pear/MDB2_Schema/MDB2/Schema/Parser.php diff -u pear/MDB2_Schema/MDB2/Schema/Parser.php:1.40 pear/MDB2_Schema/MDB2/Schema/Parser.php:1.41 --- pear/MDB2_Schema/MDB2/Schema/Parser.php:1.40 Tue Aug 15 21:44:30 2006 +++ pear/MDB2_Schema/MDB2/Schema/Parser.php Wed Aug 16 19:59:05 2006 @@ -42,7 +42,7 @@ // | Author: Christian Dickmann <dickmann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Parser.php,v 1.40 2006/08/15 21:44:30 lsmith Exp $ +// $Id: Parser.php,v 1.41 2006/08/16 19:59:05 ifeghali Exp $ // require_once 'XML/Parser.php'; @@ -163,7 +163,7 @@ case 'database-table-initialization-update-where': case 'database-table-initialization-delete-where': $this->init_expression = array(); - $this->init['data']['where'] = array('expression' => array()); + $this->init['data']['where'] = array('type' => '', 'data' => array()); break; /* Definition */ @@ -222,7 +222,8 @@ /* Delete and Update */ case 'database-table-initialization-update-where-expression': case 'database-table-initialization-delete-where-expression': - $this->init['data']['where']['expression'] = $this->init_expression; + $this->init['data']['where']['type'] = 'expression'; + $this->init['data']['where']['data'] = $this->init_expression; break; /* All */
« previous php.pear.cvs (#41638) next »