| 1 | |||
| 2 | package Xmldoom::Schema::Column; | ||
| 3 | |||
| 4 | use Xmldoom::Threads; | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | sub new | ||
| 8 | { | ||
| 9 | my $class = shift; | ||
| 10 | my $args = shift; | ||
| 11 | |||
| 12 | my $parent; | ||
| 13 | my $name; | ||
| 14 | my $required; | ||
| 15 | my $primary_key; | ||
| 16 | my $id_generator; | ||
| 17 | my $type; | ||
| 18 | my $description; | ||
| 19 | my $size; | ||
| 20 | my $options; | ||
| 21 | my $auto_increment; | ||
| 22 | my $default; | ||
| 23 | my $timestamp; | ||
| 24 | |||
| 25 | if ( ref($args) eq 'HASH' ) | ||
| 26 | { | ||
| 27 | $parent = $args->{parent}; | ||
| 28 | $name = $args->{name}; | ||
| 29 | $type = $args->{type}; | ||
| 30 | $size = $args->{size}; | ||
| 31 | $options = $args->{options}; | ||
| 32 | $required = $args->{required}; | ||
| 33 | $primary_key = $args->{primary_key}; | ||
| 34 | $id_generator = $args->{id_generator}; | ||
| 35 | $description = $args->{description}; | ||
| 36 | $auto_increment = $args->{auto_increment}; | ||
| 37 | $default = $args->{default}; | ||
| 38 | $timestamp = $args->{timestamp}; | ||
| 39 | } | ||
| 40 | else | ||
| 41 | { | ||
| 42 | $parent = $args; | ||
| 43 | $name = shift; | ||
| 44 | $type = shift; | ||
| 45 | $size = shift; | ||
| 46 | $required = shift; | ||
| 47 | $primary_key = shift; | ||
| 48 | $description = shift; | ||
| 49 | } | ||
| 50 | |||
| 51 | if ( not defined $name or not defined $type ) | ||
| 52 | { | ||
| 53 | die "Cannot create a column without setting both name and type"; | ||
| 54 | } | ||
| 55 | |||
| 56 | my $self = { | ||
| 57 | parent => $parent, | ||
| 58 | name => $name, | ||
| 59 | type => uc($type), | ||
| 60 | size => $size, | ||
| 61 | options => $options, | ||
| 62 | required => $required || 0, | ||
| 63 | primary_key => $primary_key || 0, | ||
| 64 | auto_increment => $auto_increment || 0, | ||
| 65 | id_generator => $id_generator, | ||
| 66 | description => $description, | ||
| 67 | default => $default, | ||
| 68 | timestamp => $timestamp || 0, | ||
| 69 | }; | ||
| 70 | |||
| 71 | bless $self, $class; | ||
| 72 | return Xmldoom::Threads::make_shared($self, $args->{shared}); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub DESTROY | ||
| 76 | { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | # we don't need no stinking weak references! | ||
| 80 | $self->{parent} = undef; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub get_table { return shift->{parent}; } | ||
| 84 | sub get_name { return shift->{name}; } | ||
| 85 | sub get_type { return shift->{type}; } | ||
| 86 | sub get_size { return shift->{size}; } | ||
| 87 | sub get_options { return shift->{options}; } | ||
| 88 | sub get_description { return shift->{description}; } | ||
| 89 | sub get_default { return shift->{default}; } | ||
| 90 | sub get_timestamp { return shift->{timestamp}; } | ||
| 91 | sub get_id_generator { return shift->{id_generator}; } | ||
| 92 | sub is_primary_key { return shift->{primary_key}; } | ||
| 93 | sub is_required { return shift->{required}; } | ||
| 94 | sub is_auto_increment { return shift->{auto_increment}; } | ||
| 95 | |||
| 96 | sub get_data_type | ||
| 97 | { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | my $value = { }; | ||
| 101 | |||
| 102 | if ( $self->{type} =~ /char|text/i ) | ||
| 103 | { | ||
| 104 | $value->{type} = "string"; | ||
| 105 | $value->{size} = $self->{size}; | ||
| 106 | } | ||
| 107 | elsif ( $self->{type} =~ /enum/i ) | ||
| 108 | { | ||
| 109 | $value->{type} = "string"; | ||
| 110 | $value->{options} = $self->{options}; | ||
| 111 | } | ||
| 112 | elsif ( $self->{type} =~ /int/i ) | ||
| 113 | { | ||
| 114 | $value->{type} = "integer"; | ||
| 115 | } | ||
| 116 | elsif ( $self->{type} =~ /float/i ) | ||
| 117 | { | ||
| 118 | $value->{type} = "float"; | ||
| 119 | } | ||
| 120 | elsif ( $self->{type} =~ /date|time/i ) | ||
| 121 | { | ||
| 122 | $value->{type} = "date"; | ||
| 123 | } | ||
| 124 | |||
| 125 | return $value; | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; | ||
| 129 |