Skip to content

Commit 5cd2f33

Browse files
author
Igor V. Gulyaev
committed
* merge master
2 parents 6cda990 + 9505da2 commit 5cd2f33

File tree

136 files changed

+5756
-2247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+5756
-2247
lines changed

core/Cache/PeclMemcached.class.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class PeclMemcached extends CachePeer
2323
const DEFAULT_HOST = '127.0.0.1';
2424
const DEFAULT_TIMEOUT = 1;
2525

26+
protected $host = null;
27+
protected $port = null;
2628
private $instance = null;
2729
private $requestTimeout = null;
2830
private $connectTimeout = null;
29-
private $host = null;
30-
private $port = null;
3131
private $triedConnect = false;
3232

3333
/**
@@ -193,21 +193,8 @@ protected function ensureTriedToConnect()
193193
return $this;
194194

195195
$this->triedConnect = true;
196-
$this->instance = new Memcache();
197196

198-
try {
199-
200-
try {
201-
$this->instance->pconnect($this->host, $this->port, $this->connectTimeout);
202-
} catch (BaseException $e) {
203-
$this->instance->connect($this->host, $this->port, $this->connectTimeout);
204-
}
205-
206-
$this->alive = true;
207-
208-
} catch (BaseException $e) {
209-
// bad luck.
210-
}
197+
$this->connect();
211198

212199
return $this;
213200
}
@@ -235,4 +222,23 @@ protected function store(
235222
Assert::isUnreachable();
236223
}
237224

225+
protected function connect()
226+
{
227+
$this->instance = new Memcache();
228+
229+
try {
230+
231+
try {
232+
$this->instance->pconnect($this->host, $this->port, $this->connectTimeout);
233+
} catch (BaseException $e) {
234+
$this->instance->connect($this->host, $this->port, $this->connectTimeout);
235+
}
236+
237+
$this->alive = true;
238+
239+
} catch (BaseException $e) {
240+
// bad luck.
241+
}
242+
}
238243
}
244+
?>

core/DB/DB.class.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ abstract class DB
2020
const FULL_TEXT_OR = 2;
2121

2222
protected $link = null;
23+
protected $dialect = null;
2324

2425
protected $persistent = false;
2526

@@ -62,6 +63,11 @@ abstract public function queryCount(Query $query);
6263

6364
// actually set's encoding
6465
abstract public function setDbEncoding();
66+
67+
/**
68+
* @return Dialect
69+
*/
70+
abstract protected function spawnDialect();
6571

6672
public function __destruct()
6773
{
@@ -74,9 +80,10 @@ public function __destruct()
7480
}
7581
}
7682

77-
public static function getDialect()
83+
public function getDialect()
7884
{
79-
throw new UnimplementedFeatureException('implement me, please');
85+
return $this->dialect = $this->dialect
86+
?: ($this->spawnDialect()->setDB($this));
8087
}
8188

8289
/**

core/DB/Dialect.class.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,40 @@
1515
* @ingroup DB
1616
* @ingroup Module
1717
**/
18-
abstract class /* ANSI's */ Dialect
19-
extends Singleton
20-
implements Instantiatable
21-
{
18+
abstract class /* ANSI's */ Dialect {
2219
const LITERAL_NULL = 'NULL';
2320
const LITERAL_TRUE = 'TRUE';
2421
const LITERAL_FALSE = 'FALSE';
2522

23+
/**
24+
* @var DB
25+
*/
26+
protected $db = null;
27+
2628
abstract public function preAutoincrement(DBColumn $column);
2729
abstract public function postAutoincrement(DBColumn $column);
2830

2931
abstract public function hasTruncate();
3032
abstract public function hasMultipleTruncate();
3133
abstract public function hasReturning();
3234

35+
abstract public function quoteValue($value);
36+
3337
/**
34-
must be implemented too:
35-
36-
public static function quoteValue($value);
38+
* @deprecated remove after onPHP 1.2+
39+
* @return LiteDialect
3740
**/
41+
public static function me()
42+
{
43+
throw new UnimplementedFeatureException('Deprecated: dialects not extends Singleton now');
44+
}
3845

39-
public static function quoteField($field)
46+
public function quoteField($field)
4047
{
41-
return self::quoteTable($field);
48+
return $this->quoteTable($field);
4249
}
4350

44-
public static function quoteTable($table)
51+
public function quoteTable($table)
4552
{
4653
return '"'.$table.'"';
4754
}
@@ -67,6 +74,16 @@ public static function dropTableMode($cascade = false)
6774
: ' RESTRICT';
6875
}
6976

77+
/**
78+
* @param DB $db
79+
* @return Dialect
80+
*/
81+
public function setDB(DB $db)
82+
{
83+
$this->db = $db;
84+
return $this;
85+
}
86+
7087
public function quoteBinary($data)
7188
{
7289
return $this->quoteValue($data);
@@ -159,5 +176,16 @@ public function quoteIpInRange($range, $ip)
159176
{
160177
throw new UnimplementedFeatureException();
161178
}
179+
180+
protected function getLink()
181+
{
182+
if (!$this->db)
183+
throw new WrongStateException('Expected setted db');
184+
if (!$this->db->isConnected()) {
185+
$this->db->connect();
186+
}
187+
188+
return $this->db->getLink();
189+
}
162190
}
163191
?>

core/DB/ImaginaryDialect.class.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
**/
1818
final class ImaginaryDialect extends Dialect
1919
{
20+
private static $self = null;
21+
2022
/**
2123
* @return ImaginaryDialect
2224
**/
2325
public static function me()
2426
{
25-
return Singleton::getInstance(__CLASS__);
27+
if (!self::$self) {
28+
self::$self = new self();
29+
}
30+
return self::$self;
2631
}
2732

2833
public function preAutoincrement(DBColumn $column)
@@ -35,17 +40,17 @@ public function postAutoincrement(DBColumn $column)
3540
return 'AUTOINCREMENT';
3641
}
3742

38-
public static function quoteValue($value)
43+
public function quoteValue($value)
3944
{
4045
return $value;
4146
}
4247

43-
public static function quoteField($field)
48+
public function quoteField($field)
4449
{
4550
return $field;
4651
}
4752

48-
public static function quoteTable($table)
53+
public function quoteTable($table)
4954
{
5055
return $table;
5156
}

core/DB/LiteDialect.class.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,9 @@
1616
*
1717
* @ingroup DB
1818
**/
19-
final class LiteDialect extends Dialect
19+
class LiteDialect extends Dialect implements Instantiatable
2020
{
21-
/**
22-
* @return LiteDialect
23-
**/
24-
public static function me()
25-
{
26-
return Singleton::getInstance(__CLASS__);
27-
}
28-
29-
public static function quoteValue($value)
21+
public function quoteValue($value)
3022
{
3123
/// @see Sequenceless for this convention
3224

core/DB/LitePDODialect.class.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2012 by Aleksey S. Denisov *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
/**
13+
* SQLite dialect.
14+
*
15+
* @see http://www.sqlite.org/
16+
*
17+
* @ingroup DB
18+
**/
19+
class LitePDODialect extends LiteDialect
20+
{
21+
public function quoteValue($value)
22+
{
23+
/// @see Sequenceless for this convention
24+
25+
if ($value instanceof Identifier && !$value->isFinalized())
26+
return 'null';
27+
28+
if (Assert::checkInteger($value))
29+
return $value;
30+
31+
return $this->getLink()->quote($value);
32+
}
33+
34+
public function quoteBinary($data)
35+
{
36+
//here must be PDO::PARAM_LOB, but i couldn't get success result, so used base64_encode/decode
37+
return $this->getLink()->quote(base64_encode($data), PDO::PARAM_STR);
38+
}
39+
40+
public function unquoteBinary($data)
41+
{
42+
try {
43+
return base64_decode($data);
44+
} catch (Exception $e) {
45+
throw new UnimplementedFeatureException('Wrong encoding, if you get it, throw correct exception');
46+
}
47+
}
48+
49+
/**
50+
* @return PDO
51+
*/
52+
protected function getLink() {
53+
return parent::getLink();
54+
}
55+
}
56+
?>

core/DB/MyDialect.class.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,17 @@ class MyDialect extends Dialect
2121
{
2222
const IN_BOOLEAN_MODE = 1;
2323

24-
/**
25-
* @return MyDialect
26-
**/
27-
public static function me()
28-
{
29-
return Singleton::getInstance(__CLASS__);
30-
}
31-
32-
public static function quoteValue($value)
24+
public function quoteValue($value)
3325
{
3426
/// @see Sequenceless for this convention
3527

3628
if ($value instanceof Identifier && !$value->isFinalized())
3729
return "''"; // instead of 'null', to be compatible with v. 4
3830

39-
return "'" . mysql_real_escape_string($value) . "'";
31+
return "'" . mysql_real_escape_string($value, $this->getLink()) . "'";
4032
}
4133

42-
public static function quoteField($field)
34+
public function quoteField($field)
4335
{
4436
if (strpos($field, '.') !== false)
4537
throw new WrongArgumentException();
@@ -49,7 +41,7 @@ public static function quoteField($field)
4941
return "`{$field}`";
5042
}
5143

52-
public static function quoteTable($table)
44+
public function quoteTable($table)
5345
{
5446
return "`{$table}`";
5547
}

core/DB/MyImprovedDialect.class.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,7 @@
1919
**/
2020
final class MyImprovedDialect extends MyDialect
2121
{
22-
/**
23-
* @return MyImprovedDialect
24-
**/
25-
public static function me($link = null)
26-
{
27-
return Singleton::getInstance(__CLASS__, $link);
28-
}
29-
30-
public static function quoteValue($value)
22+
public function quoteValue($value)
3123
{
3224
/// @see Sequenceless for this convention
3325

@@ -38,7 +30,7 @@ public static function quoteValue($value)
3830
"'"
3931
.mysqli_real_escape_string(
4032
// can't find better way atm.
41-
DBPool::me()->getLink()->getLink(),
33+
$this->getLink(),
4234
$value
4335
)
4436
."'";

core/DB/MySQL.class.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@
1919
**/
2020
final class MySQL extends Sequenceless
2121
{
22-
/**
23-
* @return MyDialect
24-
**/
25-
public static function getDialect()
26-
{
27-
return MyDialect::me();
28-
}
29-
3022
/**
3123
* @return MySQL
3224
**/
@@ -265,6 +257,14 @@ protected function getInsertId()
265257
return mysql_insert_id($this->link);
266258
}
267259

260+
/**
261+
* @return MyDialect
262+
**/
263+
protected function spawnDialect()
264+
{
265+
return new MyDialect();
266+
}
267+
268268
private function checkSingle($result)
269269
{
270270
if (mysql_num_rows($result) > 1)

0 commit comments

Comments
 (0)