Odyssey
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
DbLoanCreator Class Reference

Public Member Functions

 __construct ($DbObject)
 
 AddSampleData ($data=[])
 
 GetRecord ($table, $keyfield, $value)
 
 GetLastRecord ($table, $keyfield)
 
 GetList ($table, $keyfield, $value)
 

Protected Member Functions

 Set ($prop, $object)
 
 InitResponse ()
 
 AddRecords ()
 
 PopulateTable ($table='', $data=[])
 
 BuildInsertStatement ($table)
 
 IsAtLeastTableAndData ($table, $data)
 
 ReturnResponse ()
 
 IsErrors ()
 

Protected Attributes

 $DbObj
 
 $data = []
 
 $response = []
 

Detailed Description

A class specifically for adding sample data to loans. Not used directly; inject this object into your consumer class then call public addSampleData() with an array of prepared/mapped data.

$obj->addSampleData([array data]);

Expects

array[ [table identifier] = [ [ [field identifier] => [value], // for each field to insert ] ], // for each table to insert data ]

Returns array [ 'success' => [ [table name] => [int n count of successes], // for each table ], 'errors' => [ [table name] => array[ [string specific error], // for each error in this table ], // for each table ] ]

Definition at line 38 of file DbLoanCreator.php.

Constructor & Destructor Documentation

◆ __construct()

DbLoanCreator::__construct (   $DbObject)

Constructor, initialize the database link object (your "$dbh")

Parameters
object$DbObject
Returns
void

Definition at line 54 of file DbLoanCreator.php.

54  {
55 
56  $this->Set('DbObj', $DbObject);
57  }
Set($prop, $object)

Member Function Documentation

◆ AddRecords()

DbLoanCreator::AddRecords ( )
protected

Add records using $this->data.

Returns
$this

Definition at line 193 of file DbLoanCreator.php.

193  {
194 
195  foreach ($this->data as $table => $data) {
196 
197  if ($this->IsErrors()) {
198 
199  return $this;
200  }
201 
202  if ($this->PopulateTable($table, $data)) {
203  if (isset($this->response['success'][$table])) {
204  $this->response['success'][$table]++;
205  continue;
206  }
207  // else ...
208  $this->response['success'][$table] = 1;
209  }
210  }
211 
212  return $this;
213  }
PopulateTable($table='', $data=[])

◆ AddSampleData()

DbLoanCreator::AddSampleData (   $data = [])

Entry point, add data to the DB and respond with errors or success log. Note we flush out response array with each call or it will exponentially add counts and errors to earlier values.

Parameters
array$dataalready mapped to DB fields
Returns
array

Definition at line 66 of file DbLoanCreator.php.

66  {
67 
68  return $this
69  ->Set('data', $data)
70  ->InitResponse()
71  ->AddRecords()
72  ->ReturnResponse();
73  }

◆ BuildInsertStatement()

DbLoanCreator::BuildInsertStatement (   $table)
protected

Build the insert statements. Generally I would prefer to dynamically create fields and values but you have two choices in PHP for PostgreSql: fiddle about with data types and figure out which to escape and which not, or use pg_query_params which solves that and other issues (such as SQL injection.) The problem then becomes that the placeholders can't be dynamic, you need placeholders like $1, $2, etc. We only have a few tables in a test script so letting it slide. Just be sure the fields correspond . . .

Parameters
string$table
Returns
string

Definition at line 253 of file DbLoanCreator.php.

253  {
254 
255  switch($table) {
256 
257  case 'lnappuser':
258  return "insert into $table (email, pwd, allow_e_comm, cu, confidenceword,
259  failedloginattempts, challenge_quest_id, userlogintype, banking_user_id,
260  session_account) values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)";
261 
262  case 'lnappschemadetail':
263  return "insert into $table (loanid, pageid, groupid, lineid, fieldtype,
264  fieldvalue, fieldattr) values($1, $2, $3, $4, $5, $6, $7)";
265 
266  case 'lnappuserresponse':
267  return "insert into $table (userid, loanid, respstatus, respcomments,
268  respcoreloanappid, respstarton, respsubmiton, respmodifiedon, respfname,
269  resplname, respmname, respssn, respdob, respmember, respphone, respapplication,
270  respstatusdesc, respamt, resplastinquire) values($1, $2, $3, $4, $5, $6, $7, $8,
271  $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)";
272 
273  case 'lnappschemamaster':
274  return "insert into $table (cu, loantitle, loandisclosure_fragment, lastmodified) values($1, $2, $3, $4)";
275 
276  case 'lnappuser_questselect':
277  return "insert into $table(userid, questid, user_answer) values($1, $2, $3)";
278 
279  default:
280  return '';
281  }
282  }

◆ GetLastRecord()

DbLoanCreator::GetLastRecord (   $table,
  $keyfield 
)

We should be using the native OID method OR "returning [keyfield]" on inserts but for various reasons this is problematic. Get the last inserted ID.

Parameters
string$table
string$keyfield
Returns
array

Definition at line 111 of file DbLoanCreator.php.

111  {
112 
113  $sql = "select $keyfield from $table order by $keyfield desc limit 1";
114  $msg_part = "table $table, key field $keyfield SQL $sql";
115  $result = pg_query($this->DbObj, $sql);
116 
117  if (! $result) {
118  return [
119  'error' => "No DB result in GetLastRecord $msg_part " . pg_last_error(),
120  'data' => []
121  ];
122  }
123 
124  $res = pg_fetch_assoc($result);
125 
126  return [
127  'error' => ($res)? null : "No result found for $msg_part in GetLastRecord",
128  'data' => ($res)? $res : []
129  ];
130  }

◆ GetList()

DbLoanCreator::GetList (   $table,
  $keyfield,
  $value 
)

Generally used to get the entire schema template record, get multiple rows by a single keyfield.

Parameters
string$table
string$keyfield
int$value
Returns
array

Definition at line 140 of file DbLoanCreator.php.

140  {
141 
142  $sql = "select * from $table where $keyfield = $1 order by $keyfield asc";
143  $msg_part = "table $table, key field $keyfield value $value SQL $sql";
144  $result = pg_query_params($this->DbObj, $sql, [$value]);
145 
146  if (! $result) {
147  return [
148  'error' => "No DB result in GetList $msg_part " . pg_last_error(),
149  'data' => []
150  ];
151  }
152 
153  $res = pg_fetch_all($result);
154 
155  return [
156  'error' => ($res)? null : "No result found for $msg_part in GetList",
157  'data' => ($res)? $res : []
158  ];
159  }

◆ GetRecord()

DbLoanCreator::GetRecord (   $table,
  $keyfield,
  $value 
)

The following three don't really belong in a "creator" but since there's no DB layer, here they are. GetRecord() gets a single record by a single field value (usually id.)

Parameters
string$table
string$keyfield
int$value
Returns
array

Definition at line 83 of file DbLoanCreator.php.

83  {
84 
85  $sql = "select * from $table where $keyfield = $1";
86  $msg_part = "table $table, key field $keyfield, value $value SQL $sql";
87  $result = pg_query_params($this->DbObj, $sql, [$value]);
88 
89  if (! $result) {
90  return [
91  'error' => "No DB result in getRecord $msg_part " . pg_last_error(),
92  'data' => []
93  ];
94  }
95 
96  $res = pg_fetch_assoc($result);
97 
98  return [
99  'error' => ($res)? null : "No result found for $msg_part in getRecord",
100  'data' => ($res)? $result : []
101  ];
102  }

◆ InitResponse()

DbLoanCreator::InitResponse ( )
protected

Set the response array. Response will be two arrays, successes and errors. Both are returned giving a running log of the process, see doc block at head.

Returns
$this

Definition at line 179 of file DbLoanCreator.php.

179  {
180 
181  $this->response = [
182  'success' => [],
183  'errors' => []
184  ];
185 
186  return $this;
187  }

◆ IsAtLeastTableAndData()

DbLoanCreator::IsAtLeastTableAndData (   $table,
  $data 
)
protected

Not much validation, being a command line script you should only be sending The Good Stuff. Setting a var so we can collect multiple errors (and improve validation if needed)

Parameters
string$table
array$data
Returns
bool

Definition at line 291 of file DbLoanCreator.php.

291  {
292 
293  $valid = true;
294 
295  if (empty($table)) {
296  $this->response['errors'][$table][] = "Empty table value found, fix it.";
297  $valid = false;
298  }
299 
300  if (count($data) == 0) {
301  $this->response['errors'][$table][] = "Empty data array found, fix it.";
302  $valid = false;
303  }
304 
305  return $valid;
306  }

◆ IsErrors()

DbLoanCreator::IsErrors ( )
protected

Got errors?

Returns
bool

Definition at line 321 of file DbLoanCreator.php.

321  {
322 
323  return count($this->response['errors']) > 0;
324  }

◆ PopulateTable()

DbLoanCreator::PopulateTable (   $table = '',
  $data = [] 
)
protected

Populate the table with records from the data rows, one by one, using pg_query_params to avoid futzing about with quoting and escaping. Could probably compile as one statement, but it's cmd line and light.

Parameters
string$table
array$data
Returns
bool

Definition at line 223 of file DbLoanCreator.php.

223  {
224 
225  if (! $this->IsAtLeastTableAndData($table, $data)) {
226  return false;
227  }
228 
229  foreach ($data as $arr) {
230 
231  $sql = $this->BuildInsertStatement($table);
232  $result = pg_query_params($this->DbObj, $sql, $arr);
233 
234  if (! $result) {
235  $this->response['errors'][$table][] = "Failed on insert: $sql last error: " . pg_last_error();
236  return false;
237  }
238  }
239 
240  return true;
241  }
IsAtLeastTableAndData($table, $data)
BuildInsertStatement($table)

◆ ReturnResponse()

DbLoanCreator::ReturnResponse ( )
protected

Return a response, this approach allows chaining of methods above it.

Returns
array

Definition at line 312 of file DbLoanCreator.php.

312  {
313 
314  return $this->response;
315  }

◆ Set()

DbLoanCreator::Set (   $prop,
  $object 
)
protected

A typical setter, sets internal properties (DbObj, cu) Usually these are public, don't think we'll need it

Parameters
stringprop property identifier
mixedproperty to set
Returns
$this

Definition at line 168 of file DbLoanCreator.php.

168  {
169 
170  $this->{$prop} = $object;
171  return $this;
172  }

The documentation for this class was generated from the following file: