3 namespace RedUNIT\Mysql;
5 use RedUNIT\Mysql as Mysql;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\RedException\SQL as SQL;
12 * Tests the parameter binding functionality in RedBeanPHP.
13 * These test scenarios include for instance: NULL handling,
14 * binding parameters in LIMIT clauses and so on.
16 * @file RedUNIT/Mysql/Parambind.php
17 * @desc Tests\PDO parameter binding.
18 * @author Gabor de Mooij and the RedBeanPHP Community
19 * @license New BSD/GPLv2
21 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
22 * This source file is subject to the New BSD/GPLv2 License that is bundled
23 * with this source code in the file license.txt.
25 class Parambind extends Mysql
28 * Test parameter binding with\PDO.
32 public function testPDOParameterBinding()
34 $toolbox = R::getToolBox();
35 $adapter = $toolbox->getDatabaseAdapter();
36 $writer = $toolbox->getWriter();
37 $redbean = $toolbox->getRedBean();
38 $pdo = $adapter->getDatabase();
39 R::getDatabaseAdapter()->getDatabase()->setUseStringOnlyBinding( TRUE );
41 R::getAll( "select * from job limit ? ", array( 1 ) );
43 } catch (\Exception $e ) {
47 R::getAll( "select * from job limit :l ", array( ":l" => 1 ) );
49 } catch (\Exception $e ) {
53 R::exec( "select * from job limit ? ", array( 1 ) );
55 } catch (\Exception $e ) {
59 R::exec( "select * from job limit :l ", array( ":l" => 1 ) );
61 } catch (\Exception $e ) {
64 R::getDatabaseAdapter()->getDatabase()->setUseStringOnlyBinding( FALSE );
66 R::getAll( "select * from job limit ? ", array( 1 ) );
68 } catch (\Exception $e ) {
72 R::getAll( "select * from job limit :l ", array( ":l" => 1 ) );
74 } catch (\Exception $e ) {
78 R::exec( "select * from job limit ? ", array( 1 ) );
80 } catch (\Exception $e ) {
84 R::exec( "select * from job limit :l ", array( ":l" => 1 ) );
86 } catch (\Exception $e ) {
89 testpack( "Test findOrDispense" );
90 $person = R::findOrDispense( "person", " job = ? ", array( "developer" ) );
91 asrt( ( count( $person ) > 0 ), TRUE );
92 $person = R::findOrDispense( "person", " job = ? ", array( "musician" ) );
93 asrt( ( count( $person ) > 0 ), TRUE );
94 $musician = array_pop( $person );
95 asrt( intval( $musician->id ), 0 );
97 $adapter->exec( "an invalid query" );
102 asrt( (int) $adapter->getCell( "SELECT 123" ), 123 );
103 asrt( (int) $adapter->getCell( "SELECT ?", array( "987" ) ), 987 );
104 asrt( (int) $adapter->getCell( "SELECT ?+?", array( "987", "2" ) ), 989 );
105 asrt( (int) $adapter->getCell( "SELECT :numberOne+:numberTwo", array(
106 ":numberOne" => 42, ":numberTwo" => 50 ) ), 92 );
107 $pair = $adapter->getAssoc( "SELECT 'thekey','thevalue' " );
108 asrt( is_array( $pair ), TRUE );
109 asrt( count( $pair ), 1 );
110 asrt( isset( $pair["thekey"] ), TRUE );
111 asrt( $pair["thekey"], "thevalue" );
112 testpack( 'Test whether we can properly bind and receive NULL values' );
113 asrt( $adapter->getCell( 'SELECT :nil ', array( ':nil' => 'NULL' ) ), 'NULL' );
114 asrt( $adapter->getCell( 'SELECT :nil ', array( ':nil' => NULL ) ), NULL );
115 asrt( $adapter->getCell( 'SELECT ? ', array( 'NULL' ) ), 'NULL' );
116 asrt( $adapter->getCell( 'SELECT ? ', array( NULL ) ), NULL );