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
702476d8
Commit
702476d8
authored
Dec 28, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
1295fbe2
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
180 additions
and
130 deletions
+180
-130
Model.php
framework/base/Model.php
+13
-13
ModelBehavior.php
framework/base/ModelBehavior.php
+4
-4
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+144
-98
QueryBuilder.php
framework/db/dao/mysql/QueryBuilder.php
+10
-6
QueryBuilder.php
framework/db/dao/sqlite/QueryBuilder.php
+7
-7
CommandTest.php
tests/unit/framework/db/dao/CommandTest.php
+2
-2
No files found.
framework/base/Model.php
View file @
702476d8
...
...
@@ -22,9 +22,9 @@ namespace yii\base;
*
* Model also provides a set of events for further customization:
*
* - [[onAfter
Construct]]: an event raised at the end of constructor
* - [[onBeforeValidate]]: an event raised at the beginning of [[validate]]
* - [[onAfterValidate]]: an event raised at the end of [[validate]]
* - [[onAfter
Init]]: an event raised at the end of [[init()]]
* - [[onBeforeValidate]]: an event raised at the beginning of [[validate
()
]]
* - [[onAfterValidate]]: an event raised at the end of [[validate
()
]]
*
* You may directly use Model to store model data, or extend it with customization.
* You may also customize Model by attaching [[ModelBehavior|model behaviors]].
...
...
@@ -46,7 +46,6 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
public
function
__construct
(
$scenario
=
''
)
{
$this
->
_scenario
=
$scenario
;
$this
->
afterConstruct
();
}
/**
...
...
@@ -62,6 +61,7 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
public
function
init
()
{
$this
->
attachBehaviors
(
$this
->
behaviors
());
$this
->
afterInit
();
}
/**
...
...
@@ -136,7 +136,7 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
* - validator type: required, specifies the validator to be used. It can be the name of a model
* class method, the name of a built-in validator, or a validator class (or its path alias).
* - on: optional, specifies the [[scenario|scenarios]] (separated by commas) when the validation
* rule can be applied. If this option is not set, the rule will apply to a
ny scenario
.
* rule can be applied. If this option is not set, the rule will apply to a
ll scenarios
.
* - additional name-value pairs can be specified to initialize the corresponding validator properties.
* Please refer to individual validator class API for possible properties.
*
...
...
@@ -232,15 +232,15 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
}
/**
* This method is invoked at the end of
model constructor
.
* The default implementation raises the [[onAfter
Construc
t]] event.
* This method is invoked at the end of
[[init()]]
.
* The default implementation raises the [[onAfter
Ini
t]] event.
* You may override this method to do postprocessing after model creation.
* Make sure you call the parent implementation so that the event is raised properly.
*/
public
function
after
Construc
t
()
public
function
after
Ini
t
()
{
if
(
$this
->
hasEventHandlers
(
'onAfter
Construc
t'
))
{
$this
->
onAfter
Construc
t
(
new
Event
(
$this
));
if
(
$this
->
hasEventHandlers
(
'onAfter
Ini
t'
))
{
$this
->
onAfter
Ini
t
(
new
Event
(
$this
));
}
}
...
...
@@ -285,10 +285,10 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
}
/**
* This event is raised a
fter the model instance is created by new operator
.
* This event is raised a
t the end of [[init()]]
.
* @param Event $event the event parameter
*/
public
function
onAfter
Construc
t
(
$event
)
public
function
onAfter
Ini
t
(
$event
)
{
$this
->
raiseEvent
(
__FUNCTION__
,
$event
);
}
...
...
@@ -584,7 +584,7 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
public
function
onUnsafeAttribute
(
$name
,
$value
)
{
if
(
YII_DEBUG
)
{
\Yii
::
warning
(
sprintf
(
'Failed to set unsafe attribute "%s" in "%s".'
,
$name
,
get_class
(
$this
))
);
\Yii
::
warning
(
"Failed to set unsafe attribute '
$name
' in '"
.
get_class
(
$this
)
.
"'."
);
}
}
...
...
framework/base/ModelBehavior.php
View file @
702476d8
...
...
@@ -24,7 +24,7 @@ class ModelBehavior extends Behavior
* Declares event handlers for owner's events.
* The default implementation returns the following event handlers:
*
* - `onAfter
Construct` event: [[afterConstruc
t]]
* - `onAfter
Init` event: [[afterIni
t]]
* - `onBeforeValidate` event: [[beforeValidate]]
* - `onAfterValidate` event: [[afterValidate]]
*
...
...
@@ -34,18 +34,18 @@ class ModelBehavior extends Behavior
public
function
events
()
{
return
array
(
'onAfter
Construct'
=>
'afterConstruc
t'
,
'onAfter
Init'
=>
'afterIni
t'
,
'onBeforeValidate'
=>
'beforeValidate'
,
'onAfterValidate'
=>
'afterValidate'
,
);
}
/**
* Responds to [[Model::onAfter
Construc
t]] event.
* Responds to [[Model::onAfter
Ini
t]] event.
* Override this method if you want to handle the corresponding event of the [[owner]].
* @param Event $event event parameter
*/
public
function
after
Construc
t
(
$event
)
public
function
after
Ini
t
(
$event
)
{
}
...
...
framework/db/dao/QueryBuilder.php
View file @
702476d8
...
...
@@ -44,9 +44,13 @@ class QueryBuilder extends \yii\base\Object
public
$separator
=
" "
;
/**
* @var Query the Query object that is currently processed by the query builder to generate a SQL statement.
*
After the SQL statement is generated by [[build()]], this property will be set null
.
*
This property will be set null upon completion of [[build()]]
.
*/
public
$query
;
/**
* @var boolean whether to automatically quote table and column names when generating SQL statements.
*/
public
$autoQuote
=
true
;
/**
* Constructor.
...
...
@@ -116,7 +120,7 @@ class QueryBuilder extends \yii\base\Object
$placeholders
=
array
();
$count
=
0
;
foreach
(
$columns
as
$name
=>
$value
)
{
$names
[]
=
$this
->
driver
->
quoteColumnName
(
$name
);
$names
[]
=
$this
->
quoteColumnName
(
$name
);
if
(
$value
instanceof
Expression
)
{
$placeholders
[]
=
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
...
...
@@ -132,7 +136,7 @@ class QueryBuilder extends \yii\base\Object
$this
->
query
->
addParams
(
$params
);
}
return
'INSERT INTO '
.
$this
->
driver
->
quoteTableName
(
$table
)
return
'INSERT INTO '
.
$this
->
quoteTableName
(
$table
)
.
' ('
.
implode
(
', '
,
$names
)
.
') VALUES ('
.
implode
(
', '
,
$placeholders
)
.
')'
;
}
...
...
@@ -163,12 +167,12 @@ class QueryBuilder extends \yii\base\Object
$count
=
0
;
foreach
(
$columns
as
$name
=>
$value
)
{
if
(
$value
instanceof
Expression
)
{
$lines
[]
=
$this
->
driver
->
quoteSimpleColumnName
(
$nam
e
)
.
'='
.
$value
->
expression
;
$lines
[]
=
$this
->
quoteColumnName
(
$name
,
tru
e
)
.
'='
.
$value
->
expression
;
foreach
(
$value
->
params
as
$n
=>
$v
)
{
$params
[
$n
]
=
$v
;
}
}
else
{
$lines
[]
=
$this
->
driver
->
quoteSimpleColumnName
(
$nam
e
)
.
'=:p'
.
$count
;
$lines
[]
=
$this
->
quoteColumnName
(
$name
,
tru
e
)
.
'=:p'
.
$count
;
$params
[
':p'
.
$count
]
=
$value
;
$count
++
;
}
...
...
@@ -176,7 +180,7 @@ class QueryBuilder extends \yii\base\Object
if
(
$this
->
query
instanceof
Query
)
{
$this
->
query
->
addParams
(
$params
);
}
$sql
=
'UPDATE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' SET '
.
implode
(
', '
,
$lines
);
$sql
=
'UPDATE '
.
$this
->
quoteTableName
(
$table
)
.
' SET '
.
implode
(
', '
,
$lines
);
if
((
$where
=
$this
->
buildCondition
(
$condition
))
!=
''
)
{
$sql
.=
' WHERE '
.
$where
;
}
...
...
@@ -199,7 +203,7 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
delete
(
$table
,
$condition
=
''
)
{
$sql
=
'DELETE FROM '
.
$this
->
driver
->
quoteTableName
(
$table
);
$sql
=
'DELETE FROM '
.
$this
->
quoteTableName
(
$table
);
if
((
$where
=
$this
->
buildCondition
(
$condition
))
!=
''
)
{
$sql
.=
' WHERE '
.
$where
;
}
...
...
@@ -237,13 +241,12 @@ class QueryBuilder extends \yii\base\Object
$cols
=
array
();
foreach
(
$columns
as
$name
=>
$type
)
{
if
(
is_string
(
$name
))
{
$cols
[]
=
"
\t
"
.
$this
->
driver
->
quoteColumnName
(
$name
)
.
' '
.
$this
->
getColumnType
(
$type
);
}
else
{
$cols
[]
=
"
\t
"
.
$this
->
quoteColumnName
(
$name
)
.
' '
.
$this
->
getColumnType
(
$type
);
}
else
{
$cols
[]
=
"
\t
"
.
$type
;
}
}
$sql
=
"CREATE TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
)
.
" (
\n
"
.
implode
(
",
\n
"
,
$cols
)
.
"
\n
)"
;
$sql
=
"CREATE TABLE "
.
$this
->
quoteTableName
(
$table
)
.
" (
\n
"
.
implode
(
",
\n
"
,
$cols
)
.
"
\n
)"
;
return
$options
===
null
?
$sql
:
$sql
.
' '
.
$options
;
}
...
...
@@ -255,7 +258,7 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
renameTable
(
$oldName
,
$newName
)
{
return
'RENAME TABLE '
.
$this
->
driver
->
quoteTableName
(
$oldName
)
.
' TO '
.
$this
->
driver
->
quoteTableName
(
$newName
);
return
'RENAME TABLE '
.
$this
->
quoteTableName
(
$oldName
)
.
' TO '
.
$this
->
quoteTableName
(
$newName
);
}
/**
...
...
@@ -265,7 +268,7 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
dropTable
(
$table
)
{
return
"DROP TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
);
return
"DROP TABLE "
.
$this
->
quoteTableName
(
$table
);
}
/**
...
...
@@ -275,7 +278,7 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
truncateTable
(
$table
)
{
return
"TRUNCATE TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
);
return
"TRUNCATE TABLE "
.
$this
->
quoteTableName
(
$table
);
}
/**
...
...
@@ -289,8 +292,8 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
addColumn
(
$table
,
$column
,
$type
)
{
return
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' ADD '
.
$this
->
driver
->
quoteColumnName
(
$column
)
.
' '
return
'ALTER TABLE '
.
$this
->
quoteTableName
(
$table
)
.
' ADD '
.
$this
->
quoteColumnName
(
$column
)
.
' '
.
$this
->
getColumnType
(
$type
);
}
...
...
@@ -302,8 +305,8 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
dropColumn
(
$table
,
$column
)
{
return
"ALTER TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
)
.
" DROP COLUMN "
.
$this
->
driver
->
quoteSimpleColumnName
(
$column
);
return
"ALTER TABLE "
.
$this
->
quoteTableName
(
$table
)
.
" DROP COLUMN "
.
$this
->
quoteColumnName
(
$column
,
true
);
}
/**
...
...
@@ -315,9 +318,9 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
renameColumn
(
$table
,
$oldName
,
$newName
)
{
return
"ALTER TABLE "
.
$this
->
driver
->
quoteTableName
(
$table
)
.
" RENAME COLUMN "
.
$this
->
driver
->
quoteSimpleColumnName
(
$oldNam
e
)
.
" TO "
.
$this
->
driver
->
quoteSimpleColumnName
(
$newNam
e
);
return
"ALTER TABLE "
.
$this
->
quoteTableName
(
$table
)
.
" RENAME COLUMN "
.
$this
->
quoteColumnName
(
$oldName
,
tru
e
)
.
" TO "
.
$this
->
quoteColumnName
(
$newName
,
tru
e
);
}
/**
...
...
@@ -332,9 +335,9 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
alterColumn
(
$table
,
$column
,
$type
)
{
return
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' CHANGE '
.
$this
->
driver
->
quoteSimpleColumnName
(
$column
)
.
' '
.
$this
->
driver
->
quoteSimpleColumnName
(
$column
)
.
' '
return
'ALTER TABLE '
.
$this
->
quoteTableName
(
$table
)
.
' CHANGE '
.
$this
->
quoteColumnName
(
$column
,
true
)
.
' '
.
$this
->
quoteColumnName
(
$column
,
true
)
.
' '
.
$this
->
getColumnType
(
$type
);
}
...
...
@@ -354,23 +357,11 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
addForeignKey
(
$name
,
$table
,
$columns
,
$refTable
,
$refColumns
,
$delete
=
null
,
$update
=
null
)
{
if
(
!
is_array
(
$columns
))
{
$columns
=
preg_split
(
'/\s*,\s*/'
,
$columns
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
foreach
(
$columns
as
$i
=>
$col
)
{
$columns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$col
);
}
if
(
!
is_array
(
$refColumns
))
{
$refColumns
=
preg_split
(
'/\s*,\s*/'
,
$refColumns
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
foreach
(
$refColumns
as
$i
=>
$col
)
{
$refColumns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$col
);
}
$sql
=
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' ADD CONSTRAINT '
.
$this
->
driver
->
quoteColumnName
(
$name
)
.
' FOREIGN KEY ('
.
implode
(
', '
,
$columns
)
.
')'
.
' REFERENCES '
.
$this
->
driver
->
quoteTableName
(
$refTable
)
.
' ('
.
implode
(
', '
,
$refColumns
)
.
')'
;
$sql
=
'ALTER TABLE '
.
$this
->
quoteTableName
(
$table
)
.
' ADD CONSTRAINT '
.
$this
->
quoteColumnName
(
$name
)
.
' FOREIGN KEY ('
.
$this
->
buildColumns
(
$columns
)
.
')'
.
' REFERENCES '
.
$this
->
quoteTableName
(
$refTable
)
.
' ('
.
$this
->
buildColumns
(
$refColumns
)
.
')'
;
if
(
$delete
!==
null
)
{
$sql
.=
' ON DELETE '
.
$delete
;
}
...
...
@@ -388,8 +379,8 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
dropForeignKey
(
$name
,
$table
)
{
return
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' DROP CONSTRAINT '
.
$this
->
driver
->
quoteColumnName
(
$name
);
return
'ALTER TABLE '
.
$this
->
quoteTableName
(
$table
)
.
' DROP CONSTRAINT '
.
$this
->
quoteColumnName
(
$name
);
}
/**
...
...
@@ -404,19 +395,10 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
createIndex
(
$name
,
$table
,
$columns
,
$unique
=
false
)
{
if
(
!
is_array
(
$columns
))
{
$columns
=
preg_split
(
'/\s*,\s*/'
,
$columns
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
foreach
(
$columns
as
$i
=>
$column
)
{
if
(
strpos
(
$column
,
'('
)
!==
false
)
{
$columns
[
$i
]
=
$column
;
}
else
{
$columns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$column
);
}
}
return
(
$unique
?
'CREATE UNIQUE INDEX '
:
'CREATE INDEX '
)
.
$this
->
driver
->
quoteTableName
(
$name
)
.
' ON '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' ('
.
implode
(
', '
,
$columns
)
.
')'
;
.
$this
->
quoteTableName
(
$name
)
.
' ON '
.
$this
->
quoteTableName
(
$table
)
.
' ('
.
$this
->
buildColumns
(
$columns
)
.
')'
;
}
/**
...
...
@@ -427,7 +409,7 @@ class QueryBuilder extends \yii\base\Object
*/
public
function
dropIndex
(
$name
,
$table
)
{
return
'DROP INDEX '
.
$this
->
driver
->
quoteTableName
(
$name
)
.
' ON '
.
$this
->
driver
->
quoteTableName
(
$table
);
return
'DROP INDEX '
.
$this
->
quoteTableName
(
$name
)
.
' ON '
.
$this
->
quoteTableName
(
$table
);
}
/**
...
...
@@ -525,7 +507,7 @@ class QueryBuilder extends \yii\base\Object
$column
=
$condition
[
1
];
if
(
strpos
(
$column
,
'('
)
===
false
)
{
$column
=
$this
->
connection
->
quoteColumnName
(
$column
);
$column
=
$this
->
quoteColumnName
(
$column
);
}
if
(
$operator
===
'BETWEEN'
||
$operator
===
'NOT BETWEEN'
)
{
...
...
@@ -592,26 +574,32 @@ class QueryBuilder extends \yii\base\Object
return
$select
.
' *'
;
}
if
(
is_string
(
$columns
))
{
if
(
$this
->
autoQuote
)
{
if
(
!
is_array
(
$columns
))
{
if
(
strpos
(
$columns
,
'('
)
!==
false
)
{
return
$select
.
' '
.
$columns
;
}
}
else
{
$columns
=
preg_split
(
'/\s*,\s*/'
,
trim
(
$columns
),
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
}
foreach
(
$columns
as
$i
=>
$column
)
{
if
(
is_object
(
$column
))
{
$columns
[
$i
]
=
(
string
)
$column
;
}
elseif
(
strpos
(
$column
,
'('
)
===
false
)
{
if
(
preg_match
(
'/^(.*?)(?i:\s+as\s+|\s+)([\w\-\.])$/'
,
$column
,
$matches
))
{
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$matches
[
1
])
.
' AS '
.
$this
->
driver
->
quoteSimpleColumnName
(
$matches
[
2
]);
$columns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$matches
[
1
])
.
' AS '
.
$this
->
driver
->
quoteSimpleColumnName
(
$matches
[
2
]);
}
else
{
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$column
);
$columns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$column
);
}
}
}
}
return
$select
.
' '
.
implode
(
', '
,
$columns
);
if
(
is_array
(
$columns
))
{
$columns
=
implode
(
', '
,
$columns
);
}
return
$select
.
' '
.
$columns
;
}
/**
...
...
@@ -624,24 +612,31 @@ class QueryBuilder extends \yii\base\Object
}
$tables
=
$this
->
query
->
from
;
if
(
is_string
(
$tables
)
&&
strpos
(
$tables
,
'('
)
!==
false
)
{
return
'FROM '
.
$tables
;
}
if
(
$this
->
autoQuote
)
{
if
(
!
is_array
(
$tables
))
{
if
(
strpos
(
$tables
,
'('
)
!==
false
)
{
return
'FROM '
.
$tables
;
}
else
{
$tables
=
preg_split
(
'/\s*,\s*/'
,
trim
(
$tables
),
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
}
foreach
(
$tables
as
$i
=>
$table
)
{
if
(
strpos
(
$table
,
'('
)
===
false
)
{
if
(
preg_match
(
'/^(.*?)(?i:\s+as\s+|\s+)(.*)$/i'
,
$table
,
$matches
))
{
// with alias
$tables
[
$i
]
=
$this
->
connection
->
quoteTableName
(
$matches
[
1
])
.
' '
.
$this
->
connection
->
quoteTableName
(
$matches
[
2
]);
$tables
[
$i
]
=
$this
->
driver
->
quoteTableName
(
$matches
[
1
])
.
' '
.
$this
->
driver
->
quoteTableName
(
$matches
[
2
]);
}
else
{
$tables
[
$i
]
=
$this
->
connection
->
quoteTableName
(
$table
);
$tables
[
$i
]
=
$this
->
driver
->
quoteTableName
(
$table
);
}
}
}
}
return
'FROM '
.
implode
(
', '
,
$tables
);
if
(
is_array
(
$tables
))
{
$tables
=
implode
(
', '
,
$tables
);
}
return
'FROM '
.
$tables
;
}
/**
...
...
@@ -661,11 +656,11 @@ class QueryBuilder extends \yii\base\Object
if
(
is_array
(
$join
))
{
// join type, table name, on-condition
if
(
isset
(
$join
[
0
],
$join
[
1
]))
{
$table
=
$join
[
1
];
if
(
strpos
(
$table
,
'('
)
===
false
)
{
if
(
$this
->
autoQuote
&&
strpos
(
$table
,
'('
)
===
false
)
{
if
(
preg_match
(
'/^(.*?)(?i:\s+as\s+|\s+)(.*)$/'
,
$table
,
$matches
))
{
// with alias
$table
=
$this
->
connection
->
quoteTableName
(
$matches
[
1
])
.
' '
.
$this
->
connection
->
quoteTableName
(
$matches
[
2
]);
$table
=
$this
->
driver
->
quoteTableName
(
$matches
[
1
])
.
' '
.
$this
->
driver
->
quoteTableName
(
$matches
[
2
]);
}
else
{
$table
=
$this
->
connection
->
quoteTableName
(
$table
);
$table
=
$this
->
driver
->
quoteTableName
(
$table
);
}
}
$joins
[
$i
]
=
strtoupper
(
$join
[
0
])
.
' '
.
$table
;
...
...
@@ -695,25 +690,11 @@ class QueryBuilder extends \yii\base\Object
*/
protected
function
buildGroupBy
()
{
$columns
=
$this
->
query
->
groupBy
;
if
(
empty
(
$columns
))
{
if
(
empty
(
$this
->
query
->
groupBy
))
{
return
''
;
}
else
{
return
'GROUP BY '
.
$this
->
buildColumns
(
$this
->
query
->
groupBy
);
}
if
(
is_string
(
$columns
)
&&
strpos
(
$columns
,
'('
)
!==
false
)
{
return
'GROUP BY '
.
$columns
;
}
if
(
!
is_array
(
$columns
))
{
$columns
=
preg_split
(
'/\s*,\s*/'
,
trim
(
$columns
),
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
foreach
(
$columns
as
$i
=>
$column
)
{
if
(
is_object
(
$column
))
{
$columns
[
$i
]
=
(
string
)
$column
;
}
elseif
(
strpos
(
$column
,
'('
)
===
false
)
{
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$column
);
}
}
return
'GROUP BY '
.
implode
(
', '
,
$columns
);
}
/**
...
...
@@ -730,29 +711,34 @@ class QueryBuilder extends \yii\base\Object
*/
protected
function
buildOrderBy
()
{
$columns
=
$this
->
query
->
orderBy
;
if
(
empty
(
$columns
))
{
if
(
empty
(
$this
->
query
->
orderBy
))
{
return
''
;
}
if
(
is_string
(
$columns
)
&&
strpos
(
$columns
,
'('
)
!==
false
)
{
return
'ORDER BY '
.
$columns
;
}
$columns
=
$this
->
query
->
orderBy
;
if
(
$this
->
autoQuote
)
{
if
(
!
is_array
(
$columns
))
{
if
(
strpos
(
$columns
,
'('
)
!==
false
)
{
return
'ORDER BY '
.
$columns
;
}
else
{
$columns
=
preg_split
(
'/\s*,\s*/'
,
trim
(
$columns
),
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
}
foreach
(
$columns
as
$i
=>
$column
)
{
if
(
is_object
(
$column
))
{
$columns
[
$i
]
=
(
string
)
$column
;
}
elseif
(
strpos
(
$column
,
'('
)
===
false
)
{
if
(
preg_match
(
'/^(.*?)\s+(asc|desc)$/i'
,
$column
,
$matches
))
{
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$matches
[
1
])
.
' '
.
strtoupper
(
$matches
[
2
])
;
$columns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$matches
[
1
])
.
' '
.
$matches
[
2
]
;
}
else
{
$columns
[
$i
]
=
$this
->
connection
->
quoteColumnName
(
$column
);
$columns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$column
);
}
}
}
return
'ORDER BY '
.
implode
(
', '
,
$columns
);
}
if
(
is_array
(
$columns
))
{
$columns
=
implode
(
', '
,
$columns
);
}
return
'ORDER BY '
.
$columns
;
}
/**
...
...
@@ -789,4 +775,64 @@ class QueryBuilder extends \yii\base\Object
}
return
"UNION (
\n
"
.
implode
(
"
\n
) UNION (
\n
"
,
$unions
)
.
"
\n
)"
;
}
/**
* Processes columns and properly quote them if necessary.
* This method will quote columns if [[autoQuote]] is true.
* It will join all columns into a string with comma as separators.
* @param string|array $columns the columns to be processed
* @return string the processing result
*/
protected
function
buildColumns
(
$columns
)
{
if
(
$this
->
autoQuote
)
{
if
(
!
is_array
(
$columns
))
{
if
(
strpos
(
$columns
,
'('
)
!==
false
)
{
return
$columns
;
}
else
{
$columns
=
preg_split
(
'/\s*,\s*/'
,
$columns
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
}
foreach
(
$columns
as
$i
=>
$column
)
{
if
(
is_object
(
$column
))
{
$columns
[
$i
]
=
(
string
)
$column
;
}
elseif
(
strpos
(
$column
,
'('
)
===
false
)
{
$columns
[
$i
]
=
$this
->
driver
->
quoteColumnName
(
$column
);
}
}
}
return
is_array
(
$columns
)
?
implode
(
', '
,
$columns
)
:
$columns
;
}
/**
* Quotes a table name for use in a query.
* This method will perform name quoting only when [[autoQuote]] is true.
* @param string $name table name
* @param boolean $simple whether the name should be treated as a simple table name without any prefix.
* @return string the properly quoted table name
*/
protected
function
quoteTableName
(
$name
,
$simple
=
false
)
{
if
(
$this
->
autoQuote
)
{
return
$simple
?
$this
->
driver
->
quoteSimpleTableName
(
$name
)
:
$this
->
driver
->
quoteTableName
(
$name
);
}
else
{
return
$name
;
}
}
/**
* Quotes a column name for use in a query.
* This method will perform name quoting only when [[autoQuote]] is true.
* @param string $name column name
* @param boolean $simple whether the name should be treated as a simple column name without any prefix.
* @return string the properly quoted column name
*/
protected
function
quoteColumnName
(
$name
,
$simple
=
false
)
{
if
(
$this
->
autoQuote
)
{
return
$simple
?
$this
->
driver
->
quoteSimpleColumnName
(
$name
)
:
$this
->
driver
->
quoteColumnName
(
$name
);
}
else
{
return
$name
;
}
}
}
framework/db/dao/mysql/QueryBuilder.php
View file @
702476d8
...
...
@@ -50,7 +50,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
renameColumn
(
$table
,
$oldName
,
$newName
)
{
$quotedTable
=
$this
->
driver
->
quoteTableName
(
$table
);
$quotedTable
=
$this
->
quoteTableName
(
$table
);
$row
=
$this
->
connection
->
createCommand
(
'SHOW CREATE TABLE '
.
$quotedTable
)
->
queryRow
();
if
(
$row
===
false
)
{
throw
new
Exception
(
"Unable to find '
$oldName
' in table '
$table
'."
);
...
...
@@ -64,13 +64,17 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
if
(
preg_match_all
(
'/^\s*`(.*?)`\s+(.*?),?$/m'
,
$sql
,
$matches
))
{
foreach
(
$matches
[
1
]
as
$i
=>
$c
)
{
if
(
$c
===
$oldName
)
{
return
"ALTER TABLE
$quotedTable
CHANGE "
.
$this
->
driver
->
quoteColumnName
(
$oldName
)
.
' '
.
$this
->
driver
->
quoteColumnName
(
$newName
)
.
' '
.
$matches
[
2
][
$i
];
return
"ALTER TABLE
$quotedTable
CHANGE "
.
$this
->
quoteColumnName
(
$oldName
,
true
)
.
' '
.
$this
->
quoteColumnName
(
$newName
,
true
)
.
' '
.
$matches
[
2
][
$i
];
}
}
}
// try to give back a SQL anyway
return
"ALTER TABLE
$quotedTable
CHANGE "
.
$this
->
driver
->
quoteColumnName
(
$oldName
)
.
' '
.
$newName
;
return
"ALTER TABLE
$quotedTable
CHANGE "
.
$this
->
quoteColumnName
(
$oldName
,
true
)
.
' '
.
$this
->
quoteColumnName
(
$newName
,
true
);
}
/**
...
...
@@ -81,7 +85,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
dropForeignKey
(
$name
,
$table
)
{
return
'ALTER TABLE '
.
$this
->
driver
->
quoteTableName
(
$table
)
.
' DROP FOREIGN KEY '
.
$this
->
driver
->
quoteColumnName
(
$name
);
return
'ALTER TABLE '
.
$this
->
quoteTableName
(
$table
)
.
' DROP FOREIGN KEY '
.
$this
->
quoteColumnName
(
$name
);
}
}
framework/db/dao/sqlite/QueryBuilder.php
View file @
702476d8
...
...
@@ -83,7 +83,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
truncateTable
(
$table
)
{
return
"DELETE FROM "
.
$this
->
driver
->
quoteTableName
(
$table
);
return
"DELETE FROM "
.
$this
->
quoteTableName
(
$table
);
}
/**
...
...
@@ -94,7 +94,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
dropIndex
(
$name
,
$table
)
{
return
'DROP INDEX '
.
$this
->
driver
->
quoteTableName
(
$name
);
return
'DROP INDEX '
.
$this
->
quoteTableName
(
$name
);
}
/**
...
...
@@ -105,7 +105,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
dropColumn
(
$table
,
$column
)
{
throw
new
Exception
(
'Dropping DB column
is not supported by SQLite.'
);
throw
new
Exception
(
__METHOD__
.
'
is not supported by SQLite.'
);
}
/**
...
...
@@ -117,7 +117,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
renameColumn
(
$table
,
$oldName
,
$newName
)
{
throw
new
Exception
(
'Renaming a DB column
is not supported by SQLite.'
);
throw
new
Exception
(
__METHOD__
.
'
is not supported by SQLite.'
);
}
/**
...
...
@@ -136,7 +136,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
addForeignKey
(
$name
,
$table
,
$columns
,
$refTable
,
$refColumns
,
$delete
=
null
,
$update
=
null
)
{
throw
new
Exception
(
'Adding a foreign key constraint to an existing table
is not supported by SQLite.'
);
throw
new
Exception
(
__METHOD__
.
'
is not supported by SQLite.'
);
}
/**
...
...
@@ -147,7 +147,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
dropForeignKey
(
$name
,
$table
)
{
throw
new
Exception
(
'Dropping a foreign key constraint
is not supported by SQLite.'
);
throw
new
Exception
(
__METHOD__
.
'
is not supported by SQLite.'
);
}
/**
...
...
@@ -162,6 +162,6 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public
function
alterColumn
(
$table
,
$column
,
$type
)
{
throw
new
Exception
(
'Altering a DB column
is not supported by SQLite.'
);
throw
new
Exception
(
__METHOD__
.
'
is not supported by SQLite.'
);
}
}
tests/unit/framework/db/dao/CommandTest.php
View file @
702476d8
...
...
@@ -26,14 +26,14 @@ class CommandTest extends \yiiunit\MysqlTestCase
$query
=
new
Query
;
$query
->
select
(
'id'
)
->
from
(
'tbl_user'
);
$command
=
$db
->
createCommand
(
$query
);
$this
->
assertEquals
(
"SELECT `id`
\n
FROM `tbl_user`"
,
$command
->
sql
);
$this
->
assertEquals
(
"SELECT `id`
FROM `tbl_user`"
,
$command
->
sql
);
// array
$command
=
$db
->
createCommand
(
array
(
'select'
=>
'name'
,
'from'
=>
'tbl_user'
,
));
$this
->
assertEquals
(
"SELECT `name`
\n
FROM `tbl_user`"
,
$command
->
sql
);
$this
->
assertEquals
(
"SELECT `name`
FROM `tbl_user`"
,
$command
->
sql
);
}
function
testGetSetSql
()
...
...
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