Skip to content

Commit 43fb4da

Browse files
committed
Mass insert for Table
1 parent 7d40467 commit 43fb4da

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

Tests/Unit/InsertTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,75 @@
44

55
class InsertTest extends \PHPUnit\Framework\TestCase
66
{
7+
public function testMultipleInserts() : void
8+
{
9+
$transaction = new \PHPFUI\ORM\Transaction();
10+
$customer1 = new \Tests\App\Record\Customer();
11+
$customer1->address = '123 Broadway';
12+
$customer1->business_phone = '212-987-6543';
13+
$customer1->city = 'New York';
14+
$customer1->company = 'PHPFUI';
15+
$customer1->country_region = 'USA';
16+
$customer1->email_address = 'bruce@phpfui.com';
17+
$customer1->fax_number = '212-345-6789';
18+
$customer1->first_name = 'Bruce';
19+
$customer1->home_phone = '987-654-3210';
20+
$customer1->job_title = 'Head Honcho';
21+
$customer1->last_name = 'Wells';
22+
$customer1->mobile_phone = '123-456-7890';
23+
$customer1->state_province = 'NY';
24+
$customer1->web_page = 'http://www.phpfui.com';
25+
$customer1->zip_postal_code = '10021';
26+
27+
$customer2 = new \Tests\App\Record\Customer();
28+
$customer2->address = '123 Main Street';
29+
$customer2->business_phone = '212-555-1212';
30+
$customer2->city = 'New York City';
31+
$customer2->company = 'PHPFUI';
32+
$customer2->country_region = 'USA';
33+
$customer2->email_address = 'bruce2@phpfui.com';
34+
$customer2->fax_number = '212-111-3333';
35+
$customer2->first_name = 'Bruce';
36+
$customer2->home_phone = '987-654-3210';
37+
$customer2->job_title = 'Head Honcho';
38+
$customer2->last_name = 'Wells';
39+
$customer2->state_province = 'NY';
40+
$customer2->web_page = 'http://buriedtreasure.phpfui.com';
41+
42+
$customer3 = new \Tests\App\Record\Customer();
43+
$customer3->address = '456 Elm';
44+
$customer3->business_phone = '212-987-6543';
45+
$customer3->city = 'Rochester';
46+
$customer3->company = 'PHPFUI';
47+
$customer3->email_address = 'bruce3@phpfui.net';
48+
$customer3->fax_number = '212-345-6789';
49+
$customer3->first_name = 'Fred';
50+
$customer3->home_phone = '987-654-3210';
51+
$customer3->job_title = 'Honcho';
52+
$customer3->last_name = 'Willis';
53+
$customer3->mobile_phone = '123-456-7890';
54+
$customer3->state_province = 'NY';
55+
56+
$customers = [];
57+
$customers[] = $customer1;
58+
$customers[] = $customer2;
59+
$customers[] = $customer3;
60+
61+
$customerTable = new \Tests\App\Table\Customer();
62+
$this->assertCount(29, $customerTable);
63+
64+
$customerTable->insert($customers);
65+
$this->assertCount(32, $customerTable);
66+
$customerTable->setWhere(new \PHPFUI\ORM\Condition('zip_postal_code', operator:new \PHPFUI\ORM\Operator\IsNull()));
67+
$this->assertCount(2, $customerTable);
68+
$customerTable->setWhere(new \PHPFUI\ORM\Condition('email_address', '%@phpfui%', new \PHPFUI\ORM\Operator\Like()));
69+
$this->assertCount(3, $customerTable);
70+
71+
$this->assertTrue($transaction->rollBack());
72+
$customerTable->setWhere();
73+
$this->assertCount(29, $customerTable);
74+
}
75+
776
public function testDateNullInsert() : void
877
{
978
$transaction = new \PHPFUI\ORM\Transaction();

src/PHPFUI/ORM/Table.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,53 @@ public function getWhereCondition() : \PHPFUI\ORM\Condition
698698
return $this->whereCondition;
699699
}
700700

701+
/**
702+
* Mass insertion. Does not use a transaction, so surround by a transaction if needed
703+
*
704+
* @param array<\PHPFUI\ORM\Record> $records
705+
*/
706+
public function insert(array $records) : bool
707+
{
708+
$tableName = $this->getTableName();
709+
$sql = "insert into `{$tableName}` (";
710+
711+
$fields = array_keys($this->getFields());
712+
$comma = '';
713+
foreach ($fields as $fieldName)
714+
{
715+
$sql .= "{$comma}`{$fieldName}`";
716+
$comma = ',';
717+
}
718+
719+
$sql .= ') values ';
720+
721+
$input = [];
722+
$comma = '(';
723+
foreach ($records as $record)
724+
{
725+
if ($record->getTableName() != $tableName)
726+
{
727+
$myType = get_debug_type($this->instance);
728+
$haveType = get_debug_type($record);
729+
throw new \PHPFUI\ORM\Exception(__METHOD__ . ": record should be of type {$myType} but is of type {$haveType}");
730+
}
731+
foreach ($fields as $fieldName)
732+
{
733+
$sql .= $comma . '?';
734+
$comma = ',';
735+
$input[] = $record[$fieldName];
736+
}
737+
$comma = '),(';
738+
}
739+
$sql .= ')';
740+
741+
$this->lastSql = $sql;
742+
$this->lastInput = $input;
743+
\PHPFUI\ORM::execute($this->lastSql, $this->lastInput);
744+
745+
return \PHPFUI\ORM::getLastErrorCode() == 0;
746+
}
747+
701748
public function setDistinct(string $distinct = 'DISTINCT') : static
702749
{
703750
$this->distinct = $distinct;

src/PHPFUI/ORM/Tool/Generate/CRUD.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ protected function getLine(\PHPFUI\ORM\Schema\Field $field) : string
161161
case 'bool':
162162
case 'datetime':
163163
case 'string':
164-
if ('CURRENT_TIMESTAMP' == $field->defaultValue || 'CURRENT_DATE' == $field->defaultValue)
164+
if ('NULL' === $field->defaultValue)
165+
{
166+
$defaultValue = 'NULL';
167+
}
168+
elseif ('CURRENT_TIMESTAMP' == $field->defaultValue || 'CURRENT_DATE' == $field->defaultValue)
165169
{
166170
$defaultValue = null;
167171
}

0 commit comments

Comments
 (0)