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
60e35263
Commit
60e35263
authored
Jul 07, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor DB\Connector class.
parent
9454927b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
42 deletions
+49
-42
connector.php
system/db/connector.php
+49
-42
No files found.
system/db/connector.php
View file @
60e35263
...
@@ -22,17 +22,29 @@ class Connector {
...
@@ -22,17 +22,29 @@ class Connector {
*/
*/
public
static
function
connect
(
$config
)
public
static
function
connect
(
$config
)
{
{
// -----------------------------------------------------
// Connect to SQLite.
// -----------------------------------------------------
if
(
$config
->
driver
==
'sqlite'
)
if
(
$config
->
driver
==
'sqlite'
)
{
{
// -----------------------------------------------------
return
static
::
connect_to_sqlite
(
$config
);
// Check the application/db directory first.
}
//
elseif
(
$config
->
driver
==
'mysql'
or
$config
->
driver
==
'pgsql'
)
// If the database doesn't exist there, maybe the full
{
// path was specified as the database name?
return
static
::
connect_to_server
(
$config
);
// -----------------------------------------------------
}
throw
new
\Exception
(
'Database driver '
.
$config
->
driver
.
' is not supported.'
);
}
/**
* Establish a PDO connection to a SQLite database.
*
* @param array $config
* @return PDO
*/
private
static
function
connect_to_sqlite
(
$config
)
{
// Database paths can either be specified relative to the application/storage/db
// directory, or as an absolute path.
if
(
file_exists
(
$path
=
APP_PATH
.
'storage/db/'
.
$config
->
database
.
'.sqlite'
))
if
(
file_exists
(
$path
=
APP_PATH
.
'storage/db/'
.
$config
->
database
.
'.sqlite'
))
{
{
return
new
\PDO
(
'sqlite:'
.
$path
,
null
,
null
,
static
::
$options
);
return
new
\PDO
(
'sqlite:'
.
$path
,
null
,
null
,
static
::
$options
);
...
@@ -46,14 +58,15 @@ class Connector {
...
@@ -46,14 +58,15 @@ class Connector {
throw
new
\Exception
(
"SQLite database ["
.
$config
->
database
.
"] could not be found."
);
throw
new
\Exception
(
"SQLite database ["
.
$config
->
database
.
"] could not be found."
);
}
}
}
}
// -----------------------------------------------------
// Connect to MySQL or Postgres.
/**
// -----------------------------------------------------
* Connect to a MySQL or PostgreSQL database server.
elseif
(
$config
->
driver
==
'mysql'
or
$config
->
driver
==
'pgsql'
)
*
* @param array $config
* @return PDO
*/
private
static
function
connect_to_server
(
$config
)
{
{
// -----------------------------------------------------
// Build the PDO connection DSN.
// -----------------------------------------------------
$dsn
=
$config
->
driver
.
':host='
.
$config
->
host
.
';dbname='
.
$config
->
database
;
$dsn
=
$config
->
driver
.
':host='
.
$config
->
host
.
';dbname='
.
$config
->
database
;
if
(
isset
(
$config
->
port
))
if
(
isset
(
$config
->
port
))
...
@@ -63,9 +76,6 @@ class Connector {
...
@@ -63,9 +76,6 @@ class Connector {
$connection
=
new
\PDO
(
$dsn
,
$config
->
username
,
$config
->
password
,
static
::
$options
);
$connection
=
new
\PDO
(
$dsn
,
$config
->
username
,
$config
->
password
,
static
::
$options
);
// -----------------------------------------------------
// Set the appropriate character set for the datbase.
// -----------------------------------------------------
if
(
isset
(
$config
->
charset
))
if
(
isset
(
$config
->
charset
))
{
{
$connection
->
prepare
(
"SET NAMES '"
.
$config
->
charset
.
"'"
)
->
execute
();
$connection
->
prepare
(
"SET NAMES '"
.
$config
->
charset
.
"'"
)
->
execute
();
...
@@ -74,7 +84,4 @@ class Connector {
...
@@ -74,7 +84,4 @@ class Connector {
return
$connection
;
return
$connection
;
}
}
throw
new
\Exception
(
'Database driver '
.
$config
->
driver
.
' is not supported.'
);
}
}
}
\ No newline at end of file
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