/** * Escape a column value, in order to insert it into a SQL query * * @param $value mixed * @param $double_backquote boolean * @return string */ public static function escape($value, $double_backquote = false) { // no is_numeric(), as sql numeric search make numeric conversion of string fields // ie WHERE NAME = 500 instead of '500' will give you '500' and '500L', which is not correct if (is_integer($value) || is_float($value)) { $string_value = strval($value); } elseif (is_bool($value)) { $string_value = $value ? '1' : '0'; } elseif ($value === null) { $string_value = 'NULL'; } elseif (is_array($value)) { $do = false; $string_value = ''; foreach ($value as $object_value) { if ($object_value !== null) { if ($do) { $string_value .= ','; } $string_value .= str_replace(DQ, DQ . DQ, $object_value); $do = true; } } $string_value = substr($string_value, 2); } elseif ($value instanceof Date_Time) { $string_value = DQ . ($value->toISO() ?: '0000-00-00 00:00:00') . DQ; } else { $string_value = DQ . Dao::current()->escapeString($value) . DQ; } return $double_backquote ? str_replace(BS, BS . BS, $string_value) : $string_value; }
/** * @param $source Reflection_Source the PHP source file object * @param $compiler PHP\Compiler the main compiler * @return boolean false as compilation do never change source */ public function compile(Reflection_Source $source, PHP\Compiler $compiler = null) { $dao = Dao::current(); if ($dao instanceof Link) { //$dao->begin(); //$tables = []; /* // drop empty tables foreach ($dao->getConnection()->getTables() as $table_name) { if ($dao->query('SELECT COUNT(*) FROM ' . BQ . $table_name . BQ)->fetch_row()[0]) { $tables[$table_name] = true; } else { @$dao->query('DROP TABLE IF EXISTS ' . BQ . $table_name . BQ); } } */ /* // update tables structures foreach ($source->getClasses() as $class) { if (isset($tables[$dao->storeNameOf($class->name)])) { $dao->createStorage($class->name); } } */ //$dao->commit(); } return false; }
/** * Builds a Table object using a Php class definition * * This takes care of excluded properties, so buildLinkTable() should be called * before buildClassTable(). * * @param $class Reflection_Class * @param $more_field Column * @return Table */ private function buildClassTable(Reflection_Class $class, $more_field) { $table_name = Dao::current()->storeNameOf($class->name); $table = new Table($table_name); if (!in_array('id', $this->excluded_properties)) { $table->addColumn(Column::buildId()); } if ($more_field) { $table->addColumn($more_field); } if ($class->isAbstract()) { $table->addColumn(new Column('class', 'varchar(255)')); } else { foreach ($class->accessProperties() as $property) { if (!in_array($property->name, $this->excluded_properties)) { $type = $property->getType(); if (($type->isMultipleString() || !$type->isMultiple()) && !$property->isStatic()) { $table->addColumn(Column::buildProperty($property)); if ($property->getAnnotation('link')->value == Link_Annotation::OBJECT && $property->getAnnotation('store')->value != 'string') { $class_name = $property->getType()->asString(); $this->dependencies_context[$class_name] = $class_name; $table->addForeignKey(Foreign_Key::buildProperty($table_name, $property)); $table->addIndex(Index::buildLink($property->getAnnotation('storage')->value)); } } } } } return $table; }
/** * Build SQL tables list, based on calculated joins for where array properties paths * * @return string */ public function build() { $tables = BQ . Dao::current()->storeNameOf($this->class_name) . BQ . SP . 't0'; foreach ($this->joins->getJoins() as $join) { if ($join) { $tables .= $join->toSql(); } } return $tables; }
/** * @param $configuration array */ public function __construct($configuration) { if (isset($configuration[self::LINKS_LIST])) { self::$list = $configuration[self::LINKS_LIST]; unset($configuration[self::LINKS_LIST]); } $class_name = $configuration[Configuration::CLASS_NAME]; unset($configuration[Configuration::CLASS_NAME]); Dao::current(new $class_name($configuration)); }
/** * @param $last_time integer compile only files modified since this time */ public function compile($last_time = 0) { set_time_limit(900); clearstatcache(); $cache_dir = $this->getCacheDir(); // create data set for dependencies, check for dependencies for deleted files Dao::createStorage(Dependency::class); Dao::begin(); if (isset($_GET['Z'])) { $link = Dao::current(); if ($link instanceof Link) { $link->query('TRUNCATE TABLE `dependencies`'); } } foreach (Dao::select(Dependency::class, ['file_name' => Func::distinct()]) as $file_dependency) { /** @var $file_dependency List_Row */ $file_name = $file_dependency->getValue('file_name'); if (!file_exists($file_name)) { foreach (Dao::search(['file_name' => $file_name], Dependency::class) as $dependency) { /** @var $dependency Dependency */ Dao::delete($dependency); foreach (Dao::search(['dependency_name' => $dependency->class_name], Dependency::class) as $sub_dependency) { /** @var $sub_dependency Dependency */ Dao::delete($sub_dependency); } } } } Dao::commit(); $this->sources = array_merge($this->more_sources, $this->getFilesToCompile($last_time)); $first_group = true; foreach ($this->compilers as $compilers) { /** @var $compilers ICompiler[] */ $saved_sources = $this->sources; while ($this->sources) { // get source and update dependencies foreach ($this->sources as $source) { /** @var Reflection_Source $source inspector bug */ /** @noinspection PhpParamsInspection inspector bug (a Dependency is an object) */ (new Set())->replace($source->getDependencies(true), Dependency::class, ['file_name' => $source->file_name]); } do { $added = []; // ask each compiler for adding of compiled files, until they have nothing to add foreach ($compilers as $compiler) { if ($compiler instanceof Needs_Main) { $compiler->setMainController($this->main_controller); } $added = array_merge($added, $compiler->moreSourcesToCompile($this->sources)); } foreach ($added as $source) { /** @var Reflection_Source $source inspector bug */ /** @noinspection PhpParamsInspection inspector bug (a Dependency is an object) */ (new Set())->replace($source->getDependencies(true), Dependency::class, ['file_name' => $source->file_name]); $this->sources[$source->file_name] = $source; } if (count($compilers) == 1) { $added = []; } } while ($added); $saved_sources = array_merge($saved_sources, $this->sources); // fill in sources cache $sources_count = count($this->sources); foreach ($this->sources as $source) { foreach (array_keys($source->getClasses()) as $class_name) { $this->sources_cache[$class_name] = $source; } if ($sources_count > self::MAX_OPENED_SOURCES) { $source->free(self::SOURCES_FREE); } } // compile sources foreach ($this->sources as $source) { $this->compileSource($source, $compilers, $cache_dir, $first_group, $sources_count); } $this->sources = $this->more_sources; $this->more_sources = []; foreach ($this->sources as $source) { if (!isset($saved_sources[$source->file_name])) { $saved_sources[$source->file_name] = $source; } } } $this->sources = $saved_sources; $first_group = false; } $this->sources = null; }
/** * Build a SQL UPDATE query * * @param $class Reflection_Class | string * @param $write array the data to write for each column : key is the column name * @param $id integer|integer[] * @return string */ public static function buildUpdate($class, $write, $id) { $sql_update = 'UPDATE ' . BQ . Dao::current()->storeNameOf($class) . BQ . LF . 'SET '; $i = 0; foreach ($write as $key => $value) { if ($i++) { $sql_update .= ', '; } if ($key != 'id' && substr($key, 0, 3) != 'id_') { $key = BQ . $key . BQ; } $sql_update .= $key . ' = ' . Value::escape($value); } $sql_update .= LF . 'WHERE'; if (is_numeric($id)) { $sql_update .= ' id = ' . $id; } elseif (is_array($id)) { $first = true; foreach ($id as $key => $value) { $sql_update .= $first ? $first = false : ' AND'; $sql_update .= ' ' . $key . ' = ' . $value; } } else { trigger_error("id must be an integer of an array of integer values", E_USER_ERROR); } return $sql_update; }
/** * Constructs a new Select object, read to use, with all its context data * * @param $class_name string The name of the main business class to start from * @param $columns string[] If not set, the columns names will be taken from the query result * @param $link Link If not set, the default link will be Dao::current() */ public function __construct($class_name = null, $columns = null, Link $link = null) { $this->link = $link ?: Dao::current(); $this->class_name = $class_name; if (isset($columns)) { $this->columns = $columns; $this->columns[] = 'id'; } }
/** * Construct the SQL WHERE section of a query * * Supported columns naming forms are : * column_name : column_name must correspond to a property of class * column.foreign_column : column must be a property of class, foreign_column must be a property of column's var class * * @param $class_name string base object class name * @param $where_array array|Func\Where where array expression, indices are columns names * @param $sql_link Link * @param $joins Joins */ public function __construct($class_name, $where_array = null, Link $sql_link = null, Joins $joins = null) { $this->joins = $joins ? $joins : new Joins($class_name); $this->sql_link = $sql_link ? $sql_link : Dao::current(); $this->where_array = $where_array; }
/** * The constructor can be called with a specific data link. * The default data link will be Dao::current() * This works only for Identifier_Map data links * * @param $dao Identifier_Map */ public function __construct(Identifier_Map $dao = null) { $this->dao = isset($dao) ? $dao : Dao::current(); }
/** * Generic getter for mapped objects * * @param $stored object[] actual value of the property (will be returned if not null) * @param $object object the parent object * @param $property string|Reflection_Property the source property (or name) for map reading * @return Component[] */ public static function &getMap(&$stored, $object, $property) { if (!(self::$ignore || isset($stored))) { if (Dao::getObjectIdentifier($object)) { if (!$property instanceof Reflection_Property) { $property = new Reflection_Property(get_class($object), $property); } $dao = ($dao = $property->getAnnotation('dao')->value) ? Dao::get($dao) : Dao::current(); $class_name = get_class($object); $linked_class_name = (new Link_Class($class_name))->getLinkedClassName(); if ($linked_class_name) { $object = (new Link_Class($class_name))->getCompositeProperty()->getValue($object); $class_name = $linked_class_name; } $element_type = $property->getType()->getElementType(); $is_abstract = $element_type->asReflectionClass()->isAbstract(); $sort = $is_abstract ? Dao::sort(['id']) : Dao::sort(); $stored = $dao->search([$class_name . '->' . $property->name => $object], $element_type->asString(), [$sort]); if ($is_abstract) { $sort->sortObjects($stored); } } else { $stored = []; } } return $stored; }
/** * @param $data_link Identifier_Map */ public function __construct(Identifier_Map $data_link = null) { $this->data_link = $data_link ?: Dao::current(); }
/** * The constructor initialises logged information for a call on script beginning. * * @param $uri string * @param $arguments array * @param $form array * @param $files array */ public function __construct($uri, $arguments = null, $form = null, $files = null) { if (!isset($this->start)) { $this->duration = microtime(true); $this->start = new Date_Time(); } if (!isset($this->process_id)) { $this->process_id = getmypid(); } if (!isset($this->mysql_thread_id)) { $dao = Dao::current(); if ($dao instanceof Link) { $this->mysql_thread_id = $dao->getConnection()->thread_id; } } if (!isset($this->session_id)) { $this->session_id = session_id(); } if (isset($arguments) && !isset($this->arguments)) { $this->arguments = $this->serialize($arguments); } if (isset($uri) && !isset($this->uri)) { $this->uri = $uri; } if (isset($files) && !isset($this->files)) { $this->files = $this->serialize($files); } if (isset($form) && !isset($this->form)) { $this->form = $this->serialize($form); } }