Commit 9a876908 authored by Carsten  Rose's avatar Carsten Rose
Browse files

UsersManual/index.rst: Updated examples.

FormAction.php: cleanup
Store.php: cleanup
Database.php: Added TRUNCATE
fixtures/Generic.sql: added Person.adrId - just as testcase
parent e9e96d41
......@@ -2377,30 +2377,30 @@ Relation: Person.id = Address.personId
* Form: wizard
* Name: wizard
* Titile: Person Wizard
* Title: Person Wizard
* Table: Person
* Render: bootstrap
* FormElement: firstname
* Class: **native**
* Type: **text**
* Name: firstname
* Label: Firstname
* Class: native
* Type: text
* FormElement: email, text, 20
* Class: **native**
* Type: **text**
* Name: city
* Label: City
* Value: {{SELECT city FROM Address WHERE personId={{r}} ORDER BY id LIMIT 1}}
* Class: native
* Type: text
* FormElement: insert/update address record
* Class: **action**
* Type: **afterSave**
* Label: Manage Address
* Class: action
* Type: afterSave
* Parameter:
* slaveId={{SELECT id FROM Address WHERE personId={{r}} ORDER BY id LIMIT 1}}
* sqlInsert={{INSERT INTO Address (personId, city) VALUES ({{r}}, '{{city:F:allbut:s}}') }}
......
......@@ -280,6 +280,7 @@ class Database {
break;
case 'UPDATE':
case 'DELETE':
case 'TRUNCATE':
$queryType = QUERY_TYPE_UPDATE;
$stat[DB_AFFECTED_ROWS] = $this->mysqli->affected_rows;
$count = $stat[DB_AFFECTED_ROWS];
......@@ -372,6 +373,7 @@ class Database {
case 'UPDATE':
case 'DELETE':
case 'REPLACE':
case 'TRUNCATE':
return true;
}
return false;
......
......@@ -203,7 +203,6 @@ class FormAction {
* @throws UserFormException
*/
private function doSlave(array $fe, $recordId) {
$flagUpdateMasterRecord = false;
// Get the slaveId
$slaveId = $this->evaluate->parse($fe[FE_SLAVE_ID]);
......
......@@ -221,11 +221,11 @@ class Store {
* @throws \qfq\CodeException
*/
public static function setVarArray(array $dataArray, $store, $flagOverwrite = false) {
// Check valid Storename
if (!isset(self::$sanitizeStore))
throw new UserFormException("Unknown Store: $store", ERROR_UNNOWN_STORE);
if ($store === STORE_ZERO)
throw new CodeException("setVarArray() for STORE_ZERO is impossible - there are no values.", ERROR_SET_STORE_ZERO);
......@@ -524,6 +524,7 @@ class Store {
if (isset(self::$raw[$store])) {
return self::$raw[$store];
}
return array();
}
......
......@@ -38,14 +38,16 @@ class DatabaseTest extends AbstractDatabaseTest {
[
'id' => '1',
'name' => 'Doe',
'firstname' => 'John',
'firstName' => 'John',
'adrId' => '0',
'gender' => 'male',
'groups' => 'c'
],
[
'id' => '2',
'name' => 'Smith',
'firstname' => 'Jane',
'firstName' => 'Jane',
'adrId' => '0',
'gender' => 'female',
'groups' => 'a,c'
],
......@@ -268,7 +270,7 @@ class DatabaseTest extends AbstractDatabaseTest {
$rc = $this->db->sql($sql, ROW_REGULAR, $dummy, 'fake', $dummy, $stat);
// DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
$this->assertEquals(5, $stat[DB_NUM_ROWS]);
$this->assertEquals(6, $stat[DB_NUM_ROWS]);
}
/**
......@@ -362,7 +364,8 @@ class DatabaseTest extends AbstractDatabaseTest {
$expected = [
['Field' => 'id', 'Type' => 'bigint(20)', 'Null' => 'NO', 'Key' => 'PRI', 'Default' => '', 'Extra' => 'auto_increment'],
['Field' => 'name', 'Type' => 'varchar(128)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => ''],
['Field' => 'firstname', 'Type' => 'varchar(128)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => ''],
['Field' => 'firstName', 'Type' => 'varchar(128)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => ''],
['Field' => 'adrId', 'Type' => 'int(11)', 'Null' => 'NO', 'Key' => '', 'Default' => '0', 'Extra' => ''],
['Field' => 'gender', 'Type' => "enum('','male','female')", 'Null' => 'NO', 'Key' => '', 'Default' => 'male', 'Extra' => ''],
['Field' => 'groups', 'Type' => "set('','a','b','c')", 'Null' => 'NO', 'Key' => '', 'Default' => '', 'Extra' => ''],
];
......
......@@ -46,15 +46,15 @@ class EvaluateTest extends \AbstractDatabaseTest {
$eval = new \qfq\Evaluate($this->store, $this->db);
// database: lower case
$this->assertEquals('1DoeJohnmalec2SmithJanefemalea,c', $eval->parse('{{select * from Person where id < 3 order by id}}'));
$this->assertEquals('1DoeJohn0malec2SmithJane0femalea,c', $eval->parse('{{select * from Person where id < 3 order by id}}'));
// database: upper case
$this->assertEquals('1DoeJohnmalec2SmithJanefemalea,c', $eval->parse('{{SELECT * FROM Person WHERE id < 3 ORDER BY id}}'));
$this->assertEquals('1DoeJohn0malec2SmithJane0femalea,c', $eval->parse('{{SELECT * FROM Person WHERE id < 3 ORDER BY id}}'));
$this->store->setVar('sql', 'SELECT * FROM Person WHERE id < 3 ORDER BY id', 'C');
$this->assertEquals('SELECT * FROM Person WHERE id < 3 ORDER BY id', $eval->parse('{{sql:C:all}}'));
$this->assertEquals('1DoeJohnmalec2SmithJanefemalea,c', $eval->parse('{{{{sql:C:all}}}}'));
$this->assertEquals('1DoeJohn0malec2SmithJane0femalea,c', $eval->parse('{{{{sql:C:all}}}}'));
// Get 2 row Array
$expected = [
......@@ -86,7 +86,7 @@ class EvaluateTest extends \AbstractDatabaseTest {
$this->assertEquals('1', $eval->parse('{{SELECT count(id) FROM Person WHERE name="Zappa" AND firstname="Frank"}}'));
// SHOW tables
$expected = "idbigint(20)NOPRIauto_incrementnamevarchar(128)YESfirstnamevarchar(128)YESgenderenum('','male','female')NOmalegroupsset('','a','b','c')NO";
$expected = "idbigint(20)NOPRIauto_incrementnamevarchar(128)YESfirstNamevarchar(128)YESadrIdint(11)NO0genderenum('','male','female')NOmalegroupsset('','a','b','c')NO";
$this->assertEquals($expected, $eval->parse('{{SHOW COLUMNS FROM Person}}'));
......
......@@ -194,27 +194,108 @@ class FormActionTest extends \AbstractDatabaseTest {
/**
* Process INSERT
**/
// public function testInsert() {
// $formSpec[F_TABLE_NAME] = 'Person';
// $formAction = new FormAction($formSpec, $this->db, true);
//
// $feSpecAction[FE_NAME] = '';
// $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_SAVE;
// $feSpecAction[FE_SLAVE_ID] = '{{ UPDATE Address SET city="Donwtown" WHERE id={{slaveID:V}} }} ';
// $feSpecAction[FE_SQL_UPDATE] = '{{ UPDATE Address SET city="Donwtown" WHERE id={{slaveID:V}} }} ';
// $formAction->elements(0, [ $feSpecAction, $feSpecAction ], FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD . ',' . FE_TYPE_AFTER_UPDATE . ',' . FE_TYPE_BEFORE_SAVE);
// }
// public function testBeforeLoad1() {
// $formSpec[F_TABLE_NAME] = 'Person';
// $formAction = new FormAction($formSpec, $this->db, true);
//
//
// // no variable
// $this->assertEquals('nothing', $eval->parse('nothing'));
// $this->assertEquals('TestFormName', $eval->parse('{{form:T}}'));
// }
public function testInsert() {
$formSpec[F_TABLE_NAME] = 'Person';
$formAction = new FormAction($formSpec, $this->db, true);
$feSpecAction[FE_NAME] = '';
$feSpecAction[FE_TYPE] = FE_TYPE_AFTER_SAVE;
$feSpecAction[FE_SQL_INSERT] = '{{ INSERT INTO Address (city, personId) VALUES ("Downtown", {{r}}) }} ';
$feSpecAction[FE_SQL_UPDATE] = '{{ UPDATE Address SET city="invalid" WHERE id={{r}} }} ';
$feSpecAction[FE_SQL_DELETE] = '{{ DELETE FROM Address WHERE personId={{r}} AND id=0 }} ';
$this->store->setVar('r', '2', STORE_SIP, true);
// slaveId: 0
$feSpecAction[FE_SLAVE_ID] = '0';
$result = $this->db->sql('TRUNCATE Address');
$formAction->elements(2, [$feSpecAction], FE_TYPE_AFTER_SAVE);
$result = $this->db->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
$this->assertEquals('1Downtown2', $result);
// slaveId: SELECT ... >> ''
$feSpecAction[FE_SLAVE_ID] = '{{SELECT id FROM Address WHERE personId={{r}} ORDER BY id LIMIT 1}}';
$result = $this->db->sql('TRUNCATE Address');
$formAction->elements(2, [$feSpecAction], FE_TYPE_AFTER_SAVE);
$result = $this->db->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
$this->assertEquals('1Downtown2', $result);
// slaveId: slaveId through column in master record & update Master record with slaveId
$this->store->setVar('adrId', '0', STORE_RECORD);
$feSpecAction[FE_NAME] = 'adrId';
$feSpecAction[FE_SLAVE_ID] = '';
$result = $this->db->sql('TRUNCATE Address');
$formAction->elements(2, [$feSpecAction], FE_TYPE_AFTER_SAVE);
// get the new slave record
$result = $this->db->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
$this->assertEquals('1Downtown2', $result);
// get the updated id in the master record
$result = $this->db->sql('SELECT id, name, adrId FROM Person WHERE id=2', ROW_IMPLODE_ALL);
$this->assertEquals('2Smith1', $result);
}
/**
* Process UPDATE
**/
public function testUpdate() {
$formSpec[F_TABLE_NAME] = 'Person';
$formAction = new FormAction($formSpec, $this->db, true);
$masterId = 2;
$feSpecAction[FE_NAME] = '';
$feSpecAction[FE_TYPE] = FE_TYPE_AFTER_SAVE;
$feSpecAction[FE_SQL_INSERT] = "{{ INSERT INTO Address (city, personId) VALUES ('invalid', {{r}}) }} ";
$feSpecAction[FE_SQL_UPDATE] = "{{ UPDATE Address SET city='Uptown' WHERE id={{slaveId:V}} }} ";
$feSpecAction[FE_SQL_DELETE] = "{{ DELETE FROM Address WHERE personId={{r}} AND id=0 }} ";
$result = $this->db->sql("TRUNCATE Address");
$result = $this->db->sql("INSERT INTO Address (city, personId) VALUES ('Downtown1', 1)");
$result = $this->db->sql("INSERT INTO Address (city, personId) VALUES ('Downtown2', 1)");
$result = $this->db->sql("INSERT INTO Address (city, personId) VALUES ('Downtown3', $masterId)");
$this->store->setVar('r', "$masterId", STORE_SIP, true);
// slaveId: 1 - hard coded
$feSpecAction[FE_SLAVE_ID] = '3';
$formAction->elements($masterId, [$feSpecAction], FE_TYPE_AFTER_SAVE);
$result = $this->db->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
$this->assertEquals('3Uptown' . $masterId, $result);
// slaveId: SELECT ... >> ''
$feSpecAction[FE_SLAVE_ID] = "{{SELECT id FROM Address WHERE personId={{r}} ORDER BY id LIMIT 1}}";
$formAction->elements($masterId, [$feSpecAction], FE_TYPE_AFTER_SAVE);
$result = $this->db->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
$this->assertEquals('3Uptown' . $masterId, $result);
// slaveId: column in master record
$this->db->sql("UPDATE Person SET adrId=3 WHERE id=$masterId", ROW_IMPLODE_ALL);
// $this->store->setVar('adrId', '3', STORE_RECORD);
$feSpecAction[FE_NAME] = 'adrId';
$feSpecAction[FE_SLAVE_ID] = '';
$formAction->elements($masterId, [$feSpecAction], FE_TYPE_AFTER_SAVE);
$result = $this->db->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
$this->assertEquals('3Uptown' . $masterId, $result);
$result = $this->db->sql("SELECT id, name, adrId FROM Person WHERE id=$masterId", ROW_IMPLODE_ALL);
$this->assertEquals('2Smith3', $result);
}
protected function setUp() {
......
......@@ -117,7 +117,9 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
public function testStorePriority() {
//default prio FSRD
$this->store->unsetStore(STORE_RECORD);
$this->store->fillStoreTableDefaultColumnType('Person');
$this->assertEquals('male', $this->store->getVar('gender'), "Get default definition from table person.gender");
$this->store->setVar('gender', 'female', STORE_RECORD);
......
DROP TABLE IF EXISTS Person;
CREATE TABLE Person (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(128),
firstname VARCHAR(128),
gender ENUM('', 'male', 'female') NOT NULL DEFAULT 'male',
groups SET('', 'a', 'b', 'c') NOT NULL DEFAULT ''
firstName VARCHAR(128),
adrId INT(11) NOT NULL DEFAULT 0,
gender ENUM('', 'male', 'female') NOT NULL DEFAULT 'male',
groups SET('', 'a', 'b', 'c') NOT NULL DEFAULT ''
);
INSERT INTO Person (name, firstname, gender, groups) VALUES
INSERT INTO Person (name, firstName, gender, groups) VALUES
('Doe', 'John', 'male', 'c'),
('Smith', 'Jane', 'female', 'a,c');
DROP TABLE IF EXISTS Note;
CREATE TABLE Note (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
gr_id INT(11) NOT NULL DEFAULT 0,
person_id BIGINT(20) DEFAULT NULL,
value VARCHAR(128),
t1 TEXT,
t2 TEXT
id BIGINT AUTO_INCREMENT PRIMARY KEY,
grId INT(11) NOT NULL DEFAULT 0,
personId BIGINT(20) DEFAULT NULL,
value VARCHAR(128),
t1 TEXT,
t2 TEXT
);
INSERT INTO Note (gr_id, value, t1, t2) VALUES
INSERT INTO Note (grId, value, t1, t2) VALUES
(1, 'First note - constants', 'Latest Information', 'Dear User\nt\n you\'re invited to to our Christmas party'),
(2, 'Second note - some variables', 'Latest Information: {{party:C:all}}',
'Dear {{Firstname:C:all}} {{Lastname:C:all}}\n\n you\'re invited to to our Christmas party');
......@@ -30,11 +31,11 @@ INSERT INTO Note (gr_id, value, t1, t2) VALUES
DROP TABLE IF EXISTS Address;
CREATE TABLE Address (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
person_id BIGINT(20) NOT NULL DEFAULT 0,
street VARCHAR(128) NOT NULL DEFAULT '',
city VARCHAR(128) NOT NULL DEFAULT '',
country ENUM('', 'Switzerland', 'Austria', 'France', 'Germany') NOT NULL DEFAULT '',
id BIGINT(20) NOT NULL AUTO_INCREMENT,
personId BIGINT(20) NOT NULL DEFAULT 0,
street VARCHAR(128) NOT NULL DEFAULT '',
city VARCHAR(128) NOT NULL DEFAULT '',
country ENUM('', 'Switzerland', 'Austria', 'France', 'Germany') NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment