<?php
/* This file has been auto-generated. Do not edit this file directly. */
namespace SabaiApps\Directories\Component\Calendar\Model\Base;

use SabaiApps\Framework\Model\Model;

abstract class OccurrenceGateway extends \SabaiApps\Framework\Model\AbstractGateway {

	public function getName() {
		return 'calendar_occurrence';
	}

	public function getFields() {
		return array(
			'occurrence_entity_type'        => Model::KEY_TYPE_VARCHAR,
			'occurrence_bundle_name'        => Model::KEY_TYPE_VARCHAR,
			'occurrence_entity_id'          => Model::KEY_TYPE_INT,
			'occurrence_slot_field_name'    => Model::KEY_TYPE_VARCHAR,
			'occurrence_slot_id'            => Model::KEY_TYPE_VARCHAR,
			'occurrence_hash'               => Model::KEY_TYPE_VARCHAR,
			'occurrence_title'              => Model::KEY_TYPE_VARCHAR,
			'occurrence_start_date'         => Model::KEY_TYPE_INT,
			'occurrence_end_date'           => Model::KEY_TYPE_INT,
			'occurrence_all_day'            => Model::KEY_TYPE_INT,
			'occurrence_status'             => Model::KEY_TYPE_INT,
			'occurrence_slot_data_snapshot' => Model::KEY_TYPE_JSON,
			'occurrence_data'               => Model::KEY_TYPE_JSON,
			'occurrence_id'                 => Model::KEY_TYPE_INT,
			'occurrence_created'            => Model::KEY_TYPE_INT,
			'occurrence_updated'            => Model::KEY_TYPE_INT,
		);
	}

	protected function _getIdFieldName() {
		return 'occurrence_id';
	}

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

	protected function _getSelectByIdsQuery( $ids, $fields ) {
		return sprintf(
			'SELECT %s FROM %scalendar_occurrence WHERE occurrence_id IN (%s)',
			empty( $fields ) ? '*' : implode( ', ', $fields ),
			$this->_db->getResourcePrefix(),
			implode( ', ', array_map( 'intval', $ids ) )
		);
	}

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

	protected function _getInsertQuery( &$values ) {
		$values['occurrence_created'] = time();
		$values['occurrence_updated'] = 0;
		return sprintf( 'INSERT INTO %scalendar_occurrence(occurrence_entity_type, occurrence_bundle_name, occurrence_entity_id, occurrence_slot_field_name, occurrence_slot_id, occurrence_hash, occurrence_title, occurrence_start_date, occurrence_end_date, occurrence_all_day, occurrence_status, occurrence_slot_data_snapshot, occurrence_data, occurrence_id, occurrence_created, occurrence_updated) VALUES(%s, %s, %d, %s, %s, %s, %s, %d, %d, %d, %d, %s, %s, %s, %d, %d)', $this->_db->getResourcePrefix(), $this->_db->escapeString( $values['occurrence_entity_type'] ), $this->_db->escapeString( $values['occurrence_bundle_name'] ), $values['occurrence_entity_id'], $this->_db->escapeString( $values['occurrence_slot_field_name'] ), $this->_db->escapeString( $values['occurrence_slot_id'] ), $this->_db->escapeString( $values['occurrence_hash'] ), $this->_db->escapeString( $values['occurrence_title'] ), $values['occurrence_start_date'], $values['occurrence_end_date'], $values['occurrence_all_day'], $values['occurrence_status'], $this->_db->escapeString( $this->_db->jsonEncode( $values['occurrence_slot_data_snapshot'] ) ), $this->_db->escapeString( $this->_db->jsonEncode( $values['occurrence_data'] ) ), empty( $values['occurrence_id'] ) ? 'NULL' : intval( $values['occurrence_id'] ), $values['occurrence_created'], $values['occurrence_updated'] );
	}

	protected function _getUpdateQuery( $id, $values ) {
		$last_update                  = $values['occurrence_updated'];
		$values['occurrence_updated'] = time();
		return sprintf( 'UPDATE %scalendar_occurrence SET occurrence_entity_type = %s, occurrence_bundle_name = %s, occurrence_entity_id = %d, occurrence_slot_field_name = %s, occurrence_slot_id = %s, occurrence_hash = %s, occurrence_title = %s, occurrence_start_date = %d, occurrence_end_date = %d, occurrence_all_day = %d, occurrence_status = %d, occurrence_slot_data_snapshot = %s, occurrence_data = %s, occurrence_updated = %d WHERE occurrence_id = %d AND occurrence_updated = %d', $this->_db->getResourcePrefix(), $this->_db->escapeString( $values['occurrence_entity_type'] ), $this->_db->escapeString( $values['occurrence_bundle_name'] ), $values['occurrence_entity_id'], $this->_db->escapeString( $values['occurrence_slot_field_name'] ), $this->_db->escapeString( $values['occurrence_slot_id'] ), $this->_db->escapeString( $values['occurrence_hash'] ), $this->_db->escapeString( $values['occurrence_title'] ), $values['occurrence_start_date'], $values['occurrence_end_date'], $values['occurrence_all_day'], $values['occurrence_status'], $this->_db->escapeString( $this->_db->jsonEncode( $values['occurrence_slot_data_snapshot'] ) ), $this->_db->escapeString( $this->_db->jsonEncode( $values['occurrence_data'] ) ), $values['occurrence_updated'], $id, $last_update );
	}

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

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

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

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