public function test_query() { $tests = array(1 => 'SELECT 1;', 3 => 'SELECT [[test]];', 4 => 'SELECT [[[test]]];', 5 => 'SELECT [[[te\\]st]]];', 6 => 'SELECT [this];', 7 => 'SELECT [useful/user];', 8 => 'SELECT [this.id];', 9 => 'SELECT [useful/user.id];', 10 => 'SELECT [useful/user.date_created(date)];', 11 => 'SELECT [useful/user.date_created as date];', 12 => 'SELECT [useful/user.date_created(date) as date];', 13 => 'SELECT [*];', 14 => 'SELECT [this.*];', 15 => 'SELECT [useful/user: id];', 16 => 'SELECT [useful/user: id, name];', 17 => 'SELECT [this: id, name];', 18 => 'SELECT [useful/user: id(type) as test];', 19 => 'SELECT [useful/user: id(type) as test, date(subtype) as subtest];', 20 => 'SELECT [@telefone];', 21 => 'SELECT [@telefone(phone)];', 22 => 'SELECT [@1];', 23 => 'SELECT [@1(phone)];', 24 => 'SELECT [@telefone(phone)?];', 25 => 'SELECT [@telefone?];', 26 => 'SELECT [@telefone?null];'); foreach ($tests as $key => $test) { $this->test($key, core_model_query::parse_query($test), $test); } $this->set_prefix('query'); $conn = connection(); $model = model('useful/user')->model(); $model_args_1 = array('key' => 'test', 'int' => '1234', 'float' => '12.34', 'float2' => '12,34', 'sql' => 'DATE()'); $this->test(1, core_model_query::query($conn, 'SELECT [this];', $model)); $this->test(2, core_model_query::query($conn, 'SELECT [this.id];', $model)); $this->test(3, core_model_query::query($conn, 'SELECT [this.id(int)];', $model)); $this->test(4, core_model_query::query($conn, 'SELECT [this.id(int) as id_user];', $model)); $this->test(5, core_model_query::query($conn, 'SELECT [this: id, name];', $model)); $this->test(6, core_model_query::query($conn, 'SELECT [this.*];', $model)); $this->test(7, core_model_query::query($conn, 'SELECT [@int];', $model, $model_args_1)); $this->test(8, core_model_query::query($conn, 'SELECT [@int(int)];', $model, $model_args_1)); $this->test(9, core_model_query::query($conn, 'SELECT [@float(float)];', $model, $model_args_1)); $this->test(10, core_model_query::query($conn, 'SELECT [@float2(float)];', $model, $model_args_1)); $this->test(11, core_model_query::query($conn, 'SELECT [@float(int)];', $model, $model_args_1)); $this->test(12, core_model_query::query($conn, 'SELECT [@float2(int)];', $model, $model_args_1)); $this->test(13, core_model_query::query($conn, 'SELECT [@sql(sql)];', $model, $model_args_1)); $this->test(14, core_model_query::query($conn, 'SELECT [@key(key)];', $model, $model_args_1)); $this->test(16, core_model_query::query($conn, 'SELECT [@fake(int)?];', $model, $model_args_1)); $this->test(17, core_model_query::query($conn, 'SELECT [@fake(float)?];', $model, $model_args_1)); $this->test(18, core_model_query::query($conn, 'SELECT [@fake(string)?];', $model, $model_args_1)); $this->test(19, core_model_query::query($conn, 'SELECT [@fake?];', $model, $model_args_1)); $this->test(20, core_model_query::query($conn, 'SELECT [@fake(int)?null];', $model, $model_args_1)); $this->test(21, core_model_query::query($conn, 'SELECT [@fake?null];', $model, $model_args_1)); $this->set_prefix('model'); $row = model('useful/user'); $this->test(1, $row->query('SELECT [this.id] FROM [this] ORDER BY [this.id];')->fetch_object()); $this->test(2, $row->query('SELECT [@test];', array('test' => 'okay'))->fetch_object()); $row = model('useful/user', 1); $this->test(3, $row->query('SELECT [@this.id(int)] AS `test`;')->fetch_object()); }
public function query($conn, $query, $args, $row) { return $conn->query(core_model_query::query($conn, $query, $this, $args, $row)); }
public function __call($func, $args) { // Se for um key válido if (preg_match(core_model::METHOD_KEY_VALIDATE, $func)) { // Obtém as configurações da chave $key = $this->_model->_get_key($func); // A depender do tipo de chave... switch ($key->type) { // Chave load carrega uma informação para os dados internos case 'load': // Armazena o método de carregamento $this->_loader_method = array(array($this, '__call'), array($func, $args)); $query = $this->query($key->sql, core_model_query::merge_args($args, $key)); $this->_apply_data($query->fetch_assoc()); return $this; break; // Chave exists apenas retorna true se a informação existir (ao menos um registro) // Chave count retorna a quantidade de registros compatíveis // Chave exists apenas retorna true se a informação existir (ao menos um registro) // Chave count retorna a quantidade de registros compatíveis case 'exists': case 'count': $query = $this->query($key->sql, core_model_query::merge_args($args, $key)); // Se for exists, retorna se existe algum registro if ($key->type === 'exists') { return $query->num_rows > 0; } // Em outro caso (count) verifica se há somente uma coluna e se ela se chama COUNT(...) $fields = $query->fetch_fields(); // Se houver apenas um campo e este for COUNT(*) retorna o seu valor if (count($fields) === 1 && preg_match('/^COUNT(.+)$/i', $fields[0]->name)) { $fetch = $query->fetch_row(); return intval($fetch[0]); } // Em último caso, retorna o número de resultados encontrados return $query->num_rows; break; // Chave one retorna um objeto de outro modelo (ou o mesmo) baseado em uma coluna local // Chave one retorna um objeto de outro modelo (ou o mesmo) baseado em uma coluna local case 'one': $model = model($key->model, $this->_get_typed_value($key->column), $this->_conn); $model->_from = $this; return $model; // Chave multi retorna múltiplos resultados do mesmo tipo deste modelo // Chave multi retorna múltiplos resultados do mesmo tipo deste modelo case 'multi': $query = $this->query($key->sql, core_model_query::merge_args($args, $key)); return new core_model_results($this->_conn, $query, $this->_model, $this); // Chave many retorna múltiplos resultados de um diferente modelo // Chave many retorna múltiplos resultados de um diferente modelo case 'many': $model = model($key->model); $query = $model->query($key->sql, core_model_query::merge_args($args, $key), $this); return new core_model_results($this->_conn, $query, $model->model(), $this); } } // Se o método existir no modelo, executa if (method_exists($this->_model, $func)) { array_unshift($args, $this); return call_user_func_array(array($this->_model, $func), $args); } // Em último caso, retorna o valor armazenado normalmente return $this->__get($func); }