1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-31 03:10:14 +02:00

improved coding style

This commit is contained in:
David Grudl
2015-06-19 03:11:36 +02:00
parent 462ef6934b
commit ca99b0b822
55 changed files with 297 additions and 306 deletions

View File

@@ -11,11 +11,10 @@ require __DIR__ . '/../dibi/dibi.php';
echo '<p>Connecting to Sqlite: ';
try {
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -26,11 +25,10 @@ echo "</p>\n";
echo '<p>Connecting to Sqlite: ';
try {
$connection = new DibiConnection(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -42,7 +40,6 @@ echo '<p>Connecting to MySQL: ';
try {
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=cp1250');
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -53,18 +50,17 @@ echo "</p>\n";
echo '<p>Connecting to MySQLi: ';
try {
dibi::connect(array(
'driver' => 'mysqli',
'host' => 'localhost',
'driver' => 'mysqli',
'host' => 'localhost',
'username' => 'root',
'password' => 'xxx',
'database' => 'dibi',
'options' => array(
MYSQLI_OPT_CONNECT_TIMEOUT => 30
'options' => array(
MYSQLI_OPT_CONNECT_TIMEOUT => 30,
),
'flags' => MYSQLI_CLIENT_COMPRESS,
'flags' => MYSQLI_CLIENT_COMPRESS,
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -75,13 +71,12 @@ echo "</p>\n";
echo '<p>Connecting to ODBC: ';
try {
dibi::connect(array(
'driver' => 'odbc',
'driver' => 'odbc',
'username' => 'root',
'password' => '***',
'dsn' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq='.__DIR__.'/data/sample.mdb',
'dsn' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq='.__DIR__.'/data/sample.mdb',
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -92,12 +87,11 @@ echo "</p>\n";
echo '<p>Connecting to PostgreSql: ';
try {
dibi::connect(array(
'driver' => 'postgre',
'string' => 'host=localhost port=5432 dbname=mary',
'driver' => 'postgre',
'string' => 'host=localhost port=5432 dbname=mary',
'persistent' => TRUE,
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -108,11 +102,10 @@ echo "</p>\n";
echo '<p>Connecting to Sqlite via PDO: ';
try {
dibi::connect(array(
'driver' => 'pdo',
'dsn' => 'sqlite2::memory:',
'driver' => 'pdo',
'dsn' => 'sqlite2::memory:',
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -123,13 +116,12 @@ echo "</p>\n";
echo '<p>Connecting to MS SQL: ';
try {
dibi::connect(array(
'driver' => 'mssql',
'host' => 'localhost',
'driver' => 'mssql',
'host' => 'localhost',
'username' => 'root',
'password' => 'xxx',
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -140,14 +132,13 @@ echo "</p>\n";
echo '<p>Connecting to MS SQL 2005: ';
try {
dibi::connect(array(
'driver' => 'mssql2005',
'host' => '(local)',
'driver' => 'mssql2005',
'host' => '(local)',
'username' => 'Administrator',
'password' => 'xxx',
'database' => 'main',
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
@@ -158,13 +149,12 @@ echo "</p>\n";
echo '<p>Connecting to Oracle: ';
try {
dibi::connect(array(
'driver' => 'oracle',
'driver' => 'oracle',
'username' => 'root',
'password' => 'xxx',
'database' => 'db',
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

View File

@@ -8,7 +8,7 @@ require __DIR__ . '/../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
@@ -37,7 +37,7 @@ foreach ($table->getColumns() as $column) {
echo "</ul>\n";
echo "Indexes";
echo 'Indexes';
echo "<ul>\n";
foreach ($table->getIndexes() as $index) {
echo "<li>{$index->name} " . ($index->primary ? 'primary ' : '') . ($index->unique ? 'unique' : '') . ' (';

View File

@@ -8,7 +8,7 @@ require __DIR__ . '/../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));

View File

@@ -12,7 +12,7 @@ Tracy\Debugger::enable();
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));

View File

@@ -8,7 +8,7 @@ require __DIR__ . '/../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));

View File

@@ -8,7 +8,7 @@ require __DIR__ . '/../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
@@ -32,13 +32,13 @@ dibi::test('
// if & else & (optional) end
dibi::test("
dibi::test('
SELECT *
FROM people
WHERE id > 0
%if", ($foo > 0), "AND foo=?", $foo, "
%else %if", ($bar > 0), "AND bar=?", $bar, "
");
%if', ($foo > 0), 'AND foo=?', $foo, '
%else %if', ($bar > 0), 'AND bar=?', $bar, '
');
// -> SELECT * FROM people WHERE id > 0 AND bar=2

View File

@@ -10,7 +10,7 @@ date_default_timezone_set('Europe/Prague');
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
@@ -45,26 +45,26 @@ $array = array(
'brand' => NULL,
'created' => new DateTime,
);
dibi::test("INSERT INTO products", $array, $array, $array);
dibi::test('INSERT INTO products', $array, $array, $array);
// -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
// dibi detects UPDATE command
dibi::test("
UPDATE colors SET", array(
dibi::test('
UPDATE colors SET', array(
'color' => 'blue',
'order' => 12,
), "
WHERE id=?", 123);
), '
WHERE id=?', 123);
// -> UPDATE colors SET [color]='blue', [order]=12 WHERE id=123
// modifier applied to array
$array = array(1, 2, 3);
dibi::test("
dibi::test('
SELECT *
FROM people
WHERE id IN (?)", $array
WHERE id IN (?)', $array
);
// -> SELECT * FROM people WHERE id IN ( 1, 2, 3 )
@@ -74,11 +74,11 @@ $order = array(
'field1' => 'asc',
'field2' => 'desc',
);
dibi::test("
dibi::test('
SELECT *
FROM people
ORDER BY %by", $order, "
");
ORDER BY %by', $order, '
');
// -> SELECT * FROM people ORDER BY [field1] ASC, [field2] DESC

View File

@@ -14,7 +14,7 @@ date_default_timezone_set('Europe/Prague');
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
@@ -27,7 +27,7 @@ $res->setType('customer_id', Dibi::INTEGER)
->setFormat(dibi::DATETIME, 'Y-m-d H:i:s');
Tracy\Dumper::dump( $res->fetch() );
Tracy\Dumper::dump($res->fetch());
// outputs:
// DibiRow(3) {
// customer_id => 1
@@ -38,7 +38,7 @@ Tracy\Dumper::dump( $res->fetch() );
// using auto-detection (works well with MySQL or other strictly typed databases)
$res = dibi::query('SELECT * FROM [customers]');
Tracy\Dumper::dump( $res->fetch() );
Tracy\Dumper::dump($res->fetch());
// outputs:
// DibiRow(3) {
// customer_id => 1

View File

@@ -16,11 +16,11 @@ Tracy\Debugger::enable();
$connection = dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => array(
'run' => TRUE,
)
),
));

View File

@@ -18,11 +18,11 @@ Tracy\Debugger::enable();
$connection = dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => array(
'run' => TRUE,
)
),
));
@@ -35,4 +35,4 @@ $panel->register($connection);
dibi::query('SELECT 123');
// result set will be dumped
Tracy\Debugger::barDump( dibi::fetchAll('SELECT * FROM customers WHERE customer_id < ?', 38), '[customers]' );
Tracy\Debugger::barDump(dibi::fetchAll('SELECT * FROM customers WHERE customer_id < ?', 38), '[customers]');

View File

@@ -11,7 +11,7 @@ date_default_timezone_set('Europe/Prague');
// CHANGE TO REAL PARAMETERS!
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'formatDate' => "'Y-m-d'",
'formatDateTime' => "'Y-m-d H-i-s'",
@@ -19,10 +19,10 @@ dibi::connect(array(
// generate and dump SQL
dibi::test("
INSERT INTO [mytable]", array(
'id' => 123,
'date' => new DateTime('12.3.2007'),
dibi::test('
INSERT INTO [mytable]', array(
'id' => 123,
'date' => new DateTime('12.3.2007'),
'stamp' => new DateTime('23.1.2007 10:23'),
)
);

View File

@@ -12,14 +12,13 @@ Tracy\Debugger::enable();
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
// using the "prototype" to add custom method to class DibiResult
DibiResult::extensionMethod('fetchShuffle', function(DibiResult $obj)
{
DibiResult::extensionMethod('fetchShuffle', function (DibiResult $obj) {
$all = $obj->fetchAll();
shuffle($all);
return $all;

View File

@@ -10,15 +10,15 @@ date_default_timezone_set('Europe/Prague');
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
$id = 10;
$record = array(
'title' => 'Super product',
'price' => 318,
'title' => 'Super product',
'price' => 318,
'active' => TRUE,
);

View File

@@ -8,7 +8,7 @@ require __DIR__ . '/../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));

View File

@@ -10,7 +10,7 @@ date_default_timezone_set('Europe/Prague');
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
// enable query logging to this file
'profiler' => array(
@@ -26,13 +26,12 @@ try {
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < ?', 5);
$res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < ?', 38);
} catch (DibiException $e) {
echo '<p>', get_class($e), ': ', $e->getMessage(), '</p>';
}
// outputs a log file
echo "<h2>File data/log.sql:</h2>";
echo '<h2>File data/log.sql:</h2>';
echo '<pre>', file_get_contents('data/log.sql'), '</pre>';

View File

@@ -10,16 +10,16 @@ require __DIR__ . '/../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => array(
'run' => TRUE,
)
),
));
// execute some queries...
for ($i=0; $i<20; $i++) {
for ($i = 0; $i < 20; $i++) {
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < ?', $i);
}

View File

@@ -8,7 +8,7 @@ require __DIR__ . '/../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));
@@ -16,7 +16,7 @@ dibi::connect(array(
// create new substitution :blog: ==> wp_
dibi::getSubstitutes()->blog = 'wp_';
dibi::test("SELECT * FROM [:blog:items]");
dibi::test('SELECT * FROM [:blog:items]');
// -> SELECT * FROM [wp_items]

View File

@@ -8,7 +8,7 @@ require __DIR__ . '/../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite3',
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));