|
1 | 1 | <?php |
| 2 | + |
2 | 3 | namespace Cooperl\Database\DB2\Connectors; |
3 | 4 |
|
4 | 5 | use Illuminate\Database\Connectors\Connector; |
5 | 6 | use Illuminate\Database\Connectors\ConnectorInterface; |
6 | 7 |
|
| 8 | +/** |
| 9 | + * Class ODBCConnector |
| 10 | + * |
| 11 | + * @package Cooperl\Database\DB2\Connectors |
| 12 | + */ |
7 | 13 | class ODBCConnector extends Connector implements ConnectorInterface |
8 | 14 | { |
9 | 15 |
|
| 16 | + /** |
| 17 | + * @param array $config |
| 18 | + * |
| 19 | + * @return \PDO |
| 20 | + */ |
10 | 21 | public function connect(array $config) |
11 | 22 | { |
12 | 23 | $dsn = $this->getDsn($config); |
13 | | - |
14 | 24 | $options = $this->getOptions($config); |
15 | | - |
16 | 25 | $connection = $this->createConnection($dsn, $config, $options); |
17 | 26 |
|
18 | | - if (isset($config['schema'])) |
19 | | - { |
| 27 | + if (isset($config['schema'])) { |
20 | 28 | $schema = $config['schema']; |
21 | 29 |
|
22 | | - $connection->prepare("set schema $schema")->execute(); |
| 30 | + $connection->prepare('set schema '.$schema)->execute(); |
23 | 31 | } |
24 | 32 |
|
25 | 33 | return $connection; |
26 | 34 | } |
27 | 35 |
|
28 | | - protected function getDsn(array $config) { |
29 | | - extract($config); |
| 36 | + /** |
| 37 | + * @param array $config |
| 38 | + * |
| 39 | + * @return string |
| 40 | + */ |
| 41 | + protected function getDsn(array $config) |
| 42 | + { |
| 43 | + $dsnParts = [ |
| 44 | + 'odbc:DRIVER=%s', 'SYSTEM=%s', 'UserID=%s', 'Password=%s', 'DATABASE=%s', 'SIGNON=%s', 'SSL=%s', |
| 45 | + 'CommitMode=%s', 'ConnectionType=%s', 'DefaultLibraries=%s', 'Naming=%s', 'UNICODESQL=%s', 'DateFormat=%s', |
| 46 | + 'DateSeperator=%s', 'Decimal=%s', 'TimeFormat=%s', 'TimeSeparator=%s', 'BLOCKFETCH=%s', 'BlockSizeKB=%s', |
| 47 | + 'AllowDataCompression=%s', 'CONCURRENCY=%s', 'LAZYCLOSE=%s', 'MaxFieldLength=%s', 'PREFETCH=%s', |
| 48 | + 'QUERYTIMEOUT=%s', 'DefaultPkgLibrary=%s', 'DefaultPackage=%s', 'ExtendedDynamic=%s', 'QAQQINILibrary=%s', |
| 49 | + 'SQDIAGCODE=%s', 'LANGUAGEID=%s', 'SORTTABLE=%s', 'SortSequence=%s', 'SORTWEIGHT=%s', |
| 50 | + 'AllowUnsupportedChar=%s', 'CCSID=%s', 'GRAPHIC=%s', 'ForceTranslation=%s', 'ALLOWPROCCALLS=%s', |
| 51 | + 'DB2SQLSTATES=%s', 'DEBUG=%s', 'TRUEAUTOCOMMIT=%s', 'CATALOGOPTIONS=%s', 'LibraryView=%s', 'ODBCRemarks=%s', |
| 52 | + 'SEARCHPATTERN=%s', 'TranslationDLL=%s', 'TranslationOption=%s', 'MAXTRACESIZE=%s', 'MultipleTraceFiles=%s', |
| 53 | + 'TRACE=%s', 'TRACEFILENAME=%s', 'ExtendedColInfo=%s', |
| 54 | + '', // Just to add a semicolon to the end of string |
| 55 | + ]; |
30 | 56 |
|
31 | | - $dsn = "odbc:" |
32 | | - // General settings |
33 | | - . "DRIVER=$driverName;" |
34 | | - . "SYSTEM=$host;" |
35 | | - . "UserID=$username;" |
36 | | - . "Password=$password;" |
37 | | - //Server settings |
38 | | - . "DATABASE=$database;" |
39 | | - . "SIGNON=$signon;" |
40 | | - . "SSL=$ssl;" |
41 | | - . "CommitMode=$commitMode;" |
42 | | - . "ConnectionType=$connectionType;" |
43 | | - . "DefaultLibraries=$defaultLibraries;" |
44 | | - . "Naming=$naming;" |
45 | | - . "UNICODESQL=$unicodeSql;" |
46 | | - // Format settings |
47 | | - . "DateFormat=$dateFormat;" |
48 | | - . "DateSeperator=$dateSeperator;" |
49 | | - . "Decimal=$decimal;" |
50 | | - . "TimeFormat=$timeFormat;" |
51 | | - . "TimeSeparator=$timeSeparator;" |
52 | | - // Performances settings |
53 | | - . "BLOCKFETCH=$blockFetch;" |
54 | | - . "BlockSizeKB=$blockSizeKB;" |
55 | | - . "AllowDataCompression=$allowDataCompression;" |
56 | | - . "CONCURRENCY=$concurrency;" |
57 | | - . "LAZYCLOSE=$lazyClose;" |
58 | | - . "MaxFieldLength=$maxFieldLength;" |
59 | | - . "PREFETCH=$prefetch;" |
60 | | - . "QUERYTIMEOUT=$queryTimeout;" |
61 | | - // Modules settings |
62 | | - . "DefaultPkgLibrary=$defaultPkgLibrary;" |
63 | | - . "DefaultPackage=$defaultPackage;" |
64 | | - . "ExtendedDynamic=$extendedDynamic;" |
65 | | - // Diagnostic settings |
66 | | - . "QAQQINILibrary=$QAQQINILibrary;" |
67 | | - . "SQDIAGCODE=$sqDiagCode;" |
68 | | - // Sort settings |
69 | | - . "LANGUAGEID=$languageId;" |
70 | | - . "SORTTABLE=$sortTable;" |
71 | | - . "SortSequence=$sortSequence;" |
72 | | - . "SORTWEIGHT=$sortWeight;" |
73 | | - // Conversion settings |
74 | | - . "AllowUnsupportedChar=$allowUnsupportedChar;" |
75 | | - . "CCSID=$ccsid;" |
76 | | - . "GRAPHIC=$graphic;" |
77 | | - . "ForceTranslation=$forceTranslation;" |
78 | | - // Other settings |
79 | | - . "ALLOWPROCCALLS=$allowProcCalls;" |
80 | | - . "DB2SQLSTATES=$DB2SqlStates;" |
81 | | - . "DEBUG=$debug;" |
82 | | - . "TRUEAUTOCOMMIT=$trueAutoCommit;" |
83 | | - . "CATALOGOPTIONS=$catalogOptions;" |
84 | | - . "LibraryView=$libraryView;" |
85 | | - . "ODBCRemarks=$ODBCRemarks;" |
86 | | - . "SEARCHPATTERN=$searchPattern;" |
87 | | - . "TranslationDLL=$translationDLL;" |
88 | | - . "TranslationOption=$translationOption;" |
89 | | - . "MAXTRACESIZE=$maxTraceSize;" |
90 | | - . "MultipleTraceFiles=$multipleTraceFiles;" |
91 | | - . "TRACE=$trace;" |
92 | | - . "TRACEFILENAME=$traceFilename;" |
93 | | - . "ExtendedColInfo=$extendedColInfo;" |
94 | | - ; |
| 57 | + $dsnConfig = [ |
| 58 | + // General settings |
| 59 | + $config['driverName'], $config['host'], $config['username'], $config['password'], |
| 60 | + //Server settings |
| 61 | + $config['database'], $config['signon'], $config['ssl'], $config['commitMode'], $config['connectionType'], |
| 62 | + $config['defaultLibraries'], $config['naming'], $config['unicodeSql'], |
| 63 | + // Format settings |
| 64 | + $config['dateFormat'], $config['dateSeperator'], $config['decimal'], $config['timeFormat'], |
| 65 | + $config['timeSeparator'], |
| 66 | + // Performances settings |
| 67 | + $config['blockFetch'], $config['blockSizeKB'], $config['allowDataCompression'], $config['concurrency'], |
| 68 | + $config['lazyClose'], $config['maxFieldLength'], $config['prefetch'], $config['queryTimeout'], |
| 69 | + // Modules settings |
| 70 | + $config['defaultPkgLibrary'], $config['defaultPackage'], $config['extendedDynamic'], |
| 71 | + // Diagnostic settings |
| 72 | + $config['QAQQINILibrary'], $config['sqDiagCode'], |
| 73 | + // Sort settings |
| 74 | + $config['languageId'], $config['sortTable'], $config['sortSequence'], $config['sortWeight'], |
| 75 | + // Conversion settings |
| 76 | + $config['allowUnsupportedChar'], $config['ccsid'], $config['graphic'], $config['forceTranslation'], |
| 77 | + // Other settings |
| 78 | + $config['allowProcCalls'], $config['DB2SqlStates'], $config['debug'], $config['trueAutoCommit'], |
| 79 | + $config['catalogOptions'], $config['libraryView'], $config['ODBCRemarks'], $config['searchPattern'], |
| 80 | + $config['translationDLL'], $config['translationOption'], $config['maxTraceSize'], |
| 81 | + $config['multipleTraceFiles'], $config['trace'], $config['traceFilename'], $config['extendedColInfo'], |
| 82 | + ]; |
95 | 83 |
|
96 | | - return $dsn; |
| 84 | + return sprintf(implode(';', $dsnParts), ...$dsnConfig); |
97 | 85 | } |
98 | | - |
99 | 86 | } |
0 commit comments