<?php
/*
This file has been generated by the Sabai scaffold script. Do not edit this file directly.
If you need to customize the class, use the following file:
pluginsy/User/Model/AuthdataGateway.php
*/
abstract class Plugg_User_Model_Base_AuthdataGateway extends Sabai_Model_Gateway
{
    public function getName()
    {
        return 'authdata';
    }

    public function getFields()
    {
        return array('authdata_id' => Sabai_Model::KEY_TYPE_INT, 'authdata_created' => Sabai_Model::KEY_TYPE_INT, 'authdata_updated' => Sabai_Model::KEY_TYPE_INT, 'authdata_claimed_id' => Sabai_Model::KEY_TYPE_TEXT, 'authdata_display_id' => Sabai_Model::KEY_TYPE_TEXT, 'authdata_lastused' => Sabai_Model::KEY_TYPE_INT, 'authdata_auth_id' => Sabai_Model::KEY_TYPE_INT_NULL, 'authdata_userid' => Sabai_Model::KEY_TYPE_VARCHAR);
    }

    protected function _getSelectByIdQuery($id, array $fields)
    {
        $fields = empty($fields) ? '*' : implode(', t.', $fields);
        return sprintf('SELECT t.%s FROM %sauthdata t WHERE authdata_id = %d', $fields, $this->_db->getResourcePrefix(), $id);
    }

    protected function _getSelectByCriteriaQuery($criteriaStr, array $fields)
    {
        $fields = empty($fields) ? '*' : implode(', t.', $fields);
        return sprintf('SELECT t.%1$s FROM %2$sauthdata t WHERE %3$s', $fields, $this->_db->getResourcePrefix(), $criteriaStr);
    }

    protected function _getInsertQuery(array $values)
    {
        $values['authdata_created'] = time();
        $values['authdata_updated'] = 0;
        $values['authdata_auth_id'] = !empty($values['authdata_auth_id']) ? intval($values['authdata_auth_id']) : 'NULL';
        return sprintf("INSERT INTO %sauthdata(authdata_created, authdata_updated, authdata_claimed_id, authdata_display_id, authdata_lastused, authdata_auth_id, authdata_userid) VALUES(%d, %d, %s, %s, %d, %s, %s)", $this->_db->getResourcePrefix(), $values['authdata_created'], $values['authdata_updated'], $this->_db->escapeString($values['authdata_claimed_id']), $this->_db->escapeString($values['authdata_display_id']), $values['authdata_lastused'], $values['authdata_auth_id'], $this->_db->escapeString($values['authdata_userid']));
    }

    protected function _getUpdateQuery($id, array $values)
    {
        $values['authdata_auth_id'] = !empty($values['authdata_auth_id']) ? intval($values['authdata_auth_id']) : 'NULL';
        $last_update = $values['authdata_updated'];
        $values['authdata_updated'] = time();
        return sprintf("UPDATE %sauthdata SET authdata_updated = %d, authdata_claimed_id = %s, authdata_display_id = %s, authdata_lastused = %d, authdata_auth_id = %s, authdata_userid = %s WHERE authdata_id = %d AND authdata_updated = %d", $this->_db->getResourcePrefix(), $values['authdata_updated'], $this->_db->escapeString($values['authdata_claimed_id']), $this->_db->escapeString($values['authdata_display_id']), $values['authdata_lastused'], $values['authdata_auth_id'], $this->_db->escapeString($values['authdata_userid']), $id, $last_update);
    }

    protected function _getDeleteQuery($id)
    {
        return sprintf('DELETE FROM %1$sauthdata WHERE authdata_id = %2$d', $this->_db->getResourcePrefix(), $id);
    }

    protected function _getUpdateByCriteriaQuery($criteriaStr, array $sets)
    {
        $sets['authdata_updated'] = 'authdata_updated=' . time();
        return sprintf('UPDATE %sauthdata SET %s WHERE %s', $this->_db->getResourcePrefix(), implode(',', $sets), $criteriaStr);
    }

    protected function _getDeleteByCriteriaQuery($criteriaStr)
    {
        return sprintf('DELETE FROM %1$sauthdata WHERE %2$s', $this->_db->getResourcePrefix(), $criteriaStr);
    }

    protected function _getCountByCriteriaQuery($criteriaStr)
    {
        return sprintf('SELECT COUNT(*) FROM %1$sauthdata WHERE %2$s', $this->_db->getResourcePrefix(), $criteriaStr);
    }

    protected function _afterInsertTrigger1($id, array $new)
    {
        if (!empty($new['authdata_auth_id'])) {
            $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count + 1, auth_authdata_last = %d, auth_authdata_lasttime = %d WHERE auth_id = %d', $this->_db->getResourcePrefix(), $id, $new['authdata_created'], $new['authdata_auth_id']));
        }
    }

    protected function _afterDeleteTrigger1($id, array $old)
    {
        if (!empty($old['authdata_auth_id'])) {
            $sql = sprintf('SELECT authdata_id, authdata_created FROM %sauthdata WHERE authdata_auth_id = %d ORDER BY authdata_created DESC', $this->_db->getResourcePrefix(), $old['authdata_auth_id']);
            if (($rs = $this->_db->query($sql, 1, 0)) && ($rs->rowCount() > 0)) {
                $row = $rs->fetchAssoc();
                $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count - 1, auth_authdata_last = %d, auth_authdata_lasttime = %d WHERE auth_id = %d', $this->_db->getResourcePrefix(), $row['authdata_id'], $row['authdata_created'], $old['authdata_auth_id']));
            } else {
                $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count - 1, auth_authdata_last = 0, auth_authdata_lasttime = auth_created WHERE auth_id = %d', $this->_db->getResourcePrefix(), $old['authdata_auth_id']));
            }
        }
    }

    protected function _afterUpdateTrigger1($id, array $new, array $old)
    {
        if (empty($old['authdata_auth_id']) && !empty($new['authdata_auth_id'])) {
            $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count + 1, auth_authdata_last = %d, auth_authdata_lasttime = %d WHERE auth_id = %d', $this->_db->getResourcePrefix(), $id, $new['authdata_created'], $new['authdata_auth_id']));
        } elseif (!empty($old['authdata_auth_id']) && empty($new['authdata_auth_id'])) {
            $sql = sprintf('SELECT authdata_id, authdata_created FROM %sauthdata WHERE authdata_auth_id = %d ORDER BY authdata_created DESC', $this->_db->getResourcePrefix(), $old['authdata_auth_id']);
            if (($rs = $this->_db->query($sql, 1, 0)) && ($rs->rowCount() > 0)) {
                $row = $rs->fetchAssoc();
                $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count - 1, auth_authdata_last = %d, auth_authdata_lasttime = %d WHERE auth_id = %d', $this->_db->getResourcePrefix(), $row['authdata_id'], $row['authdata_created'], $old['authdata_auth_id']));
            } else {
                $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count - 1, auth_authdata_last = 0, auth_authdata_lasttime = auth_created WHERE auth_id = %d', $this->_db->getResourcePrefix(), $old['authdata_auth_id']));
            }
        } elseif ($old['authdata_auth_id'] != $new['authdata_auth_id']) {
            $sql = sprintf('SELECT authdata_id, authdata_created FROM %sauthdata WHERE authdata_auth_id = %d ORDER BY authdata_created DESC', $this->_db->getResourcePrefix(), $old['authdata_auth_id']);
            if (($rs = $this->_db->query($sql, 1, 0)) && ($rs->rowCount() > 0)) {
                $row = $rs->fetchAssoc();
                $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count - 1, auth_authdata_last = %d, auth_authdata_lasttime = %d WHERE auth_id = %d', $this->_db->getResourcePrefix(), $row['authdata_id'], $row['authdata_created'], $old['authdata_auth_id']));
            } else {
                $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count - 1, auth_authdata_last = 0, auth_authdata_lasttime = auth_created WHERE auth_id = %d', $this->_db->getResourcePrefix(), $old['authdata_auth_id']));
            }
            $this->_db->exec(sprintf('UPDATE %sauth SET auth_authdata_count = auth_authdata_count + 1, auth_authdata_last = %d, auth_authdata_lasttime = %d WHERE auth_id = %d', $this->_db->getResourcePrefix(), $id, $new['authdata_created'], $new['authdata_auth_id']));
        }
    }

    protected function _afterInsertTrigger($id, array $new)
    {
        $this->_afterInsertTrigger1($id, $new);
    }

    protected function _afterUpdateTrigger($id, array $new, array $old)
    {
        $this->_afterUpdateTrigger1($id, $new, $old);
    }

    protected function _afterDeleteTrigger($id, array $old)
    {
        $this->_afterDeleteTrigger1($id, $old);
    }
}