From 5a626629d7e9d64cd945255dbc353563b1560cb9 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 8 Nov 2012 20:02:31 -0500 Subject: [PATCH] Enable Query() function as a alias function --- common.php | 41 ++++++++++++++++++++++++++++++++-- tests/core/db_qb_test.php | 9 ++++++++ tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/common.php b/common.php index e5d684a..73f6baa 100644 --- a/common.php +++ b/common.php @@ -89,14 +89,34 @@ function db_filter($array, $index) return $new_array; } +// -------------------------------------------------------------------------- + /** * Connection function * * @param mixed $params * @return Query_Builder */ -function Query($params) +function Query($params = '') { + static $connections; + + // If there's existing connection(s) just return it + if ( ! empty($connections)) + { + // If the paramater is a string, use it as an array index + if (is_scalar($params) && isset($connections[$params])) + { + return $connections[$params]; + } + elseif (empty($params) && isset($connections[0])) + { + return end($connections); + } + } + + // -------------------------------------------------------------------------- + // Convert array to object if (is_array($params)) { @@ -122,6 +142,8 @@ function Query($params) { throw new BadDBDriverException('Database driver does not exist, or is not supported'); } + + // -------------------------------------------------------------------------- // Create the dsn for the database to connect to switch($dbtype) @@ -161,15 +183,30 @@ function Query($params) { throw new BadConnectionException('Connection failed, invalid arguments', 2); } + + // -------------------------------------------------------------------------- // Set the table prefix, if it exists if (isset($params->prefix)) { $db->table_prefix = $params->prefix; } + + // Create the database connection + $conn = new Query_Builder($db, $params); + + // Save it for later + if (isset($params->alias)) + { + $connections[$params->alias] =& $conn; + } + else + { + $connections[] =& $conn; + } // Return the Query Builder object - return new Query_Builder($db, $params); + return $conn; } // End of common.php \ No newline at end of file diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index 96551a9..7bcfb6b 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -21,6 +21,15 @@ abstract class QBTest extends UnitTestCase { // -------------------------------------------------------------------------- // ! Get Tests // -------------------------------------------------------------------------- + + public function TestQueryFunctionAlias() + { + if (empty($this->db)) return; + + $db = Query(); + + $this->assertReference($this->db, $db); + } public function TestGet() { diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index fffeff1aa6b6f88f82d66fb896231f5e8112c7c0..bda2b3abe5bf738e13ffa2905869bd41586186f9 100644 GIT binary patch delta 1896 zcmb7E&1(};5TCc*O}3pTd8uMctF>)Rnu3J{TC^uEQV=TQp$DlRdMMP=j}Rr)BB7}y zkG3cZ3h&@SJScb(QEYn<#9L2VPvXbHgBSk;FAKie%}X|mrR{{>w=-{MGV^=CH>Fax zRLWjvFp}Bnhq`bqHB~iQiz$GTdH@Xe)g{gNj^|p8FP3$fg zoC$6S-XypwxFvW<@UY;v;LV%7#pG;t%j#))+5G4{4{f~~Xt@*;nU>8Anpfsdd#P*h zNp=Lznn}2A_keAu;e9v>H$o9ehkC#a^}#PI0U0|1+g2JLtM7R$30Kwr?{ESxs+DBI z?t`=$fhjWu3vMs2S$-(v7zPaOtWAC`<6wu+4vhVEz%qfs&_}sj#-nn#f*ou@{;A-& zT84#Ma&lGRg;9#zR{5KhH-VycBcRhR?ax;ILF@8e8GEZ$AnjJgD>q=MmFu(bRqm`D zS;JnLs$f)h2t4jfZpd>2J6Wro6xbc7Gp81GNkz9LtIYx6@<$m5SW!M-$K){8?}-gu zeNc}8wtCc>vtco}IJlI%In5nPSrzJ|J*1MUE%~#IT~VKB)G5lb4eT`*b8@GGCyiW@ zKptY#vRmLW^yhi7hNEnSQ14!`RBbges2KH!RE(h>wK@i5hjd@=(M&Rz+tjCbG`yyB z6+yAf0USv8L`4uKnoY1^CO0!LKb6t;0fco|pnkg!0aYNh0jg{RR6fs!GY3mG2V6l? zLvdMtyH`VzEz9J6#n?OY%Nk}T{(}@{CS+oSgi5A$kJP-TYf={iNEL#NLGq}|;Q)y{ zfzUkxiTOSE1Ee6=H|l^CWMxeOvP6I!kW9K|nhFGw)`>by5m4(cgsSs=03y;o*^f*t zf4JKgcj~!a_B9~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