Пример #1
0
 static function dump()
 {
     $result = DB_SQL::db()->connection->prepare('show profiles')->execute();
     $rows = $result->fetch_all();
     print '<table style="margin: 10px;" cellspacing="1" bgcolor="#000"><tr style="background-color: #888;color: #fff"><th style="padding:3px;">ID</th><th style="padding:3px;">Duration</th><th style="padding:3px;">Query</th></tr>';
     foreach ($rows as $row) {
         print '<tr style="background-color: white; color: black;">';
         print '<td nowrap style="padding:3px;">' . $row['Query_ID'] . '</td>';
         print '<td nowrap style="padding:3px;">' . $row['Duration'] . '</td>';
         print '<td style="padding:3px;">' . $row['Query'] . '</td>';
         print '</tr';
     }
     print '</table>';
 }
Пример #2
0
 public function run()
 {
     $binds = count($args = func_get_args()) > 1 ? $args : $args[0];
     $run_callbacks = $binds instanceof DB_SQL_Entity && $this->view && $binds instanceof $this->view->prototype;
     $sql = 'DELETE FROM ' . ($this->table ? $this->table : $this->view->table->name) . "\n";
     if (count($where = Core_Arrays::merge($this->view ? $this->view->__get('where') : array(), $this->where))) {
         $sql .= 'WHERE (' . Core_Arrays::join_with(') AND (', $where) . ')';
     }
     if ($run_callbacks ? $binds->before_delete() : true) {
         $rc = DB_SQL::db()->connection->prepare($sql)->bind($binds)->execute();
     }
     if ($run_callbacks && $rc) {
         $binds->after_delete();
     }
     return $rc;
 }
Пример #3
0
 public function item_id($item)
 {
     if ($mapper = $this->orm_mapper()) {
         $options = $mapper->options;
         $key = $options['key'];
         $key = current($key);
         return $item->{$key};
     }
     if ($tbl = $this->dbtable) {
         if ($serial = DB_SQL::db()->tables[$tbl]->serial) {
             return $item->{$serial};
         }
         return $item->id;
     }
     return $item->id();
 }
Пример #4
0
 protected function multilink_load($table, $kname, $fname, $mask = false, $afield = false)
 {
     if ($this->id() > 0) {
         if (!$afield) {
             $afield = $table;
         }
         if (!$mask) {
             $mask = "{$fname}%";
             $regexp = "/^{$fname}(\\d+)\$/";
         }
         foreach ($this->attributes as $key => $value) {
             if ($m = Core_Regexps::match_with_results($regexp, $key)) {
                 unset($this->attributes[$key]);
             }
         }
         $query = DB_SQL::db()->{$table}->select->where("{$kname}=:{$kname}");
         $rows = $query->run($this->id());
         $arr = array();
         foreach ($rows as $row) {
             $arr[] = $row->{$fname};
             $p = str_replace('%', $row->{$fname}, $mask);
             $this->attributes[$p] = 1;
         }
         $this->attributes[$afield] = $arr;
     }
 }
Пример #5
0
 public function full_code($id = false)
 {
     if (!$id) {
         $row = $this;
     } else {
         $row = DB_SQL::db()->vars->find($id);
     }
     $out = $row->code;
     while ($row->parent_id > 0) {
         $row = DB_SQL::db()->vars->find($row->parent_id);
         $out = $row->code . '.' . $out;
     }
     if (trim($row->component) != '') {
         $out = $row->component . ':' . $out;
     }
     return $out;
 }
Пример #6
0
 static function items_for_select_generate($s)
 {
     if (Core_Types::is_callable($s)) {
         $s = Core::invoke($s);
     }
     if (is_string($s)) {
         if ($m = Core_Regexps::match_with_results('/^:(.+)$/', $s)) {
             $method = trim($m[1]);
             $s = CMS::$current_controller->{$method}();
         }
     }
     if (Core_Types::is_iterable($s)) {
         $items = array();
         foreach ($s as $k => $v) {
             if ($v == '' && (is_string($k) && Core_Regexps::match('/^(var|db|orm)/', $k))) {
                 $items += self::items_for_select($k);
             } elseif ($v instanceof DB_ORM_Mapper) {
                 $items += self::items_for_select($v->select());
             } else {
                 if ($v instanceof DB_ORM_Entity) {
                     $items[$v->id()] = $v;
                 } else {
                     if (is_int($k) && (Core_Types::is_callable($v) || is_string($v) && Core_Regexps::match('/^(var|db|orm)/', $v))) {
                         $items += self::items_for_select($v);
                     } else {
                         $items[$k] = $v;
                     }
                 }
             }
         }
         return $items;
     } else {
         if ($m = Core_Regexps::match_with_results('/^var:(.+)$/', $s)) {
             return self::get_var_value($m[1]);
         } else {
             if ($m = Core_Regexps::match_with_results('/^orm:(.+)$/', $s)) {
                 $items = array();
                 foreach (self::orm()->downto($m[1]) as $row) {
                     $items[$row->id] = $row;
                 }
                 return $items;
             } else {
                 if ($m = Core_Regexps::match_with_results('/^(.+)::(.+)$/', $s)) {
                     $class = str_replace('.', '_', trim($m[1]));
                     $method = trim($m[2]);
                     $ref = new ReflectionMethod($class, $method);
                     return $ref->invoke(null);
                 } else {
                     if ($m = Core_Regexps::match_with_results('/^db:([a-z0-9_]+)(.*)$/i', $s)) {
                         $table = $m[1];
                         $s = $m[2];
                         $value = 'id';
                         $title = 'title';
                         $query = 'select';
                         if ($m = Core_Regexps::match_with_results('/^->([a-z0-9_]+)(.*)$/', $s)) {
                             $query = $m[1];
                             $s = $m[2];
                         }
                         if ($m = Core_Regexps::match_with_results('/^\\((.+),(.+)\\)$/', $s)) {
                             $value = $m[1];
                             $title = $m[2];
                         }
                         $rows = DB_SQL::db()->{$table}->{$query}->run();
                         $items = array();
                         foreach ($rows as $row) {
                             $items[$row->{$value}] = $row->{$title};
                         }
                         return $items;
                     } else {
                         return array();
                     }
                 }
             }
         }
     }
 }
Пример #7
0
 protected function update($item)
 {
     if ($this->storage_name) {
         return $this->storage()->update($item);
     }
     if ($mapper = $this->orm_mapper()) {
         return $mapper->update($item);
     }
     if ($tbl = $this->dbtable) {
         return DB_SQL::db()->{$tbl}->update($item);
     }
     return $item->update();
 }