5
5
6
6
use Doctrine \DBAL \Connection ;
7
7
use Doctrine \DBAL \Platforms \MySqlPlatform ;
8
+ use Mouf \Database \MagicQuery ;
8
9
use TheCodingMachine \TDBM \QueryFactory \SmartEagerLoad \ManyToOneDataLoader ;
9
10
use TheCodingMachine \TDBM \QueryFactory \SmartEagerLoad \StorageNode ;
10
11
@@ -13,59 +14,56 @@ class ManyToOnePartialQuery implements PartialQuery
13
14
/**
14
15
* @var string
15
16
*/
16
- private $ queryFrom ;
17
+ private $ mainTable ;
17
18
/**
18
19
* @var string
19
20
*/
20
- private $ mainTable ;
21
+ private $ key ;
21
22
/**
22
- * @var StorageNode
23
+ * @var string
23
24
*/
24
- private $ storageNode ;
25
+ private $ pk ;
26
+ /**
27
+ * @var PartialQuery
28
+ */
29
+ private $ partialQuery ;
25
30
/**
26
31
* @var string
27
32
*/
28
- private $ key ;
33
+ private $ originTableName ;
29
34
/**
30
35
* @var string
31
36
*/
32
- private $ pk ;
37
+ private $ columnName ;
33
38
34
39
public function __construct (PartialQuery $ partialQuery , string $ originTableName , string $ tableName , string $ pk , string $ columnName )
35
40
{
36
41
// TODO: move this in a separate function. The constructor is called for every bean.
37
- $ mysqlPlatform = new MySqlPlatform ();
38
- $ this ->queryFrom = 'FROM ' .$ mysqlPlatform ->quoteIdentifier ($ tableName ).
39
- ' WHERE ' .$ mysqlPlatform ->quoteIdentifier ($ tableName ).'. ' .$ mysqlPlatform ->quoteIdentifier ($ pk ).' IN ' .
40
- '(SELECT ' .$ mysqlPlatform ->quoteIdentifier ($ originTableName ).'. ' .$ mysqlPlatform ->quoteIdentifier ($ columnName ).' ' .$ partialQuery ->getQueryFrom ().') ' ;
42
+ $ this ->partialQuery = $ partialQuery ;
41
43
$ this ->mainTable = $ tableName ;
42
- $ this ->storageNode = $ partialQuery ->getStorageNode ();
43
44
$ this ->key = $ partialQuery ->getKey ().'__ ' .$ columnName ;
44
45
$ this ->pk = $ pk ;
46
+ $ this ->originTableName = $ originTableName ;
47
+ $ this ->columnName = $ columnName ;
45
48
}
46
49
47
50
/**
48
51
* Returns the SQL of the query, starting at the FROM keyword.
49
52
*/
50
53
public function getQueryFrom (): string
51
54
{
52
- return $ this ->queryFrom ;
53
- }
54
-
55
- /**
56
- * Returns the name of the main table (main objects returned by this query)
57
- */
58
- public function getMainTable (): string
59
- {
60
- return $ this ->mainTable ;
55
+ $ mysqlPlatform = new MySqlPlatform ();
56
+ return 'FROM ' .$ mysqlPlatform ->quoteIdentifier ($ this ->mainTable ).
57
+ ' WHERE ' .$ mysqlPlatform ->quoteIdentifier ($ this ->mainTable ).'. ' .$ mysqlPlatform ->quoteIdentifier ($ this ->pk ).' IN ' .
58
+ '(SELECT ' .$ mysqlPlatform ->quoteIdentifier ($ this ->originTableName ).'. ' .$ mysqlPlatform ->quoteIdentifier ($ this ->columnName ).' ' .$ this ->partialQuery ->getQueryFrom ().') ' ;
61
59
}
62
60
63
61
/**
64
62
* Returns the object in charge of storing the dataloader associated to this query.
65
63
*/
66
64
public function getStorageNode (): StorageNode
67
65
{
68
- return $ this ->storageNode ;
66
+ return $ this ->partialQuery -> getStorageNode () ;
69
67
}
70
68
71
69
/**
@@ -81,13 +79,24 @@ public function getKey(): string
81
79
*/
82
80
public function registerDataLoader (Connection $ connection ): void
83
81
{
84
- if ($ this ->storageNode ->hasManyToOneDataLoader ($ this ->key )) {
82
+ $ storageNode = $ this ->getStorageNode ();
83
+ if ($ storageNode ->hasManyToOneDataLoader ($ this ->key )) {
85
84
return ;
86
85
}
87
86
88
87
$ mysqlPlatform = new MySqlPlatform ();
89
- $ sql = 'SELECT DISTINCT ' .$ mysqlPlatform ->quoteIdentifier ($ this ->mainTable ).'.* ' .$ this ->queryFrom ;
88
+ $ sql = 'SELECT DISTINCT ' .$ mysqlPlatform ->quoteIdentifier ($ this ->mainTable ).'.* ' .$ this ->getQueryFrom () ;
90
89
91
- $ this ->storageNode ->setManyToOneDataLoader ($ this ->key , new ManyToOneDataLoader ($ connection , $ sql , $ this ->pk ));
90
+ if (!$ connection ->getDatabasePlatform () instanceof MySqlPlatform) {
91
+ // We need to convert the query from MySQL dialect to something else
92
+ $ sql = $ this ->getMagicQuery ()->buildPreparedStatement ($ sql );
93
+ }
94
+
95
+ $ storageNode ->setManyToOneDataLoader ($ this ->key , new ManyToOneDataLoader ($ connection , $ sql , $ this ->pk ));
96
+ }
97
+
98
+ public function getMagicQuery (): MagicQuery
99
+ {
100
+ return $ this ->partialQuery ->getMagicQuery ();
92
101
}
93
102
}
0 commit comments