2
2
3
3
namespace Uasoft \Badaso \Database \Types ;
4
4
5
- use Doctrine \DBAL \Platforms \AbstractPlatform as DoctrineAbstractPlatform ;
6
- use Doctrine \DBAL \Types \Type as DoctrineType ;
5
+ use Illuminate \Support \Facades \DB ;
7
6
use Uasoft \Badaso \Database \Platforms \Platform ;
8
- use Uasoft \Badaso \Database \Schema \SchemaManager ;
7
+ use Doctrine \DBAL \Types \Type as DoctrineType ;
8
+ use Doctrine \DBAL \Platforms \AbstractPlatform as DoctrineAbstractPlatform ;
9
9
10
10
abstract class Type extends DoctrineType
11
11
{
@@ -15,6 +15,7 @@ abstract class Type extends DoctrineType
15
15
protected static $ platform_types = [];
16
16
protected static $ custom_type_options = [];
17
17
protected static $ type_categories = [];
18
+ protected static $ types = [];
18
19
19
20
const NAME = 'UNDEFINED_TYPE_NAME ' ;
20
21
const NOT_SUPPORTED = 'notSupported ' ;
@@ -29,7 +30,7 @@ public function getName()
29
30
return static ::NAME ;
30
31
}
31
32
32
- public static function toArray (DoctrineType $ type )
33
+ public static function toArray ($ type )
33
34
{
34
35
$ custom_type_options = $ type ->customOptions ?? [];
35
36
@@ -44,15 +45,15 @@ public static function getPlatformTypes()
44
45
return static ::$ platform_types ;
45
46
}
46
47
47
- if (! static ::$ custom_type_registered ) {
48
+ if (!static ::$ custom_type_registered ) {
48
49
static ::registerCustomPlatformTypes ();
49
50
}
50
51
51
- $ platform = SchemaManager:: getDatabasePlatform ();
52
+ $ platform_name = DB :: getDriverName ();
52
53
53
54
static ::$ platform_types = Platform::getPlatformTypes (
54
- $ platform -> getName () ,
55
- static ::getPlatformTypeMapping ($ platform )
55
+ $ platform_name ,
56
+ static ::getPlatformTypeMapping ($ platform_name )
56
57
);
57
58
58
59
static ::$ platform_types = static ::$ platform_types ->map (function ($ type ) {
@@ -62,52 +63,75 @@ public static function getPlatformTypes()
62
63
return static ::$ platform_types ;
63
64
}
64
65
65
- public static function getPlatformTypeMapping (DoctrineAbstractPlatform $ platform )
66
+ public static function getPlatformTypeMapping (DoctrineAbstractPlatform $ platform_name )
66
67
{
67
68
if (static ::$ platform_type_mapping ) {
68
69
return static ::$ platform_type_mapping ;
69
70
}
70
71
72
+ // Anda perlu mendefinisikan cara mengambil pemetaan tipe khusus untuk platform tertentu
71
73
static ::$ platform_type_mapping = collect (
72
- get_protected_property ($ platform , 'doctrineTypeMapping ' )
74
+ get_protected_property ($ platform_name , 'doctrineTypeMapping ' )
73
75
);
74
76
75
77
return static ::$ platform_type_mapping ;
76
78
}
77
79
78
80
public static function registerCustomPlatformTypes ($ force = false )
79
81
{
80
- if (static ::$ custom_type_registered && ! $ force ) {
82
+ if (static ::$ custom_type_registered && !$ force ) {
81
83
return ;
82
84
}
83
85
84
- $ platform = SchemaManager::getDatabasePlatform ();
85
- $ platform_name = ucfirst ($ platform ->getName ());
86
-
86
+ $ platform_name = DB ::getDriverName ();
87
+
87
88
$ custom_types = array_merge (
88
89
static ::getPlatformCustomTypes ('Common ' ),
89
90
static ::getPlatformCustomTypes ($ platform_name )
90
91
);
91
-
92
+
92
93
foreach ($ custom_types as $ type ) {
93
94
$ name = $ type ::NAME ;
94
95
95
- if (static ::hasType ($ name )) {
96
- static ::overrideType ($ name , $ type );
97
- } else {
96
+ if (!static ::hasType ($ name )) {
98
97
static ::addType ($ name , $ type );
98
+ } else {
99
+ static ::overrideType ($ name , $ type );
99
100
}
100
101
101
102
$ db_type = defined ("{$ type }::DBTYPE " ) ? $ type ::DBTYPE : $ name ;
102
-
103
- $ platform -> registerDoctrineTypeMapping ($ db_type , $ name );
103
+
104
+ static :: registerDoctrineTypeMapping ($ db_type , $ name );
104
105
}
105
106
106
107
static ::addCustomTypeOptions ($ platform_name );
107
108
108
109
static ::$ custom_type_registered = true ;
109
110
}
110
111
112
+ protected static function registerDoctrineTypeMapping ($ db_type , $ name )
113
+ {
114
+ $ databaseConfig = config ('database.connections. ' . config ('database.default ' ));
115
+
116
+ $ connectionParams = [
117
+ 'dbname ' => $ databaseConfig ['database ' ],
118
+ 'user ' => $ databaseConfig ['username ' ],
119
+ 'password ' => $ databaseConfig ['password ' ],
120
+ 'host ' => $ databaseConfig ['host ' ],
121
+ 'driver ' => 'pdo_mysql ' , // Sesuaikan dengan driver yang digunakan, misal: 'pdo_pgsql' untuk PostgreSQL
122
+ 'port ' => $ databaseConfig ['port ' ],
123
+ ];
124
+
125
+ $ doctrine_connection = \Doctrine \DBAL \DriverManager::getConnection ($ connectionParams );
126
+ $ platform = $ doctrine_connection ->getDatabasePlatform ();
127
+
128
+ if ($ platform ->hasDoctrineTypeMappingFor ($ db_type )) {
129
+ $ platform ->registerDoctrineTypeMapping ($ db_type , $ name );
130
+ } else {
131
+ throw new \Doctrine \DBAL \Exception ("Type to be overwritten $ db_type does not exist. " );
132
+ }
133
+ }
134
+
111
135
protected static function addCustomTypeOptions ($ platform_name )
112
136
{
113
137
static ::registerCommonCustomTypeOptions ();
@@ -126,12 +150,12 @@ protected static function addCustomTypeOptions($platform_name)
126
150
127
151
protected static function getPlatformCustomTypes ($ platform_name )
128
152
{
129
- $ types_path = __DIR__ . DIRECTORY_SEPARATOR . $ platform_name. DIRECTORY_SEPARATOR ;
130
- $ namespace = __NAMESPACE__ . '\\' . $ platform_name. '\\' ;
153
+ $ types_path = __DIR__ . DIRECTORY_SEPARATOR . $ platform_name . DIRECTORY_SEPARATOR ;
154
+ $ namespace = __NAMESPACE__ . '\\' . $ platform_name . '\\' ;
131
155
$ types = [];
132
156
133
- foreach (glob ($ types_path. '*.php ' ) as $ class_file ) {
134
- $ types [] = $ namespace. str_replace (
157
+ foreach (glob ($ types_path . '*.php ' ) as $ class_file ) {
158
+ $ types [] = $ namespace . str_replace (
135
159
'.php ' ,
136
160
'' ,
137
161
str_replace ($ types_path , '' , $ class_file )
@@ -330,4 +354,24 @@ public static function getTypeCategories()
330
354
331
355
return static ::$ type_categories ;
332
356
}
357
+
358
+ public static function hasType ($ name )
359
+ {
360
+ return DoctrineType::hasType ($ name );
361
+ }
362
+
363
+ public static function getType ($ name )
364
+ {
365
+ return static ::$ types [$ name ] ?? null ;
366
+ }
367
+
368
+ public static function addType ($ name , $ type )
369
+ {
370
+ static ::$ types [$ name ] = new $ type ();
371
+ }
372
+
373
+ public static function overrideType ($ name , $ type )
374
+ {
375
+ static ::$ types [$ name ] = new $ type ();
376
+ }
333
377
}
0 commit comments