Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
91e0c5ae
Commit
91e0c5ae
authored
Jul 11, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Connection::useMaster() and refactored code.
parent
d2cb245d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
15 deletions
+38
-15
Command.php
framework/db/Command.php
+1
-1
Connection.php
framework/db/Connection.php
+25
-0
QueryBuilder.php
framework/db/oci/QueryBuilder.php
+4
-5
Schema.php
framework/db/oci/Schema.php
+4
-5
QueryBuilder.php
framework/db/sqlite/QueryBuilder.php
+4
-4
No files found.
framework/db/Command.php
View file @
91e0c5ae
...
...
@@ -69,7 +69,7 @@ class Command extends \yii\base\Component
/**
* @var array the parameters (name => value) that are bound to the current PDO statement.
* This property is maintained by methods such as [[bindValue()]]. It is mainly provided for logging purpose
* and is used to generate
d
[[rawSql]]. Do not modify it directly.
* and is used to generate [[rawSql]]. Do not modify it directly.
*/
public
$params
=
[];
/**
...
...
framework/db/Connection.php
View file @
91e0c5ae
...
...
@@ -700,6 +700,31 @@ class Connection extends Component
}
/**
* Executes the provided callback by using the master connection.
*
* This method is provided so that you can temporarily force using the master connection to perform
* DB operations. For example,
*
* ```php
* $result = $db->useMaster(function ($db) {
* return $db->createCommand('SELECT * FROM user LIMIT 1')->queryOne();
* });
* ```
*
* @param callable $callback a PHP callable to be executed by this method. Its signature is
* `function ($db)`. Its return value will be returned by this method.
* @return mixed the return value of the callback
*/
public
function
useMaster
(
callable
$callback
)
{
$enableSlave
=
$this
->
enableSlave
;
$this
->
enableSlave
=
false
;
$result
=
call_user_func
(
$callback
,
$this
);
$this
->
enableSlave
=
$enableSlave
;
return
$result
;
}
/**
* Selects a slave and opens the connection.
* @param array $slaves the list of candidate slave configurations
* @return Connection the opened slave connection, or null if no slave is available
...
...
framework/db/oci/QueryBuilder.php
View file @
91e0c5ae
...
...
@@ -8,6 +8,7 @@
namespace
yii\db\oci
;
use
yii\base\InvalidParamException
;
use
yii\db\Connection
;
/**
* QueryBuilder is the query builder for Oracle databases.
...
...
@@ -133,11 +134,9 @@ EOD;
$value
=
(
int
)
$value
;
}
else
{
// use master connection to get the biggest PK value
$enableSlave
=
$this
->
db
->
enableSlave
;
$this
->
db
->
enableSlave
=
false
;
$value
=
(
int
)
$this
->
db
->
createCommand
(
"SELECT MAX(
\"
{
$tableSchema
->
primaryKey
}
\"
) FROM
\"
{
$tableSchema
->
name
}
\"
"
)
->
queryScalar
();
$this
->
db
->
enableSlave
=
$enableSlave
;
$value
++
;
$value
=
$this
->
db
->
useMaster
(
function
(
Connection
$db
)
use
(
$tableSchema
)
{
return
$db
->
createCommand
(
"SELECT MAX(
\"
{
$tableSchema
->
primaryKey
}
\"
) FROM
\"
{
$tableSchema
->
name
}
\"
"
)
->
queryScalar
();
})
+
1
;
}
return
"DROP SEQUENCE
\"
{
$tableSchema
->
name
}
_SEQ
\"
;"
...
...
framework/db/oci/Schema.php
View file @
91e0c5ae
...
...
@@ -8,6 +8,7 @@
namespace
yii\db\oci
;
use
yii\base\InvalidCallException
;
use
yii\db\Connection
;
use
yii\db\TableSchema
;
use
yii\db\ColumnSchema
;
...
...
@@ -196,11 +197,9 @@ EOD;
{
if
(
$this
->
db
->
isActive
)
{
// get the last insert id from the master connection
$enableSlave
=
$this
->
db
->
enableSlave
;
$this
->
db
->
enableSlave
=
false
;
$id
=
$this
->
db
->
createCommand
(
"SELECT
{
$sequenceName
}
.CURRVAL FROM DUAL"
)
->
queryScalar
();
$this
->
db
->
enableSlave
=
$enableSlave
;
return
$id
;
return
$this
->
db
->
useMaster
(
function
(
Connection
$db
)
use
(
$sequenceName
)
{
return
$db
->
createCommand
(
"SELECT
{
$sequenceName
}
.CURRVAL FROM DUAL"
)
->
queryScalar
();
});
}
else
{
throw
new
InvalidCallException
(
'DB Connection is not active.'
);
}
...
...
framework/db/sqlite/QueryBuilder.php
View file @
91e0c5ae
...
...
@@ -7,6 +7,7 @@
namespace
yii\db\sqlite
;
use
yii\db\Connection
;
use
yii\db\Exception
;
use
yii\base\InvalidParamException
;
use
yii\base\NotSupportedException
;
...
...
@@ -120,10 +121,9 @@ class QueryBuilder extends \yii\db\QueryBuilder
if
(
$value
===
null
)
{
$key
=
reset
(
$table
->
primaryKey
);
$tableName
=
$db
->
quoteTableName
(
$tableName
);
$enableSlave
=
$this
->
db
->
enableSlave
;
$this
->
db
->
enableSlave
=
false
;
$value
=
$db
->
createCommand
(
"SELECT MAX('
$key
') FROM
$tableName
"
)
->
queryScalar
();
$this
->
db
->
enableSlave
=
$enableSlave
;
$value
=
$this
->
db
->
useMaster
(
function
(
Connection
$db
)
use
(
$key
,
$tableName
)
{
return
$db
->
createCommand
(
"SELECT MAX('
$key
') FROM
$tableName
"
)
->
queryScalar
();
});
}
else
{
$value
=
(
int
)
$value
-
1
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment