@@ -29,25 +29,7 @@ public function __construct( \PDO $pdo = null )
2929 public function table ( $ table )
3030 {
3131 $ this ->reset ( );
32- $ table = ( is_array ( $ table ) ) ? $ table : array ( $ table );
33- foreach ( $ table as $ v )
34- {
35- if ( $ val = $ this ->_checkRawValue ( $ v ) )
36- {
37- $ this ->_table .= ' ' . $ val . ', ' ;
38- continue ;
39- }
40- $ divider = ( strpos ( $ v , ' as ' ) ) ? ' as ' : ' AS ' ;
41- $ table = explode ( $ divider , $ v );
42- $ t = $ this ->addBackTicks ( $ table [ 0 ] );
43- if ( array_key_exists ( 1 , $ table ) )
44- {
45- $ t .= ' as ' . $ this ->addBackTicks ( $ table [ 1 ] );
46- }
47- $ this ->_table .= $ this ->sanitize ( $ t ) . ', ' ;
48- //$this->_table .= $this->addBackTicks( $this->sanitize( $v ) ) . ',';
49- }
50- $ this ->_table = substr ( $ this ->_table , 0 , strlen ( $ this ->_table ) - 1 );
32+ $ this ->_table = $ this ->_formatTable ( $ table );
5133 $ this ->_currentQueryType = 'select ' ; // set query type as select by default
5234 return $ this ;
5335 }
@@ -115,22 +97,23 @@ public function raw( $value ){ return $this->_randomId . 'RAW{' .$value . '}'; }
11597 public function addBackTicks ( $ string )
11698 {
11799 if ( $ val = $ this ->_checkRawValue ( $ string ) ){ return $ val ; }
118- $ raw = explode ( '. ' , $ string );
100+ $ raw = array_map ( ' trim ' , explode ( '. ' , $ string ) );
119101 $ string = ( $ raw [ 0 ] === '* ' ) ? $ raw [ 0 ] : '` ' . $ raw [ 0 ] . '` ' ;
120- return $ string .= ( @$ raw [ 1 ] ) ? '.` ' . $ raw [ 1 ] . '` ' : '' ;
102+ if ( @$ raw [ 1 ] ){ $ string .= ( $ raw [ 1 ] === '* ' ) ? '. ' . $ raw [ 1 ] : '.` ' . $ raw [ 1 ] . '` ' ; }
103+ return $ string ;
121104 }
122105 /**
123106 * Creates a join based on the parameters. See @ref qb_joins
124- * @param string $table the name of the table to join
125- * @param string $first the first column
107+ * @param string $table the name of the table to join
108+ * @param string $first the first column
126109 * @param string $operator the operator to use for the join
127110 * @param string $second the second column
128- * @param string $type the type of join
111+ * @param string $type the type of join
129112 */
130113 public function join ( $ table , $ first , $ operator = null , $ second = null , $ type = 'inner ' )
131114 {
132115 if ( !$ this ->_isTableSet ( ) ) { return false ; }
133- $ this ->_join .= ' ' . strtoupper ( $ type ) . ' JOIN ' . $ this ->addBackTicks ( $ table );
116+ $ this ->_join .= ' ' . strtoupper ( $ type ) . ' JOIN ' . $ this ->_formatTable ( $ table );
134117 $ this ->_isClosure = true ;
135118 if ( $ first instanceof \Closure ){ $ this ->_runClosure ( $ first , 'join ' ); }
136119 else { $ this ->on ( $ first , $ operator , $ second ); }
@@ -208,8 +191,7 @@ public function run( $query = null , $bind = null , $type = null )
208191 if ( !$ type )
209192 {
210193 $ type = 3 ;
211- // check statement that needs data in return
212- foreach ( $ this ->_returnStatements as $ statement )
194+ foreach ( $ this ->_returnStatements as $ statement ) // statements that need return data
213195 {
214196 if ( strpos ( trim ( strtoupper ( $ this ->_currentQuery ) ) , $ statement ) === 0 )
215197 {
@@ -256,7 +238,7 @@ public function prepare( )
256238 {
257239 if ( is_string ( $ v ) )
258240 {
259- if ( false === strpos ( $ v , ': ' ) ) { continue ; }
241+ if ( false === strpos ( $ v , ': ' ) ) { continue ; }
260242 $ query = preg_replace ( '/\?/ ' , $ v , $ query , 1 );
261243 }
262244 }
@@ -266,7 +248,7 @@ public function prepare( )
266248 /**
267249 * Adds order to the query. See @ref qb_order_group_limit
268250 * @param string $column the column names
269- * @param string $direction asc or desc
251+ * @param string $direction asc or desc
270252 */
271253 public function order ( $ column , $ direction = 'asc ' )
272254 {
@@ -408,7 +390,7 @@ public function setFetchMode( $mode , $class = null )
408390 return false ;
409391 }
410392 $ this ->_fetchMode = ( $ class ) ? array ( $ mode , $ class ) : array ( $ mode );
411-
393+ return $ this ;
412394 }
413395 /**
414396 * Adds where operators and joins to the query
@@ -783,6 +765,33 @@ protected function _checkPdo( )
783765 return true ;
784766 }
785767 /**
768+ * Adds backticks properly to table names
769+ * @param string|array $table the table name
770+ */
771+ protected function _formatTable ( $ table )
772+ {
773+ $ string = null ;
774+ $ table = ( is_array ( $ table ) ) ? $ table : array ( $ table );
775+ foreach ( $ table as $ v )
776+ {
777+ if ( $ val = $ this ->_checkRawValue ( $ v ) )
778+ {
779+ $ string .= ' ' . $ val . ', ' ;
780+ continue ;
781+ }
782+ $ divider = ( strpos ( $ v , ' as ' ) ) ? ' as ' : ' AS ' ;
783+ $ table = explode ( $ divider , $ v );
784+ $ t = $ this ->addBackTicks ( $ table [ 0 ] );
785+ if ( array_key_exists ( 1 , $ table ) )
786+ {
787+ $ t .= ' as ' . $ this ->addBackTicks ( $ table [ 1 ] );
788+ }
789+ $ string .= $ this ->sanitize ( $ t ) . ', ' ;
790+ //$string .= $this->addBackTicks( $this->sanitize( $v ) ) . ',';
791+ }
792+ return $ string = substr ( $ string , 0 , strlen ( $ string ) - 1 );
793+ }
794+ /**
786795 * Checks if a table was set
787796 */
788797 protected function _isTableSet ( )
@@ -851,10 +860,10 @@ protected function _debugQuery( $string , $data )
851860 }
852861 /**
853862 * Adds execution time and query results to the PtcDebug class
854- * @param string $reference a reference to look for ("$statement")
855- * @param string $type the type of debug (timer, attach)
863+ * @param string $reference a reference to look for ("$statement")
864+ * @param string $type the type of debug (timer, attach)
856865 * @param mixed $string the string to pass
857- * @param mixed $statement some new statement if required
866+ * @param mixed $statement some new statement if required
858867 */
859868 protected static function _debugBuffer ( $ reference , $ type = null , $ string = null , $ statement = null )
860869 {
0 commit comments