Ejemplo n.º 1
0
 /**
  * @param simpleXmlElement $method  the xml element describing the method to generate
  * @param jDaoParser  $parser the parser on a dao file
  */
 function __construct($method, $parser)
 {
     $this->_parser = $parser;
     $params = $parser->getAttr($method, array('name', 'type', 'call', 'distinct', 'eventbefore', 'eventafter', 'groupby'));
     if ($params['name'] === null) {
         throw new jDaoXmlException($this->_parser->selector, 'missing.attr', array('name', 'method'));
     }
     $this->name = $params['name'];
     $this->type = $params['type'] ? strtolower($params['type']) : 'select';
     if (isset($method->parameter)) {
         foreach ($method->parameter as $param) {
             $attr = $param->attributes();
             if (strpos($attr['name'], '$') !== false) {
                 throw new jDaoXmlException($this->_parser->selector, 'method.parameter.invalidname', array($method->name, $attr['name']));
             }
             if (!isset($attr['name'])) {
                 throw new jDaoXmlException($this->_parser->selector, 'method.parameter.unknowname', array($this->name));
             }
             $this->_parameters[] = (string) $attr['name'];
             if (isset($attr['default'])) {
                 $this->_parametersDefaultValues[(string) $attr['name']] = (string) $attr['default'];
             }
         }
     }
     if ($this->type == 'sql') {
         if ($params['call'] === null) {
             throw new jDaoXmlException($this->_parser->selector, 'method.procstock.name.missing');
         }
         $this->_procstock = $params['call'];
         return;
     }
     if ($this->type == 'php') {
         if (isset($method->body)) {
             $this->_body = (string) $method->body;
         } else {
             throw new jDaoXmlException($this->_parser->selector, 'method.body.missing');
         }
         return;
     }
     $this->_conditions = new jDaoConditions();
     if (isset($method->conditions)) {
         $this->_parseConditions($method->conditions[0], false);
     }
     if ($this->type == 'update' || $this->type == 'delete') {
         if ($params['eventbefore'] == 'true') {
             $this->eventBeforeEnabled = true;
         }
         if ($params['eventafter'] == 'true') {
             $this->eventAfterEnabled = true;
         }
     }
     if ($this->type == 'update') {
         if ($this->_parser->hasOnlyPrimaryKeys) {
             throw new jDaoXmlException($this->_parser->selector, 'method.update.forbidden', array($this->name));
         }
         if (isset($method->values) && isset($method->values[0]->value)) {
             foreach ($method->values[0]->value as $val) {
                 $this->_addValue($val);
             }
         } else {
             throw new jDaoXmlException($this->_parser->selector, 'method.values.undefine', array($this->name));
         }
         return;
     }
     if (strlen($params['distinct'])) {
         if ($this->type == 'select') {
             $this->distinct = $this->_parser->getBool($params['distinct']);
         } elseif ($this->type == 'count') {
             $props = $this->_parser->getProperties();
             if (!isset($props[$params['distinct']])) {
                 throw new jDaoXmlException($this->_parser->selector, 'method.property.unknown', array($this->name, $params['distinct']));
             }
             $this->distinct = $params['distinct'];
         } else {
             throw new jDaoXmlException($this->_parser->selector, 'forbidden.attr.context', array('distinct', '<method name="' . $this->name . '"'));
         }
     }
     if ($this->type == 'count') {
         return;
     }
     if (isset($method->order) && isset($method->order[0]->orderitem)) {
         foreach ($method->order[0]->orderitem as $item) {
             $this->_addOrder($item);
         }
     }
     if (strlen($params['groupby'])) {
         if ($this->type == 'select' || $this->type == 'selectfirst') {
             $this->_groupBy = preg_split("/[\\s,]+/", $params['groupby']);
             $props = $this->_parser->getProperties();
             foreach ($this->_groupBy as $p) {
                 if (!isset($props[$p])) {
                     throw new jDaoXmlException($this->_parser->selector, 'method.property.unknown', array($this->name, $p));
                 }
             }
         } else {
             throw new jDaoXmlException($this->_parser->selector, 'forbidden.attr.context', array('groupby', '<method name="' . $this->name . '"'));
         }
     }
     if (isset($method->limit)) {
         if (isset($method->limit[1])) {
             throw new jDaoXmlException($this->_parser->selector, 'tag.duplicate', array('limit', $this->name));
         }
         if ($this->type == 'select') {
             $this->_addLimit($method->limit[0]);
         } else {
             throw new jDaoXmlException($this->_parser->selector, 'method.limit.forbidden', $this->name);
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * constructor.
  * @param array  $attributes  list of attributes of a simpleXmlElement
  * @param jDaoParser $parser the parser on the dao file
  * @param jDbTools $tools
  */
 function __construct($aAttributes, $parser, $tools)
 {
     $needed = array('name', 'fieldname', 'table', 'datatype', 'required', 'minlength', 'maxlength', 'regexp', 'sequence', 'default', 'autoincrement');
     // Allowed attributes names
     $allowed = array('name', 'fieldname', 'table', 'datatype', 'required', 'minlength', 'maxlength', 'regexp', 'sequence', 'default', 'autoincrement', 'updatepattern', 'insertpattern', 'selectpattern', 'comment');
     foreach ($aAttributes as $attributeName => $attributeValue) {
         if (!in_array($attributeName, $allowed)) {
             throw new jDaoXmlException($parser->selector, 'unknown.attr', array($attributeName, 'property'));
         }
     }
     $params = $parser->getAttr($aAttributes, $needed);
     if ($params['name'] === null) {
         throw new jDaoXmlException($parser->selector, 'missing.attr', array('name', 'property'));
     }
     $this->name = $params['name'];
     if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $this->name)) {
         throw new jDaoXmlException($parser->selector, 'property.invalid.name', $this->name);
     }
     $this->fieldName = $params['fieldname'] !== null ? $params['fieldname'] : $this->name;
     $this->table = $params['table'] !== null ? $params['table'] : $parser->getPrimaryTable();
     $tables = $parser->getTables();
     if (!isset($tables[$this->table])) {
         throw new jDaoXmlException($parser->selector, 'property.unknown.table', $this->name);
     }
     $this->required = $this->requiredInConditions = $parser->getBool($params['required']);
     $this->maxlength = $params['maxlength'] !== null ? intval($params['maxlength']) : null;
     $this->minlength = $params['minlength'] !== null ? intval($params['minlength']) : null;
     $this->regExp = $params['regexp'];
     $this->autoIncrement = $parser->getBool($params['autoincrement']);
     if ($params['datatype'] === null) {
         throw new jDaoXmlException($parser->selector, 'missing.attr', array('datatype', 'property'));
     }
     $params['datatype'] = trim(strtolower($params['datatype']));
     if ($params['datatype'] == '') {
         throw new jDaoXmlException($parser->selector, 'wrong.attr', array($params['datatype'], $this->fieldName, 'property'));
     }
     $this->datatype = strtolower($params['datatype']);
     $ti = $tools->getTypeInfo($this->datatype);
     $this->unifiedType = $ti[1];
     if (!$this->autoIncrement) {
         $this->autoIncrement = $ti[6];
     }
     if ($this->unifiedType == 'integer' || $this->unifiedType == 'numeric') {
         if ($params['sequence'] !== null) {
             $this->sequenceName = $params['sequence'];
             $this->autoIncrement = true;
         }
     }
     $this->isPK = in_array($this->fieldName, $tables[$this->table]['pk']);
     if (!$this->isPK && $this->table == $parser->getPrimaryTable()) {
         foreach ($tables as $table => $info) {
             if ($table == $this->table) {
                 continue;
             }
             if (isset($info['fk']) && in_array($this->fieldName, $info['fk'])) {
                 $this->isFK = true;
                 break;
             }
         }
     } else {
         $this->required = true;
         $this->requiredInConditions = true;
     }
     if ($this->autoIncrement) {
         $this->required = false;
         $this->requiredInConditions = true;
     }
     if ($params['default'] !== null) {
         $this->defaultValue = $tools->stringToPhpValue($this->unifiedType, $params['default']);
     }
     // insertpattern is allowed on primary keys noy autoincremented
     if ($this->isPK && !$this->autoIncrement && isset($aAttributes['insertpattern'])) {
         $this->insertPattern = (string) $aAttributes['insertpattern'];
     }
     if ($this->isPK) {
         $this->updatePattern = '';
     }
     // we ignore *pattern attributes on PK and FK fields
     if (!$this->isPK && !$this->isFK) {
         if (isset($aAttributes['updatepattern'])) {
             $this->updatePattern = (string) $aAttributes['updatepattern'];
         }
         if (isset($aAttributes['insertpattern'])) {
             $this->insertPattern = (string) $aAttributes['insertpattern'];
         }
         if (isset($aAttributes['selectpattern'])) {
             $this->selectPattern = (string) $aAttributes['selectpattern'];
         }
     }
     // no update and insert patterns for field of external tables
     if ($this->table != $parser->getPrimaryTable()) {
         $this->updatePattern = '';
         $this->insertPattern = '';
         $this->required = false;
         $this->requiredInConditions = false;
         $this->ofPrimaryTable = false;
     } else {
         $this->ofPrimaryTable = true;
     }
     // field comment
     if (isset($aAttributes['comment'])) {
         $this->comment = (string) $aAttributes['comment'];
     }
 }