[ulaform] SQL Action Driver

Vilius Sumskas vilius at lnk.lt
Tue Feb 3 08:09:11 PST 2004


Hi,

i'm sending first draft of my sql action class. It's my first Horde class, so i
want you to check if i'm working in right direction. Any suggestions and ideas
are welcome.

one question:

is there any efficient way to convert Horde_Form field types to database native
SQL types? for example:

'text' -> 'VARCHAR(255)',
'email' -> 'VARCHAR(255)',
'number' -> 'INT',
.....

--
  Best Regards,

  Vilius Sumskas
  LNK TV system administrator
  Mob. +370-614-75713
  http://www.lnk.lt




-------------- next part --------------
<?php
/**
 * Ulaform_Action_sql Class provides a Ulaform action driver to submit the
 * results of a form to database.
 *
 * Copyright 2004 Vilius Sumskas <vilius at lnk.lt>
 *
 * See the enclosed file COPYING for license information (GPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * $Horde: ulaform/lib/Action/sql.php,v 1.1 2004/01/01 16:17:54 jan Exp $
 *
 * @author Vilius Sumskas <vilius at lnk.lt>
 * @version $Revision: 1.1 $
 * @package ulaform
 */
class Ulaform_Action_sql extends Ulaform_Action {

    var $_db;

    /**
     * Actually carry out the action.
     *
     * @return mixed True or PEAR Error.
     */
    function doAction($form_params, $form_data, $fields)
    {
        /*  Connect to database. */
        $this->initialise();

        /* Check if table exists. */
        if (!in_array($form_params['table'], $this->_db->getListOf('tables'))) {
            $this->createDataTable($form_params, $fields);
        }

        /* Submit data to databse. */
        $sql = 'INSERT INTO ' . $form_params['table'] . ' SET ';
        foreach ($fields as $field) {
            $sql .= $field['field_name'] . ' = ';
            $sql .= $this->_db->quote($form_data[$field['field_name']]);
            if ($field['field_order'] != count($fields)) {
                $sql .= ', ';
                }
        }

        Horde::logMessage('SQL Query by Ulaform_Action_sql::doAction(): ' . $sql, __FILE__, __LINE__, PEAR_LOG_DEBUG);
        $result = $this->_db->query($sql);
        if (is_a($result, 'PEAR_Error')) {
            return $return;
        } else {
            return true;
        }
    }

    /**
     * Identifies this action driver and returns a brief description, used by
     * admin when configuring an action for a form and set up using Horde_Form.
     * 
     * @return array  Array of required parameters.
     */
    function getInfo()
    {
        $info['name'] = _("SQL");
        $info['desc'] = _("This drivers allows to submit form to database.");

        return $info;
    }

    /**
     * Returns the required parameters for this action driver, used by admin
     * when configuring an action for a form and set up using Horde_Form.
     * 
     * @return array  Array of required parameters.
     */
    function getParams()
    {
        $params = array();
        $params['table'] = array('label' => _("Table"), 'type' => 'text');

        return $params;
    }

    /**
     * Create table for submiting data. 
     * 
     * @return mixed True or PEAR Error.
     */
    function createDataTable($form_params, $fields)
    {
        /* Generate SQL query. */
        $sql = 'CREATE TABLE ' . $form_params['table'] . ' (';

        foreach ($fields as $field) {
            $sql .= $field['field_name'] . ' VARCHAR(255)';
            if ($field['field_order'] != count($fields)) {
                $sql .= ', ';
            }
        }

        $sql .= ')';

        /* Create table. */
        $result = $this->_db->query($sql);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        } else {
            return true;
        }
    }

    function initialise()
    {
        global $registry;

        Horde::assertDriverConfig($this->_params, 'sql',
                                  array('phptype', 'hostspec', 'username', 'database'));

        /* Connect to the SQL server using the supplied parameters. */
        require_once 'DB.php';
        $this->_db = &DB::connect($this->_params,
                                  array('persistent' => !empty($this->_params['persistent'])));
        if (is_a($this->_db, 'PEAR_Error')) {
            Horde::fatal($this->_db, __FILE__, __LINE__);
        }

        /* Enable the "portability" option. */
        $this->_db->setOption('optimize', 'portability');

        return true;
    }

}
-------------- next part --------------
--- Driver.php.orig     Thu Jan  1 08:17:53 2004
+++ Driver.php  Tue Feb  3 12:32:32 2004
@@ -147,7 +147,7 @@
         $fields = $this->getFields($form_data['form_id']);

         require_once ULAFORM_BASE . '/lib/Action.php';
-        $action = &Ulaform_Action::singleton($form['form_action']);
+        $action = &Ulaform_Action::singleton($form['form_action'], $this->_params);

         return $action->doAction($form['form_params'], $form_data, $fields);
     }


More information about the ulaform mailing list