Splits an SQL column identifier into table and column.

@param string $sqlString SQL column identifier

@return array [table|null, column]
342292
private function splitTableAndColumn($sqlString)
    {
        // Most column identifiers don't contain a database or table name
        // In this case just return what we've got
        if (strpos($sqlString, '.') === false) {
            return [null, $sqlString];
        }

        $parts = explode('.', $sqlString);

        // If we have three parts, the identifier also contains the database name
        if (count($parts) === 3) {
            $table = $parts[1];
        }
        // Otherwise it contains table and column
        else {
            $table = $parts[0];
        }

        // Column is always the last part
        $column = end($parts);

        return [$table, $column];
    }
1110524
public String[] getColumnsBySql(Sql sql) throws SQLException {
		String columns[] = null;
		
		List<Map<String, String>> table = getMetadataBySql(sql);
		if (table != null) {
			columns = new String[table.size()];
			for (int i = 0 ; i < table.size() ; i++) {
				Map<String, String> row = table.get(i);
				if (row.containsKey("NAME") && row.get("NAME") != null) {
					columns[i] = row.get("NAME");
				}
			}
		}
		
		return columns;
	}
1078572
public function toString()
    {
        if (!$this->_parts['table']) {
            throw new SqlException("Invalid `DROP TABLE` statement, missing `TABLE` clause.");
        }

        return 'DROP TABLE' .
            $this->_buildFlag('IF EXISTS', $this->_parts['ifExists']) .
            $this->_buildChunk($this->dialect()->names($this->_parts['table'])) .
            $this->_buildFlag('CASCADE', $this->_parts['cascade']) .
            $this->_buildFlag('RESTRICT', $this->_parts['restrict']);
    }
66049
protected function getQueryInstanceFromQueryString($queryString)
    {
        if (preg_match('/^[ \r\n\t\f]*(?:(only[ \r\n\t\f]+)|(not[ \r\n\t\f]+))?([^ \r\n\t\f]*)[ \r\n\t\f]*(?:(?:and)?[ \r\n\t\f]*)*$/iD', $queryString, $matches)) {
            $type = $matches[3] === "" ? MediaQuery::TYPE_ALL : $matches[3];
            $query = new MediaQuery($type);
            if (!empty($matches[1])) {
                $query->setIsOnly(true);
            }
            if (!empty($matches[2])) {
                $query->setIsNot(true);
            }
            return $query;
        }
        return null;
    }
360084
protected function guess_id_field_from_sql(string $sql) : string {
        // We are not interested in any subquery/view/conditions for the purpose of this method, so
        // let's reduce the query to the interesting parts by recursively cleaning all
        // contents within parenthesis. If there are problems (null), we keep the text unmodified.
        // So just top-level sql will remain after the reduction.
        $recursiveregexp = '/\((([^()]*|(?R))*)\)/';
        $sql = (preg_replace($recursiveregexp, '', $sql) ?: $sql);
        // Get the list of relevant words from the SQL Query.
        // We explode the SQL by the space character, then trim any extra whitespace (e.g. newlines), before we filter
        // empty value, and finally we re-index the array.
        $sql = rtrim($sql, ';');
        $words = array_map('trim', preg_split('/\s+/', $sql));
        $words = array_filter($words, function($word) {
            return $word !== '';
        });
        $words = array_values($words);
        $uwords = array_map('strtoupper', $words); // Uppercase all them.

        // If the query has boolean operators (UNION, it is the only one we support cross-db)
        // then we cannot guarantee whats coming after the first query, it can be anything.
        if (array_search('UNION', $uwords)) {
            return '';
        }

        if ($firstfrom = array_search('FROM', $uwords)) {
            // Found a FROM keyword.
            // Select the previous word.
            $fieldname = $words[$firstfrom - 1];
            if (is_numeric($fieldname)) {
                return $fieldname;
            }

            if ($hasdot = strpos($fieldname, '.')) {
                // This field is against a table alias. Take off the alias.
                $fieldname = substr($fieldname, $hasdot + 1);
            }

            return $fieldname;

        } else if ((count($words) == 1) && (is_numeric($words[0]))) {
            // Not a real SQL, just a single numerical value - such as one returned by {@link self::add_system_context()}.
            return $words[0];

        } else if ((count($words) == 2) && (strtoupper($words[0]) === 'SELECT') && (is_numeric($words[1]))) {
            // SQL returning a constant numerical value.
            return $words[1];
        }

        return '';
    }
401760
@Override
	public StringBuilder sqlBuilder() {
		StringBuilder builder = this.sqlStringBuilder.get();
		builder.delete(0, builder.length());

		return builder;
	}
1142842
public function trimXmlString($xmlString)
    {
        $xmlString = preg_replace('/[\r\n]+(?![^(]*\))/', '', $xmlString);
        $xmlString = preg_replace('/[\s]{2,}/', ' ', $xmlString);
        $xmlString = str_replace(["\r\n", "\n"], '', $xmlString);

        return trim($xmlString);
    }
6931
public function field($field)
    {
        if (!isset($field['name'])) {
            throw new SqlException("Missing column name.");
        }
        if (!isset($field['use'])) {
            if (isset($field['type'])) {
                $field += $this->type($field['type']);
            } else {
                $field += $this->type('string');
            }
        }
        return $field + [
            'name'      => null,
            'type'      => null,
            'length'    => null,
            'precision' => null,
            'serial'    => false,
            'default'   => null,
            'null'      => null
        ];
    }
999242
private function splitTabSql($sql)
    {
        $result = [];
        //find opening bracket, get the prefix along with it
        $openBracketPos = $this->getDelimiterPos($sql, 0, '(');

        if ($openBracketPos === false) {
            trigger_error('[WARNING] can not find opening bracket in table definition');
            return false;
        }

        $prefix = substr($sql, 0, $openBracketPos + 1); // prefix can not be empty, so do not check it, just trim
        $result[] = trim($prefix);
        $body = substr($sql, strlen($prefix)); // fields, indexes and part after closing bracket

        //split by commas, get part by part
        while (($commaPos = $this->getDelimiterPos($body, 0, ',', true)) !== false) {
            $part = trim(substr($body, 0, $commaPos + 1));//read another part and shorten $body
            if ($part) {
                $result[] = $part;
            }
            $body = substr($body, $commaPos + 1);
        }

        // here we have last field (or index) definition + part after closing bracket (ENGINE, ect)
        $closeBracketPos = $this->getDelimiterRightPosition($body, 0, ')');
        if ($closeBracketPos === false) {
            trigger_error('[WARNING] can not find closing bracket in table definition');
            return false;
        }

        // get last field / index definition before closing bracket
        $part = substr($body, 0, $closeBracketPos);
        $result[] = trim($part);

        // get the suffix part along with the closing bracket
        $suffix = substr($body, $closeBracketPos);
        $suffix = trim($suffix);
        if ($suffix) {
            $result[] = $suffix;
        }

        return $result;
    }
333684
public function sqlQuery($queryString)
    {
        try {
            // execute query
            $query = $this->connection->prepare(
                $queryString,
                [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]
            );
            $query->execute();

            return $query;
        } catch (\PDOException $e) {
            throw new \Exception($e->getMessage());
        }
    }
848913
public function toString()
    {
        if (!$this->_parts['table']) {
            throw new SqlException("Invalid `TRUNCATE` statement, missing `TABLE` clause.");
        }

        return 'DELETE' . $this->_buildClause('FROM', $this->dialect()->names($this->_parts['table'])) . ';' .
            'DELETE' . $this->_buildClause('FROM', $this->dialect()->names('SQLITE_SEQUENCE')) .
                ' WHERE name=' . $this->dialect()->names($this->_parts['table']);
    }
595053
protected function fromString($queryString)
    {
        if (empty($queryString)) {
            return $this;
        }

        $data = array();

        parse_str($queryString, $data);

        $this->fromArray($data);

        return $this;
    }
395297
public function get_sql_filter($data) {
        $field  = $this->field;
        if (is_array($field)) {
            $res = " {$field[0]} = {$field[1]} ";
        } else {
            $res = " {$field} = 0 ";
        }
        return array($res, array());
    }
1079233
public function sql(&$parameters = array())
    {
        // Build each component as needed
        $sql = DB::build_sql($this, $parameters);

        if (empty($sql)) {
            return null;
        }

        if ($this->replacementsOld) {
            $sql = str_replace($this->replacementsOld, $this->replacementsNew, $sql);
        }

        return $sql;
    }
233614
private function formatSql($sql)
    {
        if (isset($this->options['auto_replace']) && !empty(trim($this->options['auto_replace']))) {
            $sql = str_replace($this->options['auto_replace'], $this->getDatabase() .'.'. $this->getTablePrefix(), $sql);
        }
        return $sql;
    }
26423
public static SqlBuilder sql(final String queryStr) {
    SqlBuilder sqlQuery = new SqlBuilder();
    sqlQuery.statement = queryStr;
    return sqlQuery;
  }
624864
protected final Split prepare(List<String> sqls) {
		if (!sqls.isEmpty()) {
			sqlNormalizer = sqls.size() == 1 ? new SqlNormalizer(sqls.get(0)) : new SqlNormalizer(sqls);
			sqlCmdLabel = prefix + ".sql." + sqlNormalizer.getType();
			return startSplit();
		} else {
			return null;
		}
	}