Exemple #1
0
 public function get()
 {
     if (!$this->_ready) {
         return false;
     }
     if (in_array($this->_type, array('replace', 'insert', 'update'))) {
         if (!is_array($this->_data)) {
             return false;
         }
         if (empty($this->_data)) {
             return false;
         }
     }
     $ret = array();
     $at = $this->_jmay ? '`[+prefix+]' . $this->owner->add['table'] . '`' : '';
     $mt = $this->table;
     switch ($this->_type) {
         case 'replace':
         case 'insert':
             foreach ($this->_data as $row) {
                 if (!isset($row['main']['create'])) {
                     continue;
                 }
                 if (!is_array($row['main']['create'])) {
                     continue;
                 }
                 $q = array();
                 foreach ($row['main']['create'] as $alias => $value) {
                     $q[] = $this->value($alias, $value);
                 }
                 $ret[] = $this->_query . " (" . implode(',', $q) . ")";
                 if (!empty($row['replace']['create']) && $this->_jmay) {
                     $v = array();
                     foreach ($row['replace']['create'] as $fn => $fv) {
                         $v[] = "([+last_insert_id+]," . $this->_adds[$fn] . "," . $this->value($fn, $fv) . ")";
                     }
                     if (!empty($v)) {
                         $ret[] = "insert into {$at} values " . implode(',', $v);
                     }
                 }
             }
             break;
         case 'select':
             if ($_ = $this->selectFields) {
                 $_ = str_replace('[+selectfields+]', $_, $this->_query);
                 return array($_ . $this->join_get() . $this->cond_get());
             } else {
                 return false;
             }
         case 'update':
             $p = $this->owner->primary;
             $join = $this->join_get();
             $limit = empty($this->_limit) ? '' : " limit " . $this->_limit;
             foreach ($this->_data as $row) {
                 $q = array();
                 foreach ($row['main']['update'] as $fn => $fv) {
                     $q[] = "tmain.`{$fn}` = " . $this->value($fn, $fv);
                 }
                 if (empty($q)) {
                     continue;
                 }
                 $_ = " set " . implode(',', $q);
                 $w = array();
                 if (!empty($this->_where)) {
                     $w[] = "(" . $this->_where . ")";
                 }
                 if (!empty($row['primary'])) {
                     $w[] = "(tmain.`{$p}` = " . $row['primary'] . ")";
                 }
                 $w = empty($w) ? '' : " where " . implode(' and ', $w);
                 $_ .= $w;
                 $ret[] = $this->_query . "{$join} {$_}" . $limit;
                 $cfs = "select tmain.`{$p}` from {$mt} as tmain" . $join . $w . $limit;
                 if (!empty($row['replace']['update']) && $this->_jmay) {
                     $ret[] = $cfs;
                     $v = array();
                     foreach ($row['replace']['update'] as $fn => $fv) {
                         $v[] = "([+id+]," . $this->_adds[$fn] . "," . $this->value($fn, $fv) . ")";
                     }
                     if (!empty($v)) {
                         $ret[] = "replace into {$at} values " . implode(',', $v);
                     }
                 }
                 if (!empty($row['delete']['update'])) {
                     $ret[] = "delete from {$at}" . " where (`field` in (" . implode(',', $row['delete']['update']) . "))" . " and (`" . $this->owner->add['field'] . "` in ([+ids+]))";
                 }
             }
             break;
         case 'delete':
             return array($this->_query . $this->join_get() . $this->cond_get());
         case 'clear':
             return array($this->_query);
         case 'table':
             $f = array();
             $k = array();
             $p = '';
             foreach ($this->_fields as $alias => $v) {
                 $f[] = "`{$alias}` " . xbDataFields::SQLType($this->owner->fields[$alias]);
                 if ($this->owner->fields[$alias]['primary']) {
                     $p = $alias;
                 }
                 if (!is_null($this->owner->fields[$alias]['key'])) {
                     $k[$alias] = $this->owner->fields[$alias]['key'];
                 }
             }
             $_ = $this->_query . " (" . implode(',', $f);
             if (is_array($this->owner->indexes)) {
                 foreach ($this->owner->indexes as $alias) {
                     $_ .= ",index (`{$alias}`)`";
                 }
             }
             if (is_array($this->owner->unique)) {
                 foreach ($this->owner->unique as $alias => $key) {
                     $_ .= ",unique key `{$alias}`(`" . implode('`,`', $key) . "`)";
                 }
             }
             if (!empty($p)) {
                 $_ .= ",primary key (`{$p}`)";
             }
             if (!empty($k)) {
                 foreach ($k as $alias => $key) {
                     $_ .= ",foreign key (`{$alias}`) references `[+prefix+]" . (is_null($key['table']) ? $this->owner->table : $key['table']) . "`(`" . $key['field'] . "`)" . " on update " . $key['update'] . " on delete " . $key['delete'];
                 }
             }
             $_ .= ") engine=InnoDB insert_method=first";
             // TODO
             return array($_);
         default:
             return false;
     }
     return $ret;
 }