Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Tomáš Trávníček
doctrine-dbal
Commits
55c60cd4
Commit
55c60cd4
authored
Nov 19, 2013
by
Steve Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add insert query
parent
25a1cced
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
180 additions
and
9 deletions
+180
-9
QueryBuilder.php
lib/Doctrine/DBAL/Query/QueryBuilder.php
+108
-9
QueryBuilderTest.php
tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
+72
-0
No files found.
lib/Doctrine/DBAL/Query/QueryBuilder.php
View file @
55c60cd4
...
...
@@ -45,6 +45,7 @@ class QueryBuilder
const
SELECT
=
0
;
const
DELETE
=
1
;
const
UPDATE
=
2
;
const
INSERT
=
3
;
/*
* The builder states.
...
...
@@ -70,7 +71,8 @@ class QueryBuilder
'where'
=>
null
,
'groupBy'
=>
array
(),
'having'
=>
null
,
'orderBy'
=>
array
()
'orderBy'
=>
array
(),
'values'
=>
array
(),
);
/**
...
...
@@ -226,6 +228,9 @@ class QueryBuilder
}
switch
(
$this
->
type
)
{
case
self
::
INSERT
:
$sql
=
$this
->
getSQLForInsert
();
break
;
case
self
::
DELETE
:
$sql
=
$this
->
getSQLForDelete
();
break
;
...
...
@@ -536,6 +541,38 @@ class QueryBuilder
));
}
/**
* Turns the query being built into an insert query that inserts into
* a certain table
*
* <code>
* $qb = $conn->createQueryBuilder()
* ->insert('users')
* ->values(
* array(
* 'name' => 'username',
* 'password' => md5('password')
* )
* );
* </code>
*
* @param string $insert The table into which the rows should be inserted.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public
function
insert
(
$insert
=
null
)
{
$this
->
type
=
self
::
INSERT
;
if
(
!
$insert
)
{
return
$this
;
}
return
$this
->
add
(
'from'
,
array
(
'table'
=>
$insert
));
}
/**
* Creates and adds a query root corresponding to the table identified by the
* given alias, forming a cartesian product with any existing query roots.
...
...
@@ -842,6 +879,56 @@ class QueryBuilder
return
$this
->
add
(
'groupBy'
,
$groupBy
,
true
);
}
/**
* Sets a value for a column in an insert query.
*
* <code>
* $qb = $conn->createQueryBuilder()
* ->insert('users')
* ->values(
* array(
* 'name' => 'username'
* )
* )
* ->setValue('password', md5('password'));
* </code>
*
* @param string $column The column into which the value should be inserted.
* @param string $value The value that should be inserted into the column.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public
function
setValue
(
$column
,
$value
)
{
$this
->
sqlParts
[
'values'
][
$column
]
=
$value
;
return
$this
;
}
/**
* Specifies values for an insert query indexed by column names.
* Replaces any previous values, if any.
*
* <code>
* $qb = $conn->createQueryBuilder()
* ->insert('users')
* ->values(
* array(
* 'name' => 'username',
* 'password' => md5('password')
* )
* );
* </code>
*
* @param array $values The values to specify for the insert query indexed by column names.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public
function
values
(
array
$values
)
{
return
$this
->
add
(
'values'
,
$values
);
}
/**
* Specifies a restriction over the groups of the query.
* Replaces any previous having restrictions, if any.
...
...
@@ -1019,16 +1106,28 @@ class QueryBuilder
}
$query
.=
implode
(
', '
,
$fromClauses
)
.
(
$this
->
sqlParts
[
'where'
]
!==
null
?
' WHERE '
.
((
string
)
$this
->
sqlParts
[
'where'
])
:
''
)
.
(
$this
->
sqlParts
[
'groupBy'
]
?
' GROUP BY '
.
implode
(
', '
,
$this
->
sqlParts
[
'groupBy'
])
:
''
)
.
(
$this
->
sqlParts
[
'having'
]
!==
null
?
' HAVING '
.
((
string
)
$this
->
sqlParts
[
'having'
])
:
''
)
.
(
$this
->
sqlParts
[
'orderBy'
]
?
' ORDER BY '
.
implode
(
', '
,
$this
->
sqlParts
[
'orderBy'
])
:
''
);
.
(
$this
->
sqlParts
[
'where'
]
!==
null
?
' WHERE '
.
((
string
)
$this
->
sqlParts
[
'where'
])
:
''
)
.
(
$this
->
sqlParts
[
'groupBy'
]
?
' GROUP BY '
.
implode
(
', '
,
$this
->
sqlParts
[
'groupBy'
])
:
''
)
.
(
$this
->
sqlParts
[
'having'
]
!==
null
?
' HAVING '
.
((
string
)
$this
->
sqlParts
[
'having'
])
:
''
)
.
(
$this
->
sqlParts
[
'orderBy'
]
?
' ORDER BY '
.
implode
(
', '
,
$this
->
sqlParts
[
'orderBy'
])
:
''
);
return
(
$this
->
maxResults
===
null
&&
$this
->
firstResult
==
null
)
?
$query
:
$this
->
connection
->
getDatabasePlatform
()
->
modifyLimitQuery
(
$query
,
$this
->
maxResults
,
$this
->
firstResult
);
}
/**
* Converts this instance into an INSERT string in SQL.
*
* @return string
*/
private
function
getSQLForInsert
()
{
return
'INSERT INTO '
.
$this
->
sqlParts
[
'from'
][
'table'
]
.
' ('
.
implode
(
', '
,
array_keys
(
$this
->
sqlParts
[
'values'
]))
.
')'
.
' VALUES('
.
implode
(
', '
,
$this
->
sqlParts
[
'values'
])
.
')'
;
}
/**
* Converts this instance into an UPDATE string in SQL.
*
...
...
@@ -1038,8 +1137,8 @@ class QueryBuilder
{
$table
=
$this
->
sqlParts
[
'from'
][
'table'
]
.
(
$this
->
sqlParts
[
'from'
][
'alias'
]
?
' '
.
$this
->
sqlParts
[
'from'
][
'alias'
]
:
''
);
$query
=
'UPDATE '
.
$table
.
' SET '
.
implode
(
", "
,
$this
->
sqlParts
[
'set'
])
.
(
$this
->
sqlParts
[
'where'
]
!==
null
?
' WHERE '
.
((
string
)
$this
->
sqlParts
[
'where'
])
:
''
);
.
' SET '
.
implode
(
", "
,
$this
->
sqlParts
[
'set'
])
.
(
$this
->
sqlParts
[
'where'
]
!==
null
?
' WHERE '
.
((
string
)
$this
->
sqlParts
[
'where'
])
:
''
);
return
$query
;
}
...
...
@@ -1150,8 +1249,8 @@ class QueryBuilder
if
(
isset
(
$this
->
sqlParts
[
'join'
][
$fromAlias
]))
{
foreach
(
$this
->
sqlParts
[
'join'
][
$fromAlias
]
as
$join
)
{
$sql
.=
' '
.
strtoupper
(
$join
[
'joinType'
])
.
' JOIN '
.
$join
[
'joinTable'
]
.
' '
.
$join
[
'joinAlias'
]
.
' ON '
.
((
string
)
$join
[
'joinCondition'
]);
.
' JOIN '
.
$join
[
'joinTable'
]
.
' '
.
$join
[
'joinAlias'
]
.
' ON '
.
((
string
)
$join
[
'joinCondition'
]);
$knownAliases
[
$join
[
'joinAlias'
]]
=
true
;
$sql
.=
$this
->
getSQLForJoins
(
$join
[
'joinAlias'
],
$knownAliases
);
...
...
tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
View file @
55c60cd4
...
...
@@ -451,6 +451,78 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this
->
assertSame
(
$qb2
,
$qb
);
}
public
function
testInsertValues
()
{
$qb
=
new
QueryBuilder
(
$this
->
conn
);
$qb
->
insert
(
'users'
)
->
values
(
array
(
'foo'
=>
'?'
,
'bar'
=>
'?'
)
);
$this
->
assertEquals
(
QueryBuilder
::
INSERT
,
$qb
->
getType
());
$this
->
assertEquals
(
'INSERT INTO users (foo, bar) VALUES(?, ?)'
,
(
string
)
$qb
);
}
public
function
testInsertReplaceValues
()
{
$qb
=
new
QueryBuilder
(
$this
->
conn
);
$qb
->
insert
(
'users'
)
->
values
(
array
(
'foo'
=>
'?'
,
'bar'
=>
'?'
)
)
->
values
(
array
(
'bar'
=>
'?'
,
'foo'
=>
'?'
)
);
$this
->
assertEquals
(
QueryBuilder
::
INSERT
,
$qb
->
getType
());
$this
->
assertEquals
(
'INSERT INTO users (bar, foo) VALUES(?, ?)'
,
(
string
)
$qb
);
}
public
function
testInsertSetValue
()
{
$qb
=
new
QueryBuilder
(
$this
->
conn
);
$qb
->
insert
(
'users'
)
->
setValue
(
'foo'
,
'bar'
)
->
setValue
(
'bar'
,
'?'
)
->
setValue
(
'foo'
,
'?'
);
$this
->
assertEquals
(
QueryBuilder
::
INSERT
,
$qb
->
getType
());
$this
->
assertEquals
(
'INSERT INTO users (foo, bar) VALUES(?, ?)'
,
(
string
)
$qb
);
}
public
function
testInsertValuesSetValue
()
{
$qb
=
new
QueryBuilder
(
$this
->
conn
);
$qb
->
insert
(
'users'
)
->
values
(
array
(
'foo'
=>
'?'
)
)
->
setValue
(
'bar'
,
'?'
);
$this
->
assertEquals
(
QueryBuilder
::
INSERT
,
$qb
->
getType
());
$this
->
assertEquals
(
'INSERT INTO users (foo, bar) VALUES(?, ?)'
,
(
string
)
$qb
);
}
public
function
testEmptyInsert
()
{
$qb
=
new
QueryBuilder
(
$this
->
conn
);
$qb2
=
$qb
->
insert
();
$this
->
assertEquals
(
QueryBuilder
::
INSERT
,
$qb
->
getType
());
$this
->
assertSame
(
$qb2
,
$qb
);
}
public
function
testGetConnection
()
{
$qb
=
new
QueryBuilder
(
$this
->
conn
);
...
...
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