text:
#include "libavcodec/flac.h"
#include "avformat.h"
#include "flac_picture.h"
#include "internal.h"
#include "rawdec.h"
#include "oggdec.h"
#include "vorbiscomment.h"
#include "replaygain.h"

#define SEEKPOINT_SIZE 18

typedef struct FLACDecContext {
    int found_seektable;
} FLACDecContext;

static void reset_index_position(int64_t metadata_head_size, AVStream *st)
{
    /* the real seek index offset should be the size of metadata blocks with the offset in the frame blocks */
    int i;
    for(i=0; i<st->nb_index_entries; i++) {
        st->index_entries[i].pos += metadata_head_size;
    }
}

static int flac_read_header(AVFormatContext *s)
{
    int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
    uint8_t header[4];
    uint8_t *buffer=NULL;
    FLACDecContext *flac = s->priv_data;
    AVStream *st = avformat_new_stream(s, NULL);
    if (!st)
        return AVERROR(ENOMEM);
    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
    st->codecpar->codec_id = AV_CODEC_ID_FLAC;
    st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
    /* the parameters will be extracted from the compressed bitstream */

    /* if fLaC marker is not found, assume there is no header */
    if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
        avio_seek(s->pb, -4, SEEK_CUR);
        return 0;
    }

    /* process metadata blocks */
    while (!avio_feof(s->pb) && !metadata_last) {
        avio_read(s->pb, header, 4);
        flac_parse_block_header(header, &metadata_last, &metadata_type,
                                   &metadata_size);
        switch (metadata_type) {
        /* allocate and read metadata block for supported types */
        case FLAC_METADATA_TYPE_STREAMINFO:
        case FLAC_METADATA_TYPE_CUESHEET:
        case FLAC_METADATA_TYPE_PICTURE:
        case FLAC_METADATA_TYPE_VORBIS_COMMENT:
        case FLAC_METADATA_TYPE_SEEKTABLE:
            buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
            if (!buffer) {
                return AVERROR(ENOMEM);
            }
            if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
                RETURN_ERROR(AVERROR(EIO));
            }
            break;
        /* skip metadata block for unsupported types */
        default:
            ret = avio_skip(s->pb, metadata_size);
            if (ret < 0)
                return ret;
        }

        if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
            uint32_t samplerate;
            uint64_t samples;

            /* STREAMINFO can only occur once */
            if (found_streaminfo) {
                RETURN_ERROR(AVERROR_INVALIDDATA);
            }
            if (metadata_size != FLAC_STREAMINFO_SIZE) {
                RETURN_ERROR(AVERROR_INVALIDDATA);
            }
            found_streaminfo = 1;
            st->codecpar->extradata      = buffer;
            st->codecpar->extradata_size = metadata_size;
            buffer = NULL;

            /* get sample rate and sample count from STREAMINFO header;
             * other parameters will be extracted by the parser */
            samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
            samples    = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);

            /* set time base and duration */
            if (samplerate > 0) {
                avpriv_set_pts_info(st, 64, 1, samplerate);
                if (samples > 0)
                    st->duration = samples;
            }
        } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
            uint8_t isrc[13];
            uint64_t start;
            const uint8_t *offset;
            int i, chapters, track, ti;
            if (metadata_size < 431)
                RETURN_ERROR(AVERROR_INVALIDDATA);
            offset = buffer + 395;
            chapters = bytestream_get_byte(&offset) - 1;
            if (chapters <= 0)
                RETURN_ERROR(AVERROR_INVALIDDATA);
            for (i = 0; i < chapters; i++) {
                if (offset + 36 - buffer > metadata_size)
                    RETURN_ERROR(AVERROR_INVALIDDATA);
                start = bytestream_get_be64(&offset);
                track = bytestream_get_byte(&offset);
                bytestream_get_buffer(&offset, isrc, 12);
                isrc[12] = 0;
                offset += 14;
                ti = bytestream_get_byte(&offset);
                if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
                offset += ti * 12;
                avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
            }
            av_freep(&buffer);
        } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
            ret = ff_flac_parse_picture(s, buffer, metadata_size);
            av_freep(&buffer);
            if (ret < 0) {
                av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
                return ret;
            }
        } else if (metadata_type == FLAC_METADATA_TYPE_SEEKTABLE) {
            const uint8_t *seekpoint = buffer;
            int i, seek_point_count = metadata_size/SEEKPOINT_SIZE;
            flac->found_seektable = 1;
            if ((s->flags&AVFMT_FLAG_FAST_SEEK)) {
                for(i=0; i<seek_point_count; i++) {
                    int64_t timestamp = bytestream_get_be64(&seekpoint);
                    int64_t pos = bytestream_get_be64(&seekpoint);
                    /* skip number of samples */
                    bytestream_get_be16(&seekpoint);
                    av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
                }
            }
            av_freep(&buffer);
        }
        else {

            /* STREAMINFO must be the first block */
            if (!found_streaminfo) {
                RETURN_ERROR(AVERROR_INVALIDDATA);
            }
            /* process supported blocks other than STREAMINFO */
            if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
                AVDictionaryEntry *chmask;

                ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
                if (ret < 0) {
                    av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
                } else if (ret > 0) {
                    s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
                }

                /* parse the channels mask if present */
                chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
                if (chmask) {
                    uint64_t mask = strtol(chmask->value, NULL, 0);
                    if (!mask || mask & ~0x3ffffULL) {
                        av_log(s, AV_LOG_WARNING,
                               "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
                    } else {
                        st->codecpar->channel_layout = mask;
                        av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
                    }
                }
            }
            av_freep(&buffer);
        }
    }

    ret = ff_replaygain_export(st, s->metadata);
    if (ret < 0)
        return ret;

    reset_index_position(avio_tell(s->pb), st);
    return 0;

fail:
    av_free(buffer);
    return ret;
}

static int raw_flac_probe(AVProbeData *p)
{
    if ((p->buf[2] & 0xF0) == 0)    // blocksize code invalid
        return 0;
    if ((p->buf[2] & 0x0F) == 0x0F) // sample rate code invalid
        return 0;
    if ((p->buf[3] & 0xF0) >= FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE << 4)
        // channel mode invalid
        return 0;
    if ((p->buf[3] & 0x06) == 0x06) // bits per sample code invalid
        return 0;
    if ((p->buf[3] & 0x01) == 0x01) // reserved bit set
        return 0;
    return AVPROBE_SCORE_EXTENSION / 4 + 1;
}

static int flac_probe(AVProbeData *p)
{
    if ((AV_RB16(p->buf) & 0xFFFE) == 0xFFF8)
        return raw_flac_probe(p);
    if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
        return 0;
    return AVPROBE_SCORE_EXTENSION;
}

static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
                                             int64_t *ppos, int64_t pos_limit)
{
    AVPacket pkt, out_pkt;
    AVStream *st = s->streams[stream_index];
    AVCodecParserContext *parser;
    int ret;
    int64_t pts = AV_NOPTS_VALUE;

    if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
        return AV_NOPTS_VALUE;

    av_init_packet(&pkt);
    parser = av_parser_init(st->codecpar->codec_id);
    if (!parser){
        return AV_NOPTS_VALUE;
    }
    parser->flags |= PARSER_FLAG_USE_CODEC_TS;

    for (;;){
        ret = ff_raw_read_partial_packet(s, &pkt);
        if (ret < 0){
            if (ret == AVERROR(EAGAIN))
                continue;
            else
                break;
        }
        av_init_packet(&out_pkt);
        av_parser_parse2(parser, st->internal->avctx,
                         &out_pkt.data, &out_pkt.size, pkt.data, pkt.size,
                         pkt.pts, pkt.dts, *ppos);

        av_packet_unref(&pkt);
        if (out_pkt.size){
            int size = out_pkt.size;
            if (parser->pts != AV_NOPTS_VALUE){
                // seeking may not have started from beginning of a frame
                // calculate frame start position from next frame backwards
                *ppos = parser->next_frame_offset - size;
                pts = parser->pts;
                break;
            }
        }
    }
    av_parser_close(parser);
    return pts;
}

static int flac_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
    int index;
    int64_t pos;
    AVIndexEntry e;
    FLACDecContext *flac = s->priv_data;

    if (!flac->found_seektable || !(s->flags&AVFMT_FLAG_FAST_SEEK)) {
        return -1;
    }

    index = av_index_search_timestamp(s->streams[0], timestamp, flags);
    if(index<0 || index >= s->streams[0]->nb_index_entries)
        return -1;

    e = s->streams[0]->index_entries[index];
    pos = avio_seek(s->pb, e.pos, SEEK_SET);
    if (pos >= 0) {
        return 0;
    }
    return -1;
}

AVInputFormat ff_flac_demuxer = {
    .name           = "flac",
    .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
    .read_probe     = flac_probe,
    .read_header    = flac_read_header,
    .read_packet    = ff_raw_read_partial_packet,
    .read_seek      = flac_seek,
    .read_timestamp = flac_read_timestamp,
    .flags          = AVFMT_GENERIC_INDEX,
    .extensions     = "flac",
    .raw_codec_id   = AV_CODEC_ID_FLAC,
    .priv_data_size = sizeof(FLACDecContext),
};


text:
climux [![GoDoc](https://godoc.org/github.com/fredr/climux?status.svg)](https://godoc.org/github.com/fredr/climux)
======

A mux for the cli in go

### Install
    go get github.com/fredr/climux

### Test
    go test -v

### Use
```go
package main

import (
	"fmt"

	"github.com/fredr/climux"
)

func main() {

	r := climux.NewRouter()
	r.HandleFunc("hello {name}", hello, "says hello to {name}")
	r.HandleFunc("hi {firstname} [lastname]", hi, "says hi to {firstname} with [lastname] if present")
	r.HandleFunc("help", help(r), "shows help")
	r.NotFoundHandler = help(r)

	climux.Handle(r)
}

func help(router *climux.Router) climux.Handler {
	return func(r *climux.Request) {
		fmt.Println("commands:")
		for _, route := range router.Routes {
			fmt.Println(route)
		}
	}
}

func hello(r *climux.Request) {
	fmt.Printf("Hello, %s!", r.Vars()["name"])
}

func hi(r *climux.Request) {
	if lastName, ok := r.Vars()["lastname"]; ok {
		fmt.Printf("Hi, %s %s!", r.Vars()["firstname"], lastName)
	} else {
		fmt.Printf("Hi, %s!", r.Vars()["firstname"])
	}
}
```

redpajama_set_name: RedPajamaGithub


text:
package org.jetbrains.plugins.scala.testingSupport.uTest.utils;

import org.jetbrains.plugins.scala.testingSupport.MyJavaConverters;
import org.jetbrains.plugins.scala.testingSupport.uTest.UTestPath;
import org.jetbrains.plugins.scala.testingSupport.uTest.UTestRunExpectedError;
import utest.framework.Tree;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;

import static org.jetbrains.plugins.scala.testingSupport.uTest.utils.UTestErrorUtils.*;

public class UTestTreeUtils {

    private UTestTreeUtils() {
    }

    public static void traverseParents(UTestPath currentPath, Consumer<UTestPath> consumer) {
        UTestPath parent = currentPath.parent();
        if (parent != null) {
            consumer.accept(parent);
            traverseParents(parent, consumer);
        }
    }

    public static void traverseLeaveNodes(Tree<String> names, UTestPath currentPath, Consumer<UTestPath> leafConsumer) throws UTestRunExpectedError {
        List<Tree<String>> children = getChildren(names);
        boolean isLeaf = children.isEmpty();
        if (isLeaf) {
            leafConsumer.accept(currentPath);
        } else {
            for (Tree<String> child : children)
                traverseLeaveNodes(child, currentPath.append(child.value()), leafConsumer);
        }
    }

    public static List<Tree<String>> getChildren(Tree<String> names) throws UTestRunExpectedError {
        scala.collection.Seq<Tree<String>> children;
        try {
            children = names.children();
        } catch (NoSuchMethodError error) {
            children = getChildrenScala_2_13(names);
        }
        return MyJavaConverters.toJava(children);
    }

    @SuppressWarnings("unchecked")
    private static scala.collection.Seq<Tree<String>> getChildrenScala_2_13(Tree<String> names) throws UTestRunExpectedError {
        scala.collection.Seq<Tree<String>> children;
        try {
            Class<?> clazz = names.getClass();
            Method method = clazz.getMethod("children");
            children = (scala.collection.Seq<Tree<String>>) method.invoke(names);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            throw expectedError(errorMessage(e));
        }
        return children;
    }

    /**
     * testPath = x.y
     *
     * root =   x (can contains extra empty root node)
     *        /   \
     *      y      a
     *     / \       \
     *    z   b       c
     *
     * result: y
     *        / \
     *       z   b
     */
    public static Tree<String> getTestsSubTree(Tree<String> root, UTestPath testPath) throws UTestRunExpectedError {
        Tree<String> currentNode = root;

        for (String nodeName : testPath.getPath()) {
            Tree<String> matchingChild = findChildWithName(currentNode, nodeName);
            if (matchingChild != null)
                currentNode = matchingChild;
            else
                throw new RuntimeException("Failure in test pathing for test " + testPath);
        }

        return currentNode;
    }

    /**
     * testPath = x.y
     *
     * root =   x (can contains extra empty root node)
     *        /   \
     *      y      a
     *     / \       \
     *    z   b       c
     *
     * result:  x
     *        /
     *      y
     *     / \
     *    z   b
     */
    public static Tree<String> getTestsSubTreeWithPathToRoot(Tree<String> root, UTestPath testPath) throws UTestRunExpectedError {
        if (testPath.getPath().isEmpty()) return null;

        Tree<String> current = root;
        List<String> walkupNodes = new LinkedList<>();

        for (String nodeName: testPath.getPath()) {
            Tree<String> matchingChild = findChildWithName(current, nodeName);
            if (matchingChild != null) {
                if (current != root)
                    walkupNodes.add(current.value());
                current = matchingChild;
            } else {
                return null;
            }
        }

        Collections.reverse(walkupNodes);
        for (String walkup : walkupNodes) {
            List<Tree<String>> dummyChildren = Collections.singletonList(current);
            current = newTree(walkup, dummyChildren);
        }
        return current;
    }

    private static Tree<String> findChildWithName(Tree<String> node, String childNodeName) throws UTestRunExpectedError {
        return UTestTreeUtils.getChildren(node).stream()
                .filter(c -> c.value().equals(childNodeName))
                .findAny().orElse(null);
    }

    private static Tree<String> newTree(String walkup, List<Tree<String>> children) throws UTestRunExpectedError {
        scala.collection.immutable.List<Tree<String>> childrenScala = MyJavaConverters.toScala(children);
        Tree<String> tree;
        try {
            tree = new Tree<>(walkup, childrenScala);
        } catch (NoSuchMethodError error) {
            tree = newTree_213(walkup, childrenScala);
        }
        return tree;
    }

    @SuppressWarnings({"rawtypes", "JavaReflectionMemberAccess", "unchecked"})
    private static <T> Tree<T> newTree_213(T walkup, scala.collection.immutable.List<Tree<String>> childrenScala) throws UTestRunExpectedError {
        try {
            Class<Tree> clazz = Tree.class;
            Constructor<Tree> constructor = clazz.getConstructor(java.lang.Object.class, scala.collection.immutable.Seq.class);
            return (Tree<T>) constructor.newInstance(walkup, childrenScala);
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            throw expectedError(errorMessage(e));
        }
    }
}


text:
#ifndef __DRIVERS_SERIAL_H
#define __DRIVERS_SERIAL_H

struct serial_chip {
	const struct serial_ops *ops;
};

struct serial_ops {
	void (*putc)(struct serial_chip *chip, int ch);
	void (*flush)(struct serial_chip *chip);
	bool (*have_rx_data)(struct serial_chip *chip);
	int (*getchar)(struct serial_chip *chip);
};

#endif /*__DRIVERS_SERIASERIAL_H*/


text:
package com.ist.cipherchat.gui;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.ist.cipherchat.R;
import com.ist.cipherchat.networking.Core;
import com.ist.cipherchat.networking.Globals;
import com.ist.cipherchat.networking.PhoneServerSocketHandler;
import com.ist.cipherchat.networking.ThreadComm;

public class Contacts extends Activity implements OnItemClickListener {

	Context androidContext;
	public static String[] users = new String[0];

	String otherusername;
	String thisUsername;

	public final static String EXTRA_ENUM = "com.ist.cryptochat.ENUM";
	public final static String EXTRA_THIS_USERNAME = "com.ist.cryptochat.EXTRA_THIS_USERNAME";
	public final static String EXTRA_OTHER_USERNAME = "com.ist.cryptochat.EXTRA_OTHER_USERNAME";

	public static ThreadComm threadComm = new ThreadComm();

	public static Core sslSocketHandler;
	public static PhoneServerSocketHandler serverSocket;

	public static CyclicBarrier barrier = new CyclicBarrier(2);

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contacts);

		androidContext = this.getApplicationContext();

		Intent intent = getIntent();

		thisUsername = intent.getStringExtra(ChooseServerActivity.EXTRA_USERNAME);
		String password = intent.getStringExtra(ChooseServerActivity.EXTRA_PASSWORD);
		String option = intent.getStringExtra(ChooseServerActivity.EXTRA_OPTION);
		String serverIP = intent.getStringExtra(ChooseServerActivity.EXTRA_SERVER_IP);

		Globals.setThisUsername(thisUsername);

		int serverPort = Integer.parseInt(intent.getStringExtra(ChooseServerActivity.EXTRA_SERVER_PORT));

		if (serverPort < 0 || serverPort > 65535)
			System.exit(-1);

		// instantiates the ssl socket handler class (static) that connects to the server.
		Contacts.sslSocketHandler = new Core(serverPort, serverIP, thisUsername, password, option, androidContext);

		Thread t = new Thread(Contacts.sslSocketHandler);
		t.start();

		try {
			try {
				barrier.await();
			} catch (BrokenBarrierException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		final ListView listview = (ListView) findViewById(R.id.listView1);

		String[] usersLocal;

		if (threadComm.error) {

			String toastMessage = resolveString();

			Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_LONG).show();

			usersLocal = new String[0];

			if (threadComm.contactsFail || threadComm.registerFail) {
				threadComm.clean();
				finish();
				return;
			}

			threadComm.clean();
		} else {
			usersLocal = Contacts.users;
			threadComm.clean();
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, usersLocal);

		listview.setAdapter(adapter);

		listview.setOnItemClickListener(this);

		Contacts.serverSocket = new PhoneServerSocketHandler(androidContext, this);

		t = new Thread(Contacts.serverSocket);
		t.start();

		Log.d("register", "ServerSock open on:" + PhoneServerSocketHandler.serverSocket.getInetAddress().toString());
	}

	private String resolveString() {

		if (threadComm.loginFail)
			return "Failed to Login. Please try again.";
		if (threadComm.registerFail)
			return "Failed to register. Please try again.";
		if (threadComm.contactsFail)
			return "Failed to retrieve contacts. Please try again.";
		if (threadComm.noUsers)
			return "Sorry, there are no online contacts";

		return "Alguma coisa esta mal";
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chat, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		String name = (String) parent.getItemAtPosition(position);
		Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();

		Intent forwardIntent = new Intent(this, ChatActivity.class);

		Origin origin = Origin.CONTACTS;

		forwardIntent.putExtra(EXTRA_ENUM, origin.toString());

		forwardIntent.putExtra(EXTRA_THIS_USERNAME, thisUsername);
		forwardIntent.putExtra(EXTRA_OTHER_USERNAME, name);

		ChatActivity.origin = Origin.CONTACTS;
		startActivityForResult(forwardIntent, 777);
	}
}


text:
Query Builder
=============

Built on top of [Database Access Objects](db-dao.md), query builder allows you to construct a SQL query
in a programmatic and DBMS-agnostic way. Compared to writing raw SQL statements, using query builder will help you write
more readable SQL-related code and generate more secure SQL statements.

Using query builder usually involves two steps:

1. Build a [[yii\db\Query]] object to represent different parts (e.g. `SELECT`, `FROM`) of a SELECT SQL statement.
2. Execute a query method (e.g. `all()`) of [[yii\db\Query]] to retrieve data from the database.

The following code shows a typical way of using query builder:

```php
$rows = (new \yii\db\Query())
    ->select(['id', 'email'])
    ->from('user')
    ->where(['last_name' => 'Smith'])
    ->limit(10)
    ->all();
```

The above code generates and executes the following SQL query, where the `:last_name` parameter is bound with the
string `'Smith'`.

```sql
SELECT `id`, `email`
FROM `user`
WHERE `last_name` = :last_name
LIMIT 10
```

> Info: You usually mainly work with [[yii\db\Query]] instead of [[yii\db\QueryBuilder]]. The latter is invoked
  by the former implicitly when you call one of the query methods. [[yii\db\QueryBuilder]] is the class responsible
  for generating DBMS-dependent SQL statements (e.g. quoting table/column names differently) from DBMS-independent
  [[yii\db\Query]] objects.

## Building Queries

To build a [[yii\db\Query]] object, you call different query building methods to specify different parts of
a SQL query. The names of these methods resemble the SQL keywords used in the corresponding parts of the SQL
statement. For example, to specify the `FROM` part of a SQL query, you would call the [[yii\db\Query::from()|from()]] method.
All the query building methods return the query object itself, which allows you to chain multiple calls together.

In the following, we will describe the usage of each query building method.

### [[yii\db\Query::select()|select()]]

The [[yii\db\Query::select()|select()]] method specifies the `SELECT` fragment of a SQL statement. You can specify
columns to be selected in either an array or a string, like the following. The column names being selected will
be automatically quoted when the SQL statement is being generated from a query object.

```php
$query->select(['id', 'email']);

// equivalent to:

$query->select('id, email');
```

The column names being selected may include table prefixes and/or column aliases, like you do when writing raw SQL queries.
For example,

```php
$query->select(['user.id AS user_id', 'email']);

// equivalent to:

$query->select('user.id AS user_id, email');
```

If you are using the array format to specify columns, you can also use the array keys to specify the column aliases.
For example, the above code can be rewritten as follows,

```php
$query->select(['user_id' => 'user.id', 'email']);
```

If you do not call the [[yii\db\Query::select()|select()]] method when building a query, `*` will be selected, which
means selecting *all* columns.

Besides column names, you can also select DB expressions. You must use the array format when selecting a DB expression
that contains commas to avoid incorrect automatic name quoting. For example,

```php
$query->select(["CONCAT(first_name, ' ', last_name) AS full_name", 'email']);
```

As with all places where raw SQL is involved, you may use the [DBMS agnostic quoting syntax](db-dao.md#quoting-table-and-column-names)
for table and column names when writing DB expressions in select.

Starting from version 2.0.1, you may also select sub-queries. You should specify each sub-query in terms of
a [[yii\db\Query]] object. For example,

```php
$subQuery = (new Query())->select('COUNT(*)')->from('user');

// SELECT `id`, (SELECT COUNT(*) FROM `user`) AS `count` FROM `post`
$query = (new Query())->select(['id', 'count' => $subQuery])->from('post');
```

To select distinct rows, you may call [[yii\db\Query::distinct()|distinct()]], like the following:

```php
// SELECT DISTINCT `user_id` ...
$query->select('user_id')->distinct();
```

You can call [[yii\db\Query::addSelect()|addSelect()]] to select additional columns. For example,

```php
$query->select(['id', 'username'])
    ->addSelect(['email']);
```

### [[yii\db\Query::from()|from()]]

The [[yii\db\Query::from()|from()]] method specifies the `FROM` fragment of a SQL statement. For example,

```php
// SELECT * FROM `user`
$query->from('user');
```

You can specify the table(s) being selected from in either a string or an array. The table names may contain
schema prefixes and/or table aliases, like you do when writing raw SQL statements. For example,

```php
$query->from(['public.user u', 'public.post p']);

// equivalent to:

$query->from('public.user u, public.post p');
```

If you are using the array format, you can also use the array keys to specify the table aliases, like the following:

```php
$query->from(['u' => 'public.user', 'p' => 'public.post']);
```

Besides table names, you can also select from sub-queries by specifying them in terms of [[yii\db\Query]] objects.
For example,

```php
$subQuery = (new Query())->select('id')->from('user')->where('status=1');

// SELECT * FROM (SELECT `id` FROM `user` WHERE status=1) u
$query->from(['u' => $subQuery]);
```

#### Prefixes
Also a default [[yii\db\Connection::$tablePrefix|tablePrefix]] can be applied. Implementation instructions
are in the ["Quoting Tables" section of the "Database Access Objects" guide](guide-db-dao.html#quoting-table-and-column-names).

### [[yii\db\Query::where()|where()]]

The [[yii\db\Query::where()|where()]] method specifies the `WHERE` fragment of a SQL query. You can use one of
the four formats to specify a `WHERE` condition:

- string format, e.g., `'status=1'`
- hash format, e.g. `['status' => 1, 'type' => 2]`
- operator format, e.g. `['like', 'name', 'test']`
- object format, e.g. `new LikeCondition('name', 'LIKE', 'test')`

#### String Format

String format is best used to specify very simple conditions or if you need to use built-in functions of the DBMS.
It works as if you are writing a raw SQL. For example,

```php
$query->where('status=1');

// or use parameter binding to bind dynamic parameter values
$query->where('status=:status', [':status' => $status]);

// raw SQL using MySQL YEAR() function on a date field
$query->where('YEAR(somedate) = 2015');
```

Do NOT embed variables directly in the condition like the following, especially if the variable values come from
end user inputs, because this will make your application subject to SQL injection attacks.

```php
// Dangerous! Do NOT do this unless you are very certain $status must be an integer.
$query->where("status=$status");
```

When using `parameter binding`, you may call [[yii\db\Query::params()|params()]] or [[yii\db\Query::addParams()|addParams()]]
to specify parameters separately.

```php
$query->where('status=:status')
    ->addParams([':status' => $status]);
```

As with all places where raw SQL is involved, you may use the [DBMS agnostic quoting syntax](db-dao.md#quoting-table-and-column-names)
for table and column names when writing conditions in string format.

#### Hash Format

Hash format is best used to specify multiple `AND`-concatenated sub-conditions each being a simple equality assertion.
It is written as an array whose keys are column names and values the corresponding values that the columns should be.
For example,

```php
// ...WHERE (`status` = 10) AND (`type` IS NULL) AND (`id` IN (4, 8, 15))
$query->where([
    'status' => 10,
    'type' => null,
    'id' => [4, 8, 15],
]);
```

As you can see, the query builder is intelligent enough to properly handle values that are nulls or arrays.

You can also use sub-queries with hash format like the following:

```php
$userQuery = (new Query())->select('id')->from('user');

// ...WHERE `id` IN (SELECT `id` FROM `user`)
$query->where(['id' => $userQuery]);
```

Using the Hash Format, Yii internally applies parameter binding for values, so in contrast to the [string format](#string-format),
here you do not have to add parameters manually. However, note that Yii never escapes column names, so if you pass
a variable obtained from user side as a column name without any additional checks, the application will become vulnerable
to SQL injection attack. In order to keep the application secure, either do not use variables as column names or
filter variable against white list. In case you need to get column name from user, read the [Filtering Data](output-data-widgets.md#filtering-data)
guide article. For example the following code is vulnerable:

```php
// Vulnerable code:
$column = $request->get('column');
$value = $request->get('value');
$query->where([$column => $value]);
// $value is safe, but $column name won't be encoded!
```

#### Operator Format

Operator format allows you to specify arbitrary conditions in a programmatic way. It takes the following format:

```php
[operator, operand1, operand2, ...]
```

where the operands can each be specified in string format, hash format or operator format recursively, while
the operator can be one of the following:

- `and`: the operands should be concatenated together using `AND`. For example,
  `['and', 'id=1', 'id=2']` will generate `id=1 AND id=2`. If an operand is an array,
  it will be converted into a string using the rules described here. For example,
  `['and', 'type=1', ['or', 'id=1', 'id=2']]` will generate `type=1 AND (id=1 OR id=2)`.
  The method will NOT do any quoting or escaping.

- `or`: similar to the `and` operator except that the operands are concatenated using `OR`.

- `not`: requires only operand 1, which will be wrapped in `NOT()`. For example, `['not', 'id=1']` will generate `NOT (id=1)`. Operand 1 may also be an array to describe multiple expressions. For example `['not', ['status' => 'draft', 'name' => 'example']]` will generate `NOT ((status='draft') AND (name='example'))`.

- `between`: operand 1 should be the column name, and operand 2 and 3 should be the
   starting and ending values of the range that the column is in.
   For example, `['between', 'id', 1, 10]` will generate `id BETWEEN 1 AND 10`.
   In case you need to build a condition where value is between two columns (like `11 BETWEEN min_id AND max_id`),
   you should use [[yii\db\conditions\BetweenColumnsCondition|BetweenColumnsCondition]].
   See [Conditions – Object Format](#object-format) chapter to learn more about object definition of conditions.

- `not between`: similar to `between` except the `BETWEEN` is replaced with `NOT BETWEEN`
  in the generated condition.

- `in`: operand 1 should be a column or DB expression. Operand 2 can be either an array or a `Query` object.
  It will generate an `IN` condition. If Operand 2 is an array, it will represent the range of the values
  that the column or DB expression should be; If Operand 2 is a `Query` object, a sub-query will be generated
  and used as the range of the column or DB expression. For example,
  `['in', 'id', [1, 2, 3]]` will generate `id IN (1, 2, 3)`.
  The method will properly quote the column name and escape values in the range.
  The `in` operator also supports composite columns. In this case, operand 1 should be an array of the columns,
  while operand 2 should be an array of arrays or a `Query` object representing the range of the columns.
  For example, `['in', ['id', 'name'], [['id' => 1, 'name' => 'oy']]]` will generate `(id, name) IN ((1, 'oy'))`.

- `not in`: similar to the `in` operator except that `IN` is replaced with `NOT IN` in the generated condition.

- `like`: operand 1 should be a column or DB expression, and operand 2 be a string or an array representing
  the values that the column or DB expression should be like.
  For example, `['like', 'name', 'tester']` will generate `name LIKE '%tester%'`.
  When the value range is given as an array, multiple `LIKE` predicates will be generated and concatenated
  using `AND`. For example, `['like', 'name', ['test', 'sample']]` will generate
  `name LIKE '%test%' AND name LIKE '%sample%'`.
  You may also provide an optional third operand to specify how to escape special characters in the values.
  The operand should be an array of mappings from the special characters to their
  escaped counterparts. If this operand is not provided, a default escape mapping will be used.
  You may use `false` or an empty array to indicate the values are already escaped and no escape
  should be applied. Note that when using an escape mapping (or the third operand is not provided),
  the values will be automatically enclosed within a pair of percentage characters.

  > Note: When using PostgreSQL you may also use [`ilike`](http://www.postgresql.org/docs/8.3/static/functions-matching.html#FUNCTIONS-LIKE)
  > instead of `like` for case-insensitive matching.

- `or like`: similar to the `like` operator except that `OR` is used to concatenate the `LIKE`
  predicates when operand 2 is an array.

- `not like`: similar to the `like` operator except that `LIKE` is replaced with `NOT LIKE`
  in the generated condition.

- `or not like`: similar to the `not like` operator except that `OR` is used to concatenate
  the `NOT LIKE` predicates.

- `exists`: requires one operand which must be an instance of [[yii\db\Query]] representing the sub-query.
  It will build an `EXISTS (sub-query)` expression.

- `not exists`: similar to the `exists` operator and builds a `NOT EXISTS (sub-query)` expression.

- `>`, `<=`, or any other valid DB operator that takes two operands: the first operand must be a column name
  while the second operand a value. For example, `['>', 'age', 10]` will generate `age>10`.

Using the Operator Format, Yii internally uses parameter binding for values, so in contrast to the [string format](#string-format),
here you do not have to add parameters manually. However, note that Yii never escapes column names, so if you pass
a variable as a column name, the application will likely become vulnerable to SQL injection attack. In order to keep
application secure, either do not use variables as column names or filter variable against white list.
In case you need to get column name from user, read the [Filtering Data](output-data-widgets.md#filtering-data)
guide article. For example the following code is vulnerable:

```php
// Vulnerable code:
$column = $request->get('column');
$value = $request->get('value');
$query->where(['=', $column, $value]);
// $value is safe, but $column name won't be encoded!
```

#### Object Format

Object Form is available since 2.0.14 and is both most powerful and most complex way to define conditions.
You need to follow it either if you want to build your own abstraction over query builder or if you want to implement
your own complex conditions.

Instances of condition classes are immutable. Their only purpose is to store condition data and provide getters
for condition builders. Condition builder is a class that holds the logic that transforms data
stored in condition into the SQL expression.

Internally the formats described above are implicitly converted to object format prior to building raw SQL,
so it is possible to combine formats in a single condition:

```php
$query->andWhere(new OrCondition([
    new InCondition('type', 'in', $types),
    ['like', 'name', '%good%'],
    'disabled=false'
]))
```

Conversion from operator format into object format is performed according to
[[yii\db\QueryBuilder::conditionClasses|QueryBuilder::conditionClasses]] property, that maps operators names
to representative class names:

- `AND`, `OR` -> `yii\db\conditions\ConjunctionCondition`
- `NOT` -> `yii\db\conditions\NotCondition`
- `IN`, `NOT IN` -> `yii\db\conditions\InCondition`
- `BETWEEN`, `NOT BETWEEN` -> `yii\db\conditions\BetweenCondition`

And so on.

Using the object format makes it possible to create your own conditions or to change the way default ones are built.
See [Adding Custom Conditions and Expressions](#adding-custom-conditions-and-expressions) chapter to learn more.

#### Appending Conditions

You can use [[yii\db\Query::andWhere()|andWhere()]] or [[yii\db\Query::orWhere()|orWhere()]] to append
additional conditions to an existing one. You can call them multiple times to append multiple conditions
separately. For example,

```php
$status = 10;
$search = 'yii';

$query->where(['status' => $status]);

if (!empty($search)) {
    $query->andWhere(['like', 'title', $search]);
}
```

If `$search` is not empty, the following `WHERE` condition will be generated:

```sql
WHERE (`status` = 10) AND (`title` LIKE '%yii%')
```

#### Filter Conditions

When building `WHERE` conditions based on input from end users, you usually want to ignore those input values, that are empty.
For example, in a search form that allows you to search by username and email, you would like to ignore the username/email
condition if the user does not enter anything in the username/email input field. You can achieve this goal by
using the [[yii\db\Query::filterWhere()|filterWhere()]] method:

```php
// $username and $email are from user inputs
$query->filterWhere([
    'username' => $username,
    'email' => $email,
]);
```

The only difference between [[yii\db\Query::filterWhere()|filterWhere()]] and [[yii\db\Query::where()|where()]]
is that the former will ignore empty values provided in the condition in [hash format](#hash-format). So if `$email`
is empty while `$username` is not, the above code will result in the SQL condition `WHERE username=:username`.

> Info: A value is considered empty if it is `null`, an empty array, an empty string or a string consisting of whitespaces only.

Like [[yii\db\Query::andWhere()|andWhere()]] and [[yii\db\Query::orWhere()|orWhere()]], you can use
[[yii\db\Query::andFilterWhere()|andFilterWhere()]] and [[yii\db\Query::orFilterWhere()|orFilterWhere()]]
to append additional filter conditions to the existing one.

Additionally, there is [[yii\db\Query::andFilterCompare()]] that can intelligently determine operator based on what's
in the value:

```php
$query->andFilterCompare('name', 'John Doe');
$query->andFilterCompare('rating', '>9');
$query->andFilterCompare('value', '<=100');
```

You can also specify operator explicitly:

```php
$query->andFilterCompare('name', 'Doe', 'like');
```

Since Yii 2.0.11 there are similar methods for `HAVING` condition:

- [[yii\db\Query::filterHaving()|filterHaving()]]
- [[yii\db\Query::andFilterHaving()|andFilterHaving()]]
- [[yii\db\Query::orFilterHaving()|orFilterHaving()]]

### [[yii\db\Query::orderBy()|orderBy()]]

The [[yii\db\Query::orderBy()|orderBy()]] method specifies the `ORDER BY` fragment of a SQL query. For example,

```php
// ... ORDER BY `id` ASC, `name` DESC
$query->orderBy([
    'id' => SORT_ASC,
    'name' => SORT_DESC,
]);
```

In the above code, the array keys are column names while the array values are the corresponding order by directions.
The PHP constant `SORT_ASC` specifies ascending sort and `SORT_DESC` descending sort.

If `ORDER BY` only involves simple column names, you can specify it using a string, just like you do when writing
raw SQL statements. For example,

```php
$query->orderBy('id ASC, name DESC');
```

> Note: You should use the array format if `ORDER BY` involves some DB expression.

You can call [[yii\db\Query::addOrderBy()|addOrderBy()]] to add additional columns to the `ORDER BY` fragment.
For example,

```php
$query->orderBy('id ASC')
    ->addOrderBy('name DESC');
```

### [[yii\db\Query::groupBy()|groupBy()]]

The [[yii\db\Query::groupBy()|groupBy()]] method specifies the `GROUP BY` fragment of a SQL query. For example,

```php
// ... GROUP BY `id`, `status`
$query->groupBy(['id', 'status']);
```

If `GROUP BY` only involves simple column names, you can specify it using a string, just like you do when writing
raw SQL statements. For example,

```php
$query->groupBy('id, status');
```

> Note: You should use the array format if `GROUP BY` involves some DB expression.

You can call [[yii\db\Query::addGroupBy()|addGroupBy()]] to add additional columns to the `GROUP BY` fragment.
For example,

```php
$query->groupBy(['id', 'status'])
    ->addGroupBy('age');
```

### [[yii\db\Query::having()|having()]]

The [[yii\db\Query::having()|having()]] method specifies the `HAVING` fragment of a SQL query. It takes
a condition which can be specified in the same way as that for [where()](#where). For example,

```php
// ... HAVING `status` = 1
$query->having(['status' => 1]);
```

Please refer to the documentation for [where()](#where) for more details about how to specify a condition.

You can call [[yii\db\Query::andHaving()|andHaving()]] or [[yii\db\Query::orHaving()|orHaving()]] to append
additional conditions to the `HAVING` fragment. For example,

```php
// ... HAVING (`status` = 1) AND (`age` > 30)
$query->having(['status' => 1])
    ->andHaving(['>', 'age', 30]);
```

### [[yii\db\Query::limit()|limit()]] and [[yii\db\Query::offset()|offset()]]

The [[yii\db\Query::limit()|limit()]] and [[yii\db\Query::offset()|offset()]] methods specify the `LIMIT`
and `OFFSET` fragments of a SQL query. For example,

```php
// ... LIMIT 10 OFFSET 20
$query->limit(10)->offset(20);
```

If you specify an invalid limit or offset (e.g. a negative value), it will be ignored.

> Info: For DBMS that do not support `LIMIT` and `OFFSET` (e.g. MSSQL), query builder will generate a SQL
  statement that emulates the `LIMIT`/`OFFSET` behavior.

### [[yii\db\Query::join()|join()]]

The [[yii\db\Query::join()|join()]] method specifies the `JOIN` fragment of a SQL query. For example,

```php
// ... LEFT JOIN `post` ON `post`.`user_id` = `user`.`id`
$query->join('LEFT JOIN', 'post', 'post.user_id = user.id');
```

The [[yii\db\Query::join()|join()]] method takes four parameters:

- `$type`: join type, e.g., `'INNER JOIN'`, `'LEFT JOIN'`.
- `$table`: the name of the table to be joined.
- `$on`: optional, the join condition, i.e., the `ON` fragment. Please refer to [where()](#where) for details
   about specifying a condition. Note, that the array syntax does **not** work for specifying a column based
   condition, e.g. `['user.id' => 'comment.userId']` will result in a condition where the user id must be equal
   to the string `'comment.userId'`. You should use the string syntax instead and specify the condition as
   `'user.id = comment.userId'`.
- `$params`: optional, the parameters to be bound to the join condition.

You can use the following shortcut methods to specify `INNER JOIN`, `LEFT JOIN` and `RIGHT JOIN`, respectively.

- [[yii\db\Query::innerJoin()|innerJoin()]]
- [[yii\db\Query::leftJoin()|leftJoin()]]
- [[yii\db\Query::rightJoin()|rightJoin()]]

For example,

```php
$query->leftJoin('post', 'post.user_id = user.id');
```

To join with multiple tables, call the above join methods multiple times, once for each table.

Besides joining with tables, you can also join with sub-queries. To do so, specify the sub-queries to be joined
as [[yii\db\Query]] objects. For example,

```php
$subQuery = (new \yii\db\Query())->from('post');
$query->leftJoin(['u' => $subQuery], 'u.id = author_id');
```

In this case, you should put the sub-query in an array and use the array key to specify the alias.

### [[yii\db\Query::union()|union()]]

The [[yii\db\Query::union()|union()]] method specifies the `UNION` fragment of a SQL query. For example,

```php
$query1 = (new \yii\db\Query())
    ->select("id, category_id AS type, name")
    ->from('post')
    ->limit(10);

$query2 = (new \yii\db\Query())
    ->select('id, type, name')
    ->from('user')
    ->limit(10);

$query1->union($query2);
```

You can call [[yii\db\Query::union()|union()]] multiple times to append more `UNION` fragments.

### [[yii\db\Query::withQuery()|withQuery()]]

The [[yii\db\Query::withQuery()|withQuery()]] method specifies the `WITH` prefix of a SQL query. You can use it instead of subquery for more readability and some unique features (recursive CTE). Read more at [modern-sql](https://modern-sql.com/feature/with). For example, this query will select all nested permissions of `admin` with their children recursively,

```php
$initialQuery = (new \yii\db\Query())
    ->select(['parent', 'child'])
    ->from(['aic' => 'auth_item_child'])
    ->where(['parent' => 'admin']);

$recursiveQuery = (new \yii\db\Query())
    ->select(['aic.parent', 'aic.child'])
    ->from(['aic' => 'auth_item_child'])
    ->innerJoin('t1', 't1.child = aic.parent');

$mainQuery = (new \yii\db\Query())
    ->select(['parent', 'child'])
    ->from('t1')
    ->withQuery($initialQuery->union($recursiveQuery), 't1', true);
```

[[yii\db\Query::withQuery()|withQuery()]] can be called multiple times to prepend more CTE's to main query. Queries will be prepend in same order as they attached. If one of query is recursive then whole CTE become recursive.

## Query Methods

[[yii\db\Query]] provides a whole set of methods for different query purposes:

- [[yii\db\Query::all()|all()]]: returns an array of rows with each row being an associative array of name-value pairs.
- [[yii\db\Query::one()|one()]]: returns the first row of the result.
- [[yii\db\Query::column()|column()]]: returns the first column of the result.
- [[yii\db\Query::scalar()|scalar()]]: returns a scalar value located at the first row and first column of the result.
- [[yii\db\Query::exists()|exists()]]: returns a value indicating whether the query contains any result.
- [[yii\db\Query::count()|count()]]: returns the result of a `COUNT` query.
- Other aggregation query methods, including [[yii\db\Query::sum()|sum($q)]], [[yii\db\Query::average()|average($q)]],
  [[yii\db\Query::max()|max($q)]], [[yii\db\Query::min()|min($q)]]. The `$q` parameter is mandatory for these methods
  and can be either a column name or a DB expression.

For example,

```php
// SELECT `id`, `email` FROM `user`
$rows = (new \yii\db\Query())
    ->select(['id', 'email'])
    ->from('user')
    ->all();

// SELECT * FROM `user` WHERE `username` LIKE `%test%`
$row = (new \yii\db\Query())
    ->from('user')
    ->where(['like', 'username', 'test'])
    ->one();
```

> Note: The [[yii\db\Query::one()|one()]] method only returns the first row of the query result. It does NOT
  add `LIMIT 1` to the generated SQL statement. This is fine and preferred if you know the query will return only one
  or a few rows of data (e.g. if you are querying with some primary keys). However, if the query may potentially
  result in many rows of data, you should call `limit(1)` explicitly to improve the performance, e.g.,
  `(new \yii\db\Query())->from('user')->limit(1)->one()`.

All these query methods take an optional `$db` parameter representing the [[yii\db\Connection|DB connection]] that
should be used to perform a DB query. If you omit this parameter, the `db` [application component](structure-application-components.md) will be used
as the DB connection. Below is another example using the [[yii\db\Query::count()|count()]] query method:

```php
// executes SQL: SELECT COUNT(*) FROM `user` WHERE `last_name`=:last_name
$count = (new \yii\db\Query())
    ->from('user')
    ->where(['last_name' => 'Smith'])
    ->count();
```

When you call a query method of [[yii\db\Query]], it actually does the following work internally:

* Call [[yii\db\QueryBuilder]] to generate a SQL statement based on the current construct of [[yii\db\Query]];
* Create a [[yii\db\Command]] object with the generated SQL statement;
* Call a query method (e.g.  [[yii\db\Command::queryAll()|queryAll()]]) of [[yii\db\Command]] to execute the SQL statement and retrieve the data.

Sometimes, you may want to examine or use the SQL statement built from a [[yii\db\Query]] object. You can
achieve this goal with the following code:

```php
$command = (new \yii\db\Query())
    ->select(['id', 'email'])
    ->from('user')
    ->where(['last_name' => 'Smith'])
    ->limit(10)
    ->createCommand();

// show the SQL statement
echo $command->sql;
// show the parameters to be bound
print_r($command->params);

// returns all rows of the query result
$rows = $command->queryAll();
```

### Indexing Query Results

When you call [[yii\db\Query::all()|all()]], it will return an array of rows which are indexed by consecutive integers.
Sometimes you may want to index them differently, such as indexing by a particular column or expression values.
You can achieve this goal by calling [[yii\db\Query::indexBy()|indexBy()]] before [[yii\db\Query::all()|all()]].
For example,

```php
// returns [100 => ['id' => 100, 'username' => '...', ...], 101 => [...], 103 => [...], ...]
$query = (new \yii\db\Query())
    ->from('user')
    ->limit(10)
    ->indexBy('id')
    ->all();
```

To index by expression values, pass an anonymous function to the [[yii\db\Query::indexBy()|indexBy()]] method:

```php
$query = (new \yii\db\Query())
    ->from('user')
    ->indexBy(function ($row) {
        return $row['id'] . $row['username'];
    })->all();
```

The anonymous function takes a parameter `$row` which contains the current row data and should return a scalar
value which will be used as the index value for the current row.

> Note: In contrast to query methods like [[yii\db\Query::groupBy()|groupBy()]] or [[yii\db\Query::orderBy()|orderBy()]]
> which are converted to SQL and are part of the query, this method works after the data has been fetched from the database.
> That means that only those column names can be used that have been part of SELECT in your query.
> Also if you selected a column with table prefix, e.g. `customer.id`, the result set will only contain `id` so you have to call
> `->indexBy('id')` without table prefix.

### Batch Query

When working with large amounts of data, methods such as [[yii\db\Query::all()]] are not suitable
because they require loading the whole query result into the client's memory. To solve this issue
Yii provides batch query support. The server holds the query result, and the client uses a cursor
to iterate over the result set one batch at a time.

> Warning: There are known limitations and workarounds for the MySQL implementation of batch queries. See below.

Batch query can be used like the following:

```php
use yii\db\Query;

$query = (new Query())
    ->from('user')
    ->orderBy('id');

foreach ($query->batch() as $users) {
    // $users is an array of 100 or fewer rows from the user table
}

// or to iterate the row one by one
foreach ($query->each() as $user) {
    // data is being fetched from the server in batches of 100,
    // but $user represents one row of data from the user table
}
```

The method [[yii\db\Query::batch()]] and [[yii\db\Query::each()]] return an [[yii\db\BatchQueryResult]]
object which implements the `Iterator` interface and thus can be used in the `foreach` construct.
During the first iteration, a SQL query is made to the database. Data is then fetched in batches
in the remaining iterations. By default, the batch size is 100, meaning 100 rows of data are being fetched in each batch.
You can change the batch size by passing the first parameter to the `batch()` or `each()` method.

Compared to the [[yii\db\Query::all()]], the batch query only loads 100 rows of data at a time into the memory.

If you specify the query result to be indexed by some column via [[yii\db\Query::indexBy()]],
the batch query will still keep the proper index.

For example:

```php
$query = (new \yii\db\Query())
    ->from('user')
    ->indexBy('username');

foreach ($query->batch() as $users) {
    // $users is indexed by the "username" column
}

foreach ($query->each() as $username => $user) {
    // ...
}
```

#### Limitations of batch query in MySQL

MySQL implementation of batch queries relies on the PDO driver library. By default, MySQL queries are
[`buffered`](https://secure.php.net/manual/en/mysqlinfo.concepts.buffering.php). This defeats the purpose
of using the cursor to get the data, because it doesn't prevent the whole result set from being
loaded into the client's memory by the driver.

> Note: When `libmysqlclient` is used (typical of PHP5), PHP's memory limit won't count the memory
  used for result sets. It may seem that batch queries work correctly, but in reality the whole
  dataset is loaded into client's memory, and has the potential of using it up.

To disable buffering and reduce client memory requirements, PDO connection property
`PDO::MYSQL_ATTR_USE_BUFFERED_QUERY` must be set to `false`. However, until the whole dataset has
been retrieved, no other query can be made through the same connection. This may prevent `ActiveRecord`
from making a query to get the table schema when it needs to. If this is not a problem
(the table schema is cached already), it is possible to switch the original connection into
unbuffered mode, and then roll back when the batch query is done.

```php
Yii::$app->db->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

// Do batch query

Yii::$app->db->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
```

> Note: In the case of MyISAM, for the duration of the batch query, the table may become locked,
  delaying or denying write access for other connections. When using unbuffered queries,
  try to keep the cursor open for as little time as possible.

If the schema is not cached, or it is necessary to run other queries while the batch query is
being processed, you can create a separate unbuffered connection to the database:

```php
$unbufferedDb = new \yii\db\Connection([
    'dsn' => Yii::$app->db->dsn,
    'username' => Yii::$app->db->username,
    'password' => Yii::$app->db->password,
    'charset' => Yii::$app->db->charset,
]);
$unbufferedDb->open();
$unbufferedDb->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
```

If you want to ensure that the `$unbufferedDb` has exactly the same PDO attributes like the original
buffered `$db` but the `PDO::MYSQL_ATTR_USE_BUFFERED_QUERY` is `false`,
[consider a deep copy of `$db`](https://github.com/yiisoft/yii2/issues/8420#issuecomment-301423833),
set it to false manually.

Then, queries are created normally. The new connection is used to run batch queries and retrieve
results either in batches or one by one:

```php
// getting data in batches of 1000
foreach ($query->batch(1000, $unbufferedDb) as $users) {
    // ...
}

// data is fetched from server in batches of 1000, but is iterated one by one
foreach ($query->each(1000, $unbufferedDb) as $user) {
    // ...
}
```

When the connection is no longer necessary and the result set has been retrieved, it can be closed:

```php
$unbufferedDb->close();
```

> Note: unbuffered query uses less memory on the PHP-side, but can increase the load on the MySQL server.
It is recommended to design your own code with your production practice for extra massive data,
[for example, divide the range for integer keys, loop them with Unbuffered Queries](https://github.com/yiisoft/yii2/issues/8420#issuecomment-296109257).

### Adding custom Conditions and Expressions

As it was mentioned in [Conditions – Object Format](#object-format) chapter, it is possible to create custom condition
classes. For example, let's create a condition that will check that specific columns are less than some value.
Using the operator format, it would look like the following:

```php
[
    'and',
    '>', 'posts', $minLimit,
    '>', 'comments', $minLimit,
    '>', 'reactions', $minLimit,
    '>', 'subscriptions', $minLimit
]
```

When such condition applied once, it is fine. In case it is used multiple times in a single query it can
be optimized a lot. Let's create a custom condition object to demonstrate it.

Yii has a [[yii\db\conditions\ConditionInterface|ConditionInterface]], that must be used to mark classes, that represent
a condition. It requires `fromArrayDefinition()` method implementation, in order to make possible to create condition
from array format. In case you don't need it, you can implement this method with exception throwing.

Since we create our custom condition class, we can build API that suits our task the most.

```php
namespace app\db\conditions;

class AllGreaterCondition implements \yii\db\conditions\ConditionInterface
{
    private $columns;
    private $value;

    /**
     * @param string[] $columns Array of columns that must be greater, than $value
     * @param mixed $value the value to compare each $column against.
     */
    public function __construct(array $columns, $value)
    {
        $this->columns = $columns;
        $this->value = $value;
    }

    public static function fromArrayDefinition($operator, $operands)
    {
        throw new InvalidArgumentException('Not implemented yet, but we will do it later');
    }

    public function getColumns() { return $this->columns; }
    public function getValue() { return $this->vaule; }
}
```

So we can create a condition object:

```php
$conditon = new AllGreaterCondition(['col1', 'col2'], 42);
```

But `QueryBuilder` still does not know, to make an SQL condition out of this object.
Now we need to create a builder for this condition. It must implement [[yii\db\ExpressionBuilderInterface]]
that requires us to implement a `build()` method.

```php
namespace app\db\conditions;

class AllGreaterConditionBuilder implements \yii\db\ExpressionBuilderInterface
{
    use \yii\db\ExpressionBuilderTrait; // Contains constructor and `queryBuilder` property.

    /**
     * @param ExpressionInterface $condition the condition to be built
     * @param array $params the binding parameters.
     * @return AllGreaterCondition
     */
    public function build(ExpressionInterface $expression, array &$params = [])
    {
        $value = $condition->getValue();

        $conditions = [];
        foreach ($expression->getColumns() as $column) {
            $conditions[] = new SimpleCondition($column, '>', $value);
        }

        return $this->queryBuilder->buildCondition(new AndCondition($conditions), $params);
    }
}
```

Then simple let [[yii\db\QueryBuilder|QueryBuilder]] know about our new condition – add a mapping for it to
the `expressionBuilders` array. It could be done right from the application configuration:

```php
'db' => [
    'class' => 'yii\db\mysql\Connection',
    // ...
    'queryBuilder' => [
        'expressionBuilders' => [
            'app\db\conditions\AllGreaterCondition' => 'app\db\conditions\AllGreaterConditionBuilder',
        ],
    ],
],
```

Now we can use our condition in `where()`:

```php
$query->andWhere(new AllGreaterCondition(['posts', 'comments', 'reactions', 'subscriptions'], $minValue));
```

If we want to make it possible to create our custom condition using operator format, we should declare it in
[[yii\db\QueryBuilder::conditionClasses|QueryBuilder::conditionClasses]]:

```php
'db' => [
    'class' => 'yii\db\mysql\Connection',
    // ...
    'queryBuilder' => [
        'expressionBuilders' => [
            'app\db\conditions\AllGreaterCondition' => 'app\db\conditions\AllGreaterConditionBuilder',
        ],
        'conditionClasses' => [
            'ALL>' => 'app\db\conditions\AllGreaterCondition',
        ],
    ],
],
```

And create a real implementation of `AllGreaterCondition::fromArrayDefinition()` method
in `app\db\conditions\AllGreaterCondition`:

```php
namespace app\db\conditions;

class AllGreaterCondition implements \yii\db\conditions\ConditionInterface
{
    // ... see the implementation above

    public static function fromArrayDefinition($operator, $operands)
    {
        return new static($operands[0], $operands[1]);
    }
}
```

After that, we can create our custom condition using shorter operator format:

```php
$query->andWhere(['ALL>', ['posts', 'comments', 'reactions', 'subscriptions'], $minValue]);
```

You might notice, that there was two concepts used: Expressions and Conditions. There is a [[yii\db\ExpressionInterface]]
that should be used to mark objects, that require an Expression Builder class, that implements
[[yii\db\ExpressionBuilderInterface]] to be built. Also there is a [[yii\db\condition\ConditionInterface]], that extends
[[yii\db\ExpressionInterface|ExpressionInterface]] and should be used to objects, that can be created from array definition
as it was shown above, but require builder as well.

To summarise:

- Expression – is a Data Transfer Object (DTO) for a dataset, that can be somehow compiled to some SQL
statement (an operator, string, array, JSON, etc).
- Condition – is an Expression superset, that aggregates multiple Expressions (or scalar values) that can be compiled
to a single SQL condition.

You can create your own classes that implement [[yii\db\ExpressionInterface|ExpressionInterface]] to hide the complexity
of transforming data to SQL statements. You will learn more about other examples of Expressions in the
[next article](db-active-record.md);


text:
package bangsoal.dao;

import bangsoal.dao.specs.QuestionDao;
import bangsoal.entity.Concept;
import bangsoal.entity.Question;
import bangsoal.shared.Common;
import bangsoal.shared.Configuration;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author radityopw
 */
public class QuestionDaoFile implements QuestionDao {

    long conceptId;
    private Map<String, Question> list;

    private Configuration conf = Configuration.getInstance();

    public QuestionDaoFile(long c){
        conceptId = c;
        list = new TreeMap<>();
    }

    @Override
    public void save(Question a) {
        File dir = new File(conf.getWorkingDir(), "questions");
        File dir2 = new File(dir, conceptId + "");
        if (!dir2.exists()) {
            dir2.mkdir();
        }
        File file = new File(dir2, a.getId() + ".data");
        try {
            Common.objectToFile(file.getAbsolutePath(), a);
            list.put(a.getId() + "", a);
        } catch (IOException ex) {
            Logger.getLogger(SubjectDaoFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Question a) {
        File dir = new File(conf.getWorkingDir(), "questions");
        File dir2 = new File(dir, "" + conceptId);
        File file = new File(dir2, a.getId() + ".data");
        if (file.exists()) {
            file.delete();
            list.remove(a.getId() + "");
            a = null;
        }
    }

    @Override
    public Question get(int id) {
        Question c = null;

        if (list == null) {
            getAll();
        }

        Iterator<Question> iterator = list.values().iterator();
        int index = -1;

        while (index < id) {
            c = iterator.next();
            index++;
        }

        return c;
    }

    @Override
    public Collection<Question> getAll() {
        if (list.size() == 0) {
            File dir = new File(conf.getWorkingDir(), "questions");
            File dir2 = new File(dir, "" + conceptId);

            if (!dir2.exists()) {
                dir2.mkdir();
            }
            List<String> subDir = Arrays.asList(dir2.list());

            for (String d : subDir) {
                File ds = new File(dir2, d);
                Question c = (Question) Common.fileToObject(ds.getAbsolutePath());
                list.put(c.getId() + "", c);
            }
        }
        return list.values();
    }

}


text:
describe TrashMan do
  describe ".version" do
    it "provides a version" do
      expect(TrashMan.version).to be
    end
  end
end


text:
    <title>Registration Form</title>
    <

Registration Form

        <form action='/process' method='post'>
               {% with messages = get_flashed_messages() %}
                {% if messages %}
                  {% for message in messages %}

{{message}}

                  {% endfor %}
                {% endif %}
              {% endwith %}
             Email: <input type='text' name='email'>

             First Name: <input type='text' name='first_name'>

             Last Name: <input type='text' name='last_name'>

             Password: <input type='text' name='password'>

             Confirm Password: <input type='text' name='password_confirm'>

             <input type="submit" name="name" value="Submit"/>
        </form>


text:
using System;

namespace Piranha.Data
{
    /// <summary>
    /// Connection between a page and a content.
    /// </summary>
    public interface IContentBlock
    {
        /// <summary>
        /// Gets/sets the unique id.
        /// </summary>
        Guid Id { get; set; }

        /// <summary>
        /// Gets/sets the optional parent id if this page block
        /// is part of a group.
        /// </summary>
        Guid? ParentId { get; set; }

        /// <summary>
        /// Gets/sets the block id.
        /// </summary>
        Guid BlockId { get; set; }

        /// <summary>
        /// Gets/sets the zero based sort index.
        /// </summary>
        int SortOrder { get; set; }

        /// <summary>
        /// Gets/sets the block data.
        /// </summary>
        Block Block { get; set; }
    }
}


text:
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;

namespace Microsoft.NetCore.Analyzers.Security.UnitTests
{
    [Trait(Traits.DataflowAnalysis, Traits.Dataflow.TaintedDataAnalysis)]
    public abstract class TaintedDataAnalyzerTestBase<TCSharpAnalyzer, TVisualBasicAnalyzer>
        where TCSharpAnalyzer : DiagnosticAnalyzer, new()
        where TVisualBasicAnalyzer : DiagnosticAnalyzer, new()
    {
        protected abstract DiagnosticDescriptor Rule { get; }

        protected virtual IEnumerable<string> AdditionalCSharpSources { get; }

        protected virtual IEnumerable<string> AdditionalVisualBasicSources { get; }

        protected DiagnosticResult GetCSharpResultAt(int sinkLine, int sinkColumn, int sourceLine, int sourceColumn, string sink, string sinkContainingMethod, string source, string sourceContainingMethod)
        {
#pragma warning disable RS0030 // Do not used banned APIs
#pragma warning disable RS0030 // Do not used banned APIs
            return new DiagnosticResult(Rule).WithArguments(sink, sinkContainingMethod, source, sourceContainingMethod)
                .WithLocation(sinkLine, sinkColumn)
#pragma warning restore RS0030 // Do not used banned APIs
                .WithLocation(sourceLine, sourceColumn);
#pragma warning restore RS0030 // Do not used banned APIs
        }

        protected async Task VerifyCSharpWithDependenciesAsync(string source, params DiagnosticResult[] expected)
        {
            var test = new CSharpSecurityCodeFixVerifier<TCSharpAnalyzer, EmptyCodeFixProvider>.Test
            {
                ReferenceAssemblies = AdditionalMetadataReferences.DefaultForTaintedDataAnalysis
            };
            test.TestState.AdditionalReferences.Add(AdditionalMetadataReferences.TestReferenceAssembly);

            test.TestState.Sources.Add(source);
            if (AdditionalCSharpSources is object)
            {
                foreach (var additionalSource in AdditionalCSharpSources)
                {
                    test.TestState.Sources.Add(additionalSource);
                }
            }

            test.TestState.ExpectedDiagnostics.AddRange(expected);
            await test.RunAsync();
        }

        protected async Task VerifyCSharpWithDependenciesAsync(string source, (string additionalFile, string fileContent) file, params DiagnosticResult[] expected)
        {
            var test = new CSharpSecurityCodeFixVerifier<TCSharpAnalyzer, EmptyCodeFixProvider>.Test
            {
                ReferenceAssemblies = AdditionalMetadataReferences.DefaultForTaintedDataAnalysis
            };
            test.TestState.AdditionalReferences.Add(AdditionalMetadataReferences.TestReferenceAssembly);

            test.TestState.Sources.Add(source);
            if (AdditionalCSharpSources is object)
            {
                foreach (var additionalSource in AdditionalCSharpSources)
                {
                    test.TestState.Sources.Add(additionalSource);
                }
            }

            test.TestState.AnalyzerConfigFiles.Add(file);

            test.TestState.ExpectedDiagnostics.AddRange(expected);
            await test.RunAsync();
        }

        protected DiagnosticResult GetBasicResultAt(int sinkLine, int sinkColumn, int sourceLine, int sourceColumn, string sink, string sinkContainingMethod, string source, string sourceContainingMethod)
        {
#pragma warning disable RS0030 // Do not used banned APIs
#pragma warning disable RS0030 // Do not used banned APIs
            return new DiagnosticResult(Rule).WithArguments(sink, sinkContainingMethod, source, sourceContainingMethod)
                .WithLocation(sinkLine, sinkColumn)
#pragma warning restore RS0030 // Do not used banned APIs
                .WithLocation(sourceLine, sourceColumn);
#pragma warning restore RS0030 // Do not used banned APIs
        }

        protected async Task VerifyVisualBasicWithDependenciesAsync(string source, params DiagnosticResult[] expected)
        {
            var test = new VisualBasicSecurityCodeFixVerifier<TVisualBasicAnalyzer, EmptyCodeFixProvider>.Test
            {
                ReferenceAssemblies = AdditionalMetadataReferences.DefaultForTaintedDataAnalysis
            };
            test.TestState.AdditionalReferences.Add(AdditionalMetadataReferences.TestReferenceAssembly);

            test.TestState.Sources.Add(source);
            if (AdditionalVisualBasicSources is object)
            {
                foreach (var additionalSource in AdditionalVisualBasicSources)
                {
                    test.TestState.Sources.Add(additionalSource);
                }
            }

            test.TestState.ExpectedDiagnostics.AddRange(expected);
            await test.RunAsync();
        }
    }
}


text:
<?php

namespace Symfony\Component\HttpClient\Chunk;

use Symfony\Component\HttpClient\Exception\TimeoutException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Contracts\HttpClient\ChunkInterface;

/**
 * @author Nicolas Grekas <p@tchwork.com>
 *
 * @internal
 */
class ErrorChunk implements ChunkInterface
{
    private $didThrow = false;
    private $offset;
    private $errorMessage;
    private $error;

    /**
     * @param \Throwable|string $error
     */
    public function __construct(int $offset, $error)
    {
        $this->offset = $offset;

        if (\is_string($error)) {
            $this->errorMessage = $error;
        } else {
            $this->error = $error;
            $this->errorMessage = $error->getMessage();
        }
    }

    /**
     * {@inheritdoc}
     */
    public function isTimeout(): bool
    {
        $this->didThrow = true;

        if (null !== $this->error) {
            throw new TransportException($this->errorMessage, 0, $this->error);
        }

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isFirst(): bool
    {
        $this->didThrow = true;
        throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
    }

    /**
     * {@inheritdoc}
     */
    public function isLast(): bool
    {
        $this->didThrow = true;
        throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
    }

    /**
     * {@inheritdoc}
     */
    public function getInformationalStatus(): ?array
    {
        $this->didThrow = true;
        throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
    }

    /**
     * {@inheritdoc}
     */
    public function getContent(): string
    {
        $this->didThrow = true;
        throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
    }

    /**
     * {@inheritdoc}
     */
    public function getOffset(): int
    {
        return $this->offset;
    }

    /**
     * {@inheritdoc}
     */
    public function getError(): ?string
    {
        return $this->errorMessage;
    }

    /**
     * @return bool Whether the wrapped error has been thrown or not
     */
    public function didThrow(bool $didThrow = null): bool
    {
        if (null !== $didThrow && $this->didThrow !== $didThrow) {
            return !$this->didThrow = $didThrow;
        }

        return $this->didThrow;
    }

    public function __sleep()
    {
        throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
    }

    public function __wakeup()
    {
        throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
    }

    public function __destruct()
    {
        if (!$this->didThrow) {
            $this->didThrow = true;
            throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
        }
    }
}


text:
author: slowe
comments: true
date: 2007-06-29 09:50:43+00:00
layout: post
slug: esx-iscsi-basic-configuration-from-the-cli
title: ESX iSCSI Basic Configuration from the CLI
wordpress_id: 481
categories: Tutorial
tags:
- ESX
- Interoperability
- iSCSI
- Storage
- Virtualization
- VMware
---

Sometimes, I find it better/faster/easier to perform tasks from the command-line interface (CLI) than going through a GUI. So, the other day, I needed to setup a new [VMware ESX Server](http://www.vmware.com/products/vi/esx/) for iSCSI storage, and thought I'd document the commands I used to set that up.

Unfortunately, the commands weren't able to do all the configuration I needed---I couldn't find the commands to let me set iSCSI security (CHAP username and password), and I needed iSCSI security for the target to which I was connecting. However, these commands _should_ work just fine for a basic iSCSI configuration.

Here are the commands I used. First, to enable the software iSCSI initiator:

	esxcfg-swiscsi -e

Next, to configure the ESX Service Console firewall (iptables) to allow the traffic from the software iSCSI initiator:

	esxcfg-firewall -e swISCSIClient

Next, set the target IP address for the vmhba40 adapter (the adapter that represents the software iSCSI initiator):

	vmkiscsi-tool -D -a 192.168.100.50 vmhba40

Finally, rescan for storage devices on vmhba40:

	esxcfg-rescan vmhba40

I'm sure there are more commands available; for more information, you can refer to Mike Laverick's excellent [Guide to ESX 3 Service Console](http://www.rtfm-ed.eu/docs/vmwdocs/esx3.x-vc2.x-serviceconsole-guide.pdf). In addition, please note that the ESX Server software iSCSI initiator is simply the open source version of an old Cisco iSCSI initiator for Linux, so you can use [that command reference](http://www.cuddletech.com/articles/iscsi/ar01s07.html) as well (I believe [this information](http://linux-iscsi.sourceforge.net/) is also applicable). Just preface the commands from the CuddleTech article with "vmk" and it should work just as listed.

One oddity: you may find that some of the command-line tools will report a Cisco IQN, but after further configuration (and especially after configuration from VirtualCenter) it will switch to a VMware IQN. This may wreak havoc with iSCSI targets on which LUN presentation is based on IQN names, so plan accordingly.


text:
<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "_route_immediate_stations".
 *
 * @property integer $id
 * @property integer $route_id
 * @property string $name
 *
 * @property Route $route
 * @property StudentTransport[] $studentTransports
 */
class RouteImmediateStations extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '_route_immediate_stations';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['route_id', 'name'], 'required'],
            [['route_id'], 'integer'],
            [['name'], 'string', 'max' => 90],
            [['route_id'], 'exist', 'skipOnError' => true, 'targetClass' => Route::className(), 'targetAttribute' => ['route_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'route_id' => Yii::t('app', 'Route ID'),
            'name' => Yii::t('app', 'Name'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRoute()
    {
        return $this->hasOne(Route::className(), ['id' => 'route_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getStudentTransports()
    {
        return $this->hasMany(StudentTransport::className(), ['station_id' => 'id']);
    }
}


text:
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Akka.Pattern;
using Akka.Streams.Kafka.Extensions;
using Akka.Streams.Kafka.Helpers;
using Akka.Streams.Kafka.Messages;
using Akka.Streams.Kafka.Settings;
using Akka.Streams.Stage;
using Akka.Streams.Supervision;
using Akka.Util.Internal;
using Confluent.Kafka;

namespace Akka.Streams.Kafka.Stages
{
    internal sealed class DefaultProducerStage<K, V, P, TIn, TOut> : GraphStage<FlowShape<TIn, Task<TOut>>>, IProducerStage<K, V, P, TIn, TOut>
        where TIn: IEnvelope<K, V, P>
        where TOut: IResults<K, V, P>
    {
        public Func<Action<IProducer<K, V>, Error>, IProducer<K, V>> ProducerProvider { get; }
        public ProducerSettings<K, V> Settings { get; }
        public TimeSpan FlushTimeout => Settings.FlushTimeout;
        public bool CloseProducerOnStop { get; }
        public Inlet<TIn> In { get; } = new Inlet<TIn>("kafka.producer.in");
        public Outlet<Task<TOut>> Out { get; } = new Outlet<Task<TOut>>("kafka.producer.out");
        public override FlowShape<TIn, Task<TOut>> Shape { get; }

        public DefaultProducerStage(
            ProducerSettings<K, V> settings,
            bool closeProducerOnStop,
            Func<IProducer<K, V>> customProducerProvider = null)
        {
            ProducerProvider = errorHandler => customProducerProvider?.Invoke() ?? Settings.CreateKafkaProducer(errorHandler);
            Settings = settings;
            CloseProducerOnStop = closeProducerOnStop;

            Shape = new FlowShape<TIn, Task<TOut>>(In, Out);
        }

        protected override GraphStageLogic CreateLogic(Attributes inheritedAttributes)
        {
            return new DefaultProducerStageLogic<K, V, P, TIn, TOut>(this, inheritedAttributes);
        }
    }

    internal class DefaultProducerStageLogic<K, V, P, TIn, TOut> : TimerGraphStageLogic, IProducerCompletionState
        where TIn: IEnvelope<K, V, P>
        where TOut: IResults<K, V, P>
    {
        private volatile bool _inIsClosed;
        private readonly IProducerStage<K, V, P, TIn, TOut> _stage;
        private readonly TaskCompletionSource<NotUsed> _completionState = new TaskCompletionSource<NotUsed>();
        private readonly Action _checkForCompletionCallback;
        private readonly Action<Exception> _failStageCallback;
        private readonly Decider _decider;

        protected IProducer<K, V> Producer { get; private set; }
        protected readonly AtomicCounter AwaitingConfirmation = new AtomicCounter(0);

        public DefaultProducerStageLogic(IProducerStage<K, V, P, TIn, TOut> stage, Attributes attributes) : base(stage.Shape)
        {
            _stage = stage;

            var supervisionStrategy = attributes.GetAttribute<ActorAttributes.SupervisionStrategy>(null);
            _decider = supervisionStrategy != null ? supervisionStrategy.Decider : Deciders.StoppingDecider;

            _checkForCompletionCallback = GetAsyncCallback(CheckForCompletion);
            _failStageCallback = GetAsyncCallback<Exception>(FailStage);

            SetHandler(_stage.In,
                onPush: () =>
                {
                    var msg = Grab(_stage.In) as IEnvelope<K, V, P>;

                    switch (msg)
                    {
                        case Message<K, V, P> message:
                        {
                            var result = new TaskCompletionSource<IResults<K, V, P>>();
                            AwaitingConfirmation.IncrementAndGet();
                            Producer.Produce(message.Record, BuildSendCallback(result, onSuccess: report =>
                            {
                                result.SetResult(new Result<K, V, P>(report, message));
                            }));
                            PostSend(msg);
                            Push(stage.Out, result.Task as Task<TOut>);
                            break;
                        }

                        case MultiMessage<K, V, P> multiMessage:
                        {
                            var tasks = multiMessage.Records.Select(record =>
                            {
                                var result = new TaskCompletionSource<MultiResultPart<K, V>>();
                                AwaitingConfirmation.IncrementAndGet();
                                Producer.Produce(record, BuildSendCallback(result, report =>
                                {
                                    result.SetResult(new MultiResultPart<K, V>(report, record));
                                }));
                                return result.Task;
                            });
                            PostSend(msg);
                            var resultTask = Task.WhenAll(tasks).ContinueWith(t => new MultiResult<K, V, P>(t.Result.ToImmutableHashSet(), multiMessage.PassThrough) as IResults<K, V, P>);
                            Push(stage.Out, resultTask as Task<TOut>);
                            break;
                        }

                        case PassThroughMessage<K, V, P> passThroughMessage:
                        {
                            PostSend(msg);
                            var resultTask = Task.FromResult(new PassThroughResult<K, V, P>(passThroughMessage.PassThrough) as IResults<K, V, P>);
                            Push(stage.Out, resultTask as Task<TOut>);
                            break;
                        }
                    }
                },
                onUpstreamFinish: () =>
                {
                    _inIsClosed = true;
                    _completionState.SetResult(NotUsed.Instance);
                    _checkForCompletionCallback();
                },
                onUpstreamFailure: exception =>
                {
                    _inIsClosed = true;
                    _completionState.SetException(exception);
                    _checkForCompletionCallback();
                });

            SetHandler(_stage.Out, onPull: () =>
            {
                TryPull(_stage.In);
            });
        }

        public override void PreStart()
        {
            base.PreStart();

            Producer = _stage.ProducerProvider(HandleProduceError);
            Log.Debug($"Producer started: {Producer.Name}");
        }

        public override void PostStop()
        {
            Log.Debug("Stage completed");

            if (_stage.CloseProducerOnStop)
            {
                try
                {
                    // we do not have to check if producer was already closed in send-callback as `flush()` and `close()` are effectively no-ops in this case
                    Producer.Flush(_stage.FlushTimeout);
                    // TODO: fix missing deferred close support: `producer.close(stage.closeTimeout.toMillis, TimeUnit.MILLISECONDS)`
                    Producer.Dispose();
                    Log.Debug($"Producer closed: {Producer.Name}");
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Problem occurred during producer close");
                }
            }

            base.PostStop();
        }

        public virtual void OnCompletionSuccess() => CompleteStage();

        public virtual void OnCompletionFailure(Exception ex) => FailStage(ex);

        protected virtual void PostSend(IEnvelope<K, V, P> msg) { }

        private Action<DeliveryReport<K, V>> BuildSendCallback<TResult>(TaskCompletionSource<TResult> completion, Action<DeliveryReport<K, V>> onSuccess)
        {
            return report =>
            {
                if (!report.Error.IsError)
                {
                    onSuccess(report);
                }
                else
                {
                    Log.Error(report.Error.Reason);
                    var exception = new KafkaException(report.Error);
                    switch (_decider(exception))
                    {
                        case Directive.Stop:
                            if (_stage.CloseProducerOnStop)
                            {
                                // TODO: address the missing Producer.Close() in the future
                                Producer.Dispose();
                            }
                            _failStageCallback(exception);
                            break;
                        default:
                            completion.SetException(exception);
                            break;
                    }
                }

                if (AwaitingConfirmation.DecrementAndGet() == 0 && _inIsClosed)
                    _checkForCompletionCallback();
            };
        }

        private void CheckForCompletion()
        {
            if (IsClosed(_stage.In) && AwaitingConfirmation.Current == 0)
            {
                Log.Debug("Completing publisher stage");
                var completionTask = _completionState.Task;

                if (completionTask.IsFaulted || completionTask.IsCanceled)
                {
                    OnCompletionFailure(completionTask.Exception);
                }
                else if (completionTask.IsCompleted)
                {
                    OnCompletionSuccess();
                }
                else
                {
                    FailStage(new IllegalStateException("Stage completed, but there is no info about status"));
                }
            }
        }

        private void HandleProduceError(IProducer<K, V> producer, Error error)
        {
            Log.Error(error.Reason);

            if (!KafkaExtensions.IsBrokerErrorRetriable(error) && !KafkaExtensions.IsLocalErrorRetriable(error))
            {
                var exception = new KafkaException(error);
                switch (_decider(exception))
                {
                    case Directive.Stop:
                        if (_stage.CloseProducerOnStop)
                        {
                            // TODO: address the missing Producer.Close() in the future.
                            producer.Dispose();
                        }
                        _failStageCallback(exception);
                        break;
                    default:
                        break;
                }
            }
        }

        /// <inheritdoc />
        protected override void OnTimer(object timerKey) { }
    }
}


text:
MY_CONFIG[:hide_empty_left_column] = false
MY_CONFIG[:hide_byline_on_page] = false
MY_CONFIG[:hide_breadcrumb] = false
MY_CONFIG[:footer_edit_on_right] = true
MY_CONFIG[:hide_menu] = true
MY_CONFIG[:menu_on_left] = true


text:
<?xml version="1.0" encoding="UTF-8"?>
<!--
   ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   ====================================================================
-->
<!DOCTYPE book PUBLIC "-//APACHE//DTD Cocoon Documentation Book V1.0//EN" "../dtd/book-cocoon-v10.dtd">

<book software="POI"
      title="POI Documentation Translations"
      copyright="@year@ POI Project"
      xmlns:xlink="http://www.w3.org/1999/xlink">

  <menu label="Translations">
    <menu-item label="Main Index" href="../index.html"/>
    <menu-item label="Guidelines" href="guidelines.html"/>
  </menu>

  <menu label="Languages">
    <menu-item label="Spanish" href="es/index.html"/>
    <menu-item label="German" href="de/index.html"/>
    <menu-item label="Japanese" href="http://jakarta.terra-intl.com/poi/"/>
    <menu-item label="Korean" href="http://jakarta.apache-korea.org/poi/"/>
  </menu>

</book>


text:
package yalantis.com.sidemenu.sample.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import yalantis.com.sidemenu.sample.R;
import yalantis.com.sidemenu.sample.network.model.leaguePreResults.Event;
import yalantis.com.sidemenu.sample.network.model.leaguePreResults.LeaguesPrev;
import yalantis.com.sidemenu.sample.network.service.OnDayEventClickListener;

/**
 * Created by TheAppExperts on 31/10/2017.
 */

public class LeaguePreResultsAdapter extends RecyclerView.Adapter <LeaguePreResultsAdapter.InfoViewHolder>{
    LeaguesPrev leaguesPrev;
    int live_score;
    Context applicationContext;
    OnDayEventClickListener listener;

    public LeaguePreResultsAdapter(LeaguesPrev leaguesPrev, int live_score, Context applicationContext, OnDayEventClickListener listener) {
        this.leaguesPrev = leaguesPrev;
        this.live_score = live_score;
        this.applicationContext = applicationContext;
        this.listener = listener;

    }

    @Override
    public InfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(live_score, parent, false);
        return new InfoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(InfoViewHolder holder, int position) {
        Event prev = leaguesPrev.getEvents().get(position);
        holder.tvHomeTeam.setText(prev.getStrHomeTeam());
        holder.tvAwayTeam.setText(prev.getStrAwayTeam());
        holder.tvDate.setText(prev.getStrDate());
        holder.tvHomeScore.setText(prev.getIntHomeScore());
        holder.tvAwayScore.setText(prev.getIntAwayScore());
        holder.tvComp.setText(prev.getStrLeague());
        holder.tvTime.setText(prev.getStrTime());

        holder.bind(prev, listener);
    }

    @Override
    public int getItemCount() {
        return leaguesPrev.getEvents().size();
    }

    public class InfoViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.homeTeam)
        TextView tvHomeTeam;
        @BindView(R.id.awayTeam)
        TextView tvAwayTeam;
        @BindView(R.id.fixDate)
        TextView tvDate;
        @BindView(R.id.homeScore)
        TextView tvHomeScore;
        @BindView(R.id.awayScore)
        TextView tvAwayScore;
        @BindView(R.id.compStr)
        TextView tvComp;
        @BindView(R.id.fixTime)
        TextView tvTime;

        public InfoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(final Event event, final OnDayEventClickListener listener) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(view, event);
                }
            });
        }
    }
}


text:
set -e
set -x

cd `readlink -f "$0" | xargs dirname`
git pull origin master
dch --distribution testing --no-force-save-on-release --release "" -c ./Changelog
VERSION=`dpkg-parsechangelog -l./Changelog -SVersion`
TAG="v${VERSION}"
CHANGESET=`dpkg-parsechangelog -l./Changelog -SChanges | sed '/^iowow.*/d' | sed '/^\s*$/d'`
git add ./Changelog

if ! git diff-index --quiet HEAD --; then
  git commit -m"${TAG} landed"
  git push origin master
fi

echo "${CHANGESET}" | git tag -f -a -F - "${TAG}"
git push origin -f --tags


text:
package objects.containers;

import java.util.ArrayList;
import java.util.List;
import objects.main.Comment;
import objects.main.SociosException;

public class CommentsContainer {
	private List<Comment> comments;
	private List<SociosException> exceptions;
	private String elapsedTime;

	public CommentsContainer() {
	}

	public List<Comment> getComments() {
		if (comments == null) {
			comments = new ArrayList<Comment>();
		}
		return this.comments;
	}

	public void setComments(List<Comment> value) {
		this.comments = value;
	}

	public List<SociosException> getExceptions() {
		if (exceptions == null) {
			exceptions = new ArrayList<SociosException>();
		}
		return this.exceptions;
	}

	public void setExceptions(List<SociosException> value) {
		this.exceptions = value;
	}

	public String getElapsedTime() {
		return elapsedTime;
	}

	public void setElapsedTime(String value) {
		this.elapsedTime = value;
	}
}


text:
<?php
// Copyright (C) 2003-2010 National Association of REALTORS(R)
//
// All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished
// to do so, provided that the above copyright notice(s) and this
// permission notice appear in all copies of the Software and that
// both the above copyright notice(s) and this permission notice
// appear in supporting documentation.
//------------
//
// includes
//
include_once('./controller.php');

//
// look for definitions
//
$EXTRACT = new Extract();
$result = $EXTRACT->getExistingNames();
if (sizeof($result) == 1) {
     $file = null;
     foreach ($result as $config => $target) {
          $file = $target;
     }
     $url = './run_extract.php?ELEMENT=' . $file;
     locate_next_screen($url);
//header('Location: ' . $url);
//exit('here');
}
$HTML = new HTMLPage();
$HTML->start(PROJECT_NAME . ' Persistant Downloader');

//
// using view.php
//
$FORMATTER = new TableFormatter();
$items = null;
$valign = null;

$field = $FORMATTER->formatRadioField("ELEMENT",
                                      null,
                                      $result,
                                      "Extract to Run",
                                      true);
$items[] = $field;
$valign[$field] = 'top';

$FORMATTER->printForm($items,
                      './run_extract.php?LOCATION=./index.php',
                      'Choose an Extract to Run',
                      'Run a Job',
                      false,
                      false,
                      false,
                      $valign);
$FORMATTER->finish();

$HTML->finish();

//
//------------

?>