Skip to content

Commit

Permalink
Adding ability to use expressions as the value for upserts in the form
Browse files Browse the repository at this point in the history
upsert plugin, by optional surrounding the value with parens.
  • Loading branch information
cheesegrits committed Mar 11, 2015
1 parent c15b0d4 commit 966e5ed
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Expand Up @@ -6,12 +6,12 @@
PLG_FORM_UPSERT_CONNECTION_DESC="Fabrik connection"
PLG_FORM_UPSERT_CONNECTION_LABEL="Connection"
PLG_FORM_UPSERT_DEFAULT_LABEL="Default"
PLG_FORM_UPSERT_FIELDS_DESC="Key/Value pairs to use when inserting / updating a record"
PLG_FORM_UPSERT_FIELDS_DESC="Key/Value/Default to use when inserting / updating a record.<br >Value can either be a simple static value (which will be automatically quoted in the SQL statement), or you can surround the value with parentheses to use an expression, like (counter+1).<br />If using an expression you may optionally provde an insert default with a :: seperator, like (counter+1::0), or ((SELECT foo FROM bar WHERE id = {rowid})::'new row').<br />Note that outer parens will be stripped, so use extra parens for subqueries.<br />The :: default is seperate from the main Default, which is used (on either update or insert) if Value is blank.<br />Can use placeholders in Value and Default."
PLG_FORM_UPSERT_FIELDS_LABEL="Values"
PLG_FORM_UPSERT_KEY_LABEL="Key"
PLG_FORM_UPSERT_PRIMARY_KEY_DESC="Upsert tables field to use as the primary key"
PLG_FORM_UPSERT_PRIMARY_KEY_LABEL="Primary key"
PLG_FORM_UPSERT_ROWID_DESC="The value for the Primary key element. Leave blank to always insert a new row. Can use {placeholders}<br />Set to {origid} to insert a new record on a new form, which will be updated when the main record is updated"
PLG_FORM_UPSERT_PRIMARY_KEY_DESC="Upsert tables field to use as the foreign key, i.e. the field in the upsert table which contains the PK value (rowid) of this form's row"
PLG_FORM_UPSERT_PRIMARY_KEY_LABEL="Foreign Key"
PLG_FORM_UPSERT_ROWID_DESC="The value for the Foreign Key element. Leave blank to always insert a new row. Can use {placeholders}<br />Set to {origid} to insert a new record on a new form, which will be updated when the main record is updated"
PLG_FORM_UPSERT_ROWID_LABEL="Row value"
PLG_FORM_UPSERT_TABLE_DESC="The Fabrik list to update/insert a record into"
PLG_FORM_UPSERT_TABLE_LABEL="List"
Expand Down
47 changes: 44 additions & 3 deletions plugins/fabrik_form/upsert/upsert.php
Expand Up @@ -61,7 +61,7 @@ public function onAfterProcess()
}

$rowid = $w->parseMessageForPlaceholder($rowid, $this->data, false);
$fields = $this->upsertData();
$fields = $this->upsertData($rowid);
$query->set($fields);

if ($rowid === '')
Expand All @@ -74,6 +74,7 @@ public function onAfterProcess()
}

$upsert_db->setQuery($query);
$sql = (string)$query;
$upsert_db->execute();

return true;
Expand Down Expand Up @@ -105,7 +106,7 @@ protected function getDb()
* @return array
*/

protected function upsertData()
protected function upsertData($rowid)
{
$params = $this->getParams();
$w = new FabrikWorker;
Expand All @@ -125,7 +126,47 @@ protected function upsertData()
$v = $w->parseMessageForPlaceholder($upsert->upsert_default[$i], $this->data);
}

$fields[] = $k . ' = ' . $upsert_db->quote($v);
/*
* $$$ hugh - permit the use of expressions, by putting the value in parens, with option use
* of double :: to provide a default for new row (rowid is empty). This default is seperate from
* the simple default used above, which is predicated on value being empty. So simple usage
* might be ..
*
* (counter+1::0)
*
* ... if you want to increment a 'counter' field. Or you might use a subquery, like ...
*
* ((SELECT foo FROM other_table WHERE fk_id = {rowid})::'foo default')
*/

if (!preg_match('#^\((.*)\)$#', $v))
{
$v = $insert_db->quote($v);
}
else
{
$matches = array();
preg_match('#^\((.*)\)$#', $v, $matches);
$v = $matches[1];
$v = explode('::', $v);
if (count($v) == 1)
{
$v = $v[0];
}
else
{
if (empty($rowid))
{
$v = $v[1];
}
else
{
$v = $v[0];
}
}
}

$fields[] = $k . ' = ' . $v;
}

return $fields;
Expand Down

0 comments on commit 966e5ed

Please sign in to comment.