33
33
34
34
use ArtificialOwl \MySmallPhpTools \Traits \TArrayTools ;
35
35
use OCA \Backup \Exceptions \SqlDumpException ;
36
+ use OCA \Backup \Exceptions \SqlParamsException ;
36
37
use OCA \Backup \ISqlDump ;
37
38
use Spatie \DbDumper \Databases \PostgreSql ;
38
39
use Throwable ;
@@ -62,14 +63,19 @@ public function __construct() {
62
63
*/
63
64
public function export (array $ params , string $ filename ): void {
64
65
try {
65
- PostgreSql::create ()
66
- ->setDbName ($ this ->get ('dbname ' , $ params ))
67
- ->setUserName ($ this ->get ('dbuser ' , $ params ))
68
- ->setPassword ($ this ->get ('dbpassword ' , $ params ))
69
- ->setHost ($ this ->get ('dbhost ' , $ params ))
70
- ->setPort ($ this ->getInt ('dbport ' , $ params ))
71
- ->addExtraOption ('--clean --inserts ' )
72
- ->dumpToFile ($ filename );
66
+ $ dump = PostgreSql::create ();
67
+ $ dump ->setDbName ($ this ->get (ISqlDump::DB_NAME , $ params ))
68
+ ->setUserName ($ this ->get (ISqlDump::DB_USER , $ params ))
69
+ ->setPassword ($ this ->get (ISqlDump::DB_PASS , $ params ))
70
+ ->setHost ($ this ->get (ISqlDump::DB_HOST , $ params ));
71
+
72
+ $ port = $ this ->getInt (ISqlDump::DB_PORT , $ params );
73
+ if ($ port > 0 ) {
74
+ $ dump ->setPort ($ port );
75
+ }
76
+
77
+ $ dump ->addExtraOption ('--clean --inserts ' )
78
+ ->dumpToFile ($ filename );
73
79
} catch (Throwable $ t ) {
74
80
throw new SqlDumpException ($ t ->getMessage ());
75
81
}
@@ -78,8 +84,34 @@ public function export(array $params, string $filename): void {
78
84
79
85
/**
80
86
* @param array $params
87
+ *
88
+ * @throws SqlParamsException
81
89
*/
82
90
public function setup (array $ params ): void {
91
+ $ port = $ this ->getInt (ISqlDump::DB_PORT , $ params );
92
+ if ($ port === 0 ) {
93
+ $ port = null ;
94
+ }
95
+ $ sql = pg_connect (
96
+ 'host= ' . $ this ->get (ISqlDump::DB_HOST , $ params ) .
97
+ ' dbname= ' . $ this ->get (ISqlDump::DB_NAME , $ params ) .
98
+ ' user= ' . $ this ->get (ISqlDump::DB_USER , $ params ) .
99
+ ' password= ' . $ this ->get (ISqlDump::DB_PASS , $ params )
100
+ );
101
+
102
+ if (is_bool ($ sql ) || is_null ($ sql )) {
103
+ throw new SqlParamsException ('cannot connect to database ' );
104
+ }
105
+
106
+ $ dbName = $ this ->get (ISqlDump::DB_NAME , $ params );
107
+ $ check = pg_query ($ sql , 'SELECT FROM pg_database WHERE datname= \'' . $ dbName . '\'' );
108
+ if (pg_num_rows ($ check ) === 0 ) {
109
+ pg_query ($ sql , 'CREATE DATABASE \'' . $ dbName . '\'' );
110
+ $ check = pg_query ($ sql , 'SELECT FROM pg_database WHERE datname= \'' . $ dbName . '\'' );
111
+ if (pg_num_rows ($ check ) === 0 ) {
112
+ throw new SqlParamsException ('cannot create database ' );
113
+ }
114
+ }
83
115
}
84
116
85
117
@@ -91,10 +123,10 @@ public function setup(array $params): void {
91
123
*/
92
124
public function import (array $ params , $ read ): bool {
93
125
$ sql = pg_connect (
94
- 'host= ' . $ params[ ' dbhost ' ] .
95
- ' dbname= ' . $ this ->get (' dbname ' , $ params ) .
96
- ' user= ' . $ this ->get (' dbuser ' , $ params ) .
97
- ' password= ' . $ this ->get (' dbpassword ' , $ params )
126
+ 'host= ' . $ this -> get (ISqlDump:: DB_HOST , $ params) .
127
+ ' dbname= ' . $ this ->get (ISqlDump:: DB_NAME , $ params ) .
128
+ ' user= ' . $ this ->get (ISqlDump:: DB_USER , $ params ) .
129
+ ' password= ' . $ this ->get (ISqlDump:: DB_PASS , $ params )
98
130
);
99
131
100
132
$ request = '' ;
0 commit comments