/** * Create the connection adapter */ protected function connect() { // Build a database connection if we don't have one connected $adapter = '\\Pixie\\ConnectionAdapters\\' . ucfirst(strtolower($this->adapter)); $adapterInstance = $this->container->build($adapter, array($this->container)); $pdo = $adapterInstance->connect($this->adapterConfig); $this->setPdoInstance($pdo); // Preserve the first database connection with a static property if (!static::$storedConnection) { static::$storedConnection = $this; } }
/** * Add a raw query * * @param $value * @param $bindings * * @return mixed */ public function raw($value, $bindings = array()) { return $this->container->build('\\Pixie\\QueryBuilder\\Raw', array($value, $bindings)); }
/** * Build generic criteria string and bindings from statements, like "a = b and c = ?" * * @param $statements * @param bool $bindValues * * @return array */ protected function buildCriteria($statements, $bindValues = true) { $criteria = ''; $bindings = array(); foreach ($statements as $statement) { $key = $this->wrapSanitizer($statement['key']); $value = $statement['value']; if (is_null($value) && $key instanceof \Closure) { // We have a closure, a nested criteria // Build a new NestedCriteria class, keep it by reference so any changes made // in the closure should reflect here $nestedCriteria = $this->container->build('\\Pixie\\QueryBuilder\\NestedCriteria', array($this->connection)); $nestedCriteria =& $nestedCriteria; // Call the closure with our new nestedCriteria object $key($nestedCriteria); // Get the criteria only query from the nestedCriteria object $queryObject = $nestedCriteria->getQuery('criteriaOnly', true); // Merge the bindings we get from nestedCriteria object $bindings = array_merge($bindings, $queryObject->getBindings()); // Append the sql we get from the nestedCriteria object $criteria .= $statement['joiner'] . ' (' . $queryObject->getSql() . ') '; } elseif (is_array($value)) { // where_in or between like query $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator']; switch ($statement['operator']) { case 'BETWEEN': $bindings = array_merge($bindings, $statement['value']); $criteria .= ' ? AND ?'; break; default: $valuePlaceholder = ''; foreach ($statement['value'] as $subValue) { $valuePlaceholder .= '?, '; $bindings[] = $subValue; } $valuePlaceholder = trim($valuePlaceholder, ', '); $criteria .= ' (' . $valuePlaceholder . ') '; break; } } else { // Usual where like criteria if (!$bindValues) { // Specially for joins // We are not binding values, lets sanitize then $value = $this->wrapSanitizer($value); $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' . $value . ' '; } elseif ($statement['key'] instanceof Raw) { $criteria .= $statement['joiner'] . ' ' . $key . ' '; $bindings = array_merge($bindings, $statement['key']->getBindings()); } else { // For wheres $valuePlaceholder = '?'; $bindings[] = $value; $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' . $valuePlaceholder . ' '; } } } // Clear all white spaces, and, or from beginning and white spaces from ending $criteria = preg_replace('/^(\\s?AND ?|\\s?OR ?)|\\s$/i', '', $criteria); return array($criteria, $bindings); }