blob: e2b225cad3de3735b3d884fdde95b04104815347 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
<?php
namespace SMW\MediaWiki\Connection;
use SMW\SQLStore\SQLStore;
use RuntimeException;
/**
* @license GNU GPL v2+
* @since 3.0
*
* @author mwjames
*/
class Sequence {
/**
* @var Database
*/
private $connection;
/**
* @var string
*/
private $tablePrefix;
/**
* @since 3.0
*/
public function __construct( $connection ) {
if ( !$connection instanceof Database && !$connection instanceof \DatabaseBase ) {
throw new RuntimeException( "Invalid connection instance!" );
}
$this->connection = $connection;
}
/**
* @since 3.0
*/
public function tablePrefix( $tablePrefix = '' ) {
$this->tablePrefix = $tablePrefix;
}
/**
* @since 3.0
*
* @param string $tableName
* @param string $field
*
* @return string
*/
public static function makeSequence( $table, $field ) {
return "{$table}_{$field}_seq";
}
/**
* @since 3.0
*
* @param string $tableName
* @param string $field
*
* @return integer
*/
public function restart( $table, $field ) {
if ( $this->connection->getType() !== 'postgres' ) {
return;
}
if ( $this->tablePrefix !== null ) {
$this->connection->tablePrefix( $this->tablePrefix );
}
$seq_num = $this->connection->selectField( $table, "max({$field})", [], __METHOD__ );
$seq_num += 1;
$sequence = self::makeSequence( $table, $field );
$this->connection->onTransactionIdle( function() use( $sequence, $seq_num ) {
$this->connection->query( "ALTER SEQUENCE {$sequence} RESTART WITH {$seq_num}", __METHOD__ );
} );
return $seq_num;
}
}
|