Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
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
Yeray Santana Hualde
syncEnrollments
Commits
8f4bcd46
Commit
8f4bcd46
authored
Aug 20, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
continued refactoring database layer.
parent
fb1acc31
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
161 additions
and
63 deletions
+161
-63
compiler.php
laravel/db/compiler.php
+135
-0
query.php
laravel/db/query.php
+22
-60
factory.php
laravel/db/query/factory.php
+4
-3
No files found.
laravel/db/compiler.php
0 → 100644
View file @
8f4bcd46
<?php
namespace
Laravel\DB
;
class
Compiler
{
/**
* Compile a SQL SELECT statment from a Query instance.
*
* @param Query $query
* @return string
*/
public
function
select
(
Query
$query
)
{
foreach
(
array
(
'add_select'
,
'add_from'
,
'add_where'
,
'add_order'
,
'add_limit'
,
'add_offset'
)
as
$builder
)
{
$sql
[]
=
$this
->
$builder
(
$query
);
}
foreach
(
$sql
as
$key
=>
$value
)
{
if
(
is_null
(
$value
)
or
$value
===
''
)
unset
(
$sql
[
$key
]);
}
return
implode
(
' '
,
$sql
);
}
/**
* Get the SELECT clause from the Query instance.
*
* @param Query $query
* @return string
*/
protected
function
add_select
(
Query
$query
)
{
return
$query
->
select
;
}
/**
* Get the FROM clause from the Query instance.
*
* @param Query $query
* @return string
*/
protected
function
add_from
(
Query
$query
)
{
return
$query
->
from
;
}
/**
* Get the WHERE clause from the Query instance.
*
* @param Query $query
* @return string
*/
protected
function
add_where
(
Query
$query
)
{
return
$query
->
where
;
}
/**
* Get the ORDER BY clause from the Query instance.
*
* @param Query $query
* @return string
*/
protected
function
add_order
(
Query
$query
)
{
if
(
count
(
$query
->
orderings
)
>
0
)
return
'ORDER BY'
.
implode
(
', '
,
$query
->
orderings
);
}
/**
* Get the LIMIT clause from the Query instance.
*
* @param Query $query
* @return string
*/
protected
function
add_limit
(
Query
$query
)
{
if
(
!
is_null
(
$query
->
limit
))
return
'LIMIT '
.
$query
->
limit
;
}
/**
* Get the OFFSET clause from the Query instance.
*
* @param Query $query
* @return string
*/
protected
function
add_offset
(
Query
$query
)
{
if
(
!
is_null
(
$query
->
offset
))
return
'OFFSET '
.
$query
->
offset
;
}
/**
* Compile a SQL INSERT statment from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public
function
insert
(
Query
$query
,
$values
)
{
$sql
=
'INSERT INTO '
.
$query
->
wrap
(
$query
->
table
);
$columns
=
array_map
(
array
(
$query
,
'wrap'
),
array_keys
(
$values
));
return
$sql
.=
' ('
.
implode
(
', '
,
$columns
)
.
') VALUES ('
.
$query
->
parameterize
(
$values
)
.
')'
;
}
/**
* Compile a SQL UPDATE statment from a Query instance.
*
* @param Query $query
* @param array $values
* @return string
*/
public
function
update
(
Query
$query
,
$values
)
{
$sql
=
'UPDATE '
.
$query
->
wrap
(
$query
->
table
)
.
' SET '
;
foreach
(
array_keys
(
$values
)
as
$column
)
{
$sets
[]
=
$query
->
wrap
(
$column
)
.
' = ?'
;
}
return
$sql
.
implode
(
', '
,
$sets
)
.
' '
.
$query
->
where
;
}
/**
* Compile a SQL DELETE statment from a Query instance.
*
* @param Query $query
* @return string
*/
public
function
delete
(
Query
$query
)
{
return
'DELETE FROM '
.
$query
->
wrap
(
$query
->
table
)
.
' '
.
$query
->
where
;
}
}
\ No newline at end of file
laravel/db/query.php
View file @
8f4bcd46
...
...
@@ -13,6 +13,13 @@ class Query {
*/
public
$connection
;
/**
* The query compiler instance.
*
* @var Compiler
*/
public
$compiler
;
/**
* The SELECT clause.
*
...
...
@@ -83,9 +90,10 @@ class Query {
* @param Connection $connection
* @return void
*/
public
function
__construct
(
$table
,
Connection
$connection
)
public
function
__construct
(
$table
,
Connection
$connection
,
Compiler
$compiler
)
{
$this
->
table
=
$table
;
$this
->
compiler
=
$compiler
;
$this
->
connection
=
$connection
;
$this
->
from
=
'FROM '
.
$this
->
wrap
(
$table
);
}
...
...
@@ -471,7 +479,7 @@ class Query {
{
$this
->
select
=
'SELECT '
.
$aggregator
.
'('
.
$this
->
wrap
(
$column
)
.
') AS '
.
$this
->
wrap
(
'aggregate'
);
$result
=
$this
->
connection
->
scalar
(
$this
->
compile
_select
(
),
$this
->
bindings
);
$result
=
$this
->
connection
->
scalar
(
$this
->
compile
r
->
select
(
$this
),
$this
->
bindings
);
// Reset the SELECT clause so more queries can be performed using the same instance.
// This is helpful for getting aggregates and then getting actual results.
...
...
@@ -518,7 +526,7 @@ class Query {
$this
->
select
(
$columns
);
}
$results
=
$this
->
connection
->
query
(
$this
->
compile
_select
(
),
$this
->
bindings
);
$results
=
$this
->
connection
->
query
(
$this
->
compile
r
->
select
(
$this
),
$this
->
bindings
);
// Reset the SELECT clause so more queries can be performed using the same instance.
// This is helpful for getting aggregates and then getting actual results.
...
...
@@ -527,33 +535,6 @@ class Query {
return
$results
;
}
/**
* Compile the query into a SQL SELECT statement.
*
* @return string
*/
private
function
compile_select
()
{
$sql
=
$this
->
select
.
' '
.
$this
->
from
.
' '
.
$this
->
where
;
if
(
count
(
$this
->
orderings
)
>
0
)
{
$sql
.=
' ORDER BY '
.
implode
(
', '
,
$this
->
orderings
);
}
if
(
!
is_null
(
$this
->
limit
))
{
$sql
.=
' LIMIT '
.
$this
->
limit
;
}
if
(
!
is_null
(
$this
->
offset
))
{
$sql
.=
' OFFSET '
.
$this
->
offset
;
}
return
$sql
;
}
/**
* Execute an INSERT statement.
*
...
...
@@ -562,7 +543,7 @@ class Query {
*/
public
function
insert
(
$values
)
{
return
$this
->
connection
->
query
(
$this
->
compile
_insert
(
$values
),
array_values
(
$values
));
return
$this
->
connection
->
query
(
$this
->
compile
r
->
insert
(
$this
,
$values
),
array_values
(
$values
));
}
/**
...
...
@@ -573,26 +554,11 @@ class Query {
*/
public
function
insert_get_id
(
$values
)
{
$this
->
connection
->
query
(
$this
->
compile
_insert
(
$values
),
array_values
(
$values
));
$this
->
connection
->
query
(
$this
->
compile
r
->
insert
(
$this
,
$values
),
array_values
(
$values
));
return
$this
->
connection
->
pdo
->
lastInsertId
();
}
/**
* Compile the query into a SQL INSERT statement.
*
* @param array $values
* @return string
*/
private
function
compile_insert
(
$values
)
{
$sql
=
'INSERT INTO '
.
$this
->
wrap
(
$this
->
table
);
$columns
=
array_map
(
array
(
$this
,
'wrap'
),
array_keys
(
$values
));
return
$sql
.=
' ('
.
implode
(
', '
,
$columns
)
.
') VALUES ('
.
$this
->
parameterize
(
$values
)
.
')'
;
}
/**
* Execute the query as an UPDATE statement.
*
...
...
@@ -601,14 +567,7 @@ class Query {
*/
public
function
update
(
$values
)
{
$sql
=
'UPDATE '
.
$this
->
wrap
(
$this
->
table
)
.
' SET '
;
foreach
(
array_keys
(
$values
)
as
$column
)
{
$sets
[]
=
$this
->
wrap
(
$column
)
.
' = ?'
;
}
return
$this
->
connection
->
query
(
$sql
.
implode
(
', '
,
$sets
)
.
' '
.
$this
->
where
,
array_merge
(
array_values
(
$values
),
$this
->
bindings
));
return
$this
->
connection
->
query
(
$this
->
compiler
->
update
(
$this
,
$values
),
array_merge
(
array_values
(
$values
),
$this
->
bindings
));
}
/**
...
...
@@ -621,7 +580,7 @@ class Query {
{
if
(
!
is_null
(
$id
))
$this
->
where
(
'id'
,
'='
,
$id
);
return
$this
->
connection
->
query
(
'DELETE FROM '
.
$this
->
wrap
(
$this
->
table
)
.
' '
.
$this
->
where
,
$this
->
bindings
);
return
$this
->
connection
->
query
(
$this
->
compiler
->
delete
(
$this
)
,
$this
->
bindings
);
}
/**
...
...
@@ -630,7 +589,7 @@ class Query {
* @param string $value
* @return string
*/
p
rivate
function
wrap
(
$value
)
p
ublic
function
wrap
(
$value
)
{
if
(
strpos
(
strtolower
(
$value
),
' as '
)
!==
false
)
return
$this
->
wrap_alias
(
$value
);
...
...
@@ -648,7 +607,7 @@ class Query {
* @param string $value
* @return string
*/
pr
ivate
function
wrap_alias
(
$value
)
pr
otected
function
wrap_alias
(
$value
)
{
$segments
=
explode
(
' '
,
$value
);
...
...
@@ -662,7 +621,10 @@ class Query {
*
* @return string
*/
protected
function
wrapper
()
{
return
'"'
;
}
protected
function
wrapper
()
{
return
'"'
;
}
/**
* Create query parameters from an array of values.
...
...
@@ -670,7 +632,7 @@ class Query {
* @param array $values
* @return string
*/
p
rivate
function
parameterize
(
$values
)
p
ublic
function
parameterize
(
$values
)
{
return
implode
(
', '
,
array_fill
(
0
,
count
(
$values
),
'?'
));
}
...
...
laravel/db/query/factory.php
View file @
8f4bcd46
<?php
namespace
Laravel\DB\Query
;
use
Laravel\DB\Query
;
use
Laravel\DB\Compiler
;
use
Laravel\DB\Connection
;
class
Factory
{
...
...
@@ -19,13 +20,13 @@ class Factory {
switch
(
$query
)
{
case
'pgsql'
:
return
new
Postgres
(
$table
,
$connection
);
return
new
Postgres
(
$table
,
$connection
,
new
Compiler
);
case
'mysql'
:
return
new
MySQL
(
$table
,
$connection
);
return
new
MySQL
(
$table
,
$connection
,
new
Compiler
);
default
:
return
new
Query
(
$table
,
$connection
);
return
new
Query
(
$table
,
$connection
,
new
Compiler
);
}
}
...
...
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