| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||
| 3 | # File: GX/Database/SQLite.pm | ||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | package GX::Database::SQLite; | ||
| 7 | |||
| 8 | use GX::Exception; | ||
| 9 | |||
| 10 | use DBD::SQLite (); | ||
| 11 | |||
| 12 | |||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 14 | # Class setup | ||
| 15 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | use GX::Class; | ||
| 18 | |||
| 19 | extends 'GX::Database'; | ||
| 20 | |||
| 21 | build; | ||
| 22 | |||
| 23 | |||
| 24 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 25 | # Export mechanism | ||
| 26 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 27 | |||
| 28 | __PACKAGE__->_install_import_method; | ||
| 29 | |||
| 30 | |||
| 31 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 32 | # Private methods | ||
| 33 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 34 | |||
| 35 | sub _connect { | ||
| 36 | |||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | return DBI->connect( | ||
| 40 | $self->_get_dsn, | ||
| 41 | '', | ||
| 42 | '', | ||
| 43 | { %{$self->_get_connect_options} } | ||
| 44 | ); | ||
| 45 | |||
| 46 | } | ||
| 47 | |||
| 48 | sub _dbi_driver { | ||
| 49 | |||
| 50 | return 'SQLite'; | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | sub _initialize_sql_builder { | ||
| 55 | |||
| 56 | require GX::SQL::Builder::SQLite; | ||
| 57 | |||
| 58 | return GX::SQL::Builder::SQLite->new; | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | sub _setup_config { | ||
| 63 | |||
| 64 | my $self = shift; | ||
| 65 | my $args = shift; | ||
| 66 | |||
| 67 | if ( exists $args->{'dsn'} ) { | ||
| 68 | |||
| 69 | my $dsn = delete $args->{'dsn'} // ''; | ||
| 70 | |||
| 71 | my ( $scheme, $driver, undef, undef, undef ) = DBI->parse_dsn( $dsn ); | ||
| 72 | |||
| 73 | if ( ! defined $scheme ) { | ||
| 74 | throw "Invalid option (\"dsn\" must be DBI data source name)"; | ||
| 75 | } | ||
| 76 | |||
| 77 | if ( ! defined $driver ) { | ||
| 78 | throw "Invalid option (\"dsn\" does not specify a DBI driver)"; | ||
| 79 | } | ||
| 80 | |||
| 81 | if ( $driver ne $self->_dbi_driver ) { | ||
| 82 | throw "Invalid option (\"dsn\" specifies an unsupported DBI driver)"; | ||
| 83 | } | ||
| 84 | |||
| 85 | $self->_set_dsn( $dsn ); | ||
| 86 | |||
| 87 | } | ||
| 88 | else { | ||
| 89 | |||
| 90 | $self->_set_dsn( 'DBI:' . $self->_dbi_driver . ':dbname=' . ( $args->{'file'} // '' ) ); | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | delete @$args{ qw( file ) }; | ||
| 95 | |||
| 96 | $self->SUPER::_setup_config( $args ); | ||
| 97 | |||
| 98 | return; | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ |