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
|
include <ctype.h>
include <tbset.h>
include "tjoin.h"
# RENAMECOL -- Rename a column to make its name unique
#
# If the name of the column pointed to by that index is unique, it is output
# as the new name. If it is not unique, a suffix of the form "_i" is appended
# to the name, where i is a digit which (hopefully) makes the name unique.
#
# B.Simon 03-Nov-87 first code
# B.Simon 04-Sep-90 Replaced call to strncmp with streq
# B.Simon 16-Apr-99 Revised version to work with tjoin
procedure renamecol (tji, jtab, jcol, colname, maxch)
pointer tji[2] # i: Array of table info descriptors
int jtab # i: Index of table containing column
int jcol # i: Index of column within table
char colname # u: Column name
int maxch # i: Max length of column name
#--
bool before
int olen, nmatch, nbefore, itab, icol
pointer sp, oldnam, tmpnam, errtxt
string notuniq "Cannot create a unique column name (%s)"
bool streq()
int strlen()
begin
# Allocate dynamic memory for strings
call smark (sp)
call salloc (oldnam, SZ_COLNAME, TY_CHAR)
call salloc (tmpnam, SZ_COLNAME, TY_CHAR)
call salloc (errtxt, SZ_LINE, TY_CHAR)
# Copy name to temporrary variable
call strcpy (colname, Memc[oldnam], SZ_COLNAME)
call strupr (Memc[oldnam])
# See if the name is unique, and if not, how many columns with
# the same name precede this one
nmatch = 0
nbefore = 0
before = true
do itab = 1, 2 {
do icol = 1, TJ_DNUM(tji[itab]) {
call tbcigt (TJ_DCOL(tji[itab],icol), TBL_COL_NAME,
Memc[tmpnam], SZ_COLNAME)
call strupr (Memc[tmpnam])
if (streq (Memc[tmpnam], Memc[oldnam])) {
nmatch = nmatch + 1
if (before)
nbefore = nbefore + 1
}
if (itab == jtab && icol == jcol)
before = false
}
}
# If the name is not unique, add a suffix of the form "_i"
if (nmatch > 1) {
# Check for ridiculous values of maxch
olen = min (maxch-2, strlen(Memc[oldnam]))
if (olen < 1) {
call sprintf (Memc[errtxt], SZ_LINE, notuniq)
call pargstr (Memc[oldnam])
call error (1, Memc[errtxt])
}
# Add the suffix
Memc[oldnam+olen] = '_'
Memc[oldnam+olen+1] = TO_DIGIT (nbefore)
Memc[oldnam+olen+2] = EOS
# Make sure it is unique
do itab = 1, 2 {
do icol = 1, TJ_DNUM(tji[itab]) {
call tbcigt (TJ_DCOL(tji[itab],icol), TBL_COL_NAME,
Memc[tmpnam], SZ_COLNAME)
if (streq (Memc[oldnam], Memc[tmpnam])) {
call sprintf (Memc[errtxt], SZ_LINE, notuniq)
call pargstr (Memc[oldnam])
call error (1, Memc[errtxt])
}
}
}
}
# Copy to the output string
call strcpy (Memc[oldnam], colname, maxch)
call sfree (sp)
end
|