This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from ezsystems/ezoracle
/
ora-update-seqs.php
221 lines (192 loc) · 6.83 KB
/
ora-update-seqs.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env php
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
#
# Updates all sequences in a given DB to contain maximum value in
# related table column: seq.val = select max(col) from related_table;
#
# You might need to run this script after bulk data loads.
#
# Usage: ora-update-seqs [--exact] <login_string>
# For example,
# ora-update-seqs scott/tiger@orcl
##############################################################################
define( 'TRIGGER_REGEXP_1',
'/BEGIN\s+' .
'IF :new.\S+ is null THEN\s+' .
'SELECT (\S+).nextval INTO :new.(\S+) FROM dual;\s+' .
'END IF;\s+' .
'END;/' );
define( 'TRIGGER_REGEXP_2',
'/BEGIN\s+' .
'SELECT (\S+).nextval INTO :new.(\S+) FROM dual;\s+' .
'END;/' );
##############################################################################
function oraParseLoginString( $loginString, &$oraUser, &$oraPass, &$oraInst )
{
if ( !preg_match( '|^(\S+)/(\S+)@(\S+)$|', $loginString, $matches ) )
return false;
$oraUser = $matches[1];
$oraPass = $matches[2];
$oraInst = $matches[3];
return true;
}
##############################################################################
function oraFetchTriggersInfo( $oraDB )
{
$triggers = array();
$query = "SELECT trigger_name,table_name,trigger_body FROM user_triggers WHERE table_name NOT LIKE 'BIN$%'";
$statement = OCIParse( $oraDB, $query );
OCIExecute( $statement );
while ( OCIFetchInto( $statement, $row,
OCI_ASSOC+OCI_RETURN_LOBS+OCI_RETURN_NULLS ) )
{
$triggers[] = array(
'table_name' => $row['TABLE_NAME'],
'trigger_body' => $row['TRIGGER_BODY'],
'trigger_name' => $row['TRIGGER_NAME'],
);
}
OCIFreeStatement( $statement );
return $triggers;
}
##############################################################################
function getSequences( $triggers )
{
$seqs = array();
foreach ( $triggers as $triggerParams )
{
//$tableName = $triggerParams['table_name'];
$triggerBody = $triggerParams['trigger_body'];
//echo "$tableName - $triggerBody\n";
if ( preg_match( TRIGGER_REGEXP_1, $triggerBody, $matches ) ||
preg_match( TRIGGER_REGEXP_2, $triggerBody, $matches ) )
{
$sequenceName = $matches[1];
$tableCol = $matches[2];
}
else
continue;
$seqs[$sequenceName] = array( $triggerParams['table_name'], $tableCol, $triggerParams['trigger_name'] );
}
ksort($seqs);
return $seqs;
}
##############################################################################
function oraSelectOneVar( $oraDB, $query )
{
$val = false;
if( !( $statement = OCIParse( $oraDB, $query ) ) )
return false;
if( OCIExecute( $statement ) )
{
OCIFetchInto( $statement, $row,
OCI_NUM+OCI_RETURN_LOBS+OCI_RETURN_NULLS );
$val = $row[0];
}
OCIFreeStatement( $statement );
return $val;
}
##############################################################################
function oraDoQuery( $oraDB, $query )
{
if ( !( $statement = OCIParse( $oraDB, $query ) ) )
return false;
$rc = OCIExecute( $statement );
OCIFreeStatement( $statement );
return $rc;
}
##############################################################################
function oraUpdateSeqence( $oraDB, $seq, $table, $col, $trig = '', $exact = false )
{
$curColVal = oraSelectOneVar( $oraDB, "SELECT MAX($col) FROM \"$table\"" );
// nb: fetching CURRVAL will not work if the sequence was never used before (ora error)
// this means that for empty tables we must first fetch the first value (1) of the sequence,
// then reset it back to 0 - unless we skip resetting the sequence altogether
if ( $curColVal === null && !$exact ) // empty table: no need to increment sequence
return;
$curColVal = (int)$curColVal;
$curSeqVal = oraSelectOneVar( $oraDB, "SELECT $seq.nextval FROM DUAL" );
$inc = $curColVal - $curSeqVal;
if( (!$exact && $inc <= 0) || ($exact && $inc === 0) ) // no need to increment: sequence is above table or at same level
return;
// work around a php 5.2.5 segfault with oci instant client 10.1.0.4 connected to oracle xe 10.2.0.1:
// sequences cannot be altered in any meaningful way
oraDoQuery( $oraDB, "DROP sequence $seq" );
$rslt = oraDoQuery( $oraDB, "CREATE SEQUENCE $seq MINVALUE ".($curColVal+1) );
if ( $rslt && $trig != '' )
{
$rslt = oraDoQuery( $oraDB, "ALTER TRIGGER $trig COMPILE" );
}
$rslt = $rslt ? "ok" : "**FAILED**";
printf( "updating %30s: %10s\n", $seq, $rslt );
}
##############################################################################
function oraUpdateSequences( $oraDB, $seqs, $exact )
{
if ( count($seqs) == 0 )
{
echo "No auto_increment sequences found.\n";
return;
}
foreach( $seqs as $seq => $tableData )
{
list( $table, $col, $trig ) = $tableData;
oraUpdateSeqence( $oraDB, $seq, $table, $col, $trig, $exact );
}
}
##############################################################################
/**
Shows command line arguments syntax and terminates script.
*/
function showUsage( $argv )
{
echo "\n";
echo "Usage: $argv[0] [options] <login_string>\n";
echo "Options:\n";
echo "\t--exact: lower the next value of sequences that are above table max value, too\n";
echo "\n";
exit( 1 );
}
##############################################################################
error_reporting( E_ALL );
$loginString = '';
$optExact = false;
if ( $argc < 2 )
showUsage( $argv );
// parse command line options
foreach ( array_slice( $argv, 1 ) as $arg )
{
if ( preg_match ( '/^--(.*)$/', $arg, $matches ) ) // an option
{
$option = $matches[1];
switch ( $option )
{
case 'exact':
$optExact = true;
break;
default:
echo "Unknown option: $option.\n";
showUsage( $argv );
}
}
elseif ( !$loginString )
$loginString = $arg;
}
$oraUser = '';
$oraPass = '';
$oraInst = ''; // oracle instance
if ( !oraParseLoginString( $loginString, $oraUser, $oraPass, $oraInst ) )
die( "Malformed login string: $argv[1]\n" );
if ( !function_exists( 'OCILogon' ) )
die( "Oci8 extension not activated, cannot execute\n" );
if( !( $oraDB = OCILogon( $oraUser, $oraPass, $oraInst ) ) )
die( "cannot connect to Oracle\n" );
$triggers = oraFetchTriggersInfo( $oraDB );
$seqs = getSequences( $triggers );
oraUpdateSequences( $oraDB, $seqs, $optExact );
OCILogOff( $oraDB );
?>