1597763760

# Calculating and Visualizing the Magnetic Field Due

It seems that most of the second semester algebra-based physics is magic. Since you need calculus to derive many of the expressions, the students just get them magically instead.

NOT TODAY. Well, I hope not. Today I am going to use python and the Biot-Savart Law to find the magnetic field due to a wire. Here is the expression I want to show:

Where I is the current in a wire and r is the distance from the wire. I guess I should start with the magnetic field due to a moving point charge.

Yes, that’s sort of a crazy equation. The weird part is the cross product. Here are some notes:

• The “times” symbol is the cross product.
• The cross product is an operation between two vectors that returns a vector as the resultant (unlike the dot-product that returns a scalar).
• The resultant of this vector is perpendicular to both of the products-that makes this only work in 3D.
• The magnitude of the resultant depends on the magnitude of the products and the sine of the angle between them.

OK, that’s enough of that. Fortunately, we don’t really need to compute cross products since it’s built into VPython (Glowscript). Let me do one more thing before calculating stuff. Suppose I have a charge q moving with a velocity v over some short length of wire, L. I can write qv as:

So, instead of dealing with qv, I can use IL. Note that L is a vector in the direction of motion for the current. Now my magnetic field looks like this:

#python #physics #programming

1667086140

## ControllerExtra for Symfony2

This bundle provides a collection of annotations for Symfony2 Controllers, designed to streamline the creation of certain objects and enable smaller and more concise actions.

## Reference

By default, all annotations are loaded, but any individual annotation can be completely disabled by setting to false `active` parameter.

Default values are:

``````controller_extra:
resolver_priority: -8
request: current
paginator:
active: true
default_name: paginator
default_page: 1
default_limit_per_page: 10
entity:
active: true
default_name: entity
default_persist: true
default_mapping_fallback: false
default_factory_method: create
default_factory_mapping: true
form:
active: true
default_name: form
object_manager:
active: true
default_name: form
flush:
active: true
default_manager: default
json_response:
active: true
default_status: 200
log:
active: true
default_level: info
default_execute: pre
``````

ResolverEventListener is subscribed to `kernel.controller` event with priority -8. This element can be configured and customized with `resolver_priority` config value. If you need to get ParamConverter entities, make sure that this value is lower than 0. The reason is that this listener must be executed always after ParamConverter one.

Entity provider

In some annotations, you can define an entity by several ways. This chapter is about how you can define them.

## By namespace

You can define an entity using its namespace. A simple new `new()` be performed.

``````/**
* Simple controller method
*
* @SomeAnnotation(
*      class = "Mmoreram\CustomBundle\Entity\MyEntity",
* )
*/
public function indexAction()
{
}
``````

## By doctrine shortcut

You can define an entity using Doctrine shortcut notations. With this format you should ensure that your Entities follow Symfony Bundle standards and your entities are placed under `Entity/` folder.

``````/**
* Simple controller method
*
* @SomeAnnotation(
*      class = "MmoreramCustomBundle:MyEntity",
* )
*/
public function indexAction()
{
}
``````

## By parameter

You can define an entity using a simple config parameter. Some projects use parameters to define all entity namespaces (To allow overriding). If you define the entity with a parameter, this bundle will try to instance it with a simple `new()` accessing directly to the container ParametersBag.

``````parameters:

#
# Entities
#
my.bundle.entity.myentity: Mmoreram\CustomBundle\Entity\MyEntity
``````
``````/**
* Simple controller method
*
* @SomeAnnotation(
*      class = "my.bundle.entity.myentity",
* )
*/
public function indexAction()
{
}
``````

Controller annotations

This bundle provide a reduced but useful set of annotations for your controller actions.

## @CreatePaginator

Creates a Doctrine Paginator object, given a request and a configuration. This annotation just injects into de controller a new `Doctrine\ORM\Tools\Pagination\Pagination` instance ready to be iterated.

You can enable/disable this bundle by overriding `active` flag in configuration file `config.yml`

``````controller_extra:
pagination:
active: true
``````

By default, if `name` option is not set, the generated object will be placed in a parameter named `\$paginator`. This behaviour can be configured using `default_name` in configuration.

This annotation can be configured with these sections

### Paginator Entity

To create a new Pagination object you need to refer to an existing Entity. You can check all available formats you can define it just reading the Entity Provider section.

``````<?php

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

### Paginator page

You need to specify Paginator annotation the page to fetch. By default, if none is specified, this bundle will use the default one defined in configuration. You can override in `config.yml`

``````controller_extra:
pagination:
default_page: 1
``````

You can refer to an existing Request attribute using `~value~` format, to any `\$_GET` element by using format `?field?` or to any `\$_POST` by using format `#field#`

You can choose between Master Request or Current Request accessing to its attributes, by configuring the request value of the configuration.

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/{foo}
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      page = "~foo~"
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

or you can hardcode the page to use.

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      page = 1
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

### Paginator limit

You need to specify Paginator annotation the limit to fetch. By default, if none is specified, this bundle will use the default one defined in configuration. You can override in `config.yml`

``````controller_extra:
pagination:
default_limit_per_page: 10
``````

You can refer to an existing Request attribute using `~value~` format, to any `\$_GET` element by using format `?field?` or to any `\$_POST` by using format `#field#`

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/{foo}/{limit}
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      page = "~foo~",
*      limit = "~limit~"
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

or you can hardcode the page to use.

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      page = 1,
*      limit = 10
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

### Paginator OrderBy

You can order your Pagination just defining the fields you want to orderBy and the desired direction. The `orderBy` section must be defined as an array of arrays, and each array should contain these positions:

• First position: Entity alias (Principal object is set as `x`)
• Second position: Entity field
• Third position: Direction
• Fourth position: Custom direction map (optional)
``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      orderBy = {
*          {"x", "createdAt", "ASC"},
*          {"x", "updatedAt", "DESC"},
*          {"x", "id", 1, {
*              0 => "ASC",
*              1 => "DESC",
*          }},
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

With the third and fourth value you can define a map where to match your own direction nomenclature with DQL one. DQL nomenclature just accept ASC for Ascendant and DESC for Descendant.

This is very useful when you need to match a url format with the DQL one. You can refer to an existing Request attribute using `~value~` format, to any `\$_GET` element by using format `?field?` or to any `\$_POST` by using format `#field#`

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/order/{field}/{direction}
*
* For example, some matchings...
*
* /myroute/paginate/order/id/1 -> ORDER BY id DESC
* /myroute/paginate/order/enabled/0 - ORDER BY enabled ASC
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      orderBy = {
*          {"x", "createdAt", "ASC"},
*          {"x", "updatedAt", "DESC"},
*          {"x", "~field~", ~direction~, {
*              0 => "ASC",
*              1 => "DESC",
*          }},
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

The order of the definitions will alter the order of the DQL query.

### Paginator Wheres

You can define some where statements in your Paginator. The `wheres` section must be defined as an array of arrays, and each array should contain these positions:

• First position: Entity alias (Principal object is set as `x`)
• Second position: Entity field
• Third position: Operator =, <=, >, LIKE...
• Fourth position: Value to compare with
• Fifth position: Is a filter. By default, false
``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      wheres = {
*          {"x", "enabled", "=", true},
*          {"x", "age", ">", 18},
*          {"x", "name", "LIKE", "Eferv%"},
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

You can refer to an existing Request attribute using `~value~` format, to any `\$_GET` element by using format `?field?` or to any `\$_POST` by using format `#field#`

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/{field}
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      wheres = {
*          {"x", "name", "LIKE", "~field~"},
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

You can use as well this feature for optional filtering by setting the last position to `true`. In that case, if the filter value is not found, such line will be ignored.

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* This Controller matches pattern /myroute?query=name%
* This Controller matches pattern /myroute as well
*
* In both cases this will work. In the first case we will apply the where line
* in the paginator. In the second case, we wont.
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      wheres = {
*          {"x", "name", "LIKE", "?query?", true},
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

### Paginator Not Nulls

You can also define some fields to not null. Is same as `wheres` section, but specific for NULL assignments. The `notNulls` section must be defined as an array of arrays, and each array should contain these positions:

• First position: Object (Principal object is set as `x`)
• Second position: Field
``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      notNulls = {
*          {"x", "enabled"},
*          {"x", "deleted"},
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

### Paginator Left Join

You can do some left joins in this section. The `leftJoins` section must be defined as an array of array, where each array can have these fields:

• First position: Entity alias (Principal object is set as `x`)
• Second position: Entity relation (Address)
• Third position: Relation identifier (a)
• Fourth position: If true, this relation is added in select group. Otherwise, wont be loaded until its request (optional)
``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      leftJoins = {
*          {"x", "User", "u", true},
*          {"x", "Cart", "c"},
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

### Paginator Inner Join

You can do some inner joins in this section. The `innerJoins` section must be defined as an array of array, where each array can have these fields:

• First position: Entity alias (x)
• Second position: Entity relation (Address)
• Third position: Relation identifier (a)
• Fourth position: If true, this relation is added in select group. Otherwise, wont be loaded until its request (optional)
``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      innerJoins = {
*          {"x", "User", "u", true},
*          {"x", "Cart", "c"},
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

### Paginator Attributes

A nice feature of this annotation is that you can also inject into your controller a `Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes` instance with some interesting information about your pagination.

• currentPage : Current page fetched
• totalElements : Total elements given your criteria. If none criteria is defined in your configuration, this value will show all elements of a certain entity.
• totalPages : Total pages you can fetch given a criteria.
• limitPerPage: Maximum number of elements in each page.

To inject this object you need to define the "attributes" annotation field with the method parameter name.

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
*      attributes = "paginatorAttributes",
*      entityNamespace = "MmoreramCustomBundle:User",
*      page = 1,
*      limit = 10
* )
*/
public function indexAction(
Paginator \$paginator,
PaginatorAttributes \$paginatorAttributes
)
{
\$currentPage = \$paginatorAttributes->getCurrentPage();
\$totalElements = \$paginatorAttributes->getTotalElements();
\$totalPages = \$paginatorAttributes->getTotalPages();
\$limitPerPage = \$paginatorAttributes->getLimitPerPage();

}
``````

### Paginator Example

This is a completed example and its DQL resolution

``````use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
* Simple controller method
*
* This Controller matches pattern /paginate/nb/{limit}/{page}
*
* Where:
*
* * limit = 10
* * page = 1
*
* @CreatePaginator(
*      entityNamespace = "ControllerExtraBundle:Fake",
*      page = "~page~",
*      limit = "~limit~",
*      orderBy = {
*          { "x", "createdAt", "ASC" },
*          { "x", "updatedAt", "DESC" },
*          { "x", "id", "0", {
*              "1" = "ASC",
*              "2" = "DESC",
*          }}
*      },
*      wheres = {
*          { "x", "enabled" , "=", true }
*      },
*      leftJoins = {
*          { "x", "relation", "r" },
*          { "x", "relation2", "r2" },
*          { "x", "relation5", "r5", true },
*      },
*      innerJoins = {
*          { "x", "relation3", "r3" },
*          { "x", "relation4", "r4", true },
*      },
*      notNulls = {
*      }
* )
*/
public function indexAction(Paginator \$paginator)
{
}
``````

The DQL generated by this annotation is

``````    SELECT x, r4, r5
FROM Mmoreram\\ControllerExtraBundle\\Tests\\FakeBundle\\Entity\\Fake x

INNER JOIN x.relation3 r3
INNER JOIN x.relation4 r4

LEFT JOIN x.relation r
LEFT JOIN x.relation2 r2
LEFT JOIN x.relation5 r5

WHERE enabled = ?where0

ORDER BY createdAt ASC, id ASC
``````

This annotation can create a PagerFanta instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a Pagerfanta object instance.

``````use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Pagerfanta\Pagerfanta;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      page = 1,
*      limit = 10
* )
*/
public function indexAction(Pagerfanta \$paginator)
{
}
``````

This annotation can create a KNPPaginator instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a KNPPaginator object instance.

``````use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Knp\Component\Pager\Pagination\PaginationInterface;

/**
* Simple controller method
*
* This Controller matches pattern /myroute/paginate/
*
* @CreatePaginator(
*      entityNamespace = "MmoreramCustomBundle:User",
*      page = 1,
*      limit = 10
* )
*/
public function indexAction(PaginationInterface \$paginator)
{
}
``````

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* @Entity(
*      namespace = "MmoreramCustomBundle:User",
*      name  = "user"
* )
*/
public function indexAction(User \$user)
{
}
``````

By default, if `name` option is not set, the generated object will be placed in a parameter named `\$entity`. This behaviour can be configured using `default_name` in configuration.

You can also use setters in Entity annotation. It means that you can simply call entity setters using Request attributes.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* @Entity(
* )
* @Entity(
*      namespace = "MmoreramCustomBundle:User",
*      name  = "user",
*      setters = {
*      }
* )
*/
{
}
``````

When `User` instance is built, method `setAddress` is called using as parameter the new `Address` instance.

New entities are just created with a simple `new()`, so they are not persisted. By default, they will be persisted using configured manager, but you can disable this feature using `persist` option.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* @Entity(
*      namespace = "MmoreramCustomBundle:User",
*      name  = "user",
*      persist = false
* )
*/
public function indexAction(User \$user)
{
}
``````

### Entity Mapping

When you define a new Entity annotation, you can also request the mapped entity given a map. It means that if a map is defined, this bundle will try to request the mapped instance satisfying it.

The keys of the map represent the names of the mapped fields and the values represent their desired values. Remember than you can refer to any Request attribute by using format `~field~`, to any `\$_GET` element by using format `?field?` or to any `\$_POST` by using format `#field#`

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* This Controller matches pattern /user/edit/{id}/{username}
*
* @Entity(
*      namespace = "MmoreramCustomBundle:User",
*      name  = "user",
*      mapping = {
*          "id": "~id~",
*      }
* )
*/
public function indexAction(User \$user)
{
}
``````

In this case, you will try to get the mapped instance of User with passed id. If some mapping is defined and any entity is found, a new EntityNotFoundException` is thrown.

### Entity Mapping Fallback

So what if one ore more than one mapping references are not found? For example, you're trying to map the {id} parameter from your route, but this parameter is not even defined. Whan happens here? Well, you can assume then that you want to pass a new entity instance by using the mappingFallback.

By default, if `mapping_fallback` option is not set, the used value will be the parameter `default_mapping_fallback` defined in configuration. By default this value is `false`

Don't confuse with the scenario where you're looking for an entity in your database, all mapping references have been resolved, and the entity is not found. In that case, a common "EntityNotFound" exception will be thrown by Doctrine.

Lets see an example. Because we have enabled the mappingFallback, and because the mapping definition does not match the assigned route, we will return a new empty User entity.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* This Controller matches pattern /user/edit/{id}
*
*      namespace = "MmoreramCustomBundle:User",
*      name  = "user",
*      mapping = {
*          "id": "~id~",
*      },
*      mappingFallback = true
* )
*/
public function indexAction(User \$user)
{
// \$user->getId() === null
}
``````

### Entity Repository

By default, the Doctrine entity manager provides the right repository per each entity (not the default one, but the right specific one). Although, you can define a custom repository to be used in your annotation by using the repository configuration.

``````/**
* Simple controller method
*
* @CreateEntity(
*      namespace = "MmoreramCustomBundle:User",
*      mapping = {
*          "id": "~id~",
*      }
*      repository = {
*          "class" = "Mmoreram\CustomBundle\Repository\AnotherRepository",
*      },
* )
*/
public function indexAction(User \$user)
{
}
``````

By default, the method findOneBy will always be used, unless you define another one.

``````/**
* Simple controller method
*
* @CreateEntity(
*      namespace = "MmoreramCustomBundle:User",
*      mapping = {
*          "id": "~id~",
*      }
*      repository = {
*          "class" = "Mmoreram\CustomBundle\Repository\AnotherRepository",
*          "method" = "find",
*      },
* )
*/
public function indexAction(User \$user)
{
}
``````

### Entity Factory

When the annotation considers that a new entity must be created, because no mapping information has been provided, or because the mapping fallback has been activated, by default a new instance will be created by using the namespace value.

This configuration block has three positions

• class - factory class
• method - Method to use when retrieving the object
• static - Method is static

You can define the factory with a simple namespace

``````/**
* Simple controller method
*
* @CreateEntity(
*      namespace = "MmoreramCustomBundle:User",
*      factory = {
*          "class" = "Mmoreram\CustomBundle\Factory\UserFactory",
*          "method" = "create",
*          "static" = true,
*      },
* )
*/
public function indexAction(User \$user)
{
}
``````

If you want to define your Factory as a service, with the possibility of overriding namespace, you can simply define service name. All other options have the same behaviour.

``````parameters:

#
# Factories
#
my.bundle.factory.user_factory: Mmoreram\CustomBundle\Factory\UserFactory
``````
``````/**
* Simple controller method
*
* @CreateEntity(
*      class = {
*          "factory" = my.bundle.factory.user_factory,
*          "method" = "create",
*          "static" = true,
*      },
* )
*/
public function indexAction(User \$user)
{
}
``````

If you do not define the `method`, default one will be used. You can override this default value by defining new one in your `config.yml`. Same with `static` value

``````controller_extra:
entity:
default_factory_method: create
default_factory_static: true
``````

## @CreateForm

Provides form injection in your controller actions. This annotation only needs a name to be defined in, where you must define namespace where your form is placed.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\AbstractType;

/**
* Simple controller method
*
* @CreateForm(
*      class = "\Mmoreram\CustomBundle\Form\Type\UserType",
*      name  = "userType"
* )
*/
public function indexAction(AbstractType \$userType)
{
}
``````

By default, if `name` option is not set, the generated object will be placed in a parameter named `\$form`. This behaviour can be configured using `default_name` in configuration.

You can not just define your Type location using the namespace, in which case a new AbstractType element will be created. but you can also define it using service alias, in which case this bundle will return an instance using Symfony DI.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\AbstractType;

/**
* Simple controller method
*
* @CreateForm(
*      class = "user_type",
*      name  = "userType"
* )
*/
public function indexAction(AbstractType \$userType)
{
}
``````

This annotation allows you to not only create an instance of FormType, but also allows you to inject a Form object or a FormView object

To inject a Form object you only need to cast method value as such.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\Form;

/**
* Simple controller method
*
* @CreateForm(
*      class = "user_type",
*      name  = "userForm"
* )
*/
public function indexAction(Form \$userForm)
{
}
``````

You can also, using [SensioFrameworkExtraBundle][1]'s [ParamConverter][2], create a Form object with an previously created entity. you can define this entity using `entity` parameter.

``````<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* @Route(
*      path = "/user/{id}",
*      name = "view_user"
* )
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @CreateForm(
*      class  = "user_type",
*      entity = "user"
*      name   = "userForm",
* )
*/
public function indexAction(User \$user, Form \$userForm)
{
}
``````

To handle current request, you can set `handleRequest` to true. By default this value is set to `false`

``````<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* @Route(
*      path = "/user/{id}",
*      name = "view_user"
* )
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @CreateForm(
*      class         = "user_type",
*      entity        = "user"
*      handleRequest = true,
*      name          = "userForm",
* )
*/
public function indexAction(User \$user, Form \$userForm)
{
}
``````

You can also add as a method parameter if the form is valid, using `validate` setting. Annotation will place result of `\$form->isValid()` in specified method argument.

``````<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* @Route(
*      path = "/user/{id}",
*      name = "view_user"
* )
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @CreateForm(
*      class         = "user_type",
*      entity        = "user"
*      handleRequest = true,
*      name          = "userForm",
*      validate      = "isValid",
* )
*/
public function indexAction(User \$user, Form \$userForm, \$isValid)
{
}
``````

To inject a FormView object you only need to cast method variable as such.

``````<?php

use Symfony\Component\Form\FormView;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;

/**
* Simple controller method
*
* @CreateForm(
*      class = "user_type",
*      name  = "userFormView"
* )
*/
public function indexAction(FormView \$userFormView)
{
}
``````

## @Flush

Flush annotation allows you to flush entityManager at the end of request using kernel.response event

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Flush;

/**
* Simple controller method
*
* @Flush
*/
public function indexAction()
{
}
``````

If not otherwise specified, default Doctrine Manager will be flushed with this annotation. You can overwrite default Manager in your `config.yml` file.

``````controller_extra:
flush:
default_manager: my_custom_manager
``````

You can also override this value in every single Flush Annotation instance defining `manager` value

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Flush;

/**
* Simple controller method
*
* @Flush(
*      manager = "my_own_manager"
* )
*/
public function indexAction()
{
}
``````

If you want to change default manager in all annotation instances, you should override bundle parameter in your `config.yml` file.

``````controller_extra:
flush:
default_manager: my_own_manager
``````

If any parameter is set, annotation will flush all. If you only need to flush one or many entities, you can define explicitly which entity must be flushed.

``````<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

use Mmoreram\ControllerExtraBundle\Annotation\Flush;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @Flush(
*      entity = "user"
* )
*/
public function indexAction(User \$user)
{
}
``````

You can also define a set of entities to flush

``````<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

use Mmoreram\ControllerExtraBundle\Annotation\Flush;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
* Simple controller method
*
* @ParamConverter("user", class="MmoreramCustomBundle:User")
* @Flush(
*      entity = {
*          "user",
*      }
* )
*/
{
}
``````

If multiple @Mmoreram\Flush are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.

## @ToJsonResponse

JsonResponse annotation allows you to create a `Symfony\Component\HttpFoundation\JsonResponse` object, given a simple controller return value.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\ToJsonResponse;

/**
* Simple controller method
*
* @ToJsonResponse
*/
{
return array(
'This is my response'
);
}
``````

By default, JsonResponse is created using default `status` and `headers` defined in bundle parameters. You can overwrite them in your `config.yml` file.

``````controller_extra:
json_response:
default_status: 403
``````

You can also overwrite these values in each `@JsonResponse` annotation.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\ToJsonResponse;

/**
* Simple controller method
*
* @ToJsonResponse(
*      status = 403,
*      }
* )
*/
{
return array(
'This is my response'
);
}
``````

If an Exception is returned the response status is set by default to 500 and the Exception message is returned as response.

`STATUS 500 Internal server error`

``````{
message : 'Exception message'
}
``````

In case we use a HttpExceptionInterface the use the exception status code as status code. In case we launch this exception

``````use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

...

``````

`STATUS 404 Not Found`

``````{
}
``````

If the exception is being launched on an annotation (e.g. Entity annotation) remember to add the JsonResponse annotation at the beginning or at least before any annotation that could cause an exception.

If multiple @Mmoreram\JsonResponse are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.

## @Log

Log annotation allows you to log any plain message before or after controller action execution

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
* Simple controller method
*
* @Log("Executing index Action")
*/
public function indexAction()
{
}
``````

You can define the level of the message. You can define default one if none is specified overriding it in your `config.yml` file.

``````controller_extra:
log:
default_level: warning
``````

Every Annotation instance can overwrite this value using `level` field.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
* Simple controller method
*
* @Log(
*      value   = "Executing index Action",
*      level   = @Log::LVL_WARNING
* )
*/
public function indexAction()
{
}
``````

Several levels can be used, as defined in [Psr\Log\LoggerInterface][6] interface

• @Mmoreram\Log::LVL_EMERG
• @Mmoreram\Log::LVL_CRIT
• @Mmoreram\Log::LVL_ERR
• @Mmoreram\Log::LVL_WARN
• @Mmoreram\Log::LVL_NOTICE
• @Mmoreram\Log::LVL_INFO
• @Mmoreram\Log::LVL_DEBUG
• @Mmoreram\Log::LVL_LOG

You can also define the execution of the log. You can define default one if none is specified overriding it in your `config.yml` file.

``````controller_extra:
log:
default_execute: pre
``````

Every Annotation instance can overwrite this value using `level` field.

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
* Simple controller method
*
* @Log(
*      value   = "Executing index Action",
*      execute = @Log::EXEC_POST
* )
*/
public function indexAction()
{
}
``````

Several executions can be used,

• @Mmoreram\Log::EXEC_PRE - Logged before controller execution
• @Mmoreram\Log::EXEC_POST - Logged after controller execution
• @Mmoreram\Log::EXEC_BOTH - Logged both

## @Get

The Get annotation allows you to get any parameter from the request query string.

For a `GET` request like:

``````GET /my-page?foo=bar HTTP/1.1
``````

You can can simply get the `foo` var using the `GET` annotation

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Get;

/**
* Simple controller method
*
* @Get(
*     path = "foo"
* )
*/
public function indexAction(\$foo)
{
// Use the foo var
}
``````

You can also customize the var name and the default value in case the var is not sent on the query string.

For a `GET` request like:

``````GET /my-page HTTP/1.1
``````

And this annotation

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Get;

/**
* Simple controller method
*
* @Get(
*     path = "foo",
*     name = "varName",
*     default = 'bar',
* )
*/
public function indexAction(\$varName)
{
// This would print 'bar'
echo \$varName;
}
``````

## @Post

The Post annotation allows you to get any parameter from the post request body.

For a `POST` request like:

``````POST /my-page HTTP/1.1
foo=bar
``````

You can can simply get the `foo` var using the `POST` annotation

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Post;

/**
* Simple controller method
*
* @Post(
*     path = "foo"
* )
*/
public function indexAction(\$foo)
{
// Use the foo var
}
``````

You can also customize the var name and the default value in case the var is not sent on the query string.

For a `POST` request like:

``````POST /my-page HTTP/1.1
``````

And this annotation

``````<?php

use Mmoreram\ControllerExtraBundle\Annotation\Post;

/**
* Simple controller method
*
* @Post(
*     path = "foo",
*     name = "varName",
*     default = 'bar',
* )
*/
public function indexAction(\$varName)
{
// This would print 'bar'
echo \$varName;
}
``````

Custom annotations

Using this bundle you can now create, in a very easy way, your own controller annotation.

## Annotation

The annotation object. You need to define the fields your custom annotation will contain. Must extends `Mmoreram\ControllerExtraBundle\Annotation\Annotation` abstract class.

``````<?php

namespace My\Bundle\Annotation;

use Mmoreram\ControllerExtraBundle\Annotation\Annotation;

/**
* Entity annotation driver
*
* @Annotation
* @Target({"METHOD"})
*/
final class MyCustomAnnotation extends Annotation
{
/**
* @var string
*
* Dummy field
*/
public \$field;

/**
* Get Dummy field
*
* @return string Dummy field
*/
public function getField()
{
return \$this->field;
}
}
``````

## Resolver

Once you have defined your own annotation, you have to resolve how this annotation works in a controller. You can manage this using a Resolver. Must extend `Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver;` abstract class.

``````<?php

namespace My\Bundle\Resolver;

use Symfony\Component\HttpFoundation\Request;

use Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver;
use Mmoreram\ControllerExtraBundle\Annotation\Annotation;

/**
* MyCustomAnnotation Resolver
*/
class MyCustomAnnotationResolver extends AnnotationResolver
{
/**
* Specific annotation evaluation.
*
* This method must be implemented in every single EventListener
* with specific logic
*
* All method code will executed only if specific active flag is true
*
* @param Request          \$request
* @param Annotation       \$annotation
* @param ReflectionMethod \$method
*/
public function evaluateAnnotation(
Request \$request,
Annotation \$annotation,
ReflectionMethod \$method
)
{
/**
* You can now manage your annotation.
*
* Annotation fields can be public and can be acceded directly,
* but is better for testing to use getters; they can be mocked.
*/
\$field = \$annotation->getField();

/**
*
* Available parameters are:
*
* # ParamConverter parameters ( See `resolver_priority` config value )
* # All method defined parameters, included Request object if is set.
*/
\$entity = \$request->attributes->get('entity');

/**
* And you can now place new elements in the controller action.
* In this example we are creating new method parameter
* called \$myNewField with some value
*/
\$request->attributes->set(
'myNewField',
new \$field()
);

return \$this;
}

}
``````

This class will be defined as a service, so this method is computed just before executing current controller. You can also subscribe to some kernel events and do whatever you need to do ( You can check `Mmoreram\ControllerExtraBundle\Resolver\LogAnnotationResolver` for some examples.

## Definition

Once Resolver is done, we need to define our service as an Annotation Resolver. We will use a custom `tag`.

``````parameters:
#
# Resolvers
#
my.bundle.resolver.my_custom_annotation_resolver.class: My\Bundle\Resolver\MyCustomAnnotationResolver

services:
#
# Resolvers
#
my.bundle.resolver.my_custom_annotation_resolver:
class: %my.bundle.resolver.my_custom_annotation_resolver.class%
tags:
- { name: controller_extra.annotation }
``````

## Registration

We need to register our annotation inside our application. We can just do it in the `boot()` method of `bundle.php` file.

``````<?php

namespace My\Bundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\Common\Annotations\AnnotationRegistry;

/**
* MyBundle
*/
class ControllerExtraBundle extends Bundle
{

/**
* Boots the Bundle.
*/
public function boot()
{
\$kernel = \$this->container->get('kernel');

AnnotationRegistry::registerFile(\$kernel
->locateResource("@MyBundle/Annotation/MyCustomAnnotation.php")
);
}
}
``````

Et voilà! We can now use our custom Annotation in our project controllers.

Author: mmoreram
Source Code: https://github.com/mmoreram/ControllerExtraBundle

1659408900

## TinyTDS: FreeTDS Bindings for Ruby using DB-Library

TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.

• - TravisCI
• - Appveyor
• - Gem Version
• - Community

The TinyTDS gem is meant to serve the extremely common use-case of connecting, querying and iterating over results to Microsoft SQL Server or Sybase databases from Ruby using the FreeTDS's DB-Library API.

TinyTDS offers automatic casting to Ruby primitives along with proper encoding support. It converts all SQL Server datatypes to native Ruby primitives while supporting :utc or :local time zones for time-like types. To date it is the only Ruby client library that allows client encoding options, defaulting to UTF-8, while connecting to SQL Server. It also properly encodes all string and binary data. The motivation for TinyTDS is to become the de-facto low level connection mode for the SQL Server Adapter for ActiveRecord.

The API is simple and consists of these classes:

• TinyTds::Client - Your connection to the database.
• TinyTds::Result - Returned from issuing an #execute on the connection. It includes Enumerable.
• TinyTds::Error - A wrapper for all FreeTDS exceptions.

## Install

Installing with rubygems should just work. TinyTDS is currently tested on Ruby version 2.0.0 and upward.

``````\$ gem install tiny_tds
``````

If you use Windows, we pre-compile TinyTDS with static versions of FreeTDS and supporting libraries. If you're using RubyInstaller the binary gem will require that devkit is installed and in your path to operate properly.

On all other platforms, we will find these dependencies. It is recommended that you install the latest FreeTDS via your method of choice. For example, here is how to install FreeTDS on Ubuntu. You might also need the `build-essential` and possibly the `libc6-dev` packages.

``````\$ apt-get install wget
\$ apt-get install build-essential
\$ apt-get install libc6-dev

\$ wget http://www.freetds.org/files/stable/freetds-1.1.24.tar.gz
\$ tar -xzf freetds-1.1.24.tar.gz
\$ cd freetds-1.1.24
\$ ./configure --prefix=/usr/local --with-tdsver=7.3
\$ make
\$ make install
``````

Please read the MiniPortile and/or Windows sections at the end of this file for advanced configuration options past the following:

``````--with-freetds-dir=DIR
Use the freetds library placed under DIR.
``````

## Getting Started

Optionally, Microsoft has done a great job writing some articles on how to get started with SQL Server and Ruby using TinyTDS. Please checkout one of the following posts that match your platform.

## FreeTDS Compatibility & Configuration

TinyTDS is developed against FreeTDS 0.95, 0.99, and 1.0 current. Our default and recommended is 1.0. We also test with SQL Server 2008, 2014, and Azure. However, usage of TinyTDS with SQL Server 2000 or 2005 should be just fine. Below are a few QA style notes about installing FreeTDS.

NOTE: Windows users of our pre-compiled native gems need not worry about installing FreeTDS and its dependencies.

Do I need to install FreeTDS? Yes! Somehow, someway, you are going to need FreeTDS for TinyTDS to compile against.

OK, I am installing FreeTDS, how do I configure it? Contrary to what most people think, you do not need to specially configure FreeTDS in any way for client libraries like TinyTDS to use it. About the only requirement is that you compile it with libiconv for proper encoding support. FreeTDS must also be compiled with OpenSSL (or the like) to use it with Azure. See the "Using TinyTDS with Azure" section below for more info.

Do I need to configure `--with-tdsver` equal to anything? Most likely! Technically you should not have to. This is only a default for clients/configs that do not specify what TDS version they want to use. We are currently having issues with passing down a TDS version with the login bit. Till we get that fixed, if you are not using a freetds.conf or a TDSVER environment variable, then make sure to use 7.1.

But I want to use TDS version 7.2 for SQL Server 2005 and up! TinyTDS uses TDS version 7.1 (previously named 8.0) and fully supports all the data types supported by FreeTDS, this includes `varchar(max)` and `nvarchar(max)`. Technically compiling and using TDS version 7.2 with FreeTDS is not supported. But this does not mean those data types will not work. I know, it's confusing If you want to learn more, read this thread. http://lists.ibiblio.org/pipermail/freetds/2011q3/027306.html

I want to configure FreeTDS using `--enable-msdblib` and/or `--enable-sybase-compat` so it works for my database. Cool? It's a waste of time and totally moot! Client libraries like TinyTDS define their own C structure names where they diverge from Sybase to SQL Server. Technically we use the MSDBLIB structures which does not mean we only work with that database vs Sybase. These configs are just a low level default for C libraries that do not define what they want. So I repeat, you do not NEED to use any of these, nor will they hurt anything since we control what C structure names we use internally!

## Data Types

Our goal is to support every SQL Server data type and covert it to a logical Ruby object. When dates or times are returned, they are instantiated to either `:utc` or `:local` time depending on the query options. Only [datetimeoffset] types are excluded. All strings are associated the to the connection's encoding and all binary data types are associated to Ruby's `ASCII-8BIT/BINARY` encoding.

Below is a list of the data types we support when using the 7.3 TDS protocol version. Using a lower protocol version will result in these types being returned as strings.

• [date]
• [datetime2]
• [datetimeoffset]
• [time]

## TinyTds::Client Usage

Connect to a database.

``````client = TinyTds::Client.new username: 'sa', password: 'secret', host: 'mydb.host.net'
``````

Creating a new client takes a hash of options. For valid iconv encoding options, see the output of `iconv -l`. Only a few have been tested and highly recommended to leave blank for the UTF-8 default.

• :username - The database server user.
• :dataserver - Can be the name for your data server as defined in freetds.conf. Raw hostname or hostname:port will work here too. FreeTDS says that named instance like 'localhost\SQLEXPRESS' work too, but I highly suggest that you use the :host and :port options below. Google how to find your host port if you are using named instances or go here.
• :host - Used if :dataserver blank. Can be an host name or IP.
• :port - Defaults to 1433. Only used if :host is used.
• :database - The default database to use.
• :appname - Short string seen in SQL Servers process/activity window.
• :tds_version - TDS version. Defaults to "7.3".
• :login_timeout - Seconds to wait for login. Default to 60 seconds.
• :timeout - Seconds to wait for a response to a SQL command. Default 5 seconds. Prior to 1.0rc5, FreeTDS was unable to set the timeout on a per-client basis, permitting only a global timeout value. This means that if you're using an older version, the timeout values for all clients will be overwritten each time you instantiate a new `TinyTds::Client` object. If you are using 1.0rc5 or later, all clients will have an independent timeout setting as you'd expect. Timeouts caused by network failure will raise a timeout error 1 second after the configured timeout limit is hit (see #481 for details).
• :encoding - Any valid iconv value like CP1251 or ISO-8859-1. Default UTF-8.
• :azure - Pass true to signal that you are connecting to azure.
• :contained - Pass true to signal that you are connecting with a contained database user.
• :use_utf16 - Instead of using UCS-2 for database wide character encoding use UTF-16. Newer Windows versions use this encoding instead of UCS-2. Default true.
• :message_handler - Pass in a `call`-able object such as a `Proc` or a method to receive info messages from the database. It should have a single parameter, which will be a `TinyTds::Error` object representing the message. For example:
``````opts = ... # host, username, password, etc
opts[:message_handler] = Proc.new { |m| puts m.message }
client = TinyTds::Client.new opts
# => Changed database context to 'master'.
# => Changed language setting to us_english.
client.execute("print 'hello world!'").do
# => hello world!
``````

Use the `#active?` method to determine if a connection is good. The implementation of this method may change but it should always guarantee that a connection is good. Current it checks for either a closed or dead connection.

``````client.dead?    # => false
client.closed?  # => false
client.active?  # => true
client.closed?  # => false
client.active?  # => false
client.close
client.closed?  # => true
client.active?  # => false
``````

Escape strings.

``````client.escape("How's It Going'") # => "How''s It Going''"
``````

Send a SQL string to the database and return a TinyTds::Result object.

``````result = client.execute("SELECT * FROM [datatypes]")
``````

## TinyTds::Result Usage

A result object is returned by the client's execute command. It is important that you either return the data from the query, most likely with the #each method, or that you cancel the results before asking the client to execute another SQL batch. Failing to do so will yield an error.

Calling #each on the result will lazily load each row from the database.

``````result.each do |row|
# By default each row is a hash.
# The keys are the fields, as you'd expect.
# The values are pre-built Ruby primitives mapped from their corresponding types.
end
``````

A result object has a `#fields` accessor. It can be called before the result rows are iterated over. Even if no rows are returned, #fields will still return the column names you expected. Any SQL that does not return columned data will always return an empty array for `#fields`. It is important to remember that if you access the `#fields` before iterating over the results, the columns will always follow the default query option's `:symbolize_keys` setting at the client's level and will ignore the query options passed to each.

``````result = client.execute("USE [tinytdstest]")
result.fields # => []
result.do

result = client.execute("SELECT [id] FROM [datatypes]")
result.fields # => ["id"]
result.cancel
result = client.execute("SELECT [id] FROM [datatypes]")
result.each(:symbolize_keys => true)
result.fields # => [:id]
``````

You can cancel a result object's data from being loading by the server.

``````result = client.execute("SELECT * FROM [super_big_table]")
result.cancel
``````

You can use results cancelation in conjunction with results lazy loading, no problem.

``````result = client.execute("SELECT * FROM [super_big_table]")
result.each_with_index do |row, i|
break if row > 10
end
result.cancel
``````

If the SQL executed by the client returns affected rows, you can easily find out how many.

``````result.each
result.affected_rows # => 24
``````

This pattern is so common for UPDATE and DELETE statements that the #do method cancels any need for loading the result data and returns the `#affected_rows`.

``````result = client.execute("DELETE FROM [datatypes]")
result.do # => 72
``````

Likewise for `INSERT` statements, the #insert method cancels any need for loading the result data and executes a `SCOPE_IDENTITY()` for the primary key.

``````result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
result.insert # => 420
``````

The result object can handle multiple result sets form batched SQL or stored procedures. It is critical to remember that when calling each with a block for the first time will return each "row" of each result set. Calling each a second time with a block will yield each "set".

``````sql = ["SELECT TOP (1) [id] FROM [datatypes]",
"SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')

set1, set2 = client.execute(sql).each
set1 # => [{"id"=>11}]
set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]

result = client.execute(sql)

result.each do |rowset|
# 1st: {"id"=>11}
# 2nd: {"bigint"=>-9223372036854775807}
# 3rd: {"bigint"=>9223372036854775806}
end

result.each do |rowset|
# Second time over (if columns cached), yields each set.
# 1st: [{"id"=>11}]
# 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
end
``````

Use the `#sqlsent?` and `#canceled?` query methods on the client to determine if an active SQL batch still needs to be processed and or if data results were canceled from the last result object. These values reset to true and false respectively for the client at the start of each `#execute` and new result object. Or if all rows are processed normally, `#sqlsent?` will return false. To demonstrate, lets assume we have 100 rows in the result object.

``````client.sqlsent?   # = false
client.canceled?  # = false

result = client.execute("SELECT * FROM [super_big_table]")

client.sqlsent?   # = true
client.canceled?  # = false

result.each do |row|
# Assume we break after 20 rows with 80 still pending.
break if row["id"] > 20
end

client.sqlsent?   # = true
client.canceled?  # = false

result.cancel

client.sqlsent?   # = false
client.canceled?  # = true
``````

It is possible to get the return code after executing a stored procedure from either the result or client object.

``````client.return_code  # => nil

result = client.execute("EXEC tinytds_TestReturnCodes")
result.do
result.return_code  # => 420
client.return_code  # => 420
``````

## Query Options

Every `TinyTds::Result` object can pass query options to the #each method. The defaults are defined and configurable by setting options in the `TinyTds::Client.default_query_options` hash. The default values are:

• :as => :hash - Object for each row yielded. Can be set to :array.
• :symbolize_keys => false - Row hash keys. Defaults to shared/frozen string keys.
• :cache_rows => true - Successive calls to #each returns the cached rows.
• :timezone => :local - Local to the Ruby client or :utc for UTC.
• :empty_sets => true - Include empty results set in queries that return multiple result sets.

Each result gets a copy of the default options you specify at the client level and can be overridden by passing an options hash to the #each method. For example

``````result.each(:as => :array, :cache_rows => false) do |row|
# Each row is now an array of values ordered by #fields.
# Rows are yielded and forgotten about, freeing memory.
end
``````

Besides the standard query options, the result object can take one additional option. Using `:first => true` will only load the first row of data and cancel all remaining results.

``````result = client.execute("SELECT * FROM [super_big_table]")
result.each(:first => true) # => [{'id' => 24}]
``````

## Row Caching

By default row caching is turned on because the SQL Server adapter for ActiveRecord would not work without it. I hope to find some time to create some performance patches for ActiveRecord that would allow it to take advantages of lazily created yielded rows from result objects. Currently only TinyTDS and the Mysql2 gem allow such a performance gain.

## Encoding Error Handling

TinyTDS takes an opinionated stance on how we handle encoding errors. First, we treat errors differently on reads vs. writes. Our opinion is that if you are reading bad data due to your client's encoding option, you would rather just find `?` marks in your strings vs being blocked with exceptions. This is how things wold work via ODBC or SMS. On the other hand, writes will raise an exception. In this case we raise the SYBEICONVO/2402 error message which has a description of `Error converting characters into server's character set. Some character(s) could not be converted.`. Even though the severity of this message is only a `4` and TinyTDS will automatically strip/ignore unknown characters, we feel you should know that you are inserting bad encodings. In this way, a transaction can be rolled back, etc. Remember, any database write that has bad characters due to the client encoding will still be written to the database, but it is up to you rollback said write if needed. Most ORMs like ActiveRecord handle this scenario just fine.

## Timeout Error Handling

TinyTDS will raise a `TinyTDS::Error` when a timeout is reached based on the options supplied to the client. Depending on the reason for the timeout, the connection could be dead or alive. When db processing is the cause for the timeout, the connection should still be usable after the error is raised. When network failure is the cause of the timeout, the connection will be dead. If you attempt to execute another command batch on a dead connection you will see a `DBPROCESS is dead or not enabled` error. Therefore, it is recommended to check for a `dead?` connection before trying to execute another command batch.

## Binstubs

The TinyTDS gem uses binstub wrappers which mirror compiled FreeTDS Utilities binaries. These native executables are usually installed at the system level when installing FreeTDS. However, when using MiniPortile to install TinyTDS as we do with Windows binaries, these binstubs will find and prefer local gem `exe` directory executables. These are the following binstubs we wrap.

• tsql - Used to test connections and debug compile time settings.
• defncopy - Used to dump schema structures.

## Using TinyTDS With Rails & The ActiveRecord SQL Server adapter.

TinyTDS is the default connection mode for the SQL Server adapter in versions 3.1 or higher. The SQL Server adapter can be found using the links below.

## Using TinyTDS with Azure

TinyTDS is fully tested with the Azure platform. You must set the `azure: true` connection option when connecting. This is needed to specify the default database name in the login packet since Azure has no notion of `USE [database]`. FreeTDS must be compiled with OpenSSL too.

IMPORTANT: Do not use `username@server.database.windows.net` for the username connection option! You must use the shorter `username@server` instead!

Also, please read the Azure SQL Database General Guidelines and Limitations MSDN article to understand the differences. Specifically, the connection constraints section!

## Connection Settings

A DBLIB connection does not have the same default SET options for a standard SMS SQL Server connection. Hence, we recommend the following options post establishing your connection.

#### SQL Server

``````SET ANSI_DEFAULTS ON

SET QUOTED_IDENTIFIER ON
SET CURSOR_CLOSE_ON_COMMIT OFF
SET IMPLICIT_TRANSACTIONS OFF
SET TEXTSIZE 2147483647
SET CONCAT_NULL_YIELDS_NULL ON
``````

#### Azure

``````SET ANSI_NULLS ON
SET ANSI_NULL_DFLT_ON ON
SET ANSI_WARNINGS ON

SET QUOTED_IDENTIFIER ON
SET CURSOR_CLOSE_ON_COMMIT OFF
SET IMPLICIT_TRANSACTIONS OFF
SET TEXTSIZE 2147483647
SET CONCAT_NULL_YIELDS_NULL ON
``````

TinyTDS must be used with a connection pool for thread safety. If you use ActiveRecord or the Sequel gem this is done for you. However, if you are using TinyTDS on your own, we recommend using the ConnectionPool gem when using threads:

## Emoji Support 😍

This is possible using FreeTDS version 0.95 or higher. You must use the `use_utf16` login option or add the following config to your `freetds.conf` in either the global section or a specfic dataserver. If you are on Windows, the default location for your conf file will be in `C:\Sites`.

``````[global]
use utf-16 = true
``````

The default is true and since FreeTDS v1.0 would do this as well.

## Compiling Gems for Windows

For the convenience of Windows users, TinyTDS ships pre-compiled gems for supported versions of Ruby on Windows. In order to generate these gems, rake-compiler-dock is used. This project provides several Docker images with rvm, cross-compilers and a number of different target versions of Ruby.

Run the following rake task to compile the gems for Windows. This will check the availability of Docker (and boot2docker on Windows or OS-X) and will give some advice for download and installation. When docker is running, it will download the docker image (once-only) and start the build:

``````\$ rake gem:windows
``````

The compiled gems will exist in `./pkg` directory.

## Development & Testing

First, clone the repo using the command line or your Git GUI of choice.

``````\$ git clone git@github.com:rails-sqlserver/tiny_tds.git
``````

After that, the quickest way to get setup for development is to use Docker. Assuming you have downloaded docker for your platform, you can use docker-compose to run the necessary containers for testing.

``````\$ docker-compose up -d
``````

This will download our SQL Server for Linux Docker image based from microsoft/mssql-server-linux/. Our image already has the `[tinytdstest]` DB and `tinytds` users created. This will also download a toxiproxy Docker image which we can use to simulate network failures for tests. Basically, it does the following.

``````\$ docker network create main-network
\$ docker run -p 1433:1433 -d --name sqlserver --network main-network metaskills/mssql-server-linux-tinytds
\$ docker pull shopify/toxiproxy
\$ docker run -p 8474:8474 -p 1234:1234 -d --name toxiproxy --network main-network shopify/toxiproxy
``````

If you are using your own database. Make sure to run these SQL commands as SA to get the test database and user installed.

``````CREATE DATABASE [tinytdstest];
``````
``````CREATE LOGIN [tinytds] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [tinytdstest];
USE [tinytdstest];
CREATE USER [tinytds] FOR LOGIN [tinytds];
``````

From here you can build and run tests against an installed version of FreeTDS.

``````\$ bundle install
\$ bundle exec rake
``````

Examples us using enviornment variables to customize the test task.

``````\$ rake TINYTDS_UNIT_DATASERVER=mydbserver
\$ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2008
\$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
\$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_UNIT_PORT=5000 TINYTDS_SCHEMA=sybase_ase
``````

## Docker Builds

If you use a multi stage Docker build to assemble your gems in one phase and then copy your app and gems into another, lighter, container without build tools you will need to make sure you tell the OS how to find dependencies for TinyTDS.

After you have built and installed FreeTDS it will normally place library files in `/usr/local/lib`. When TinyTDS builds native extensions, it already knows to look here but if you copy your app to a new container that link will be broken.

Set the LD_LIBRARY_PATH environment variable `export LD_LIBRARY_PATH=/usr/local/lib:\${LD_LIBRARY_PATH}` and run `ldconfig`. If you run `ldd tiny_tds.so` you should not see any broken links. Make sure you also copied in the library dependencies from your build container with a command like `COPY --from=builder /usr/local/lib /usr/local/lib`.

## Help & Support

My name is Ken Collins and I currently maintain the SQL Server adapter for ActiveRecord and wrote this library as my first cut into learning Ruby C extensions. Hopefully it will help promote the power of Ruby and the Rails framework to those that have not yet discovered it. My blog is metaskills.net and I can be found on twitter as @metaskills. Enjoy!

## Special Thanks

Author: rails-sqlserver
Source code: https://github.com/rails-sqlserver/tiny_tds

1650446963

To create Upload, Preview & Download Images using JavaScript & PHP. First, you need to create two Files one PHP File and another one is CSS File.

### 1: First, create a PHP file with the name of index.php

``````<?php
//getting the user img url from input field
\$imgURL = \$_POST['file']; //storing in variable
\$regPattern = '/\.(jpe?g|png|gif|bmp)\$/i'; //pattern to validataing img extension
if(preg_match(\$regPattern, \$imgURL)){ //if pattern matched to user img url
\$initCURL = curl_init(\$imgURL); //intializing curl
curl_setopt(\$initCURL, CURLOPT_RETURNTRANSFER, true);
curl_close(\$initCURL); //closing curl
// now we convert the base 64 format to jpg to download
header('Content-type: image/jpg'); //in which extension you want to save img
header('Content-Disposition: attachment;filename="image.jpg"'); //in which name you want to save img
}
}
?>
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<body>
<div class="wrapper">
<div class="preview-box">
<div class="cancel-icon"><i class="fas fa-times"></i></div>
<div class="img-preview"></div>
<div class="content">
<div class="img-icon"><i class="far fa-image"></i></div>
<div class="text">Paste the image url below, <br/>to see a preview or download!</div>
</div>
</div>
<form action="index.php" method="POST" class="input-data">
</form>
</div>
<script>
//if user focus out from the input field
\$("#field").on("focusout", function(){
//getting user entered img URL
var imgURL = \$("#field").val();
if(imgURL != ""){ //if input field isn't blank
var regPattern = /\.(jpe?g|png|gif|bmp)\$/i; //pattern to validataing img extension
if(regPattern.test(imgURL)){ //if pattern matched to image url
var imgTag = '<img src="'+ imgURL +'" alt="">'; //creating a new img tag to show img
\$(".img-preview").append(imgTag); //appending img tag with user entered img url
// adding new class which i've created in css
\$(".cancel-icon").on("click", function(){
//we'll remove all new added class on cancel icon click
\$(".preview-box").removeClass("imgActive");
\$("#button").removeClass("active");
\$("#field").removeClass("disabled");
\$(".img-preview img").remove();
// that's all in javascript/jquery now the main part is PHP
});
}else{
alert("Invalid img URL - " + imgURL);
\$("#field").val('');//if pattern not matched we'll leave the input field blank
}
}
});
});
</script>

</body>
</html>``````

### 2: Second, create a CSS file with the name of style.css

``````@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
margin: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
html,body{
display: grid;
height: 100%;
place-items: center;
}
::selection{
color: #fff;
background: #4158d0;
}
.wrapper{
height: 450px;
width: 500px;
display: flex;
align-items: center;
justify-content: space-between;
flex-direction: column;
}
.wrapper .preview-box{
position: relative;
width: 100%;
height: 320px;
display: flex;
text-align: center;
align-items: center;
justify-content: center;
border: 2px dashed #c2cdda;
}
.preview-box.imgActive{
border: 2px solid transparent;
}
.preview-box .cancel-icon{
position: absolute;
right: 20px;
top: 10px;
z-index: 999;
color: #4158d0;
font-size: 20px;
cursor: pointer;
display: none;
}
.preview-box.imgActive:hover .cancel-icon{
display: block;
}
.preview-box .cancel-icon:hover{
color: #ff0000;
}
.preview-box .img-preview{
height: 100%;
width: 100%;
position: absolute;
}
.preview-box .img-preview img{
height: 100%;
width: 100%;
}
.wrapper .preview-box .img-icon{
font-size: 100px;
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.wrapper .preview-box .text{
font-size: 18px;
font-weight: 500;
color: #5B5B7B;
}
.wrapper .input-data{
height: 130px;
width: 100%;;
display: flex;
align-items: center;
justify-content: space-evenly;
flex-direction: column;
}
.wrapper .input-data #field{
width: 100%;
height: 50px;
outline: none;
font-size: 17px;
user-select: auto;
border: 2px solid lightgrey;
transition: all 0.3s ease;
}
.input-data #field.disabled{
color: #b3b3b3;
pointer-events: none;
}
.wrapper .input-data #field:focus{
border-color: #4158d0;
}
.input-data #field::placeholder{
color: #b3b3b3;
}
.wrapper .input-data #button{
height: 50px;
width: 100%;
border: none;
outline: none;
color: #fff;
font-weight: 500;
font-size: 18px;
cursor: pointer;
opacity: 0.5;
pointer-events: none;
transition: all 0.3s ease;
}
.input-data #button.active{
opacity: 1;
pointer-events: auto;
}
.input-data #button:active{
transform: scale(0.99);
}``````

1591177440

## Visual Analytics and Advanced Data Visualization

Visual Analytics is the scientific visualization to emerge an idea to present data in such a way so that it could be easily determined by anyone.

It gives an idea to the human mind to directly interact with interactive visuals which could help in making decisions easy and fast.

Visual Analytics basically breaks the complex data in a simple way.

The human brain is fast and is built to process things faster. So Data visualization provides its way to make things easy for students, researchers, mathematicians, scientists e

#blogs #data visualization #business analytics #data visualization techniques #visual analytics #visualizing ml models

1606912089

## How to create a calculator using javascript - Pure JS tutorials |Web Tutorials

### In this video I will tell you How to create a calculator using javascript very easily.

#how to build a simple calculator in javascript #how to create simple calculator using javascript #javascript calculator tutorial #javascript birthday calculator #calculator using javascript and html