| 1 | package NKTI::general::dbconnect; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use JSON::XS; | ||
| 6 | use DBI; | ||
| 7 | |||
| 8 | # Create Module for Connection MySQL Database : | ||
| 9 | # ------------------------------------------------------------------------ | ||
| 10 | sub mysql { | ||
| 11 | # Define parameter Module : | ||
| 12 | # ---------------------------------------------------------------- | ||
| 13 | my ($self, $get_db_config) = @_; | ||
| 14 | |||
| 15 | # Declare scalar for result : | ||
| 16 | # ---------------------------------------------------------------- | ||
| 17 | my $dbh; | ||
| 18 | |||
| 19 | # Declare scalar for Database Config : | ||
| 20 | # ---------------------------------------------------------------- | ||
| 21 | my $db_config = decode_json($get_db_config); | ||
| 22 | my $db_user = $db_config->{'db_user'}; | ||
| 23 | my $db_pass = $db_config->{'db_pass'}; | ||
| 24 | my $db_name = $db_config->{'db_name'}; | ||
| 25 | my $db_host = $db_config->{'db_host'}; | ||
| 26 | |||
| 27 | # Define database conection : | ||
| 28 | # ---------------------------------------------------------------- | ||
| 29 | my $dsn = 'DBI:mysql:database='.$db_name.';host='.$db_host; | ||
| 30 | $dbh = DBI->connect($dsn, $db_user, $db_pass, { | ||
| 31 | RaiseError => 0, | ||
| 32 | PrintError => 0, | ||
| 33 | }); | ||
| 34 | |||
| 35 | # Return Result : | ||
| 36 | # ---------------------------------------------------------------- | ||
| 37 | return $dbh; | ||
| 38 | } | ||
| 39 | # End of Create Module for Connection MySQL Database. | ||
| 40 | # =========================================================================================================== | ||
| 41 | |||
| 42 | # Module for Connection PostgreSQL Database : | ||
| 43 | # ------------------------------------------------------------------------ | ||
| 44 | sub pgsql { | ||
| 45 | # ---------------------------------------------------------------- | ||
| 46 | # Defien parameter Subroutine : | ||
| 47 | # ---------------------------------------------------------------- | ||
| 48 | my ($self, $get_db_config) = @_; | ||
| 49 | # ---------------------------------------------------------------- | ||
| 50 | # Declare scalar for result : | ||
| 51 | # ---------------------------------------------------------------- | ||
| 52 | my $dbh; | ||
| 53 | # ---------------------------------------------------------------- | ||
| 54 | # Declare scalar for Database Config : | ||
| 55 | # ---------------------------------------------------------------- | ||
| 56 | my $db_config = decode_json($get_db_config); | ||
| 57 | my $db_user = $db_config->{'db_user'}; | ||
| 58 | my $db_pass = $db_config->{'db_pass'}; | ||
| 59 | my $db_name = $db_config->{'db_name'}; | ||
| 60 | my $db_host = $db_config->{'db_host'}; | ||
| 61 | # ---------------------------------------------------------------- | ||
| 62 | # Define database conection : | ||
| 63 | # ---------------------------------------------------------------- | ||
| 64 | my $dsn = 'DBI:Pg:database='.$db_name.';host='.$db_host.';port=5432'; | ||
| 65 | $dbh = DBI->connect($dsn, $db_user, $db_pass, {RaiseError => 1}); | ||
| 66 | # ---------------------------------------------------------------- | ||
| 67 | # Return Result : | ||
| 68 | # ---------------------------------------------------------------- | ||
| 69 | return $dbh; | ||
| 70 | } | ||
| 71 | # End of Module for Connection PostgreSQL DatabaseModule for Connection PostgreSQL Databas | ||
| 72 | # =========================================================================================================== | ||
| 73 | |||
| 74 | # Create Module for connection SQLite : | ||
| 75 | # ------------------------------------------------------------------------ | ||
| 76 | sub sqlite { | ||
| 77 | |||
| 78 | # Define parameter module : | ||
| 79 | # ---------------------------------------------------------------- | ||
| 80 | my ($self, $fileloc_db) = @_; | ||
| 81 | |||
| 82 | # EXPR | ||
| 83 | # ---------------------------------------------------------------- | ||
| 84 | my $driver = "SQLite"; | ||
| 85 | my $dsn = "DBI:$driver:dbname=$fileloc_db"; | ||
| 86 | my $userid = ""; | ||
| 87 | my $password = ""; | ||
| 88 | my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) | ||
| 89 | or die $DBI::errstr; | ||
| 90 | |||
| 91 | # Return Result : | ||
| 92 | # ---------------------------------------------------------------- | ||
| 93 | return $dbh; | ||
| 94 | } | ||
| 95 | # End of Create Module for connection SQLite | ||
| 96 | # =========================================================================================================== | ||
| 97 | |||
| 98 | 1; | ||
| 99 | __END__ |