| 1 | package Oryx::DBI::Util::Generic; | ||
| 2 | |||
| 3 | use base qw(Oryx::DBI::Util); | ||
| 4 | |||
| 5 | our %SQL_TYPES = ( | ||
| 6 | 'Oid' => 'integer PRIMARY KEY', | ||
| 7 | 'Integer' => 'integer', | ||
| 8 | 'String' => 'varchar', | ||
| 9 | 'Text' => 'clob', | ||
| 10 | 'Binary' => 'blob', | ||
| 11 | 'Float' => 'float', | ||
| 12 | 'Boolean' => 'boolean', | ||
| 13 | 'DateTime' => 'timestamp', | ||
| 14 | ); | ||
| 15 | |||
| 16 | sub table_create { | ||
| 17 | my ($self, $dbh, $table, $columns, $types) = @_; | ||
| 18 | |||
| 19 | $self->SUPER::table_create(@_); | ||
| 20 | |||
| 21 | unless ($self->sequence_exists($dbh, $table)) { | ||
| 22 | $self->sequence_create($dbh, $table); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | sub _create_oryx_sequence { | ||
| 27 | my $self = shift; | ||
| 28 | |||
| 29 | unless ($self->table_exists($dbh, 'oryx_sequences')) { | ||
| 30 | $self->table_create($dbh, | ||
| 31 | 'oryx_sequences', ['name', 'value'], ['VARCHAR(255)', 'BIGINT']); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _sequence_exists { | ||
| 36 | my ($self, $dbh, $table) = @_; | ||
| 37 | my $seq_name = $self->_seq_name($table); | ||
| 38 | |||
| 39 | # Create it if it doesn't yet exist | ||
| 40 | $self->_create_oryx_sequence; | ||
| 41 | |||
| 42 | if (!$self->table_exists('oryx_sequences')) { | ||
| 43 | $self->table_create | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _sequence_create { | ||
| 47 | my ($self, $dbh, $table) = @_; | ||
| 48 | |||
| 49 | # Create it if it doesn't yet exist | ||
| 50 | $self->_create_oryx_sequence; | ||
| 51 | |||
| 52 | my $sql = "INSERT INTO oryx_sequences VALUES ('".$self->_seq_name($table)."', 0)"; | ||
| 53 | my $sth = $dbh->prepare($sql); | ||
| 54 | $sth->execute(); | ||
| 55 | $sth->finish; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _seq_name { | ||
| 59 | my ($self, $table) = @_; | ||
| 60 | return $table."_id_seq"; | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 | |||
| 65 | __END__ |