* @package Mammut\DB\Sql */ trait Targetable { /** * Defines the base of the query. * * Examples: * - simple table: from('tablename') * - simple table w/alias: from(['a' => 'tablename']) * - schema table: from('tablename','mysc'); * - database table: from('tablename', NULL, 'mydb') * - subselect: from(['a' => $select]) * * @param string|array|Table $table * The query base. Allowed values are tablenames, identifier names or * select objects. Use arg1,arg2,... to use database.schema.table namestyle (NULL = default). * Subselects and aliasing can be with an [alias=>object] array. * @return $this */ public function from($table) { if(!(is_string($table) || is_array($table) || ($table instanceof Table) || ($table instanceof Select))) throw new \InvalidArgumentException('$table must be a string|array|Table|Select'); $args = func_get_args(); switch (count($args)) { case 1: if (is_string($table) || $table instanceof Table) { $this->setup[self::TABLE] = $table; } elseif(is_array($table)) { if (count($table) != 1) throw new \InvalidArgumentException('$table must be a array of size 1'); $alias = key($table); $table = current($table); if (is_array($table)) { switch (count($table)) { case 1: $table = new Table($table[0]); break; case 2: $table = new Table($table[1], NULL, $table[0]); break; case 3: $table = new Table($table[2], $table[1], $table[0]); break; case 4: $table = new Table($table[3], $table[2], $v[1], $table[0]); break; default: throw new \OutOfBoundsException('table segment array too big'); } } if (is_string($table) || $table instanceof Table || $table instanceof Select) { if (is_numeric($alias)) $this->setup[self::TABLE] = $table; else $this->setup[self::TABLE] = [$alias => $table]; } else { var_dump($alias, $table); die(); } } break; case 2: $this->setup[self::TABLE] = new Table($args[1], NULL, $args[0]); break; case 3: $this->setup[self::TABLE] = new Table($args[2], $args[1], $args[0]); break; case 4: $this->setup[self::TABLE] = new Table($args[3], $args[2], $v[1], $args[0]); break; default: throw new \OutOfBoundsException('table segment array too big'); } return $this; } /** * * @return Table */ public function getTarget() { return isset($this->setup[self::TABLE]) ? $this->setup[self::TABLE] : NULL; } }