Browse files

Merge branch 'master' of git.corneltek.com:php/SQLBuilder into devel

Conflicts:
	.travis.yml
	src/SQLBuilder/Expression.php
	tests/bootstrap.php
  • Loading branch information...
2 parents ff0581e + fb7e249 commit 1aaa5a612a4589f2d594a1d8000df9dad6ee00c2 @c9s committed Mar 20, 2013
View
14 .travis.yml
@@ -1,16 +1,18 @@
-postgres:
- adapter: postgresql
- database: sqlbuilder_test
- username: postgres
+services:
+ - postgresql
+ - mysql
language: php
php:
- 5.3
- 5.4
before_script:
- - "psql -c 'create database sqlbuilder_test;' -U postgres"
+ - psql -c 'create database sqlbuilder_test;' -U postgres
+ - mysql -e 'create database sqlbuilder_test;'
- phpenv rehash
- - curl http://install.onionphp.org/ | bash
+ - pear channel-discover pear.corneltek.com
+ - pear install -f -a corneltek/PHPUnit_TestMore
+ - pear install -f -a corneltek/Universal
- curl -O https://raw.github.com/c9s/Onion/master/onion
- php onion install
View
287 README.md
@@ -1,41 +1,73 @@
# SQLBuilder for PHP5.3
-<!--
-[![Build Status](https://secure.travis-ci.org/c9s/SQLBuilder.png)](http://travis-ci.org/c9s/SQLBuilder)
--->
+[![Build Status](https://secure.travis-ci.org/c9s/php-SQLBuilder.png)](http://travis-ci.org/c9s/php-SQLBuilder)
-SQLBuilder Simply focuses on providing a simple syntax for building SQL statement.
+SQLBuilder focuses on providing a simple syntax for building SQL statements.
-When switching database backend, you can simple change the driver type of query
+When switching database backend, you can simplely change the driver type of query
builder, and it will generate the proper SQL for your backend, you don't have
-to modify code to support different platform.
+to modify the code to support different backend.
-for example, pgsql support `returning` statement, this kind of syntax will only
+For example, pgsql support `returning` statement, this kind of syntax will only
be built when this feature is supported.
+## Features
-## Install through PEAR
+* Simple.
+* Fast & Powerful.
+* Ability to change SQL style, question-mark style, named-placeholder style.
+* Ability to change quote style, table name quoting, column name quoting..etc.
+* Configurable escaper and quoter.
+* No package dependency.
- $ sudo pear channe-discover pear.corneltek.com
- $ sudo pear install corneltek/SQLBuilder
+## Installation
+
+### Install through PEAR
+
+```sh
+$ pear channe-discover pear.corneltek.com
+$ pear install corneltek/SQLBuilder
+```
+
+### Install through Composer
+
+```json
+{
+ "require": {
+ "c9s/sqlbuilder": "*"
+ }
+}
+```
+
+## Synopsis
+
+```php
+$driver = new Driver('mysql');
+
+$b = new SQLBuilder\QueryBuilder($driver,'Member');
+$b->select('*');
+$b->where()
+ ->equal( 'a' , 'bar' );
+$sql = $b->build();
+
+// SELECT * FROM Member where a = 'bar'
+```
## Driver
-get your SQL driver
+Get your SQL driver
```php
-<?php
$driver = new SQLBuilder\Driver('pgsql');
$driver = SQLBuilder\Driver::getInstance();
$driver = SQLBuilder\Driver::create('pgsql');
```
-### Configure Driver Quoter
+### Configuring Driver Quoter
string quote/escape handler:
```php
-<?php
$driver->configure('escape',array($pg,'escape'));
$driver->configure('quoter',array($pdo,'quote'));
@@ -46,65 +78,67 @@ $driver->quoter = function($string) {
};
```
-### Configure database driver for pgsql
+### Configuring Database Driver For pgsql
```php
-<?php
$driver->configure('driver','pgsql');
```
Trim spaces for SQL ?
```php
-<?php
$driver->configure('trim',true);
```
-### Place Holder style
+### Changing Placeholder Style
+
+SQLBuilder supports two placeholder styles:
-SQLBuilder supports two styles:
- named parameter by PDO
- question-mark paramter by mysql, PDO.
-configure for named-parameter:
+#### Named Placeholder:
```php
-<?php
$driver->configure('placeholder','named');
```
This generates SQL with named-parameter for PDO:
- INSERT INTO table (foo ,bar ) values (:foo, :bar);
+```
+INSERT INTO table (foo ,bar ) values (:foo, :bar);
+```
-Configure for question-mark style:
+#### Question-mark Placeholder
-If you pass variables to build SQL with named parameters, query builder
-converts named parameters for you, to get variables, you can use `getVars` method:
+If you pass variables to build SQL with named parameters, query
+builder converts named parameters for you, to get variables, you
+can use `getVars` method:
```php
-<?php
- $vars = $sb->getVars();
+$vars = $sb->getVars();
+```
+
+Which returns:
- /*
- array(
- ':name' => 'Foo',
- ':phone' => 'Bar',
- );
- */
+```php
+array(
+ ':name' => 'Foo',
+ ':phone' => 'Bar',
+);
```
Or to use question-mark style:
```php
-<?php
$driver->configure('placeholder',true);
```
This generates:
- INSERT INTO table (foo ,bar ) values (?,?);
-
+```sql
+INSERT INTO table (foo ,bar ) values (?,?);
+```
## Query SQL Generation
@@ -113,12 +147,10 @@ This generates:
Build SQL query for table 'Member':
```php
-<?php
-$sqlbuilder = new SQLBuilder\QueryBuilder;
-$sqlbuilder->driver = $driver;
-$sqlbuilder->table('Member');
-$sqlbuilder->select('*','column1','column2');
-$sqlbuilder->select(array(
+$builder = new SQLBuilder\QueryBuilder($driver);
+$builder->table('Member');
+$builder->select('*','column1','column2');
+$builder->select(array(
'column1' => 'as1',
'column2' => 'as2',
));
@@ -127,95 +159,119 @@ $sqlbuilder->select(array(
Build Select SQL
```php
-<?php
- $sql = $sqlbuilder->table('Member')->select('*')
- ->where()
- ->equal( 'a' , 'bar' ) // a = 'bar'
- ->notEqual( 'a' , 'bar' ) // a != 'bar'
- ->is( 'a' , 'null' ) // a is null
- ->isNot( 'a' , 'null' ) // a is not equal
- ->greater( 'a' , '2011-01-01' );
- ->greater( 'a' , array('date(2011-01-01)') ); // do not escape
- ->or()->less( 'a' , 123 )
- ->and()->like( 'content' , '%content%' );
- ->group() // AND ( a = 123 AND b != 123 )
- ->is( 'a' , 123 )
- ->isNot( 'b', 123 )
- ->ungroup()
- ->back() // back to sql builder
- ->build();
-```
-
-`where()` returns Expression object.
+$sql = $builder->table('Member')->select('*')
+ ->where()
+ ->equal( 'a' , 'bar' ) // a = 'bar'
+ ->notEqual( 'a' , 'bar' ) // a != 'bar'
+ ->is( 'a' , 'null' ) // a is null
+ ->isNot( 'a' , 'null' ) // a is not equal
+ ->greater( 'a' , '2011-01-01' );
+ ->in( 'a', array(1,2,3,4,5) )
+ ->greater( 'a' , array('date(2011-01-01)') ); // do not escape
+ ->or()->less( 'a' , 123 )
+ ->and()->like( 'content' , '%content%' );
+ ->group() // AND ( a = 123 AND b != 123 )
+ ->is( 'a' , 123 )
+ ->isNot( 'b', 123 )
+ ->ungroup()
+ ->back() // back to sql builder
+ ->build();
+```
+
+The `where()` returns `SQLBuilder\Expression` object.
`Condition->back()` returns QueryBuilder object
-### Limit, Offset
+### Limit And Offset
```php
-<?php
-$sqlbuilder->select('*')->table('items')
+$builder->select('*')->table('items')
->groupBy('name')
->limit(10)->offset(100);
?>
```
-For pgsql, generates:
+For PostgreSQL, which generates:
- SELECT * FROM items OFFSET 100 LIMIT 10;
+```sql
+SELECT * FROM items OFFSET 100 LIMIT 10;
+```
-For mysql, generates:
+For MySql, which generates:
- SELECT * FROM items LIMIT 100,10;
+```sql
+SELECT * FROM items LIMIT 100,10;
+```
### Between
```php
-<?php
- $query->select('*')->table('items')
- ->where()
- ->between('created_on', '2011-01-01' , '2011-02-01' )
- ->limit(10)->offset(100);
+$query->select('*')->table('items')
+ ->where()
+ ->between('created_on', '2011-01-01' , '2011-02-01' );
+```
+
+```sql
+SELECT * FROM items WHERE created_on BETWEEN '2011-01-01' AND '2011-02-01'
+```
+
+### In
+
+```php
+$query->select('*')->table('items')
+ ->where()
+ ->in('a', array(1,2,3,4));
+```
- // SELECT * FROM items WHERE created_on BETWEEN '2011-01-01' AND '2011-02-01'
+```sql
+SELECT * FROM items WHERE a IN (1,2,3,4);
+```
+
+```php
+$query->select('*')->table('City')
+ ->where()
+ ->in('name', array('Taipei','France','Japan'));
+```
+
+```sql
+SELECT * FROM City WHERE name IN ('Taipei','France','Japan');
```
### Insert
-Do insertion:
+Insertion:
```php
-<?php
- $sqlbuilder->insert(array(
- // placeholder => 'value'
- 'foo' => 'foo',
- 'bar' => 'bar',
- ));
+$builder->insert(array(
+ // placeholder => 'value'
+ 'foo' => 'foo',
+ 'bar' => 'bar',
+));
```
For question-mark style SQL, you might need this:
```php
-<?php
- $sqlbuilder->insert(array(
- 'foo',
- 'bar',
- ));
+$builder->insert(array(
+ 'foo',
+ 'bar',
+));
```
-The last, build SQL:
+The last thing, build the SQL statement:
- $sql = $sqlbuilder->build();
+```php
+$sql = $builder->build();
+```
### Update
-
```php
-<?php
-$sb = new QueryBuilder('member');
-$sb->driver = new Driver;
-$sb->driver->configure('driver','mysql');
-$sb->driver->configure('placeholder','named');
+$driver = new Driver;
+$driver->configure('driver','mysql');
+$driver->configure('placeholder','named');
+
+$sb = new QueryBuilder('member',$driver);
$sb->update( array( 'set1' => 'value1') );
$sb->whereFromArgs(array(
'cond1' => ':blah', // is equal to where()->equal('cond1',':blah')
@@ -227,8 +283,7 @@ $sql = $sb->build(); // UPDATE member SET set1 = 'value1' WHERE cond1 = :cond1
### Join
```php
-<?php
-$sb = new QueryBuilder('Member');
+$sb = new QueryBuilder($driver,'Member');
$sb->alias('m')
->join('table_name')
->alias('t')
@@ -242,24 +297,22 @@ $sb->alias('m')
### Delete
```php
-<?php
- $sb = new QueryBuilder('member');
- $sb->driver = new Driver;
- $sb->driver->configure('driver','mysql');
- $sb->driver->configure('trim',true);
- $sb->delete();
- $sb->whereFromArgs(array( 'foo' => '123' ));
+$driver = new Driver;
+$driver->configure('driver','mysql');
+$driver->configure('trim',true);
+$sb = new QueryBuilder($driver,'member');
+$sb->delete();
+$sb->whereFromArgs(array( 'foo' => '123' ));
- $sb->where()->equal('foo',123);
+$sb->where()->equal('foo',123);
- $sql = $sb->build(); // DELETE FROM member WHERE foo = 123
+$sql = $sb->build(); // DELETE FROM member WHERE foo = 123
```
## Migration Builder
```php
-<?php
$builder = new SQLBuilder\MigrationBuilder( $driver );
$sql = $builder->addColumn( 'members' ,
SQLBuilder\Column::create('price')
@@ -284,9 +337,23 @@ $sql = $builder->dropIndex( 'members', 'email_index' );
`PHPUnit_TestMore` is needed.
- curl -s http://install.onionphp.org/ | sh
- onion -d bundle
- phpunit tests
+```sh
+$ pear channel-discover pear.corneltek.com
+$ pear install corneltek/PHPUnit_TestMore
+```
+
+Install Universal package for the classloader:
+
+```sh
+curl -s http://install.onionphp.org/ | sh
+onion -d install
+```
+
+Copy the `phpunit.xml` file for your local configuration:
+
+```sh
+phpunit -c your-phpunit.xml tests
+```
## Reference
View
13 composer.json
@@ -0,0 +1,13 @@
+{
+ "name": "c9s/sqlbuilder",
+ "description": "Fast and Powerful SQLBuilder for PHP5.3",
+ "authors": [
+ {
+ "name": "c9s",
+ "email": "cornelius.howl@gmail.com"
+ }
+ ],
+ "require": {
+
+ }
+}
View
2 package.ini
@@ -1,6 +1,6 @@
[package]
name = SQLBuilder
-version = 1.3.8
+version = 1.3.11
desc = SQL Builder
author = Yo-An Lin <cornelius.howl@gmail.com>
channel = pear.corneltek.com
View
38 package.xml
@@ -10,11 +10,11 @@
<email>cornelius.howl@gmail.com</email>
<active>yes</active>
</lead>
- <date>2013-02-18</date>
- <time>13:01:04</time>
+ <date>2013-03-11</date>
+ <time>01:27:08</time>
<version>
- <release>1.3.8</release>
- <api>1.3.8</api>
+ <release>1.3.10</release>
+ <api>1.3.10</api>
</version>
<stability>
<release>stable</release>
@@ -25,25 +25,13 @@
<contents>
<dir name="/">
<file name="doc/migration.md" role="doc" md5sum="05e39d9440a65c671cc27338e256c1aa"/>
- <file name="tests/bootstrap.php" role="test" md5sum="f5b787a45ebd96de1740b65f8869eb2e"/>
- <file name="tests/DriverFactory.php" role="test" md5sum="31d5c73962bfa5037680a1c3d058f681"/>
- <file name="tests/PHPUnit/PDO/TestCase.php" role="test" md5sum="9921e93c938431d0b3ed74525610be83"/>
- <file name="tests/schema/member.sql" role="test" md5sum="785e1eda92eb16b775099f330eac66d5"/>
- <file name="tests/SQLBuilder/ColumnTest.php" role="test" md5sum="95b22665ab5a3a8489a1fbac99cf1cfb"/>
- <file name="tests/SQLBuilder/ExpressionTest.php" role="test" md5sum="46816c673c9e015babf1e0f5fd08250a"/>
- <file name="tests/SQLBuilder/InflatorTest.php" role="test" md5sum="d760bbee212601c69400f022e9b0369c"/>
- <file name="tests/SQLBuilder/JoinExpressionTest.php" role="test" md5sum="6df3281b347a6b0fb815738443a8d01c"/>
- <file name="tests/SQLBuilder/MigrationBuilderTest.php" role="test" md5sum="71153407d5154a26b39b0c741d1f2457"/>
- <file name="tests/SQLBuilder/PlaceHolderSQLiteTest.php" role="test" md5sum="1c7b83aac42b351ba2ccf8cac3460f42"/>
- <file name="tests/SQLBuilder/SQLBuilderMySQLTest.php" role="test" md5sum="40b50e879ef810604eb55431a76b5591"/>
- <file name="tests/SQLBuilder/SQLBuilderPgTest.php" role="test" md5sum="8af8cf09479f6d069060678229c0045b"/>
- <file name="tests/SQLBuilder/SQLBuilderSQLiteTest.php" role="test" md5sum="681c68825b8304957474823c156ce102"/>
<file name="src/SQLBuilder/BaseExpression.php" role="php" md5sum="6903d8f899c11ca4c6a4e2fe79f82833"/>
<file name="src/SQLBuilder/BetweenExpression.php" role="php" md5sum="a3cbdb36a5b06ea26bced558d22dda25"/>
<file name="src/SQLBuilder/Column.php" role="php" md5sum="94418d6cd1f78c3233f6c46506059360"/>
<file name="src/SQLBuilder/Driver.php" role="php" md5sum="4d57014434b079947240d9b22e817ac8"/>
- <file name="src/SQLBuilder/Expression.php" role="php" md5sum="df6ae41010ab73d94a89003ec5454326"/>
+ <file name="src/SQLBuilder/Expression.php" role="php" md5sum="499145627c9a2dd9de706c73bc179f48"/>
<file name="src/SQLBuilder/ExpressionGroup.php" role="php" md5sum="0a182f9a447c19360b9f3c910559a507"/>
+ <file name="src/SQLBuilder/InExpression.php" role="php" md5sum="9e70c9ebefb67da4d4facdc5e0985ed7"/>
<file name="src/SQLBuilder/Inflator.php" role="php" md5sum="a4c6e3331fe569cae3e35d26248e3f18"/>
<file name="src/SQLBuilder/JoinExpression.php" role="php" md5sum="93d805df26cef4814b41cabc493bb589"/>
<file name="src/SQLBuilder/MigrationBuilder.php" role="php" md5sum="cbe2ef365c7c9dca0bf395f9dca0ca4e"/>
@@ -69,25 +57,13 @@
<phprelease>
<filelist>
<install name="doc/migration.md" as="migration.md"/>
- <install name="tests/bootstrap.php" as="bootstrap.php"/>
- <install name="tests/DriverFactory.php" as="DriverFactory.php"/>
- <install name="tests/PHPUnit/PDO/TestCase.php" as="PHPUnit/PDO/TestCase.php"/>
- <install name="tests/schema/member.sql" as="schema/member.sql"/>
- <install name="tests/SQLBuilder/ColumnTest.php" as="SQLBuilder/ColumnTest.php"/>
- <install name="tests/SQLBuilder/ExpressionTest.php" as="SQLBuilder/ExpressionTest.php"/>
- <install name="tests/SQLBuilder/InflatorTest.php" as="SQLBuilder/InflatorTest.php"/>
- <install name="tests/SQLBuilder/JoinExpressionTest.php" as="SQLBuilder/JoinExpressionTest.php"/>
- <install name="tests/SQLBuilder/MigrationBuilderTest.php" as="SQLBuilder/MigrationBuilderTest.php"/>
- <install name="tests/SQLBuilder/PlaceHolderSQLiteTest.php" as="SQLBuilder/PlaceHolderSQLiteTest.php"/>
- <install name="tests/SQLBuilder/SQLBuilderMySQLTest.php" as="SQLBuilder/SQLBuilderMySQLTest.php"/>
- <install name="tests/SQLBuilder/SQLBuilderPgTest.php" as="SQLBuilder/SQLBuilderPgTest.php"/>
- <install name="tests/SQLBuilder/SQLBuilderSQLiteTest.php" as="SQLBuilder/SQLBuilderSQLiteTest.php"/>
<install name="src/SQLBuilder/BaseExpression.php" as="SQLBuilder/BaseExpression.php"/>
<install name="src/SQLBuilder/BetweenExpression.php" as="SQLBuilder/BetweenExpression.php"/>
<install name="src/SQLBuilder/Column.php" as="SQLBuilder/Column.php"/>
<install name="src/SQLBuilder/Driver.php" as="SQLBuilder/Driver.php"/>
<install name="src/SQLBuilder/Expression.php" as="SQLBuilder/Expression.php"/>
<install name="src/SQLBuilder/ExpressionGroup.php" as="SQLBuilder/ExpressionGroup.php"/>
+ <install name="src/SQLBuilder/InExpression.php" as="SQLBuilder/InExpression.php"/>
<install name="src/SQLBuilder/Inflator.php" as="SQLBuilder/Inflator.php"/>
<install name="src/SQLBuilder/JoinExpression.php" as="SQLBuilder/JoinExpression.php"/>
<install name="src/SQLBuilder/MigrationBuilder.php" as="SQLBuilder/MigrationBuilder.php"/>
View
18 phpunit.xml
@@ -2,15 +2,21 @@
<phpunit bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
+ stopOnFailure="true"
+ stopOnError="true"
verbose="true">
<php>
+ <env name="MYSQL_DSN" value="mysql:host=localhost;dbname=sqlbuilder_test"/>
+ <env name="MYSQL_USER" value="root"/>
+ <!--
+ <env name="MYSQL_DB" value="sqlbuilder_test"/>
+ -->
- <!-- MySQL Database Testing -->
- <var name="DB_MYSQL_USER" value="root"/>
- <var name="DB_MYSQL_PASSWD" value="123123"/>
-
- <var name="DB_PGSQL_USER" value="root"/>
- <var name="DB_PGSQL_PASSWD" value="123123"/>
+ <env name="PGSQL_DSN" value="pgsql:host=localhost;dbname=sqlbuilder_test"/>
+ <env name="PGSQL_USER" value="postgres"/>
+ <!--
+ <env name="PGSQL_DB" value="sqlbuilder_test"/>
+ -->
</php>
<testsuites>
View
17 src/SQLBuilder/Expression.php
@@ -57,6 +57,15 @@ public function like($c,$n)
return $this->setOp(array( $c, 'like', $n ));
}
+ public function in($c, $values)
+ {
+ $expr = new InExpression($c, $values);
+ $expr->builder = $this->builder;
+ $expr->driver = $this->driver;
+ $this->setOp($expr);
+ return $this;
+ }
+
public function greater($c,$n)
{
return $this->setOp(array( $c, '>', $n ));
@@ -140,17 +149,19 @@ public function toSql()
$sql .= $this->parentOp . ' ';
}
-
if ( $this->op ) {
if ( is_array( $this->op ) ) {
list($k,$op,$v) = $this->op;
if ( $this->driver->placeholder ) {
-
+ // we should not escape the value
+ // if the value is wrapped with an array
if( is_array($v) ) {
$sql .= $this->driver->getQuoteColumn($k) . ' ' . $op . ' ' . $v[0];
- } else {
+ }
+ else {
+ // escape as usuall
$newK = $this->builder->setPlaceHolderVar( $k , $v );
$sql .= $this->driver->getQuoteColumn($k) . ' ' . $op . ' ' . $this->driver->getPlaceHolder($newK);
}
View
33 src/SQLBuilder/InExpression.php
@@ -0,0 +1,33 @@
+<?php
+namespace SQLBuilder;
+
+class InExpression
+{
+ public $driver;
+ public $column;
+ public $values;
+
+ /**
+ * Expression class for SQL IN syntax
+ *
+ * A IN (1,2,3,4)
+ *
+ */
+ public function __construct($column,$values)
+ {
+ $this->column = $column;
+ $this->values = $values;
+ }
+
+ public function toSql()
+ {
+ $sql = $this->driver->getQuoteColumn($this->column) . ' IN ' ;
+ $escVals = array();
+ foreach( $this->values as $val ) {
+ $escVals[] = $this->driver->inflate( $val );
+ }
+ return $sql . '(' . join(', ', $escVals) . ')';
+ }
+
+}
+
View
20 src/SQLBuilder/QueryBuilder.php
@@ -23,7 +23,7 @@
*
* @code
*/
-class QueryBuilder
+class QueryBuilder
{
/**
* table name
@@ -115,15 +115,25 @@ class QueryBuilder
/**
+ * @param Driver $driver object
* @param string $table table name
*/
- public function __construct($table = null)
+ public function __construct()
{
- $this->table = $table;
+ $args = func_get_args();
+ if ( ! empty($args) ) {
+ if ( is_string($args[0]) ) {
+ $this->table = $args[0];
+ }
+ elseif ( $args[0] instanceof Driver ) {
+ $this->driver = $args[0];
+ if ( isset($args[1]) ) {
+ $this->table = $args[1];
+ }
+ }
+ }
}
-
-
/**
* set table name
*
View
43 tests/PHPUnit/PDO/TestCase.php
@@ -46,7 +46,7 @@
/**
* @var string database connection string (DSN)
*/
- public $dsn = 'sqlite::memory:';
+ public $dsn;
/**
* @var string database username
@@ -122,21 +122,33 @@ public function getDb()
return $this->pdo;
}
- public function setup()
+ public function setUp()
{
- if( ! extension_loaded('pdo') )
+ if( ! extension_loaded('pdo') )
return skip('pdo required');
// XXX: check pdo driver
# if( ! extension_loaded('pdo_pgsql') )
# return skip('pdo pgsql required');
- $this->pdo = new PDO(
- $this->getDSN(),
- $this->getUser(),
- $this->getPass(),
- $this->getOptions() ?: null
- );
+ if ( $this->getDSN() && $this->getUser() && $this->getPass() ) {
+ $this->pdo = new PDO(
+ $this->getDSN(),
+ $this->getUser(),
+ $this->getPass(),
+ $this->getOptions() ?: null
+ );
+ } elseif ( $this->getDSN() && $this->getUser() ) {
+ $this->pdo = new PDO( $this->getDSN(), $this->getUser() );
+ } elseif ( $this->getDSN() ) {
+ $this->pdo = new PDO( $this->getDSN() );
+ } else {
+ throw new Exception("Please define DSN for class: " . get_class($this) );
+ }
+
+ if ( ! $this->pdo ) {
+ throw new Exception("Can not create PDO connection: " . get_class($this) );
+ }
// throw Exception on Error.
$this->pdo->setAttribute( PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION );
@@ -162,9 +174,10 @@ public function setupSchema()
}
$content = file_get_contents($file);
- $statements = preg_split( '#;\s*$#', $content );
- foreach( $statements as $statement )
- $this->queryOk($statement);
+ $statements = preg_split( '#;\s*$#ms', $content );
+ foreach( $statements as $statement ) {
+ $this->queryOk(trim($statement));
+ }
}
}
@@ -222,10 +235,14 @@ public function schema()
*/
public function queryOk($sql, $args = null)
{
- if( $args ) {
+ if ( ! $sql )
+ return;
+
+ if ( $args ) {
$stm = $this->pdo->prepare( $sql )->execute( $args );
}
else {
+ // echo "Query: " , $sql, "\n";
$stm = $this->pdo->query( $sql );
}
$this->noPDOError();
View
9 tests/SQLBuilder/ExpressionTest.php
@@ -10,6 +10,15 @@ public function createExpr()
return $expr;
}
+
+ public function testInExpression()
+ {
+ $expr = $this->createExpr();
+ $expr->in( 'a' , array(1,2,3,4));
+ is( 'a IN (1, 2, 3, 4)', $expr->toSql() );
+ }
+
+
public function testOpIs()
{
$expr = $this->createExpr();
View
2 tests/SQLBuilder/MigrationBuilderTest.php
@@ -4,6 +4,8 @@
class MigrationBuilderTest extends PHPUnit_PDO_TestCase
{
+ public $dsn = 'sqlite::memory:';
+
function schema()
{
$sqls = array();;
View
9 tests/SQLBuilder/PlaceHolderSQLiteTest.php
@@ -2,11 +2,14 @@
class PlaceHolderSQLiteTest extends PHPUnit_PDO_TestCase
{
+
+ public $dsn = 'sqlite::memory:';
+
public $schema = array(
- 'member.sql'
+ 'member_sqlite.sql'
);
- function getDriver()
+ public function getDriver()
{
$d = new SQLBuilder\Driver;
$d->configure('driver','sqlite');
@@ -15,7 +18,7 @@ function getDriver()
return $d;
}
- function testCasting()
+ public function testCasting()
{
$sb = new SQLBuilder\QueryBuilder;
$sb->table('member');
View
16 tests/SQLBuilder/SQLBuilderMySQLTest.php
@@ -3,13 +3,14 @@
use SQLBuilder\QueryBuilder;
use SQLBuilder\Driver;
-class QueryBuilderMySQLTest extends PHPUnit_PDO_TestCase
+class SQLQueryBuilderMySQLTest extends PHPUnit_PDO_TestCase
{
- public $schema = array(
- 'member.sql',
- );
- function getDriver()
+ public $envVariablePrefix = 'MYSQL_';
+
+ public $schema = array( 'tests/schema/member_mysql.sql' );
+
+ public function getDriver()
{
$d = new Driver;
$d->configure('driver','mysql');
@@ -18,8 +19,7 @@ function getDriver()
return $d;
}
-
- function testInsert()
+ public function testInsert()
{
$sb = new QueryBuilder;
$sb->table('member');
@@ -43,7 +43,7 @@ function testInsert()
is( 'INSERT INTO member ( foo,bar) VALUES (?,?)' , $sql );
}
- function testDelete()
+ public function testDelete()
{
$sb = new QueryBuilder;
$sb->table('member');
View
24 tests/SQLBuilder/SQLBuilderPgTest.php
@@ -5,16 +5,14 @@
// class SQLBuilderPgTest extends PHPUnit_Framework_TestCase
class SQLBuilderPgTest extends PHPUnit_PDO_TestCase
{
- public $dsn = 'pgsql:host=localhost;dbname=testing';
- public $user = 'testing';
- public $pass = 'testing';
+ public $envVariablePrefix = 'PGSQL_';
- function schema()
+ public function schema()
{
$sqls = array();
$sqls[] =<<<EOS
-DROP SEQUENCE IF EXISTS "memberno_seq"
+DROP SEQUENCE IF EXISTS "memberno_seq" CASCADE
EOS;
$sqls[] =<<<EOS
@@ -42,13 +40,7 @@ function schema()
return $sqls;
}
- public function tearDown()
- {
- $this->pdo->query('drop table "Member" ');
- $this->pdo->query('drop sequence "memberno_seq" ');
- }
-
- function getPgDriver()
+ public function getPgDriver()
{
$driver = new SQLBuilder\Driver;
$driver->configure('driver','pgsql');
@@ -59,7 +51,7 @@ function getPgDriver()
return $driver;
}
- function testInsert()
+ public function testInsert()
{
$driver = $this->getPgDriver();
@@ -106,7 +98,7 @@ function testInsert()
ok( $record['MemberConfirm'] );
}
- function testDelete()
+ public function testDelete()
{
$driver = $this->getPgDriver();
$driver->configure('placeholder',null); // inflate values
@@ -124,7 +116,7 @@ function testDelete()
is( 'DELETE FROM "Member" WHERE "foo" = :foo' , $sql );
}
- function testUpdate()
+ public function testUpdate()
{
$d = new Driver;
$d->configure('driver','pgsql');
@@ -148,7 +140,7 @@ function testUpdate()
is( 'UPDATE "Member" SET "set1" = \'value1\' WHERE "cond1" = \':blah\'' , $sql );
}
- function testSelect()
+ public function testSelect()
{
$d = new Driver;
$d->configure('driver','pgsql');
View
4 tests/SQLBuilder/SQLBuilderSQLiteTest.php
@@ -19,7 +19,9 @@ public function execute()
class SQLBuilderSQLiteTest extends PHPUnit_PDO_TestCase
{
- public $schema = array( 'tests/schema/member.sql' );
+ public $schema = array( 'tests/schema/member_sqlite.sql' );
+
+ public $dsn = 'sqlite::memory:';
function getDriver()
{
View
4 tests/bootstrap.php
@@ -1,9 +1,9 @@
<?php
require 'PHPUnit/TestMore.php';
-require 'Universal/ClassLoader/BasePathClassLoader.php';
+require 'vendor/pear/Universal/ClassLoader/BasePathClassLoader.php';
require 'tests/DriverFactory.php';
require 'tests/PHPUnit/PDO/TestCase.php';
$loader = new \Universal\ClassLoader\BasePathClassLoader(array('src','vendor/pear'));
$loader->useIncludePath(true);
// prepend to the spl class loader list
-$loader->register(true);
+$loader->register(true); // use prepend
View
8 tests/schema/member_mysql.sql
@@ -0,0 +1,8 @@
+DROP TABLE IF EXISTS member;
+CREATE TABLE member (
+ id integer primary key auto_increment,
+ name varchar(128) ,
+ phone varchar(128) ,
+ country varchar(128),
+ confirmed boolean
+);
View
2 tests/schema/member.sql → tests/schema/member_sqlite.sql
@@ -1,5 +1,5 @@
CREATE TABLE member (
- id integer primary key autoincrement,
+ id integer primary key autoincrement,
name varchar(128) ,
phone varchar(128) ,
country varchar(128),

0 comments on commit 1aaa5a6

Please sign in to comment.