| 1 | package XAS::Model::Schema; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.01'; | ||
| 7 | |||
| 8 | 1 | ▶ | use Badger::Filesystem 'File'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use XAS::Lib::Modules::Environment; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use base 'DBIx::Class::Schema::Config'; | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | |||
| 14 | # --------------------------------------------------------------------- | ||
| 15 | # Define DBIx::Class::Schema::Config configuration file locations | ||
| 16 | # | ||
| 17 | # Format of the configuration file is as follows: | ||
| 18 | # | ||
| 19 | # [progress] - corresponds to what is given to opendb() | ||
| 20 | # dbname = monitor - name of the database | ||
| 21 | # dsn = SQLite - corresponds to the dbd driver | ||
| 22 | # username = username - the user context to use | ||
| 23 | # password = password - the password for that context | ||
| 24 | # | ||
| 25 | # When using ODBC with a user level DSN or a dynamic connection, you | ||
| 26 | # should add the following items: | ||
| 27 | # | ||
| 28 | # driver = SQL Server | ||
| 29 | # server = localhost,1234 | ||
| 30 | # | ||
| 31 | # When using PostgresSQL (Pg), you can add the following items: | ||
| 32 | # | ||
| 33 | # port = 5432 | ||
| 34 | # host = localhost | ||
| 35 | # sslmode = something | ||
| 36 | # options = something | ||
| 37 | # | ||
| 38 | # Or a service name, which is not compatiable with the above. | ||
| 39 | # | ||
| 40 | # service = service name | ||
| 41 | # | ||
| 42 | # There can be multiple stanzas, the first one that matches is used. | ||
| 43 | # --------------------------------------------------------------------- | ||
| 44 | |||
| 45 | my $env = XAS::Lib::Modules::Environment->new(); | ||
| 46 | my $path = $env->root; | ||
| 47 | |||
| 48 | # ----------------------------------------------------------------- | ||
| 49 | # Defining where our database.ini file could be | ||
| 50 | # ----------------------------------------------------------------- | ||
| 51 | |||
| 52 | if ($^O eq "MSWin32") { | ||
| 53 | |||
| 54 | __PACKAGE__->config_paths([( | ||
| 55 | File($path, 'etc', 'database')->path, | ||
| 56 | File($ENV{USERPROFILE}, 'database')->path, | ||
| 57 | )]); | ||
| 58 | |||
| 59 | } else { | ||
| 60 | |||
| 61 | __PACKAGE__->config_paths([( | ||
| 62 | File($path, 'etc', 'xas', 'database')->path, | ||
| 63 | File($ENV{HOME}, '.database')->path, | ||
| 64 | )]); | ||
| 65 | |||
| 66 | } | ||
| 67 | |||
| 68 | # --------------------------------------------------------------------- | ||
| 69 | # Defining our DBIx::Class exception handler | ||
| 70 | # --------------------------------------------------------------------- | ||
| 71 | |||
| 72 | __PACKAGE__->exception_action(\&XAS::Model::Schema::dbix_exceptions); | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | # --------------------------------------------------------------------- | ||
| 77 | # Set some default configuration options | ||
| 78 | # --------------------------------------------------------------------- | ||
| 79 | |||
| 80 | sub filter_loaded_credentials { | ||
| 81 | my $class = shift; | ||
| 82 | my $config = shift; | ||
| 83 | my $connect_args = shift; | ||
| 84 | |||
| 85 | $config = {} if ($config eq ''); | ||
| 86 | |||
| 87 | $config->{dbi_attr}->{AutoCommit} = 1; | ||
| 88 | $config->{dbi_attr}->{PrintError} = 0; | ||
| 89 | $config->{dbi_attr}->{RaiseError} = 1; | ||
| 90 | |||
| 91 | if ($config->{dsn} eq 'SQLite') { | ||
| 92 | |||
| 93 | $config->{dbi_attr}->{sqlite_use_immediate_transaction} = 1; | ||
| 94 | $config->{dbi_attr}->{sqlite_see_if_its_a_number} = 1; | ||
| 95 | $config->{dbi_attr}->{on_connect_call} = 'use_foreign_keys'; | ||
| 96 | |||
| 97 | $config->{dsn} = "dbi:$config->{dsn}:dbname=$config->{dbname}"; | ||
| 98 | |||
| 99 | } elsif ($config->{dsn} eq 'ODBC') { | ||
| 100 | |||
| 101 | # http://dolio.lh.net/~apw/doc/HOWTO/HOWTO-Connect_Perl_to_SQL_Server.pdf | ||
| 102 | # | ||
| 103 | # a user level DSN or a dynamic connection needs the following: | ||
| 104 | # | ||
| 105 | # dbi:ODBC:Driver={driver};Server=server;Database=dbname | ||
| 106 | # | ||
| 107 | # a system level DSN needs the following: | ||
| 108 | # | ||
| 109 | # dbi:ODBC:dbname | ||
| 110 | # | ||
| 111 | |||
| 112 | if (defined($config->{driver})) { | ||
| 113 | |||
| 114 | $config->{dsn} = sprintf( | ||
| 115 | "dbi:%s:Driver={%s};Database=%s;Server=%s", | ||
| 116 | $config->{dsn}, $config->{driver}, | ||
| 117 | $config->{dbname}, $config->{server} | ||
| 118 | ); | ||
| 119 | |||
| 120 | } else { | ||
| 121 | |||
| 122 | $config->{dsn} = "dbi:$config->{dsn}:$config->{dbname}"; | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | } elsif ($config->{dsn} eq 'Pg') { | ||
| 127 | |||
| 128 | unless (defined($config->{service})) { | ||
| 129 | |||
| 130 | $config->{dsn} = "dbi:$config->{dsn}:dbname=$config->{dbname}"; | ||
| 131 | $config->{dsn} .= ";host=$config->{host}" if (defined($config->{host})); | ||
| 132 | $config->{dsn} .= ";port=$config->{port}" if (defined($config->{port})); | ||
| 133 | $config->{dsn} .= ";options=$config->{options}" if (defined($config->{options})); | ||
| 134 | $config->{dsn} .= ";sslnode=$config->{sslmode}" if (defined($config->{sslmode})); | ||
| 135 | |||
| 136 | } else { | ||
| 137 | |||
| 138 | $config->{dsn} = "dbi:$config->{dsn}:service=$config->{service}"; | ||
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | } else { | ||
| 143 | |||
| 144 | $config->{dsn} = "dbi:$config->{dsn}:dbname=$config->{dbname}"; | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | return $config; | ||
| 149 | |||
| 150 | } | ||
| 151 | |||
| 152 | sub dbix_exceptions { | ||
| 153 | my $error = shift; | ||
| 154 | |||
| 155 | $error =~ s/dbix.class error - //; | ||
| 156 | |||
| 157 | my $ex = XAS::Exception->new( | ||
| 158 | type => 'dbix.class', | ||
| 159 | info => sprintf("%s", $error) | ||
| 160 | ); | ||
| 161 | |||
| 162 | $ex->throw; | ||
| 163 | |||
| 164 | } | ||
| 165 | |||
| 166 | sub opendb { | ||
| 167 | my $class = shift; | ||
| 168 | |||
| 169 | return $class->connect(@_); | ||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | __END__ | ||