change usage of Tie::IxHash
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Types.pm
1 use MooseX::Declare;
2 class SQL::Translator::Types {
3     use MooseX::Types::Moose qw(ArrayRef CodeRef HashRef Int Maybe Str Undef);
4     use MooseX::Types::Parameterizable qw(Parameterizable);
5     use MooseX::Types -declare, [qw(Column Constraint ForeignKey Index PrimaryKey Procedure Schema Sequence Table Trigger View
6                                     Bit DBIHandle MatchType Parser Producer Translator DBICSchema IxHash
7                                     ColumnHash ConstraintHash IndexHash ProcedureHash SequenceHash TableHash TriggerHash ViewHash)];
8     use Tie::IxHash;
9
10     class_type DBICSchema, { class => 'DBIx::Class::Schema' };
11     
12     class_type Column, { class => 'SQL::Translator::Object::Column' };
13     class_type Constraint, { class => 'SQL::Translator::Object::Constraint' };
14     class_type ForeignKey, { class => 'SQL::Translator::Object::ForeignKey' };
15     class_type Index, { class => 'SQL::Translator::Object::Index' };
16     class_type PrimaryKey, { class => 'SQL::Translator::Object::PrimaryKey' };
17     class_type Procedure, { class => 'SQL::Translator::Object::Procedure' };
18     class_type Schema, { class => 'SQL::Translator::Object::Schema' };
19     class_type Sequence, { class=> 'SQL::Translator::Object::Sequence' };
20     class_type Table, { class => 'SQL::Translator::Object::Table' };
21     class_type Trigger, { class => 'SQL::Translator::Object::Trigger' };
22     class_type View, { class => 'SQL::Translator::Object::View' };
23     
24     class_type Parser, { class => 'SQL::Translator::Parser' };
25     class_type Producer, { class => 'SQL::Translator::Producer' };
26     class_type Translator, { class => 'SQL::Translator' };
27
28     coerce Column, from HashRef, via { SQL::Translator::Object::Column->new($_) };
29     coerce Constraint, from HashRef, via { SQL::Translator::Object::Constraint->new($_) };
30     coerce ForeignKey, from HashRef, via { SQL::Translator::Object::ForeignKey->new($_) };
31     coerce Index, from HashRef, via { SQL::Translator::Object::Index->new($_) };
32     coerce PrimaryKey, from HashRef, via { SQL::Translator::Object::PrimaryKey->new($_) };
33     coerce Procedure, from HashRef, via { SQL::Translator::Object::Procedure->new($_) };
34     coerce Schema, from HashRef, via { SQL::Translator::Object::Schema->new($_) };
35     coerce Sequence, from HashRef, via { SQL::Translator::Object::Sequence->new($_) };
36     coerce Table, from HashRef, via { SQL::Translator::Object::Table->new($_) };
37     coerce Trigger, from HashRef, via { SQL::Translator::Object::Trigger->new($_) };
38     coerce View, from HashRef, via { SQL::Translator::Object::View->new($_) };
39
40     coerce ArrayRef, from Str, via { [ $_ ] };
41
42     subtype MatchType, as Str, where { /^(full|partial|simple)$/ || $_ eq '' };
43     coerce MatchType, from Str, via { lc $_ };
44
45     subtype Bit, as Int, where { $_ == 1 || $_ == 0 };
46     coerce Bit,
47         from Undef, via { 0 },
48         from Str, via { length() ? 1 : 0 };
49
50     subtype DBIHandle, as 'DBI::db';
51     
52     coerce DBIHandle,
53         from Str,
54         via(\&_coerce_dbihandle_from_str),
55         from ArrayRef,
56         via(\&_coerce_dbihandle_from_arrayref);
57         from CodeRef,
58         via(\&_coerce_dbihandle_from_coderef);
59     
60     sub coerce_dbihandle_from_str { }
61     sub coerce_dbihandle_from_arrayref { }
62     sub coerce_dbihandle_from_coderef { }
63
64     subtype IxHash, as 'Tie::IxHash';
65     coerce IxHash, from HashRef, via { Tie::IxHash->new($_) };
66
67     subtype ColumnHash, as Parameterizable[IxHash, Maybe[Column]];
68     subtype ConstraintHash, as Parameterizable[IxHash, Maybe[Constraint]];
69     subtype IndexHash, as Parameterizable[IxHash, Maybe[Index]];
70     subtype ProcedureHash, as Parameterizable[IxHash, Maybe[Procedure]];
71     subtype SequenceHash, as Parameterizable[IxHash, Maybe[Sequence]];
72     subtype TableHash, as Parameterizable[IxHash, Maybe[Table]];
73     subtype TriggerHash, as Parameterizable[IxHash, Maybe[Trigger]];
74     subtype ViewHash, as Parameterizable[IxHash, Maybe[View]];
75 }