@@ -92,6 +92,7 @@ class MysqliDb
9292 protected $ password ;
9393 protected $ db ;
9494 protected $ port ;
95+ protected $ charset ;
9596
9697 /**
9798 * Is Subquery object
@@ -106,24 +107,36 @@ class MysqliDb
106107 * @param string $db
107108 * @param int $port
108109 */
109- public function __construct ($ host = NULL , $ username = NULL , $ password = NULL , $ db = NULL , $ port = NULL )
110+ public function __construct ($ host = NULL , $ username = NULL , $ password = NULL , $ db = NULL , $ port = NULL , $ charset = ' utf8 ' )
110111 {
111- $ this ->host = $ host ;
112+ $ isSubQuery = false ;
113+
114+ // if params were passed as array
115+ if (is_array ($ host )) {
116+ foreach ($ host as $ key => $ val )
117+ $ $ key = $ val ;
118+ }
119+ // if host were set as mysqli socket
120+ if (is_object ($ host ))
121+ $ this ->_mysqli = $ host ;
122+ else
123+ $ this ->host = $ host ;
124+
112125 $ this ->username = $ username ;
113126 $ this ->password = $ password ;
114127 $ this ->db = $ db ;
115- if ($ port == NULL )
116- $ this ->port = ini_get ('mysqli.default_port ' );
117- else
118- $ this ->port = $ port ;
128+ $ this ->port = $ port ;
129+ $ this ->charset = $ charset ;
119130
120- if ($ host == null && $ username == null && $ db == null ) {
131+ if ($ isSubQuery ) {
121132 $ this ->isSubQuery = true ;
122133 return ;
123134 }
124135
125136 // for subqueries we do not need database connection and redefine root instance
126- $ this ->connect ();
137+ if (!is_object ($ host ))
138+ $ this ->connect ();
139+
127140 $ this ->setPrefix ();
128141 self ::$ _instance = $ this ;
129142 }
@@ -137,10 +150,14 @@ public function connect()
137150 if ($ this ->isSubQuery )
138151 return ;
139152
153+ if (empty ($ this ->host ))
154+ die ('Mysql host is not set ' );
155+
140156 $ this ->_mysqli = new mysqli ($ this ->host , $ this ->username , $ this ->password , $ this ->db , $ this ->port )
141157 or die ('There was a problem connecting to the database ' );
142158
143- $ this ->_mysqli ->set_charset ('utf8 ' );
159+ if ($ this ->charset )
160+ $ this ->_mysqli ->set_charset ($ this ->charset );
144161 }
145162 /**
146163 * A method of returning the static instance to allow access to the
@@ -427,12 +444,14 @@ public function join($joinTable, $joinCondition, $joinType = '')
427444 {
428445 $ allowedTypes = array ('LEFT ' , 'RIGHT ' , 'OUTER ' , 'INNER ' , 'LEFT OUTER ' , 'RIGHT OUTER ' );
429446 $ joinType = strtoupper (trim ($ joinType ));
430- $ joinTable = filter_var ($ joinTable , FILTER_SANITIZE_STRING );
431447
432448 if ($ joinType && !in_array ($ joinType , $ allowedTypes ))
433449 die ('Wrong JOIN type: ' .$ joinType );
434450
435- $ this ->_join [$ joinType . " JOIN " . self ::$ _prefix . $ joinTable ] = $ joinCondition ;
451+ if (!is_object ($ joinTable ))
452+ $ joinTable = self ::$ _prefix . filter_var ($ joinTable , FILTER_SANITIZE_STRING );
453+
454+ $ this ->_join [] = Array ($ joinType , $ joinTable , $ joinCondition );
436455
437456 return $ this ;
438457 }
@@ -587,7 +606,7 @@ protected function _buildPair ($operator, $value) {
587606 $ subQuery = $ value ->getSubQuery ();
588607 $ this ->_bindParams ($ subQuery ['params ' ]);
589608
590- return " " . $ operator . " ( " . $ subQuery ['query ' ] . ") " ;
609+ return " " . $ operator . " ( " . $ subQuery ['query ' ] . ") " . $ subQuery [ ' alias ' ] ;
591610 }
592611
593612 /**
@@ -679,8 +698,16 @@ protected function _buildJoin () {
679698 if (empty ($ this ->_join ))
680699 return ;
681700
682- foreach ($ this ->_join as $ prop => $ value )
683- $ this ->_query .= " " . $ prop . " on " . $ value ;
701+ foreach ($ this ->_join as $ data ) {
702+ list ($ joinType , $ joinTable , $ joinCondition ) = $ data ;
703+
704+ if (is_object ($ joinTable ))
705+ $ joinStr = $ this ->_buildPair ("" , $ joinTable );
706+ else
707+ $ joinStr = $ joinTable ;
708+
709+ $ this ->_query .= " " . $ joinType . " JOIN " . $ joinStr ." on " . $ joinCondition ;
710+ }
684711 }
685712
686713 /**
@@ -946,7 +973,8 @@ public function getSubQuery () {
946973
947974 array_shift ($ this ->_bindParams );
948975 $ val = Array ('query ' => $ this ->_query ,
949- 'params ' => $ this ->_bindParams
976+ 'params ' => $ this ->_bindParams ,
977+ 'alias ' => $ this ->host
950978 );
951979 $ this ->reset ();
952980 return $ val ;
@@ -1031,9 +1059,9 @@ public function func ($expr, $bindParams = null) {
10311059 /**
10321060 * Method creates new mysqlidb object for a subquery generation
10331061 */
1034- public static function subQuery ()
1062+ public static function subQuery ($ subQueryAlias = "" )
10351063 {
1036- return new MysqliDb ( );
1064+ return new MysqliDb ( Array ( ' host ' => $ subQueryAlias , ' isSubQuery ' => true ) );
10371065 }
10381066
10391067 /**
@@ -1043,7 +1071,9 @@ public static function subQuery()
10431071 */
10441072 public function copy ()
10451073 {
1046- return clone $ this ;
1074+ $ copy = unserialize (serialize ($ this ));
1075+ $ copy ->_mysqli = $ this ->_mysqli ;
1076+ return $ copy ;
10471077 }
10481078
10491079 /**
0 commit comments