FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
MySQL-Query-Builder/DeleteQuery.php at master · indeyets/MySQL-Query-Builder · GitHub
indeyets
/
MySQL-Query-Builder
Public
Notifications
You must be signed in to change notification settings
Fork
9
Star
27
Code
Issues
1
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
MySQL-Query-Builder
/
DeleteQuery.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
136 lines (115 loc) · 4.06 KB
Breadcrumbs
MySQL-Query-Builder
/
DeleteQuery.php
Copy path
File metadata and controls
136 lines (115 loc) · 4.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 enc=utf8: */
/**
* @author Alexey Zakhlestin
* @package mysql-query-builder
**/
/*
MySQL Query Builder
Copyright © 2005-2009 Alexey Zakhlestin <indeyets@gmail.com>
Copyright © 2005-2006 Konstantin Sedov <kostya.online@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This class contains logic of "DELETE" queries
*
* @package mysql-query-builder
* @author Alexey Zakhlestin
*/
class
DeleteQuery
extends
BasicQuery
{
private
$
del_limit
=
null
;
private
$
del_tables
=
null
;
public
function
__construct
(
$
tables
,
array
$
del_tables
=
array
(
0
))
{
parent
::
__construct
(
$
tables
);
$
this
->
del_tables
=
$
del_tables
;
}
/**
* wrapper around BasicQuery::setOrderBy, which additionally checks if it is allowed, to apply order to the query.
* it is allowed only for single-table queries
*
* @param array $orderlist
* @param array $orderdirectionlist
* @return void
* @throws LogicException
*/
public
function
setOrderby
(
array
$
orderlist
,
array
$
orderdirectionlist
=
array
())
{
if
(
count
(
$
this
->
from
) !=
1
) {
throw
new
LogicException
(
"
setOrderby is allowed only in single-table delete queries
"
);
}
parent
::
setOrderby
(
$
orderlist
,
$
orderdirectionlist
);
}
/**
* Sets maximum number of rows, the DELETE query will be applied to.
* MySQL does not allow to specify offset, so, it is just a single number
*
* @param integer $limit
* @return void
* @throws LogicException, InvalidArgumentException
*/
public
function
setLimit
(
$
limit
)
{
if
(
count
(
$
this
->
from
) !=
1
) {
throw
new
LogicException
(
"
setLimit is allowed only in single-table delete queries
"
);
}
if
(!
is_numeric
(
$
limit
)
or
$
limit
<
1
)
throw
new
InvalidArgumentException
(
'
positive number should be used as a limit
'
);
$
this
->
del_limit
= (
string
)
$
limit
;
}
protected
function
getSql
(
array
&
$
parameters
)
{
$
sql
=
$
this
->
getDelete
(
$
parameters
);
$
sql
.=
$
this
->
getUsing
(
$
parameters
);
$
sql
.=
$
this
->
getWhere
(
$
parameters
);
$
sql
.=
$
this
->
getOrderby
(
$
parameters
);
$
sql
.=
$
this
->
getLimit
(
$
parameters
);
if
(
count
(
$
this
->
from
) ==
1
) {
$
sql
=
str_replace
(
'
`t0`
'
,
$
this
->
from
[
0
]->
__toString
(),
$
sql
);
}
return
$
sql
;
}
private
function
getDelete
(&
$
parameters
)
{
if
(
count
(
$
this
->
from
) ==
1
)
return
'
DELETE FROM
'
.
$
this
->
from
[
0
]->
__toString
();
else
{
$
sql
=
'
DELETE FROM
'
;
$
first
=
true
;
foreach
(
$
this
->
del_tables
as
$
tbl
) {
if
(
$
first
) {
$
first
=
false
;
}
else
{
$
sql
.=
'
,
'
;
}
$
sql
.=
'
`t
'
.
$
tbl
.
'
`
'
;
}
return
$
sql
;
}
}
protected
function
getUsing
(&
$
parameters
)
{
if
(
count
(
$
this
->
from
) ==
1
)
return
''
;
$
froms
=
array
();
for
(
$
i
=
0
;
$
i
<
count
(
$
this
->
from
);
$
i
++) {
$
froms
[] =
$
this
->
from
[
$
i
]->
__toString
().
'
AS `t
'
.
$
i
.
'
`
'
;
}
return
"
USING
"
.
implode
(
"
,
"
,
$
froms
);
}
protected
function
getLimit
()
{
return
(
null
==
$
this
->
del_limit
) ?
''
:
'
LIMIT
'
.
$
this
->
del_limit
;
}
}
Back
|
FazBrowse Home
|
New Git URL