Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Y
yii2bootstrap
Project
Project
Details
Activity
Releases
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
夏彬
yii2bootstrap
Commits
965fe5c4
Commit
965fe5c4
authored
Feb 25, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
cf10943c
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
1544 additions
and
999 deletions
+1544
-999
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+453
-0
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+341
-261
ActiveRecordBehavior.php
framework/db/ar/ActiveRecordBehavior.php
+25
-4
ActiveRelation.php
framework/db/ar/ActiveRelation.php
+39
-42
BaseQuery.php
framework/db/dao/BaseQuery.php
+635
-0
Command.php
framework/db/dao/Command.php
+5
-21
Connection.php
framework/db/dao/Connection.php
+4
-7
Query.php
framework/db/dao/Query.php
+18
-634
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+24
-30
No files found.
framework/db/ar/ActiveQuery.php
0 → 100644
View file @
965fe5c4
This diff is collapsed.
Click to expand it.
framework/db/ar/ActiveRecord.php
View file @
965fe5c4
This diff is collapsed.
Click to expand it.
framework/db/ar/ActiveRecordBehavior.php
View file @
965fe5c4
...
@@ -27,8 +27,10 @@ class CActiveRecordBehavior extends CModelBehavior
...
@@ -27,8 +27,10 @@ class CActiveRecordBehavior extends CModelBehavior
public
function
events
()
public
function
events
()
{
{
return
array_merge
(
parent
::
events
(),
array
(
return
array_merge
(
parent
::
events
(),
array
(
'onBeforeSave'
=>
'beforeSave'
,
'onBeforeInsert'
=>
'beforeInsert'
,
'onAfterSave'
=>
'afterSave'
,
'onAfterInsert'
=>
'afterInsert'
,
'onBeforeUpdate'
=>
'beforeUpdate'
,
'onAfterUpdate'
=>
'afterUpdate'
,
'onBeforeDelete'
=>
'beforeDelete'
,
'onBeforeDelete'
=>
'beforeDelete'
,
'onAfterDelete'
=>
'afterDelete'
,
'onAfterDelete'
=>
'afterDelete'
,
'onBeforeFind'
=>
'beforeFind'
,
'onBeforeFind'
=>
'beforeFind'
,
...
@@ -42,7 +44,7 @@ class CActiveRecordBehavior extends CModelBehavior
...
@@ -42,7 +44,7 @@ class CActiveRecordBehavior extends CModelBehavior
* You may set {@link CModelEvent::isValid} to be false to quit the saving process.
* You may set {@link CModelEvent::isValid} to be false to quit the saving process.
* @param CModelEvent $event event parameter
* @param CModelEvent $event event parameter
*/
*/
public
function
before
Save
(
$event
)
public
function
before
Insert
(
$event
)
{
{
}
}
...
@@ -51,7 +53,26 @@ class CActiveRecordBehavior extends CModelBehavior
...
@@ -51,7 +53,26 @@ class CActiveRecordBehavior extends CModelBehavior
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CModelEvent $event event parameter
* @param CModelEvent $event event parameter
*/
*/
public
function
afterSave
(
$event
)
public
function
afterInsert
(
$event
)
{
}
/**
* Responds to {@link CActiveRecord::onBeforeSave} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* You may set {@link CModelEvent::isValid} to be false to quit the saving process.
* @param CModelEvent $event event parameter
*/
public
function
beforeUpdate
(
$event
)
{
}
/**
* Responds to {@link CActiveRecord::onAfterSave} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CModelEvent $event event parameter
*/
public
function
afterUpdate
(
$event
)
{
{
}
}
...
...
framework/db/ar/ActiveRelation.php
View file @
965fe5c4
<?php
<?php
/**
* ActiveRelation class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\db\ar
;
namespace
yii\db\ar
;
class
ActiveRelation
extends
\yii\base\Object
use
yii\db\dao\BaseQuery
;
/**
* ActiveRelation represents the specification of a relation declared in [[ActiveRecord::relations()]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ActiveRelation
extends
BaseQuery
{
{
/**
* @var string the name of this relation
*/
public
$name
;
public
$name
;
public
$modelClass
;
public
$hasMany
;
public
$joinType
;
public
$tableAlias
;
public
$on
;
public
$via
;
public
$with
;
public
$scopes
;
/**
/**
* @var string|array the columns being selected. This refers to the SELECT clause in a SQL
* @var string the name of the model class that this relation represents
* statement. It can be either a string (e.g. `'id, name'`) or an array (e.g. `array('id', 'name')`).
* If not set, if means all columns.
* @see select()
*/
*/
public
$
select
;
public
$
modelClass
;
/**
/**
* @var string|array query condition. This refers to the WHERE clause in a SQL statement.
* @var boolean whether this relation is a one-many relation
* For example, `age > 31 AND team = 1`.
* @see where()
*/
*/
public
$
where
;
public
$
hasMany
;
/**
/**
* @var
integer maximum number of records to be returned. If not set or less than 0, it means no limit
.
* @var
string the join type (e.g. INNER JOIN, LEFT JOIN). Defaults to 'LEFT JOIN'
.
*/
*/
public
$
limit
;
public
$
joinType
=
'LEFT JOIN'
;
/**
/**
* @var integer zero-based offset from where the records are to be returned. If not set or
* @var string the table alias used for the corresponding table during query
* less than 0, it means starting from the beginning.
*/
*/
public
$
offset
;
public
$
tableAlias
;
/**
/**
* @var string
|array how to sort the query results. This refers to the ORDER BY clause in a SQL statement
.
* @var string
the name of the column that the result should be indexed by
.
*
It can be either a string (e.g. `'id ASC, name DESC'`) or an array (e.g. `array('id ASC', 'name DESC')`)
.
*
This is only useful when [[hasMany]] is true
.
*/
*/
public
$
order
By
;
public
$
index
By
;
/**
/**
* @var string|array how to group the query results. This refers to the GROUP BY clause in a SQL statement.
* @var string the ON clause of the join query
* It can be either a string (e.g. `'company, department'`) or an array (e.g. `array('company', 'department')`).
*/
*/
public
$
groupBy
;
public
$
on
;
/**
/**
* @var string|array how to join with other tables. This refers to the JOIN clause in a SQL statement.
* @var string
* It can either a string (e.g. `'LEFT JOIN tbl_user ON tbl_user.id=author_id'`) or an array (e.g.
* `array('LEFT JOIN tbl_user ON tbl_user.id=author_id', 'LEFT JOIN tbl_team ON tbl_team.id=team_id')`).
* @see join()
*/
*/
public
$
join
;
public
$
via
;
/**
/**
* @var string|array the condition to be applied in the GROUP BY clause.
* @var array the relations that should be queried together (eager loading)
* It can be either a string or an array. Please refer to [[where()]] on how to specify the condition.
*/
*/
public
$
having
;
public
$
with
;
/**
/**
* @var array list of query parameter values indexed by parameter placeholders.
* @var array the scopes that should be applied during query
* For example, `array(':name'=>'Dan', ':age'=>31)`.
*/
*/
public
$
param
s
;
public
$
scope
s
;
}
}
framework/db/dao/BaseQuery.php
0 → 100644
View file @
965fe5c4
This diff is collapsed.
Click to expand it.
framework/db/dao/Command.php
View file @
965fe5c4
...
@@ -64,27 +64,15 @@ class Command extends \yii\base\Component
...
@@ -64,27 +64,15 @@ class Command extends \yii\base\Component
/**
/**
* Constructor.
* Constructor.
* Instead of using the `new` operator, you may use [[Connection::createCommand()]]
* to create a new Command object.
* @param Connection $connection the database connection
* @param Connection $connection the database connection
* @param string|array|Query $query the DB query to be executed. This can be:
* @param string $sql the SQL statement to be executed
*
* @param array $params the parameters to be bound to the SQL statement
* - a string representing the SQL statement to be executed
* - a [[Query]] object representing the SQL query
* - an array that will be used to create and initialize the [[Query]] object
*/
*/
public
function
__construct
(
$connection
,
$
query
=
null
)
public
function
__construct
(
$connection
,
$
sql
=
null
,
$params
=
array
()
)
{
{
$this
->
connection
=
$connection
;
$this
->
connection
=
$connection
;
if
(
is_array
(
$query
))
{
$this
->
_sql
=
$sql
;
$query
=
Query
::
newInstance
(
$query
);
$this
->
bindValues
(
$params
);
}
if
(
$query
instanceof
Query
)
{
$this
->
_sql
=
$query
->
getSql
(
$connection
);
$this
->
bindValues
(
$query
->
params
);
}
else
{
$this
->
_sql
=
$query
;
}
}
}
/**
/**
...
@@ -233,8 +221,6 @@ class Command extends \yii\base\Component
...
@@ -233,8 +221,6 @@ class Command extends \yii\base\Component
$paramLog
=
"
\n
Parameters: "
.
var_export
(
$this
->
_params
,
true
);
$paramLog
=
"
\n
Parameters: "
.
var_export
(
$this
->
_params
,
true
);
}
}
echo
"Executing SQL:
{
$sql
}{
$paramLog
}
"
.
"
\n\n
"
;
\Yii
::
trace
(
"Executing SQL:
{
$sql
}{
$paramLog
}
"
,
__CLASS__
);
\Yii
::
trace
(
"Executing SQL:
{
$sql
}{
$paramLog
}
"
,
__CLASS__
);
try
{
try
{
...
@@ -368,8 +354,6 @@ echo "Executing SQL: {$sql}{$paramLog}" . "\n\n";
...
@@ -368,8 +354,6 @@ echo "Executing SQL: {$sql}{$paramLog}" . "\n\n";
$paramLog
=
"
\n
Parameters: "
.
var_export
(
$this
->
_params
,
true
);
$paramLog
=
"
\n
Parameters: "
.
var_export
(
$this
->
_params
,
true
);
}
}
echo
"Executing SQL:
{
$sql
}{
$paramLog
}
"
.
"
\n\n
"
;
\Yii
::
trace
(
"Querying SQL:
{
$sql
}{
$paramLog
}
"
,
__CLASS__
);
\Yii
::
trace
(
"Querying SQL:
{
$sql
}{
$paramLog
}
"
,
__CLASS__
);
if
(
$db
->
queryCachingCount
>
0
&&
$db
->
queryCachingDuration
>=
0
&&
$method
!==
''
)
{
if
(
$db
->
queryCachingCount
>
0
&&
$db
->
queryCachingDuration
>=
0
&&
$method
!==
''
)
{
...
...
framework/db/dao/Connection.php
View file @
965fe5c4
...
@@ -399,17 +399,14 @@ class Connection extends \yii\base\ApplicationComponent
...
@@ -399,17 +399,14 @@ class Connection extends \yii\base\ApplicationComponent
/**
/**
* Creates a command for execution.
* Creates a command for execution.
* @param string|array|Query $query the DB query to be executed. This can be:
* @param string $sql the SQL statement to be executed
*
* @param array $params the parameters to be bound to the SQL statement
* - a string representing the SQL statement to be executed
* - a [[Query]] object representing the SQL query
* - an array that will be used to initialize [[Query]]
* @return Command the DB command
* @return Command the DB command
*/
*/
public
function
createCommand
(
$
query
=
null
)
public
function
createCommand
(
$
sql
=
null
,
$params
=
array
()
)
{
{
$this
->
open
();
$this
->
open
();
return
new
Command
(
$this
,
$
query
);
return
new
Command
(
$this
,
$
sql
,
$params
);
}
}
/**
/**
...
...
framework/db/dao/Query.php
View file @
965fe5c4
This diff is collapsed.
Click to expand it.
framework/db/dao/QueryBuilder.php
View file @
965fe5c4
...
@@ -13,9 +13,9 @@ namespace yii\db\dao;
...
@@ -13,9 +13,9 @@ namespace yii\db\dao;
use
yii\db\Exception
;
use
yii\db\Exception
;
/**
/**
* QueryBuilder builds a S
QL statement based on the specification given as a [[
Query]] object.
* QueryBuilder builds a S
ELECT SQL statement based on the specification given as a [[Base
Query]] object.
*
*
* QueryBuilder
is often used behind the scenes by [[Query]] to build a DBMS-dependent SQL statement
* QueryBuilder
can also be used to build SQL statements such as INSERT, UPDATE, DELETE, CREATE TABLE,
* from a [[Query]] object.
* from a [[Query]] object.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
...
@@ -57,35 +57,25 @@ class QueryBuilder extends \yii\base\Object
...
@@ -57,35 +57,25 @@ class QueryBuilder extends \yii\base\Object
}
}
/**
/**
* Generates a SQL statement from a [[Query]] object.
* Generates a SELECT SQL statement from a [[BaseQuery]] object.
* Note that when generating SQL statements for INSERT and UPDATE queries,
* @param BaseQuery $query the [[Query]] object from which the SQL statement will be generated
* the query object's [[Query::params]] property may be appended with new parameters.
* @param Query $query the [[Query]] object from which the SQL statement will be generated
* @return string the generated SQL statement
* @return string the generated SQL statement
*/
*/
public
function
build
(
$query
)
public
function
build
(
$query
)
{
{
$this
->
query
=
$query
;
$this
->
query
=
$query
;
if
(
$query
->
operation
!==
null
)
{
$clauses
=
array
(
// non-SELECT query
$this
->
buildSelect
(),
$params
=
$query
->
operation
;
$this
->
buildFrom
(),
$method
=
array_shift
(
$params
);
$this
->
buildJoin
(),
return
call_user_func_array
(
array
(
$this
,
$method
),
$params
);
$this
->
buildWhere
(),
}
else
{
$this
->
buildGroupBy
(),
// SELECT query
$this
->
buildHaving
(),
$clauses
=
array
(
$this
->
buildUnion
(),
$this
->
buildSelect
(),
$this
->
buildOrderBy
(),
$this
->
buildFrom
(),
$this
->
buildLimit
(),
$this
->
buildJoin
(),
);
$this
->
buildWhere
(),
return
implode
(
$this
->
separator
,
array_filter
(
$clauses
));
$this
->
buildGroupBy
(),
$this
->
buildHaving
(),
$this
->
buildUnion
(),
$this
->
buildOrderBy
(),
$this
->
buildLimit
(),
);
return
implode
(
$this
->
separator
,
array_filter
(
$clauses
));
}
}
}
/**
/**
...
@@ -123,7 +113,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -123,7 +113,7 @@ class QueryBuilder extends \yii\base\Object
$count
++
;
$count
++
;
}
}
}
}
if
(
$this
->
query
instanceof
Query
)
{
if
(
$this
->
query
instanceof
Base
Query
)
{
$this
->
query
->
addParams
(
$params
);
$this
->
query
->
addParams
(
$params
);
}
}
...
@@ -167,7 +157,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -167,7 +157,7 @@ class QueryBuilder extends \yii\base\Object
$count
++
;
$count
++
;
}
}
}
}
if
(
$this
->
query
instanceof
Query
)
{
if
(
$this
->
query
instanceof
Base
Query
)
{
$this
->
query
->
addParams
(
$params
);
$this
->
query
->
addParams
(
$params
);
}
}
$sql
=
'UPDATE '
.
$this
->
quoteTableName
(
$table
)
.
' SET '
.
implode
(
', '
,
$lines
);
$sql
=
'UPDATE '
.
$this
->
quoteTableName
(
$table
)
.
' SET '
.
implode
(
', '
,
$lines
);
...
@@ -189,14 +179,18 @@ class QueryBuilder extends \yii\base\Object
...
@@ -189,14 +179,18 @@ class QueryBuilder extends \yii\base\Object
* @param string $table the table where the data will be deleted from.
* @param string $table the table where the data will be deleted from.
* @param mixed $condition the condition that will be put in the WHERE part. Please
* @param mixed $condition the condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* refer to [[Query::where()]] on how to specify condition.
* @param array $params the parameters to be bound to the query.
* @return integer number of rows affected by the execution.
* @return integer number of rows affected by the execution.
*/
*/
public
function
delete
(
$table
,
$condition
=
''
)
public
function
delete
(
$table
,
$condition
=
''
,
$params
=
array
()
)
{
{
$sql
=
'DELETE FROM '
.
$this
->
quoteTableName
(
$table
);
$sql
=
'DELETE FROM '
.
$this
->
quoteTableName
(
$table
);
if
((
$where
=
$this
->
buildCondition
(
$condition
))
!=
''
)
{
if
((
$where
=
$this
->
buildCondition
(
$condition
))
!=
''
)
{
$sql
.=
' WHERE '
.
$where
;
$sql
.=
' WHERE '
.
$where
;
}
}
if
(
$params
!==
array
()
&&
$this
->
query
instanceof
BaseQuery
)
{
$this
->
query
->
addParams
(
$params
);
}
return
$sql
;
return
$sql
;
}
}
...
@@ -631,7 +625,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -631,7 +625,7 @@ class QueryBuilder extends \yii\base\Object
protected
function
buildSelect
()
protected
function
buildSelect
()
{
{
$select
=
$this
->
query
->
distinct
?
'SELECT DISTINCT'
:
'SELECT'
;
$select
=
$this
->
query
->
distinct
?
'SELECT DISTINCT'
:
'SELECT'
;
if
(
$this
->
query
->
selectOption
!=
''
)
{
if
(
$this
->
query
->
selectOption
!=
=
null
)
{
$select
.=
' '
.
$this
->
query
->
selectOption
;
$select
.=
' '
.
$this
->
query
->
selectOption
;
}
}
...
...
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