ver | Stable | Develop |
---|---|---|
3.3.x | ||
3.4.x |
kohana / database Goto Github PK
View Code? Open in Web Editor NEWA Kohana module for database interactions, building queries, and prepared statements
Home Page: http://kohanaframework.org/documentation
A Kohana module for database interactions, building queries, and prepared statements
Home Page: http://kohanaframework.org/documentation
$this->_connection->query('SHOW TABLES')->fetchAll(\PDO::FETCH_COLUMN)
should work.
Any idea why it's made to throw an exception for the PDO driver?
There is two related bugs in Database_Query_Insert.
First bug is here:
https://github.com/kohana/database/blob/3.3/master/classes/Kohana/Database/Query/Builder/Insert.php#L91
func_get_args
returns array of arguments, but we except every argument to be array, we need to flattern this array by one level. This is how I would fix it:
...
// Get all of the passed values
$values = array();
$args_values = func_get_args();
foreach($args_values as $i => $arg_values)
{
if ( ! is_array($arg_values))
{
throw new InvalidArgumentException('Argument ' . $i . ' must be array, got ' . gettype($arg_values));
}
}
foreach($args_values as $arg_values)
{
// Detect if it is row or array of rows
if ( ! is_array(reset($arg_values)))
{
$arg_values = array($arg_values);
}
$values = array_merge($values, $arg_values);
}
...
Second (related bug) is here:
https://github.com/kohana/database/blob/3.3/master/classes/Kohana/Database/Query/Builder/Insert.php#L141-L160
Note that variable $quote
is not used, perhaps it migrated from previous code or it is just unfinished code. Actual problem is following. If we do bulk insert, it generates SQL-code INSERT INTO table(column1, column2) VALUES ((value1_1, value1_2), (value2_1, value2_2));
. Notice double round braces. This SQL-code is not working in PostgreSQL and SphinxQL, it works only in MySQL. Proper SQL-code:
INSERT INTO table(column1, column2) VALUES (value1_1, value1_2), (value2_1, value2_2);
. I have no fix currently.
--dry-run
still selects MySQL driver instead of MySQLi. MySQL driver is deprecated now.
Please give me a full example of correct transaction script for update N rows in one table.
The code below is doesn't pay attention to begin/commit commands and executing N queries.
P.S. There is no answer. There only "..." instead of the answer.
// in $items there is array of rows to update ($column_name => $column_data)
Database::instance()->begin();
try
{
foreach ($items as $item) {
$query = DB::update($this->_table);
foreach ($item as $key => $val) {
$query->set(array($key => $val));
}
$query->where($this->_table_primary_key, '=', (int)$item[$this->_table_primary_key]);
$query->execute();
}
Database::instance()->commit();
return count($items);
}
catch (Database_Exception $e)
{
Database::instance()->rollback();
return 0;
}
I'm creating standalone query builder based on kohana database module. I fugured out that Insert Query Builder allows to use table alias while using select subquery instead of array values. This causes sql syntax error.
Solution:
Throw an exception during insert query compilation if $this->_table is array
// Insert class
if (is_array($this->_values))
{
//[...]
}
else
{
if (is_array($this->_table)) {
throw new Exception('Cannot use table alias with INSERT INTO ... SELECT ... construction');
}
// Add the sub-query
$query .= (string) $this->_values;
}
I found an error in kohana doc in /guide/database/query/builder file
$query = DB::select(array('username', 'u'), array('password', 'p'))->from('users');
sould be
$query = DB::select(array(array('username', 'u'), array('password', 'p')))->from('users');
Would it be possible to enhance this module with connection to multiple Cassandra nodes (the one we're connecting to is picked randomly)?
Why don't add MySQLi driver to the master branch, you already have it on 3.4 develop branch https://github.com/kohana/database/tree/3.4/develop/classes/Database
I was having an issue adding session data to the database after deleting the current session (using $session->restart()). My session was removed but $this->_update_id was not set to NULL to allow inserting of a new session. The result is function _write issues an update query for a non existent session row.
I solved my issue by adding a line of code in database/classes/Kohana/Session/Database.php after
// Delete the cookie
Cookie::delete($this->_name);
//New line
$this->_update_id = NULL;
in _destroy.
Two weeks ago i run new site and made some actions to populize him in Google, Yandex and other search-engines. But... After my good work, i saw, that in my "sessions" table are more than 100k nodes. That was sessions of bots (Google, Yandex, etc.). This happened, cuz bots not accepts cookies, so to each request kohana made another one node to sessions table. Ofc, thats not OK, and this cant solve by garbage collection cleaning. So i solved that by adding to "modules/database/classes/Kohana/Session/Database.php (Kohana_session_Database class)" next code in 138 line (in function _write):
if(Request::user_agent('robot') != '')
{
return TRUE;
}
(also i've added to user_agent.php a few new user_agents (of bots), you can see it on this link http://pastebin.com/kpJdKz1D);
So, that works! Buy, by science, making changes in system modules/files is very bad and i transfered it to my helper-module by this path: "myhelpermodule/classes/Session.php":
abstract class Session extends Kohana_Session
{
public function write()
{
if(Request::user_agent('robot') != '')
{
return TRUE;
}
return parent::write();
}
}
So, that works, but looks very strange. Can you, please, say something about my trouble, my solve and this situation ?
(and, to more information my session config file: http://pastebin.com/rVBGTzgD)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.