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
59a7e47f
Commit
59a7e47f
authored
Sep 13, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplified database connection configuration.
parent
045858d8
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
98 additions
and
331 deletions
+98
-331
database.php
application/config/database.php
+45
-37
container.php
laravel/config/container.php
+1
-3
connection.php
laravel/database/connection.php
+13
-64
callback.php
laravel/database/connector/callback.php
+0
-16
connector.php
laravel/database/connector/connector.php
+0
-28
mysql.php
laravel/database/connector/mysql.php
+0
-37
postgres.php
laravel/database/connector/postgres.php
+0
-32
sqlite.php
laravel/database/connector/sqlite.php
+0
-31
manager.php
laravel/database/manager.php
+15
-61
grammar.php
laravel/database/queries/grammars/grammar.php
+3
-3
mysql.php
laravel/database/queries/grammars/mysql.php
+2
-2
postgres.php
laravel/database/queries/grammars/postgres.php
+4
-2
postgres.php
laravel/database/queries/postgres.php
+2
-2
query.php
laravel/database/queries/query.php
+13
-13
No files found.
application/config/database.php
View file @
59a7e47f
...
...
@@ -10,7 +10,9 @@ return array(
| The name of your default database connection.
|
| This connection will be the default for all database operations unless a
| different connection is specified when performing the operation.
| different connection is specified when performing the operation. The name
| of the default connection should correspond to the name of a connector
| defined below.
|
*/
...
...
@@ -18,56 +20,62 @@ return array(
/*
|--------------------------------------------------------------------------
| Database Connect
ion
s
| Database Connect
or
s
|--------------------------------------------------------------------------
|
| All of the database connect
ion
s used by your application.
| All of the database connect
or
s used by your application.
|
| Supported Drivers: 'mysql', 'pgsql', 'sqlite'.
| Each connector should return a PDO connection. You may connect to any
| database system you wish. Of course, default configurations for the
| systems supported by Laravel are provided for you.
|
|
Note: When using the SQLite driver, the path and "sqlite" extention will
|
be added automatically. You only need to specify the database nam
e.
|
The entire database configuration array is passed to the connector
|
closure, so you may convenient use it when connecting to your databas
e.
|
| Using a driver that isn't supported? You can still establish a PDO
| connection. Simply specify a driver and DSN option:
|
| 'odbc' => array(
| 'driver' => 'odbc',
| 'dsn' => 'your-dsn',
| 'username' => 'username',
| 'password' => 'password',
| )
|
| Note: When using an unsupported driver, Eloquent and the fluent query
| builder may not work as expected.
| Note: When using an unsupported database, Eloquent and the fluent query
| builder may not work as expected. Currently, MySQL, Postgres, and
| SQLite are fully supported by Laravel.
|
*/
'connect
ion
s'
=>
array
(
'connect
or
s'
=>
array
(
'sqlite'
=>
array
(
'driver'
=>
'sqlite'
,
'database'
=>
'application'
,
),
'sqlite'
=>
function
(
$config
)
{
return
new
PDO
(
'sqlite:'
.
DATABASE_PATH
.
'application.sqlite'
,
null
,
null
,
$config
[
'options'
]);
}
'mysql'
=>
array
(
'driver'
=>
'mysql'
,
'host'
=>
'localhost'
,
'database'
=>
'database'
,
'username'
=>
'root'
,
'password'
=>
'password'
,
'charset'
=>
'utf8'
,
),
'mysql'
=>
function
(
$config
)
{
return
new
PDO
(
'mysql:host=localhost;dbname=database'
,
'username'
,
'password'
,
$config
[
'options'
]);
}
'pgsql'
=>
array
(
'driver'
=>
'pgsql'
,
'host'
=>
'localhost'
,
'database'
=>
'database'
,
'username'
=>
'root'
,
'password'
=>
'password'
,
'charset'
=>
'utf8'
,
return
new
PDO
(
'pgsql:host=localhost;dbname=database'
,
'username'
,
'password'
,
$config
[
'options'
]);
),
),
/*
|--------------------------------------------------------------------------
| Database PDO Options
|--------------------------------------------------------------------------
|
| Here you may specify the PDO options that should be used when connecting
| to a database. The entire database configuration array is passed to the
| database connector closures, so may convenient access these options from
| your connectors.
|
| For a list of options, visit: http://php.net/manual/en/pdo.setattribute.php
|
*/
'options'
=>
array
(
PDO
::
ATTR_CASE
=>
PDO
::
CASE_LOWER
,
PDO
::
ATTR_ERRMODE
=>
PDO
::
ERRMODE_EXCEPTION
,
PDO
::
ATTR_ORACLE_NULLS
=>
PDO
::
NULL_NATURAL
,
PDO
::
ATTR_STRINGIFY_FETCHES
=>
false
,
PDO
::
ATTR_EMULATE_PREPARES
=>
false
,
),
);
\ No newline at end of file
laravel/config/container.php
View file @
59a7e47f
...
...
@@ -49,9 +49,7 @@ return array(
'laravel.database'
=>
array
(
'singleton'
=>
true
,
'resolver'
=>
function
(
$container
)
{
$config
=
$container
->
resolve
(
'laravel.config'
);
return
new
Database\Manager
(
new
Database\Connector\Factory
,
$config
->
get
(
'database.connections'
),
$config
->
get
(
'database.default'
));
return
new
Database\Manager
(
$container
->
resolve
(
'laravel.config'
)
->
get
(
'database'
));
}),
...
...
laravel/database/connection.php
View file @
59a7e47f
...
...
@@ -5,27 +5,6 @@ use PDOStatement;
class
Connection
{
/**
* The database connector instance.
*
* @var Connector\Connector
*/
protected
$connector
;
/**
* The connection name.
*
* @var string
*/
protected
$name
;
/**
* The connection configuration.
*
* @var array
*/
protected
$config
;
/**
* The PDO connection.
*
...
...
@@ -41,38 +20,14 @@ class Connection {
public
$queries
=
array
();
/**
* Create a new Connection instance.
*
* @param Connector\Connector $connector
* @param string $name
* @param array $config
* @return void
*/
public
function
__construct
(
Connector\Connector
$connector
,
$name
,
$config
)
{
$this
->
name
=
$name
;
$this
->
config
=
$config
;
$this
->
connector
=
$connector
;
}
/**
* Establish the PDO connection for the connection instance.
* Create a new database connection instance.
*
* @param PDO $pdo
* @return void
*/
public
function
connect
(
)
public
function
__construct
(
PDO
$pdo
)
{
$this
->
pdo
=
$this
->
connector
->
connect
(
$this
->
config
);
}
/**
* Determine if a PDO connection has been established for the connection.
*
* @return bool
*/
public
function
connected
()
{
return
!
is_null
(
$this
->
pdo
);
$this
->
pdo
=
$pdo
;
}
/**
...
...
@@ -141,8 +96,6 @@ class Connection {
*/
public
function
query
(
$sql
,
$bindings
=
array
())
{
if
(
!
$this
->
connected
())
$this
->
connect
();
$this
->
queries
[]
=
compact
(
'sql'
,
'bindings'
);
return
$this
->
execute
(
$this
->
pdo
->
prepare
(
trim
(
$sql
)),
$bindings
);
...
...
@@ -190,32 +143,30 @@ class Connection {
switch
(
$this
->
driver
())
{
case
'pgsql'
:
return
new
Quer
y\Postgres
(
$this
,
$this
->
compile
r
(),
$table
);
return
new
Quer
ies\Postgres
(
$this
,
$this
->
gramma
r
(),
$table
);
default
:
return
new
Quer
y\Query
(
$this
,
$this
->
compile
r
(),
$table
);
return
new
Quer
ies\Query
(
$this
,
$this
->
gramma
r
(),
$table
);
}
}
/**
* Create a new query
compile
r for the connection.
* Create a new query
gramma
r for the connection.
*
* @return Quer
y\Compile
r
* @return Quer
ies\Grammars\Gramma
r
*/
protected
function
compile
r
()
protected
function
gramma
r
()
{
$compiler
=
(
isset
(
$this
->
config
[
'compiler'
]))
?
$this
->
config
[
'compiler'
]
:
$this
->
driver
();
switch
(
$compiler
)
switch
(
$this
->
driver
())
{
case
'mysql'
:
return
new
Quer
y\Compiler
\MySQL
;
return
new
Quer
ies\Grammars
\MySQL
;
case
'pgsql'
:
return
new
Quer
y\Compiler
\Postgres
;
return
new
Quer
ies\Grammars
\Postgres
;
default
:
return
new
Quer
y\Compiler\Compile
r
;
return
new
Quer
ies\Grammars\Gramma
r
;
}
}
...
...
@@ -226,8 +177,6 @@ class Connection {
*/
public
function
driver
()
{
if
(
!
$this
->
connected
())
$this
->
connect
();
return
$this
->
pdo
->
getAttribute
(
PDO
::
ATTR_DRIVER_NAME
);
}
...
...
laravel/database/connector/callback.php
deleted
100644 → 0
View file @
045858d8
<?php
namespace
Laravel\Database\Connector
;
class
Callback
extends
Connector
{
/**
* Establish a PDO database connection.
*
* @param array $config
* @return PDO
*/
public
function
connect
(
$config
)
{
return
call_user_func
(
$config
[
'connector'
]);
}
}
\ No newline at end of file
laravel/database/connector/connector.php
deleted
100644 → 0
View file @
045858d8
<?php
namespace
Laravel\Database\Connector
;
use
PDO
;
abstract
class
Connector
{
/**
* The PDO connection options.
*
* @var array
*/
public
$options
=
array
(
PDO
::
ATTR_CASE
=>
PDO
::
CASE_LOWER
,
PDO
::
ATTR_ERRMODE
=>
PDO
::
ERRMODE_EXCEPTION
,
PDO
::
ATTR_ORACLE_NULLS
=>
PDO
::
NULL_NATURAL
,
PDO
::
ATTR_STRINGIFY_FETCHES
=>
false
,
PDO
::
ATTR_EMULATE_PREPARES
=>
false
,
);
/**
* Establish a PDO database connection.
*
* @param array $config
* @return PDO
*/
abstract
public
function
connect
(
$config
);
}
\ No newline at end of file
laravel/database/connector/mysql.php
deleted
100644 → 0
View file @
045858d8
<?php
namespace
Laravel\Database\Connector
;
use
PDO
;
class
MySQL
extends
Connector
{
/**
* Establish a PDO database connection.
*
* @param array $config
* @return PDO
*/
public
function
connect
(
$config
)
{
$dsn
=
$config
[
'driver'
]
.
':host='
.
$config
[
'host'
]
.
';dbname='
.
$config
[
'database'
];
if
(
isset
(
$config
[
'port'
]))
{
$dsn
.=
';port='
.
$config
[
'port'
];
}
if
(
isset
(
$config
[
'socket'
]))
{
$dsn
.=
';unix_socket='
.
$config
[
'socket'
];
}
$connection
=
new
PDO
(
$dsn
,
$config
[
'username'
],
$config
[
'password'
],
$this
->
options
);
if
(
isset
(
$config
[
'charset'
]))
{
$connection
->
prepare
(
"SET NAMES '"
.
$config
[
'charset'
]
.
"'"
)
->
execute
();
}
return
$connection
;
}
}
\ No newline at end of file
laravel/database/connector/postgres.php
deleted
100644 → 0
View file @
045858d8
<?php
namespace
Laravel\Database\Connector
;
use
PDO
;
class
Postgres
extends
Connector
{
/**
* Establish a PDO database connection.
*
* @param array $config
* @return PDO
*/
public
function
connect
(
$config
)
{
$dsn
=
$config
[
'driver'
]
.
':host='
.
$config
[
'host'
]
.
';dbname='
.
$config
[
'database'
];
if
(
isset
(
$config
[
'port'
]))
{
$dsn
.=
';port='
.
$config
[
'port'
];
}
$connection
=
new
PDO
(
$dsn
,
$config
[
'username'
],
$config
[
'password'
],
$this
->
options
);
if
(
isset
(
$config
[
'charset'
]))
{
$connection
->
prepare
(
"SET NAMES '"
.
$config
[
'charset'
]
.
"'"
)
->
execute
();
}
return
$connection
;
}
}
\ No newline at end of file
laravel/database/connector/sqlite.php
deleted
100644 → 0
View file @
045858d8
<?php
namespace
Laravel\Database\Connector
;
use
PDO
;
class
SQLite
extends
Connector
{
/**
* Establish a PDO database connection.
*
* @param array $config
* @return PDO
*/
public
function
connect
(
$config
)
{
if
(
$config
[
'database'
]
==
':memory:'
)
{
return
new
PDO
(
'sqlite::memory:'
,
null
,
null
,
$this
->
options
);
}
elseif
(
file_exists
(
$path
=
DATABASE_PATH
.
$config
[
'database'
]
.
'.sqlite'
))
{
return
new
PDO
(
'sqlite:'
.
$path
,
null
,
null
,
$this
->
options
);
}
elseif
(
file_exists
(
$config
[
'database'
]))
{
return
new
PDO
(
'sqlite:'
.
$config
[
'database'
],
null
,
null
,
$this
->
options
);
}
throw
new
\Exception
(
"SQLite database ["
.
$config
[
'database'
]
.
"] could not be found."
);
}
}
\ No newline at end of file
laravel/database/manager.php
View file @
59a7e47f
...
...
@@ -9,45 +9,22 @@ class Manager {
*/
protected
$connections
=
array
();
/**
* The connector factory instance.
*
* @var Connector\Factory
*/
protected
$connector
;
/**
* The database connection configurations.
*
* @var array
*/
protected
$config
;
/**
* The default database connection name.
*
* @var string
*/
protected
$default
;
/**
* Create a new database manager instance.
*
* @param Connector\Factory $connector
* @param array $config
* @param string $default
* @param array $config
* @return void
*/
public
function
__construct
(
Connector\Factory
$connector
,
$config
,
$default
)
public
function
__construct
(
$config
)
{
$this
->
config
=
$config
;
$this
->
default
=
$default
;
$this
->
connector
=
$connector
;
}
/**
* Get a database connection. If no database name is specified, the default
* connection will be returned as defined in the database configuration file.
* Get a database connection.
*
* If no database name is specified, the default connection will be returned as
* defined in the database configuration file.
*
* Note: Database connections are managed as singletons.
*
...
...
@@ -64,48 +41,25 @@ class Manager {
*/
public
function
connection
(
$connection
=
null
)
{
if
(
is_null
(
$connection
))
$connection
=
$this
->
default
;
if
(
is_null
(
$connection
))
$connection
=
$this
->
config
[
'default'
]
;
if
(
!
array_key_exists
(
$connection
,
$this
->
connections
))
{
if
(
!
isset
(
$this
->
config
[
$connection
]))
if
(
!
isset
(
$this
->
config
[
'connectors'
][
$connection
]))
{
throw
new
\Exception
(
"Database connection
[
$connection
] is not defined
."
);
throw
new
\Exception
(
"Database connection
configuration is not defined for connection [
$connection
]
."
);
}
$config
=
$this
->
config
[
$connection
];
$this
->
connections
[
$connection
]
=
new
Connection
(
$this
->
connector
(
$config
),
$connection
,
$config
);
// Database connections are established by developer configurable connector closures.
// This provides the developer the maximum amount of freedom in establishing their
// database connections, and allows the framework to remain agonstic to ugly database
// specific PDO connection details. Less code. Less bugs.
$this
->
connections
[
$connection
]
=
new
Connection
(
call_user_func
(
$this
->
config
[
'connectors'
][
$connection
],
$this
->
config
));
}
return
$this
->
connections
[
$connection
];
}
/**
* Create a new database connector instance base on a connection configuration.
*
* @param array $config
* @return Connector\Connector
*/
protected
function
connector
(
$config
)
{
if
(
isset
(
$config
[
'connector'
]))
return
new
Connector\Callback
;
switch
(
$config
[
'driver'
])
{
case
'sqlite'
:
return
new
Connector\SQLite
;
case
'mysql'
:
return
new
Connector\MySQL
;
case
'pgsql'
:
return
new
Connector\Postgres
;
}
throw
new
\Exception
(
"Database configuration is invalid. Please verify your configuration."
);
}
/**
* Begin a fluent query against a table.
*
...
...
@@ -119,7 +73,7 @@ class Manager {
*
* @param string $table
* @param string $connection
* @return
Database
\Query
* @return
Queries
\Query
*/
public
function
table
(
$table
,
$connection
=
null
)
{
...
...
laravel/database/quer
y/compiler/compile
r.php
→
laravel/database/quer
ies/grammars/gramma
r.php
View file @
59a7e47f
<?php
namespace
Laravel\Database\Query\
Compiler
;
<?php
namespace
Laravel\Database\Query\
Grammars
;
use
Laravel\Database\Query
;
use
Laravel\Database\Quer
ies\Quer
y
;
class
Compile
r
{
class
Gramma
r
{
/**
* Compile a SQL SELECT statment from a Query instance.
...
...
laravel/database/quer
y/compiler
/mysql.php
→
laravel/database/quer
ies/grammars
/mysql.php
View file @
59a7e47f
<?php
namespace
Laravel\Database\Query\
Compiler
;
<?php
namespace
Laravel\Database\Query\
Grammars
;
class
MySQL
extends
Compile
r
{
class
MySQL
extends
Gramma
r
{
/**
* Get the keyword identifier wrapper for the connection.
...
...
laravel/database/quer
y/compiler
/postgres.php
→
laravel/database/quer
ies/grammars
/postgres.php
View file @
59a7e47f
<?php
namespace
Laravel\Database\Query\
Compiler
;
<?php
namespace
Laravel\Database\Query\
Grammars
;
class
Postgres
extends
Compiler
{
use
Laravel\Database\Queries\Query
;
class
Postgres
extends
Grammar
{
/**
* Compile a SQL INSERT statment that returns an auto-incrementing ID from a Query instance.
...
...
laravel/database/quer
y
/postgres.php
→
laravel/database/quer
ies
/postgres.php
View file @
59a7e47f
<?php
namespace
Laravel\Database\Quer
y
;
<?php
namespace
Laravel\Database\Quer
ies
;
use
PDO
;
...
...
@@ -12,7 +12,7 @@ class Postgres extends Query {
*/
public
function
insert_get_id
(
$values
)
{
$query
=
$this
->
connection
->
pdo
->
prepare
(
$this
->
compile
r
->
insert_get_id
(
$this
,
$values
));
$query
=
$this
->
connection
->
pdo
->
prepare
(
$this
->
gramma
r
->
insert_get_id
(
$this
,
$values
));
$query
->
execute
(
array_values
(
$values
));
...
...
laravel/database/quer
y
/query.php
→
laravel/database/quer
ies
/query.php
View file @
59a7e47f
<?php
namespace
Laravel\Database\Quer
y
;
<?php
namespace
Laravel\Database\Quer
ies
;
use
Laravel\Database\Connection
;
...
...
@@ -12,11 +12,11 @@ class Query {
public
$connection
;
/**
* The query
compile
r instance.
* The query
gramma
r instance.
*
* @var
Compiler\Compile
r
* @var
Grammars\Gramma
r
*/
public
$
compile
r
;
public
$
gramma
r
;
/**
* The SELECT clause.
...
...
@@ -93,14 +93,14 @@ class Query {
* Create a new query instance.
*
* @param Database\Connection $connection
* @param
Compiler\Compiler $compile
r
* @param
Grammars\Grammar $gramma
r
* @param string $table
* @return void
*/
public
function
__construct
(
Connection
$connection
,
Compiler\Compiler
$compile
r
,
$table
)
public
function
__construct
(
Connection
$connection
,
Grammars\Grammar
$gramma
r
,
$table
)
{
$this
->
table
=
$table
;
$this
->
compiler
=
$compile
r
;
$this
->
grammar
=
$gramma
r
;
$this
->
connection
=
$connection
;
}
...
...
@@ -551,7 +551,7 @@ class Query {
{
$this
->
aggregate
=
compact
(
'aggregator'
,
'column'
);
$result
=
$this
->
connection
->
scalar
(
$this
->
compile
r
->
select
(
$this
),
$this
->
bindings
);
$result
=
$this
->
connection
->
scalar
(
$this
->
gramma
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.
...
...
@@ -581,7 +581,7 @@ class Query {
{
if
(
is_null
(
$this
->
select
))
$this
->
select
(
$columns
);
$results
=
$this
->
connection
->
query
(
$this
->
compile
r
->
select
(
$this
),
$this
->
bindings
);
$results
=
$this
->
connection
->
query
(
$this
->
gramma
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.
...
...
@@ -603,7 +603,7 @@ class Query {
*/
public
function
insert
(
$values
)
{
return
$this
->
connection
->
query
(
$this
->
compile
r
->
insert
(
$this
,
$values
),
array_values
(
$values
));
return
$this
->
connection
->
query
(
$this
->
gramma
r
->
insert
(
$this
,
$values
),
array_values
(
$values
));
}
/**
...
...
@@ -619,7 +619,7 @@ class Query {
*/
public
function
insert_get_id
(
$values
)
{
$this
->
connection
->
query
(
$this
->
compile
r
->
insert
(
$this
,
$values
),
array_values
(
$values
));
$this
->
connection
->
query
(
$this
->
gramma
r
->
insert
(
$this
,
$values
),
array_values
(
$values
));
return
(
int
)
$this
->
connection
->
pdo
->
lastInsertId
();
}
...
...
@@ -637,7 +637,7 @@ class Query {
*/
public
function
update
(
$values
)
{
return
$this
->
connection
->
query
(
$this
->
compile
r
->
update
(
$this
,
$values
),
array_merge
(
array_values
(
$values
),
$this
->
bindings
));
return
$this
->
connection
->
query
(
$this
->
gramma
r
->
update
(
$this
,
$values
),
array_merge
(
array_values
(
$values
),
$this
->
bindings
));
}
/**
...
...
@@ -663,7 +663,7 @@ class Query {
{
if
(
!
is_null
(
$id
))
$this
->
where
(
'id'
,
'='
,
$id
);
return
$this
->
connection
->
query
(
$this
->
compile
r
->
delete
(
$this
),
$this
->
bindings
);
return
$this
->
connection
->
query
(
$this
->
gramma
r
->
delete
(
$this
),
$this
->
bindings
);
}
/**
...
...
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