public static function addDbFromURI($uri) { if (strpos($uri, 'mysql://') === 0) { if (!preg_match('#mysql://([^:]*):([^@]*)@([^/]*)/(.*)#', strval($uri), $matches)) { throw new Exception("wrong mysql URI format, must be mysql://{user}:{password}@{host}/{db}"); } $db = new StdClass(); $db->host = $matches[3]; $db->username = $matches[1]; $db->password = $matches[2]; $db->dbname = $matches[4]; $config = new StdClass(); $config->master = $config->slave = $db; Pix_Table::setDefaultDb(new Pix_Table_Db_Adapter_MysqlConf(array($config))); } elseif (strpos($uri, 'pgsql://') === 0) { if (!preg_match('#pgsql://([^:]*):([^@]*)@([^/:]*):?([0-9]*)?/(.*)#', strval($uri), $matches)) { throw new Exception("wrong pgsql URI format, must be pgsql://{user}:{password}@{host}/{db}"); } $options = array('host' => $matches[3], 'user' => $matches[1], 'password' => $matches[2], 'dbname' => $matches[5]); if ($matches[4]) { $options['port'] = $matches[4]; } Pix_Table::setDefaultDb(new Pix_Table_Db_Adapter_PgSQL($options)); } elseif (strpos($uri, 'sqlite://') === 0) { $file = substr($uri, strlen('sqlite://')); Pix_Table::setDefaultDb(new Pix_Table_Db_Adapter_Sqlite($file)); } else { throw new Exception("add DB failed, unknown uri {$uri}"); } }
/** * 測試 getDb , 如果 Table 有指定應該要傳回 Table 指定的,沒有就是 default */ public function testGetDb() { $db = $this->getMock('Pix_Table_Db_Adapter_Abstract'); Pix_Table::setDefaultDb($db); $this->assertEquals(Pix_Table::getTable('Pix_Table_TableTest_Table2')->getDb(), $db); }
<?php error_reporting(E_ALL ^ E_STRICT ^ E_NOTICE); include __DIR__ . '/stdlibs/pixframework/Pix/Loader.php'; set_include_path(__DIR__ . '/stdlibs/pixframework/' . PATH_SEPARATOR . __DIR__ . '/models' . PATH_SEPARATOR . __DIR__ . '/Dropbox-master'); Pix_Loader::registerAutoLoad(); if (file_exists(__DIR__ . '/config.php')) { include __DIR__ . '/config.php'; } // TODO: 之後要搭配 geoip date_default_timezone_set('Asia/Taipei'); if (!getenv('MYSQL_DATABASE_URL')) { die('need MYSQL_DATABASE_URL'); } if (!preg_match('#mysql://([^:]*):([^@]*)@([^/]*)/(.*)#', strval(getenv('MYSQL_DATABASE_URL')), $matches)) { die('mysql only'); } $db = new StdClass(); $db->host = $matches[3]; $db->username = $matches[1]; $db->password = $matches[2]; $db->dbname = $matches[4]; $config = new StdClass(); $config->master = $config->slave = $db; Pix_Table::setDefaultDb(new Pix_Table_Db_Adapter_MysqlConf(array($config)));
<?php include __DIR__ . '/libs/pixframework/Pix/Loader.php'; set_include_path(__DIR__ . '/libs/pixframework' . PATH_SEPARATOR . __DIR__ . '/models'); Pix_Loader::registerAutoload(); if (file_exists(__DIR__ . '/config.php')) { include __DIR__ . '/config.php'; } else { if (getenv('DATABASE_URL')) { if (preg_match('#postgres://([^:]*):([^@]*)@([^/]*)/(.*)#', strval(getenv('DATABASE_URL')), $matches)) { $user = $matches[1]; $pass = $matches[2]; $host = $matches[3]; $dbname = $matches[4]; Pix_Table::setDefaultDb(new Pix_Table_Db_Adapter_PgSQL(array('user' => $user, 'password' => $pass, 'host' => $host, 'dbname' => $dbname))); } } } date_default_timezone_set('Asia/Taipei');
<?php // init: 要自動載入 Pix Framework include __DIR__ . '/../../Pix/Loader.php'; set_include_path(__DIR__ . '/../../' . PATH_SEPARATOR . __DIR__ . '/models'); Pix_Loader::registerAutoload(); // 預設所有 Db 都是 sqlite Pix_Table::setDefaultDb(new Pix_Table_Db_Adapter_Sqlite(':memory:')); // 顯示 SQL query Pix_Table::enableLog(Pix_Table::LOG_QUERY); echo 'create table' . PHP_EOL; User::createTable(); Article::createTable(); echo '增加 user alice' . PHP_EOL; $user_alice = User::insert(array('name' => 'alice', 'password' => crc32('foo'))); echo '新增一篇文章' . PHP_EOL; $article = $user_alice->create_articles(array('post_at' => time(), 'title' => '我是標題', 'body' => '我是內容'));