Permalink
Browse files

Create the database during setup if it does not already exist

  • Loading branch information...
1 parent c36c1fb commit b01de568ea8757d3304a379d652a59ec3e6117e6 @haraldpdl haraldpdl committed Aug 6, 2011
@@ -33,18 +33,24 @@ public function __construct($server, $username, $password, $database, $port, $dr
}
public function connect() {
- $dsn = 'mysql:dbname=' . $this->_database;
+ $dsn_array = array();
+
+ if ( !empty($this->_database) ) {
+ $dsn_array[] = 'dbname=' . $this->_database;
+ }
if ( (strpos($this->_server, '/') !== false) || (strpos($this->_server, '\\') !== false) ) {
- $dsn .= ';unix_socket=' . $this->_server;
+ $dsn_array[] = 'unix_socket=' . $this->_server;
} else {
- $dsn .= ';host=' . $this->_server;
+ $dsn_array[] = 'host=' . $this->_server;
if ( !empty($this->_port) ) {
- $dsn .= ';port=' . $this->_port;
+ $dsn_array[] = 'port=' . $this->_port;
}
}
+ $dsn = 'mysql:' . implode(';', $dsn_array);
+
$this->_connected = true;
$dbh = parent::__construct($dsn, $this->_username, $this->_password, $this->_driver_options);
@@ -8,10 +8,18 @@
namespace osCommerce\OM\Core\Site\Setup\Application\Install\Model;
+ use osCommerce\OM\Core\OSCOM;
use osCommerce\OM\Core\PDO;
+ use osCommerce\OM\Core\Registry;
class checkDB {
public static function execute($data) {
+ if ( $OSCOM_PDO = PDO::initialize($data['server'], $data['username'], $data['password'], null, $data['port'], $data['class']) ) {
+ Registry::set('PDO', $OSCOM_PDO);
+
+ OSCOM::callDB('Setup\Install\CreateDB', array('database' => $data['database']));
+ }
+
return PDO::initialize($data['server'], $data['username'], $data['password'], $data['database'], $data['port'], $data['class']);
}
}
@@ -0,0 +1,20 @@
+<?php
+/**
+ * osCommerce Online Merchant
+ *
+ * @copyright Copyright (c) 2011 osCommerce; http://www.oscommerce.com
+ * @license BSD License; http://www.oscommerce.com/bsdlicense.txt
+ */
+
+ namespace osCommerce\OM\Core\Site\Setup\Application\Install\SQL\MySQL\Standard;
+
+ use osCommerce\OM\Core\Registry;
+
+ class CreateDB {
+ public static function execute($data) {
+ $OSCOM_PDO = Registry::get('PDO');
+
+ return ($OSCOM_PDO->exec('create database if not exists `' . $data['database'] . '` CHARACTER SET utf8 COLLATE utf8_general_ci') !== false);
+ }
+ }
+?>

0 comments on commit b01de56

Please sign in to comment.