|
| 1 | +class PDOConfig { |
| 2 | + final String driver; |
| 3 | + final String host; |
| 4 | + final int port; |
| 5 | + final String database; |
| 6 | + final String? username; |
| 7 | + final String? password; |
| 8 | + final bool? isUnixSocket; |
| 9 | + /// win1252 | utf8 | utf8mb4_general_ci |
| 10 | + final String? charset; |
| 11 | + |
| 12 | + /// enable pool ? |
| 13 | + final bool? pool; |
| 14 | + final int? poolSize; |
| 15 | + String? applicationName; |
| 16 | + final bool? allowReconnect; |
| 17 | + |
| 18 | + /// require | disable |
| 19 | + String? sslmode; |
| 20 | + /// UTC | America/Sao_Paulo |
| 21 | + String? timezone; |
| 22 | + String? schema; |
| 23 | + |
| 24 | + /// If true, decodes the timestamp with timezone (timestamptz) as UTC. |
| 25 | + /// If false, decodes the timestamp with timezone using the timezone defined in the connection. |
| 26 | + bool forceDecodeTimestamptzAsUTC = true; |
| 27 | + |
| 28 | + /// If true, decodes the timestamp without timezone (timestamp) as UTC. |
| 29 | + /// If false, decodes the timestamp without timezone as local datetime. |
| 30 | + bool forceDecodeTimestampAsUTC = true; |
| 31 | + |
| 32 | + /// If true, decodes the date as UTC. |
| 33 | + /// If false, decodes the date as local datetime. |
| 34 | + bool forceDecodeDateAsUTC = true; |
| 35 | + |
| 36 | + PDOConfig({ |
| 37 | + required this.driver, |
| 38 | + required this.host, |
| 39 | + this.port = 5432, |
| 40 | + required this.database, |
| 41 | + this.username, |
| 42 | + this.password, |
| 43 | + this.isUnixSocket = false, |
| 44 | + this.pool = false, |
| 45 | + this.poolSize = 1, |
| 46 | + this.applicationName, |
| 47 | + this.charset = 'utf8', |
| 48 | + this.allowReconnect, |
| 49 | + this.sslmode, |
| 50 | + this.timezone, |
| 51 | + this.schema, |
| 52 | + this.forceDecodeTimestamptzAsUTC = true, |
| 53 | + this.forceDecodeTimestampAsUTC = true, |
| 54 | + this.forceDecodeDateAsUTC = true, |
| 55 | + }); |
| 56 | + |
| 57 | + Map<String, dynamic> toMap() { |
| 58 | + return <String, dynamic>{ |
| 59 | + 'driver': driver, |
| 60 | + 'host': host, |
| 61 | + 'port': port, |
| 62 | + 'database': database, |
| 63 | + 'username': username, |
| 64 | + 'password': password, |
| 65 | + 'isUnixSocket': isUnixSocket, |
| 66 | + 'charset': charset, |
| 67 | + 'pool': pool, |
| 68 | + 'poolsize': poolSize, |
| 69 | + 'applicationName': applicationName, |
| 70 | + 'allowReconnect': allowReconnect, |
| 71 | + 'sslmode': sslmode, |
| 72 | + 'timezone': timezone, |
| 73 | + 'schema': schema, |
| 74 | + 'forceDecodeTimestamptzAsUTC': forceDecodeTimestamptzAsUTC, |
| 75 | + 'forceDecodeTimestampAsUTC': forceDecodeTimestampAsUTC, |
| 76 | + 'forceDecodeDateAsUTC': forceDecodeDateAsUTC, |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + factory PDOConfig.fromMap(Map<String, dynamic> map) { |
| 81 | + final config = PDOConfig( |
| 82 | + driver: map['driver'], |
| 83 | + host: map['host'], |
| 84 | + port: map['port'] is int ? map['port'] : int.parse(map['port']), |
| 85 | + database: map['database'], |
| 86 | + username: map['username'], |
| 87 | + password: map['password'], |
| 88 | + isUnixSocket: map['is_unix_socket'], |
| 89 | + charset: map['charset'], |
| 90 | + pool: map['pool'], |
| 91 | + poolSize: map['poolsize'], |
| 92 | + applicationName: map['application_name'], |
| 93 | + allowReconnect: map['allowreconnect'], |
| 94 | + sslmode: map['sslmode'], |
| 95 | + timezone: map['timezone'], |
| 96 | + schema: map['schema'], |
| 97 | + ); |
| 98 | + if (map['forceDecodeTimestamptzAsUTC'] is bool) { |
| 99 | + config.forceDecodeTimestamptzAsUTC = map['forceDecodeTimestamptzAsUTC']; |
| 100 | + } |
| 101 | + if (map['forceDecodeTimestampAsUTC'] is bool) { |
| 102 | + config.forceDecodeTimestampAsUTC = map['forceDecodeTimestampAsUTC']; |
| 103 | + } |
| 104 | + if (map['forceDecodeDateAsUTC'] is bool) { |
| 105 | + config.forceDecodeDateAsUTC = map['forceDecodeDateAsUTC']; |
| 106 | + } |
| 107 | + |
| 108 | + return config; |
| 109 | + } |
| 110 | +} |
0 commit comments