protected function doInsert(array $data = [])
    {
        if (empty($data)) {
            $this->insertEmptyRecord();
            return $this->db->lastInsertId();
        }

        if (!is_int(key($data))) {
            // single record is inserted
            $columns  = implode(', ', array_map([$this->db, 'quoteName'], array_keys($data)));
            $params   = implode(', ', array_fill(0, count($data), '?'));
            $bindings = array_values($data);
        }
        else {
            // Bulk insert...
            $keys = [];
            foreach ($data as $row) {
                if (empty($row)) {
                    throw new InvalidArgumentException('Cannot insert an empty record on bulk mode.');
                }
                $keys = array_merge($keys, $row);
            }
            $keys     = array_keys($keys);
            $columns  = implode(', ', array_map([$this->db, 'quoteName'], $keys));
            $params   = implode(', ', array_fill(0, count($keys), '?'));
            $bindings = [];
            $temp     = [];
            foreach ($data as $i => $row) {
                foreach ($keys as $key) {
                    $bindings[] = isset($row[$key]) ? $row[$key] : null;
                }
                $temp[] = $params;
            }
            $params = implode('), (', $temp);
        }

        $table = implode(', ', $this->from);

        /** @noinspection SqlDialectInspection */
        $query = "INSERT INTO $table ($columns) VALUES ($params)";

        $this->db->exec($query, $bindings);

        return $this->db->lastInsertId();
    }