coerce from Column from Str
[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                    from Str,     via { SQL::Translator::Object::Column->new( name => $_ ) };
30     coerce Constraint, from HashRef, via { SQL::Translator::Object::Constraint->new($_) };
31     coerce ForeignKey, from HashRef, via { SQL::Translator::Object::ForeignKey->new($_) };
32     coerce Index, from HashRef, via { SQL::Translator::Object::Index->new($_) };
33     coerce PrimaryKey, from HashRef, via { SQL::Translator::Object::PrimaryKey->new($_) };
34     coerce Procedure, from HashRef, via { SQL::Translator::Object::Procedure->new($_) };
35     coerce Schema, from HashRef, via { SQL::Translator::Object::Schema->new($_) };
36     coerce Sequence, from HashRef, via { SQL::Translator::Object::Sequence->new($_) };
37     coerce Table, from HashRef, via { SQL::Translator::Object::Table->new($_) };
38     coerce Trigger, from HashRef, via { SQL::Translator::Object::Trigger->new($_) };
39     coerce View, from HashRef, via { SQL::Translator::Object::View->new($_) };
40
41     coerce ArrayRef, from Str, via { [ $_ ] };
42
43     subtype MatchType, as Str, where { /^(full|partial|simple)$/ || $_ eq '' };
44     coerce MatchType, from Str, via { lc $_ };
45
46     subtype Bit, as Int, where { $_ == 1 || $_ == 0 };
47     coerce Bit,
48         from Undef, via { 0 },
49         from Str, via { length() ? 1 : 0 };
50
51     subtype DBIHandle, as 'DBI::db';
52     
53     coerce DBIHandle,
54         from Str,
55         via(\&_coerce_dbihandle_from_str),
56         from ArrayRef,
57         via(\&_coerce_dbihandle_from_arrayref);
58         from CodeRef,
59         via(\&_coerce_dbihandle_from_coderef);
60     
61     sub coerce_dbihandle_from_str { }
62     sub coerce_dbihandle_from_arrayref { }
63     sub coerce_dbihandle_from_coderef { }
64
65     subtype IxHash, as 'Tie::IxHash';
66     coerce IxHash, from HashRef, via { Tie::IxHash->new($_) };
67
68     subtype ColumnHash, as Parameterizable[IxHash, Maybe[Column]];
69     subtype ConstraintHash, as Parameterizable[IxHash, Maybe[Constraint]];
70     subtype IndexHash, as Parameterizable[IxHash, Maybe[Index]];
71     subtype ProcedureHash, as Parameterizable[IxHash, Maybe[Procedure]];
72     subtype SequenceHash, as Parameterizable[IxHash, Maybe[Sequence]];
73     subtype TableHash, as Parameterizable[IxHash, Maybe[Table]];
74     subtype TriggerHash, as Parameterizable[IxHash, Maybe[Trigger]];
75     subtype ViewHash, as Parameterizable[IxHash, Maybe[View]];
76 }