coerce from Column from Str
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Types.pm
CommitLineData
a8eeeefc 1use MooseX::Declare;
2class SQL::Translator::Types {
59109131 3 use MooseX::Types::Moose qw(ArrayRef CodeRef HashRef Int Maybe Str Undef);
a499b4bd 4 use MooseX::Types::Parameterizable qw(Parameterizable);
ada068ed 5 use MooseX::Types -declare, [qw(Column Constraint ForeignKey Index PrimaryKey Procedure Schema Sequence Table Trigger View
a499b4bd 6 Bit DBIHandle MatchType Parser Producer Translator DBICSchema IxHash
7 ColumnHash ConstraintHash IndexHash ProcedureHash SequenceHash TableHash TriggerHash ViewHash)];
8 use Tie::IxHash;
c3653d16 9
850220ad 10 class_type DBICSchema, { class => 'DBIx::Class::Schema' };
a8eeeefc 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' };
1c355765 26 class_type Translator, { class => 'SQL::Translator' };
1e0243f3 27
a2b38aa4 28 coerce Column, from HashRef, via { SQL::Translator::Object::Column->new($_) },
29 from Str, via { SQL::Translator::Object::Column->new( name => $_ ) };
f5605c1d 30 coerce Constraint, from HashRef, via { SQL::Translator::Object::Constraint->new($_) };
31 coerce ForeignKey, from HashRef, via { SQL::Translator::Object::ForeignKey->new($_) };
e9776208 32 coerce Index, from HashRef, via { SQL::Translator::Object::Index->new($_) };
f5605c1d 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($_) };
59109131 40
59ad6549 41 coerce ArrayRef, from Str, via { [ $_ ] };
42
3c557f72 43 subtype MatchType, as Str, where { /^(full|partial|simple)$/ || $_ eq '' };
44 coerce MatchType, from Str, via { lc $_ };
45
ada068ed 46 subtype Bit, as Int, where { $_ == 1 || $_ == 0 };
47 coerce Bit,
48 from Undef, via { 0 },
e3582697 49 from Str, via { length() ? 1 : 0 };
ada068ed 50
a8eeeefc 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 { }
a499b4bd 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]];
a8eeeefc 76}