1 | <?php |
||
11 | class PostgresqlStore extends AbstractStore |
||
12 | { |
||
13 | /** |
||
14 | * @var string The name of the table to store the data into. |
||
15 | */ |
||
16 | private $tableName; |
||
17 | |||
18 | /** |
||
19 | * @var string The name of the primary key column in the table the data is being stored in. |
||
20 | */ |
||
21 | private $primaryKey; |
||
22 | |||
23 | /** |
||
24 | * @var array Data type and format metadata for each column being inserted. |
||
25 | * The Key is the CSV column position in the file and value is an array of: |
||
26 | * "pdo_type" - The PDO data type |
||
27 | * "type" - The schema data type |
||
28 | * "format" - The schema format. |
||
29 | */ |
||
30 | private $column_metadata = []; |
||
31 | |||
32 | /** |
||
33 | * @var string The CSV list of columns to be inserted into. |
||
34 | */ |
||
35 | private $columnList; |
||
36 | |||
37 | /** |
||
38 | * @var string The parameters to be used in the insert statement. |
||
39 | */ |
||
40 | private $insertParameters; |
||
41 | |||
42 | /** |
||
43 | * @var array The current CSV row being stored. |
||
44 | */ |
||
45 | private $currentCsvRow; |
||
46 | |||
47 | /** |
||
48 | * @var int The position of the current CSV row row in the CSV file. |
||
49 | */ |
||
50 | private $rowNumber; |
||
51 | |||
52 | /** |
||
53 | * @var int The position of the current CSV field in the current row. |
||
54 | */ |
||
55 | private $fieldNumber; |
||
56 | |||
57 | |||
58 | /** |
||
59 | * @static |
||
60 | * |
||
61 | * @var array Mappings of JSON table types to PDO param types. |
||
62 | */ |
||
63 | private static $pdo_type_mappings = [ |
||
64 | 'any' => \PDO::PARAM_STR, |
||
65 | 'array' => \PDO::PARAM_STR, |
||
66 | 'boolean' => \PDO::PARAM_BOOL, |
||
67 | 'date' => \PDO::PARAM_STR, |
||
68 | 'datetime' => \PDO::PARAM_STR, |
||
69 | 'time' => \PDO::PARAM_STR, |
||
70 | 'integer' => \PDO::PARAM_INT, |
||
71 | 'null' => \PDO::PARAM_NULL, |
||
72 | 'number' => \PDO::PARAM_STR, |
||
73 | 'string' => \PDO::PARAM_STR |
||
74 | ]; |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Store the data. |
||
79 | * |
||
80 | * @param string $tableName The name of the table to save the data in. With optional schema prefix. |
||
81 | * @param string $primaryKey The name of the primary key on the table. [optional] The default is "id". |
||
82 | * The primary key does not need to be listed in the CSV if it has |
||
83 | * a serial associated with it. |
||
84 | * |
||
85 | * @return boolean true |
||
86 | */ |
||
87 | 2 | public function store($tableName, $primaryKey = 'id') |
|
101 | |||
102 | |||
103 | /** |
||
104 | * Get the PDO type, schema type & schema format for each column in the CSV file. |
||
105 | * |
||
106 | * @return boolean true on success |
||
107 | */ |
||
108 | 2 | private function setColumnsMetadata() |
|
126 | |||
127 | |||
128 | /** |
||
129 | * Set the columns that will be inserted into. |
||
130 | * The columns include the "csv_row" field to store the CSV row |
||
131 | * number to help make error messages more useful. |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | 2 | private function setColumns() |
|
140 | |||
141 | |||
142 | /** |
||
143 | * Set the insert parameters string. |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | 2 | private function setInsertParameters() |
|
152 | |||
153 | |||
154 | /** |
||
155 | * Store each of the CSV rows. |
||
156 | * |
||
157 | * @return void |
||
158 | * |
||
159 | * @throws \Exception if the row couldn't be inserted into the database. |
||
160 | */ |
||
161 | 2 | private function storeCsvRows() |
|
195 | |||
196 | |||
197 | /** |
||
198 | * Do any data manipulation required on the specified column's value. |
||
199 | * |
||
200 | * @param array $columnMetadata The metadata about the column. |
||
201 | * @param string $fieldValue The value to update. |
||
202 | * |
||
203 | * @return string The updated field value. |
||
204 | */ |
||
205 | 2 | private function updateFieldValue($columnMetadata, $fieldValue) |
|
221 | |||
222 | |||
223 | /** |
||
224 | * Get the returned primary key of any rows that have been stored. |
||
225 | * |
||
226 | * @return array The primary keys. |
||
227 | */ |
||
228 | 1 | public function getInsertedIds() |
|
232 | } |
||
233 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.