2
2
3
3
namespace Leantime \Core \Db ;
4
4
5
+ use Illuminate \Database \ConnectionInterface ;
5
6
use Illuminate \Database \DatabaseManager ;
6
- use Illuminate \Support \Facades \Log ;
7
+ use Illuminate \Support \Facades \DB as dbFacade ;
7
8
use Leantime \Core \Events \DispatchesEvents ;
8
9
use PDO ;
9
10
@@ -20,12 +21,12 @@ class Db extends DatabaseManager
20
21
private string $ host = '' ;
21
22
22
23
/**
23
- * @var string username for db
24
+ * @var string username for database
24
25
*/
25
26
private string $ user = '' ;
26
27
27
28
/**
28
- * @var string password for db
29
+ * @var string password for database
29
30
*/
30
31
private string $ password = '' ;
31
32
@@ -45,47 +46,39 @@ class Db extends DatabaseManager
45
46
public PDO $ database ;
46
47
47
48
/**
48
- * __construct - connect to database and select db
49
+ * @var ConnectionInterface Laravel database connection
50
+ */
51
+ private ConnectionInterface $ connection ;
52
+
53
+ /**
54
+ * __construct - connect to database and select database
49
55
*
50
56
* @return void
51
57
*/
52
- public function __construct ($ config = null )
58
+ public function __construct ($ connection = ' mysql ' )
53
59
{
54
- if ($ config == null ) {
55
- $ config = app ('config ' );
56
- }
57
60
58
- $ this ->user = $ config ->dbUser ;
59
- $ this ->password = $ config ->dbPassword ;
60
- $ this ->databaseName = $ config ->dbDatabase ;
61
- $ this ->host = $ config ->dbHost ?? '127.0.0.1 ' ;
62
- $ this ->port = $ config ->dbPort ?? '3306 ' ;
61
+ // Get Laravel's database connection
62
+ $ this ->connection = dbFacade::connection ($ connection );
63
63
64
+ // Get the PDO connection from Laravel's connection
64
65
try {
65
- $ this ->database = new PDO (
66
- dsn: "mysql:host= {$ this ->host };port= {$ this ->port };dbname= {$ this ->databaseName }" ,
67
- username: $ this ->user ,
68
- password: $ this ->password ,
69
- options: [PDO ::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4,sql_mode="NO_ENGINE_SUBSTITUTION" ' ],
70
- );
71
- $ this ->database ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
72
- $ this ->database ->setAttribute (PDO ::ATTR_EMULATE_PREPARES , true );
73
- $ this ->database ->setAttribute (PDO ::ATTR_DEFAULT_FETCH_MODE , PDO ::FETCH_ASSOC );
74
66
75
- } catch ( \ PDOException $ e ) {
67
+ $ this -> database = $ this -> connection -> getPdo ();
76
68
77
- Log::error ("Can't connect to db " );
78
- Log::error ($ e );
69
+ } catch (\PDOException $ e ) {
70
+ \Log::error ("Can't connect to database " );
71
+ \Log::error ($ e );
79
72
80
73
exit ('Cannot connect to database ' );
81
74
}
82
75
}
83
76
84
77
/**
85
- * This function will generate a pdo binding string (":editors0,:editors1,:editors2,:editors3") to be used in a PDO
78
+ * This function will generate a PDO binding string (":editors0,:editors1,:editors2,:editors3") to be used in a PDO
86
79
* query that uses the IN() clause, to assist in proper PDO array bindings to avoid SQL injection.
87
80
*
88
- * A counted for loop is user rather than foreach with a key to avoid issues if the array passed has any
81
+ * A counted for loop is used rather than foreach with a key to avoid issues if the array passed has any
89
82
* arbitrary keys
90
83
*/
91
84
public static function arrayToPdoBindingString (string $ name , int $ count ): string
0 commit comments