/
Informix.pm
executable file
·1225 lines (1049 loc) · 34.3 KB
/
Informix.pm
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Rose::DB::Object::Metadata::Auto::Informix;
use strict;
use Carp();
use Rose::DB::Object::Metadata::ForeignKey;
use Rose::DB::Object::Metadata::UniqueKey;
use Rose::DB::Object::Metadata::Auto;
our @ISA = qw(Rose::DB::Object::Metadata::Auto);
our $VERSION = '0.60';
# syscolumns.coltype constants taken from:
#
# http://www-306.ibm.com/software/data/informix/pubs/library/datablade/dbdk/sqlr/01.fm14.html
use constant CHAR => 0;
use constant SMALLINT => 1;
use constant INTEGER => 2;
use constant FLOAT => 3;
use constant SMALLFLOAT => 4;
use constant DECIMAL => 5;
use constant SERIAL => 6;
use constant DATE => 7;
use constant MONEY => 8;
use constant NULL => 9;
use constant DATETIME => 10;
use constant BYTE => 11;
use constant TEXT => 12;
use constant VARCHAR => 13;
use constant INTERVAL => 14;
use constant NCHAR => 15;
use constant NVARCHAR => 16;
use constant INT8 => 17;
use constant SERIAL8 => 18;
use constant SET => 19;
use constant MULTISET => 20;
use constant LIST => 21;
use constant ROW => 22;
use constant COLLECTION => 23;
use constant ROWREF => 24;
use constant VARIABLE_LENGTH_OPAQUE => 40; # Variable-length opaque type
use constant FIXED_LENGTH_OPAQUE => 41; # Fixed-length opaque type
use constant NAMED_ROW_TYPE => 4118; # Named row type
# Map the Informix column type constants to type names that we can
# handle--or that are at least in our format: lowercase text.
my %Column_Types =
(
CHAR() => 'char',
SMALLINT() => 'int',
INTEGER() => 'int',
FLOAT() => 'float',
SMALLFLOAT() => 'float',
DECIMAL() => 'decimal',
SERIAL() => 'serial',
DATE() => 'date',
MONEY() => 'decimal',
NULL() => 'null',
DATETIME() => 'datetime',
BYTE() => 'byte',
TEXT() => 'text',
VARCHAR() => 'varchar',
INTERVAL() => 'interval',
NCHAR() => 'char',
NVARCHAR() => 'varchar',
INT8() => 'int',
SERIAL8() => 'bigserial',
SET() => 'set',
MULTISET() => 'multiset',
LIST() => 'list',
ROW() => 'row',
COLLECTION() => 'collection',
ROWREF() => 'rowref',
);
# http://www-306.ibm.com/software/data/informix/pubs/library/datablade/dbdk/sqlr/01.fm14.html
my %Column_Length =
(
# Data Type Length (in bytes)
SMALLINT() => 2,
INTEGER() => 4,
INT8() => 8,
);
# http://www-306.ibm.com/software/data/informix/pubs/library/datablade/dbdk/sqlr/01.fm14.html
my %Datetime_Qualifiers =
(
0 => 'year',
2 => 'month',
4 => 'day',
6 => 'hour',
8 => 'minute',
10 => 'second',
11 => 'fraction(1)',
12 => 'fraction(2)',
13 => 'fraction(3)',
14 => 'fraction(4)',
15 => 'fraction(5)',
);
# $INFORMIXDIR/etc/xpg4_is.sql
# http://www-306.ibm.com/software/data/informix/pubs/library/datablade/dbdk/sqlr/01.fm50.html
my %Numeric_Precision =
(
SMALLINT() => 5,
INTEGER() => 10,
FLOAT() => 63,
SMALLFLOAT() => 32,
);
# These constants are from the DBI documentation. Is there somewhere
# I can load these from?
use constant SQL_NO_NULLS => 0;
use constant SQL_NULLABLE => 1;
sub auto_generate_columns
{
my($self) = shift;
my($class, %columns, $table_id);
eval
{
require DBD::Informix::Metadata;
$class = $self->class or die "Missing class!";
my $db = $self->db;
my $dbh = $db->dbh or die $db->error;
# Informix does not support DBI's column_info() method so we have
# to get all that into "the hard way."
#
# Each item in @col_list is a reference to an array of values:
#
# 0 owner name
# 1 table name
# 2 column number
# 3 column name
# 4 data type (encoded)
# 5 data length (encoded)
#
# Lowercase table because Rose::DB::Informix->likes_lowercase_table_names
my @col_list = DBD::Informix::Metadata::ix_columns($dbh, lc $self->table);
# We'll also need to query the syscolumns table directly to get the
# table id, which we need to query the sysdefaults table. But to get
# the correct syscolumns record, we need to first query the systables
# table.
my $st_sth = $dbh->prepare(<<"EOF");
SELECT tabid FROM informix.systables WHERE tabname = ? AND owner = ?
EOF
my %col_info;
foreach my $item (@col_list)
{
# We're going to build a standard DBI column_info() data structure
# to pass on to the rest of the code.
my $col_info;
# Add the "proprietary" values using the DBI convention of lowercase
# names prefixed with DBD name.
my @keys = map { "informix_$_" }
qw(owner table column_number column_name column_type column_length);
@$col_info{@keys} = @$item;
# Copy the "easy" values into the standard DBI locations
$col_info->{'TABLE_NAME'} = $col_info->{'informix_table'};
$col_info->{'COLUMN_NAME'} = $col_info->{'informix_column_name'};
# Query the systables table to get the table id based on the
# table name and owner name.
$st_sth->execute(@$col_info{qw(informix_table informix_owner)});
$table_id = $st_sth->fetchrow_array;
unless(defined $table_id)
{
die "Could not find informix.systables record for table '",
$col_info->{'informix_table'}, "' with owner '",
$col_info->{'informix_owner'}, "'";
}
$col_info->{'informix_tabid'} = $table_id;
# Store the column info by column name
$col_info{$col_info->{'COLUMN_NAME'}} = $col_info;
}
# We need to query the syscolumns table directly to get the
# table id, which we need to query the sysdefaults table.
my $sc_sth = $dbh->prepare(<<"EOF");
SELECT * FROM informix.syscolumns WHERE tabid = ?
EOF
# We may need to query the sysxtdtypes table, so reserve a
# variable for that statement handle. We'll also cache the
# results, so we'll set up that hash here too. We'll also
# need a mapping from "colno" to column name.
my($sxt_sth, %extended_type, %colno_to_name);
# Query the syscolumns table to get somemore column information
$sc_sth->execute($table_id);
while(my $sc_row = $sc_sth->fetchrow_hashref)
{
my $col_info = $col_info{$sc_row->{'colname'}}
or die "No column info found for column name '$sc_sth->{'colname'}'";
# Copy all the row values into the DBI column info using the DBI
# convention of lowercase names prefixed with DBD name.
@$col_info{map { "informix_$_" } keys %$sc_row} = values %$sc_row;
# Store mapping from "colno" to column name
$colno_to_name{$sc_row->{'colno'}} = $sc_row->{'colname'};
##
## Painfully derive the data type name (TYPE_NAME)
##
# If the coltype is a value greater than 256, the the column does
# not allow null values. To determine the data type for a coltype
# column that contains a value greater than 256, subtract 256 from the
# value and evaluate the remainder, based on the possible coltype
# values. For example, if a column has a coltype value of 262,
# subtracting 256 from 262 leaves a remainder of 6, which indicates
# that this column uses a SERIAL data type.
my $type_num;
if($sc_row->{'coltype'} > 256)
{
$col_info->{'informix_type_num'} = $type_num =
$sc_row->{'coltype'} - 256;
# This situation also indicates that the column is NOT NULL,
# so set all the DBI-style attributes to indicate that.
$col_info->{'IS_NULLABLE'} = 'NO';
$col_info->{'NULLABLE'} = SQL_NO_NULLS;
}
else
{
$col_info->{'informix_type_num'} = $type_num = $sc_row->{'coltype'};
$col_info->{'IS_NULLABLE'} = 'YES';
$col_info->{'NULLABLE'} = SQL_NULLABLE;
}
#
# Now we need to turn $type_num into a type name. Hold on to your hat.
#
my $type_name;
# The following data types are implemented by the database server
# as built-in opaque types: BLOB, BOOLEAN, CLOB, and LVARCHAR
#
# A built-in opaque data type is one for which the database server
# provides the type definition. Because these data types are built-in
# opaque types, they do not have a unique coltype value. Instead, they
# have one of the coltype values for opaque types: 41 (fixed-length
# opaque type), or 40 (varying-length opaque type). The different
# fixed-length opaque types are distinguished by the extended_id column
# in the sysxtdtypes system catalog table.
#
# The following table summarizes the coltype values for the predefined
# data types.
#
# Type coltype symbolic constant
# -------- ------- -----------------
# BLOB 41 FIXED_LENGTH_OPAQUE
# CLOB 41 FIXED_LENGTH_OPAQUE
# BOOLEAN 41 FIXED_LENGTH_OPAQUE
# LVARCHAR 40 VARIABLE_LENGTH_OPAQUE
# BLOB, CLOB, or BOOLEAN
if($type_num == FIXED_LENGTH_OPAQUE)
{
# Maybe we already looked this one up
if($extended_type{$sc_row->{'extended_id'}})
{
$type_name = $extended_type{$col_info->{'informix_extended_id'}};
}
else # look it up and cache it
{
$sxt_sth ||=
$dbh->prepare("SELECT name FROM informix.sysxtdtypes WHERE extended_id = ?");
$sxt_sth->execute($sc_row->{'extended_id'});
my $name = $sxt_sth->fetchrow_array;
# We only handle BOOLEANS specially, and the name column for
# booleans is already in our type name format: "boolean"
# So just copy the name value into the cache.
$type_name = $extended_type{$sc_row->{'extended_id'}} = $name;
}
}
elsif($type_num == VARIABLE_LENGTH_OPAQUE) # LVARCHAR
{
$type_name = 'varchar';
}
elsif($type_num == DATETIME)
{
# Determine the full "datetime X to Y" type string
$type_name = _ix_datetime_specific_type($type_num, $sc_row->{'collength'});
}
else
{
$type_name = $Column_Types{$type_num};
}
# Finally, set the type name
$col_info->{'TYPE_NAME'} = $type_name;
#
# Mine column length for information
#
# COLUMN_SIZE is the maximum length in characters for character data
# types, the number of digits or bits for numeric data types or the
# length in the representation of temporal types. See the relevant
# specifications for detailed information.
$col_info->{'COLUMN_SIZE'} =
_ix_max_length($type_num, $sc_row->{'collength'});
if($type_num == SMALLINT || $type_num == INTEGER ||
$type_num == SERIAL || $type_num == DECIMAL ||
$type_num == MONEY)
{
$col_info->{'DECIMAL_DIGITS'} =
_ix_numeric_scale($type_num, $sc_row->{'collength'});
$col_info->{'COLUMN_SIZE'} =
_ix_numeric_precision($type_num, $sc_row->{'collength'});
$col_info->{'NUM_PREC_RADIX'} =
_ix_numeric_precision_radix($type_num, $sc_row->{'collength'});
}
}
#
# Get all the column default values from the sysdefaults table
#
# class 'T' means "table" (the other possible value us "t" for "row type")
# http://www-306.ibm.com/software/data/informix/pubs/library/datablade/dbdk/sqlr/01.fm16.html
my $sd_sth = $dbh->prepare(<<"EOF");
SELECT * FROM informix.sysdefaults WHERE tabid = ? AND class = 'T'
EOF
$sd_sth->execute($table_id);
while(my $sd_row = $sd_sth->fetchrow_hashref)
{
my $col_name = $colno_to_name{$sd_row->{'colno'}}
or die "While getting defaults: no column name found for colno '$sd_row->{'colno'}'";
my $col_info = $col_info{$col_name}
or die "While getting defaults: no column info found for column '$col_name'";
# The "type" column of the sysdefaults table looks like this:
#
# type CHAR(1)
#
# 'L' = Literal default
# 'U' = User
# 'C' = Current
# 'N' = Null
# 'T' = Today
# 'S' = Dbservername
#
# If a literal is specified for the default value, it is stored in
# the default column as text. If the literal value is not of type
# CHAR, the default column consists of two parts. The first part is
# the 6-bit representation of the binary value of the default-value
# structure. The second part is the default value in English text.
# The two parts are separated by a space.
#
# If the data type of the column is not CHAR or VARCHAR, a binary
# representation is encoded in the default column.
if($sd_row->{'type'} eq 'T')
{
$col_info->{'COLUMN_DEF'} = 'today';
}
elsif($sd_row->{'type'} eq 'C')
{
$col_info->{'COLUMN_DEF'} = 'current';
}
elsif($sd_row->{'type'} eq 'L')
{
if($col_info->{'informix_type_num'} == CHAR)
{
$col_info->{'COLUMN_DEF'} = $sd_row->{'default'};
}
else
{
# The first part is the 6-bit representation of the binary value
# of the default-value structure. The second part is the default
# value in English text. The two parts are separated by a space.
my $default = $sd_row->{'default'};
$default =~ s/^.+ //; # cheat by just looking for the space
$col_info->{'COLUMN_DEF'} = $default;
}
}
}
# Finally, generate the columns based on the DBI-like $col_info
# that we built in the previous steps.
foreach my $col_info (values %col_info)
{
$db->refine_dbi_column_info($col_info);
$columns{$col_info->{'COLUMN_NAME'}} =
$self->auto_generate_column($col_info->{'COLUMN_NAME'}, $col_info);
}
};
if($@ || !keys %columns)
{
Carp::croak "Could not auto-generate columns for class $class - $@";
}
$self->auto_alias_columns(values %columns);
return wantarray ? values %columns : \%columns;
}
sub auto_retrieve_primary_key_column_names
{
my($self) = shift;
unless(defined wantarray)
{
Carp::croak "Useless call to auto_retrieve_primary_key_column_names() in void context";
}
my($class, @columns);
eval
{
require DBD::Informix::Metadata;
$class = $self->class or die "Missing class!";
my $db = $self->db;
my $dbh = $db->dbh or die $db->error;
# We need the table owner. Asking for column information is the only
# way I know of to reliably get this information.
#
# Informix does not support DBI's column_info() method so we have
# to get all that into "the hard way."
#
# Each item in @col_list is a reference to an array of values:
#
# 0 owner name
# 1 table name
# 2 column number
# 3 column name
# 4 data type (encoded)
# 5 data length (encoded)
#
my @col_list = DBD::Informix::Metadata::ix_columns($dbh, lc $self->table);
my $owner = $col_list[0][0];
my $table = $col_list[0][1]; # just in case...
unless(defined $owner)
{
die "Could not find owner for table ", $self->table;
}
# Then comes this monster query to get the primary key column names.
# I'd love to know a better/easier way to do this...
my $pk_sth = $dbh->prepare(<<'EOF');
SELECT
col.colname
FROM
informix.sysconstraints con,
informix.systables tab,
informix.sysindexes idx,
informix.syscolumns col
WHERE
constrtype = 'P' AND
con.tabid = tab.tabid AND
con.tabid = idx.tabid AND
con.tabid = col.tabid AND
con.idxname = idx.idxname
AND
(
col.colno = idx.part1 OR
col.colno = idx.part2 OR
col.colno = idx.part3 OR
col.colno = idx.part4 OR
col.colno = idx.part5 OR
col.colno = idx.part6 OR
col.colno = idx.part7 OR
col.colno = idx.part8 OR
col.colno = idx.part9 OR
col.colno = idx.part10 OR
col.colno = idx.part11 OR
col.colno = idx.part12 OR
col.colno = idx.part13 OR
col.colno = idx.part14 OR
col.colno = idx.part15 OR
col.colno = idx.part16
)
AND
tab.tabname = ? AND
tab.owner = ?
EOF
$pk_sth->execute($table, $owner);
my $column;
$pk_sth->bind_columns(\$column);
while($pk_sth->fetch)
{
push(@columns, $column);
}
};
if($@ || !@columns)
{
$@ = 'no primary key coumns found' unless(defined $@);
Carp::croak "Could not auto-retrieve primary key columns for class $class - $@";
}
return wantarray ? @columns : \@columns;
}
sub auto_generate_unique_keys
{
my($self) = shift;
unless(defined wantarray)
{
Carp::croak "Useless call to auto_generate_unique_keys() in void context";
}
my($class, %unique_keys);
eval
{
require DBD::Informix::Metadata;
$class = $self->class or die "Missing class!";
my $db = $self->db;
my $dbh = $db->dbh or die $db->error;
# We need the table id. To get it, we need the "owner" name. Asking
# for column information is the only way I know of to reliably get
# this information.
#
# Informix does not support DBI's column_info() method so we have
# to get all that into "the hard way."
#
# Each item in @col_list is a reference to an array of values:
#
# 0 owner name
# 1 table name
# 2 column number
# 3 column name
# 4 data type (encoded)
# 5 data length (encoded)
#
my @col_list = DBD::Informix::Metadata::ix_columns($dbh, lc $self->table);
# Here's the query for the table id
my $st_sth = $dbh->prepare(<<"EOF");
SELECT tabid FROM informix.systables WHERE tabname = ? AND owner = ?
EOF
# Take the info from the first column (arbitrarily selected)
# table name owner name
$st_sth->execute($col_list[0][1], $col_list[0][0]);
my $table_id = $st_sth->fetchrow_array;
unless(defined $table_id)
{
die "Could not find informix.systables record for table ",
"'$col_list[0][1]' with owner '$col_list[0][0]'";
}
# Then comes this monster query to get the unique key column names.
# (The subquery fithers out any primary keys.) I'd love to know a
# better/easier way to do this...
my $uk_sth = $dbh->prepare(<<'EOF');
SELECT
col.colname,
idx.idxname
FROM
informix.sysindexes idx,
informix.syscolumns col
WHERE
idx.tabid = ? AND
idx.idxtype = 'U' AND
idx.tabid = col.tabid
AND
(
col.colno = idx.part1 OR
col.colno = idx.part2 OR
col.colno = idx.part3 OR
col.colno = idx.part4 OR
col.colno = idx.part5 OR
col.colno = idx.part6 OR
col.colno = idx.part7 OR
col.colno = idx.part8 OR
col.colno = idx.part9 OR
col.colno = idx.part10 OR
col.colno = idx.part11 OR
col.colno = idx.part12 OR
col.colno = idx.part13 OR
col.colno = idx.part14 OR
col.colno = idx.part15 OR
col.colno = idx.part16
)
AND NOT EXISTS
(
SELECT * FROM
informix.sysconstraints con
WHERE
con.tabid = ? AND
con.constrtype = 'P' AND
con.idxname = idx.idxname
);
EOF
$uk_sth->execute($table_id, $table_id);
my($column, $key);
$uk_sth->bind_columns(\$column, \$key);
while($uk_sth->fetch)
{
my $uk = $unique_keys{$key} ||=
Rose::DB::Object::Metadata::UniqueKey->new(name => $key, parent => $self);
$uk->add_column($column);
}
};
if($@)
{
Carp::croak "Could not auto-retrieve unique keys for class $class - $@";
}
# This sort order is part of the API, and is essential to make the
# test suite work.
my @uk = map { $unique_keys{$_} } sort map { lc } keys(%unique_keys);
return wantarray ? @uk : \@uk;
}
use constant FK_INDEXES_SQL =><<'EOF';
SELECT
c1.constrname referring_constraint_name,
c1.owner referring_constraint_owner,
c1.idxname referring_index_name,
t2.tabid referring_table_id,
t2.owner referring_table_owner,
t2.tabname referring_table_name,
c2.constrname referred_constraint_name,
c2.owner referred_constraint_owner,
c2.idxname referred_index_name,
t1.tabid referred_table_id,
t1.owner referred_table_owner,
t1.tabname referred_table_name
FROM
informix.sysreferences r,
informix.sysconstraints c1,
informix.sysconstraints c2,
informix.systables t1,
informix.systables t2
WHERE
c1.constrtype = 'R' AND
c1.tabid = t2.tabid AND
c1.constrid = r.constrid AND
r.ptabid = t1.tabid AND
r.primary = c2.constrid AND
t2.tabid = ?
EOF
use constant INDEX_COLUMNS_SQL =><<'EOF';
SELECT
tabname,
idxname,
c1.colname col1,
c2.colname col2,
c3.colname col3,
c4.colname col4,
c5.colname col5,
c6.colname col6,
c7.colname col7,
c8.colname col8,
c9.colname col9,
c10.colname col10,
c11.colname col11,
c12.colname col12,
c13.colname col13,
c14.colname col14,
c15.colname col15,
c16.colname col16
FROM
informix.systables t,
informix.syscolumns c1,
OUTER(informix.syscolumns c2),
OUTER(informix.syscolumns c3),
OUTER(informix.syscolumns c4),
OUTER(informix.syscolumns c5),
OUTER(informix.syscolumns c6),
OUTER(informix.syscolumns c7),
OUTER(informix.syscolumns c8),
OUTER(informix.syscolumns c9),
OUTER(informix.syscolumns c10),
OUTER(informix.syscolumns c11),
OUTER(informix.syscolumns c12),
OUTER(informix.syscolumns c13),
OUTER(informix.syscolumns c14),
OUTER(informix.syscolumns c15),
OUTER(informix.syscolumns c16),
informix.sysindexes i
WHERE
t.tabtype = 'T' AND
t.tabid = i.tabid AND
i.tabid = c1.tabid AND
i.tabid = c2.tabid AND
i.tabid = c3.tabid AND
i.tabid = c4.tabid AND
i.tabid = c5.tabid AND
i.tabid = c6.tabid AND
i.tabid = c7.tabid AND
i.tabid = c8.tabid AND
i.tabid = c9.tabid AND
i.tabid = c10.tabid AND
i.tabid = c11.tabid AND
i.tabid = c12.tabid AND
i.tabid = c13.tabid AND
i.tabid = c14.tabid AND
i.tabid = c15.tabid AND
i.tabid = c16.tabid AND
part1 = c1.colno AND
part2 = c2.colno AND
part3 = c3.colno AND
part4 = c4.colno AND
part5 = c5.colno AND
part6 = c6.colno AND
part7 = c7.colno AND
part8 = c8.colno AND
part9 = c9.colno AND
part10 = c10.colno AND
part11 = c11.colno AND
part12 = c12.colno AND
part13 = c13.colno AND
part14 = c14.colno AND
part15 = c15.colno AND
part16 = c16.colno AND
t.tabid = ? AND
i.idxname = ?
ORDER BY
idxname
EOF
sub auto_generate_foreign_keys
{
my($self, %args) = @_;
unless(defined wantarray)
{
Carp::croak "Useless call to auto_generate_foreign_keys() in void context";
}
my $no_warnings = $args{'no_warnings'};
my($class, @foreign_keys, $total_fks);
eval
{
$class = $self->class or die "Missing class!";
my $db = $self->db;
my $dbh = $db->dbh or die $db->error;
# I'm doing this to get the table id and owner. Gotta be a better way...
my @col_list = DBD::Informix::Metadata::ix_columns($dbh, lc $self->table);
my $st_sth = $dbh->prepare(<<"EOF");
SELECT tabid FROM informix.systables WHERE tabname = ? AND owner = ?
EOF
# Each item in @col_list is a reference to an array of values:
#
# 0 owner name
# 1 table name
# 2 column number
# 3 column name
# 4 data type (encoded)
# 5 data length (encoded)
#
my $table_name = $col_list[0][1];
my $owner_name = $col_list[0][0];
$st_sth->execute($table_name, $owner_name);
my $table_id = $st_sth->fetchrow_array;
my $col_sth = $dbh->prepare(INDEX_COLUMNS_SQL);
my $sth = $dbh->prepare(FK_INDEXES_SQL);
$sth->execute($table_id);
my %fk;
my $cm = $self->convention_manager;
FK: while(my $index_info = $sth->fetchrow_hashref)
{
# Sanity check - should never happen
unless(lc $self->table eq lc $index_info->{'referring_table_name'} &&
lc $owner_name eq lc $index_info->{'referring_table_owner'})
{
Carp::confess
"Fatal mismatch between table ('", lc $self->table, "' vs. '",
lc $index_info->{'referring_table_name'}, "' and/or owner ('",
lc $owner_name, "' vs. '", lc $index_info->{'referring_table_owner'},
"')";
}
my $key_name = $index_info->{'referring_index_name'};
my $foreign_table = $index_info->{'referred_table_name'};
# Get local columns
$col_sth->execute(@$index_info{qw(referring_table_id referring_index_name)});
my $local_cols_info = $col_sth->fetchrow_hashref;
my @local_cols = grep { defined && /\S/ } @$local_cols_info{map { "col$_" } 1 .. 16};
# Get foreign columns
$col_sth->execute(@$index_info{qw(referred_table_id referred_index_name)});
my $foreign_cols_info = $col_sth->fetchrow_hashref;
my @foreign_cols = grep { defined && /\S/ } @$foreign_cols_info{map { "col$_" } 1 .. 16};
# Another sanity check - should never happen
unless(@local_cols > 0 && @local_cols == @foreign_cols)
{
Carp::confess "Failed to extract matching sets of foreign key ",
"columns for table $table_name";
}
my $foreign_class = $self->class_for(table => $foreign_table);
unless($foreign_class)
{
# Add deferred task
$self->add_deferred_task(
{
class => $self->class,
method => 'auto_init_foreign_keys',
args => \%args,
code => sub
{
$self->auto_init_foreign_keys(%args);
$self->make_foreign_key_methods(%args, preserve_existing => 1);
},
check => sub
{
my $fks = $self->foreign_keys;
return @$fks == $total_fks ? 1 : 0;
}
});
unless($no_warnings || $self->allow_auto_initialization)
{
no warnings; # Allow undef coercion to empty string
warn "No Rose::DB::Object-derived class found for table ",
"'$foreign_table'";
}
$total_fks++;
next FK;
}
my %key_columns;
@key_columns{@local_cols} = @foreign_cols;
if(my $alt_key_name = $cm->auto_foreign_key_name($foreign_class, $key_name))
{
$key_name = $alt_key_name;
}
elsif($key_name !~ /^[a-zA-Z]\w*$/)
{
$key_name = undef;
}
my $fk =
Rose::DB::Object::Metadata::ForeignKey->new(
name => $key_name,
class => $foreign_class,
key_columns => \%key_columns);
push(@foreign_keys, $fk);
$total_fks++;
}
# This step is important! It ensures that foreign keys will be created
# in a deterministic order, which in turn allows the "auto-naming" of
# foreign keys to work in a predictible manner. This exact sort order
# (lowercase table name comparisons) is part of the API for foreign
# key auto generation.
#
# XXX: This is somewhat superseded by the convention manager, but it
# XXX: doesn't hurt to leave it here.
@foreign_keys =
sort { lc $a->class->meta->table cmp lc $b->class->meta->table }
@foreign_keys;
foreach my $fk (@foreign_keys)
{
my $name = $self->foreign_key_name_generator->($self, $fk);
unless(defined $name && $name =~ /^\w+$/)
{
die "Missing or invalid key name '$name' for foreign key ",
"generated in $class for ", $fk->class;
}
$fk->name($name);
}
};
if($@)
{
Carp::croak "Could not auto-generate foreign keys for class $class - $@";
}
@foreign_keys = sort { lc $a->name cmp lc $b->name } @foreign_keys;
return wantarray ? @foreign_keys : \@foreign_keys;
}
#
# Crazy Informix helper functions
#
# Helper functions from $INFORMIXDIR/etc/xpg4_is.sql
# http://www-306.ibm.com/software/data/informix/pubs/library/datablade/dbdk/sqlr/01.fm50.html
#
# In call cases, the "coltype" argument is replaces with the already-
# adjusted $type_num, so ignore the subtraction of 256 in all of the
# pasted code.
# create procedure 'informix'.ansinumprec(coltype smallint, collength smallint)
# returning int;
#
# { FLOAT and SMALLFLOAT precisions are in bits }
#
# if (coltype >= 256) then
# let coltype = coltype - 256;
# end if;
#
# if (coltype = 1) then -- smallint
# return 5;
# elif (coltype = 2) or (coltype = 6) then -- int
# return 10;
# elif (coltype = 3) then -- float
# return 64;
# elif (coltype = 4) then -- smallfloat
# return 32;
# elif (coltype = 5) or (coltype = 8) then -- decimal
# return (trunc(collength / 256));
# else
# return NULL;
# end if;
# end procedure
# document
# 'returns the precision of a numeric column',
# 'Synopsis: ansinumprec(smallint, smallint) returns int';
sub _ix_numeric_precision
{
my($type_num, $collength) = @_;
if(exists $Numeric_Precision{$type_num})