Get table checksum in Laravel 5.4 - mysql

What do you use to get the checksum of a table in Laravel? Is there something already abstracted for this or you have to use raw commands?

You have to use raw commands, but it is pretty easy, just add this method to your model:
public static function checksum()
{
$tableName = with(new static)->getTable();
$query = sprintf('CHECKSUM TABLE %s', $tableName);
return \DB::select(\DB::raw($query))[0]->Checksum;
}
You can now call this method statically to get the checksum.

Related

Automatically append database name to database table names

Is it possible to automatically append database name to a table name in laravel?
The issue is that I have to join data from multiple databases in single queries and sometime I am having to manually replace template names, which is a lot of hassle.
The only solution that I found is that I can append database name to the table name within a model, i.e.
class User extends Model
{
protected $table = 'database_name.table_name';
}
But with above we are losing support for table prefixes.
Example when database name is not applied:
$userQuery = User::where('id', 1)
->with('settings')
->select('some data');
DB::connection('x')
->table('table-on-different-connection')
->insertUsing(['some columns'], $userQuery);
$userQuery is on a different connection and database_name was not applied to the tables within that part of the query. Hence why insertUsing is trying to perform joins on connection x.
Laravel is not appending database name when generating SQL statements. To resolve that, you need to create your own MySQL wrapper and append the database name to the table name that way.
This is where the issue takes place:
vendor\laravel\framework\src\Illuminate\Database\Query\Grammar.php
public function wrapTable($table)
{
if (! $this->isExpression($table)) {
return $this->wrap($this->tablePrefix.$table, true);
}
return $this->getValue($table);
}
You need to override wrapTable method and append database name to the table that way.
i.e.
public function wrapTable($table)
{
$databaseName = $this->wrap('my_database'); // dynamically defined name here
if (! $this->isExpression($table)) {
$tableName = $this->wrap($this->tablePrefix.$table, true);
return "{$databaseName}.{$tableName}";
}
return $this->getValue("{$databaseName}.{$table}");
}
How you go about extending Grammar and override this method depends on your application and your needs. This can be done globally (i.e. via AppProvider) or for an individual query.

Laravel 5.4 formatting result set

Can someone help me convert this query so that my result set is in different format?
$sessions = new Session();
$results = $sessions->where('session_status', $status)->where('application_period_id', (int) ApplicationPeriod::all()->last()->id)->get()->pluck('speaker_id');
$speakers = Speaker::whereIn('id', $results)
->with('session.audiancesession.audiances')
->with('session.subjectsession.subjects')
->with(['session' =>
function ($query) use($status) {
$query->where('session_status', '=', $status);
}])->orderBy('last_name')->get();
This is requested via Ajax(axios)... Now this is how result is formatted:
Obj->data(array of objects)->[0]->name
->address
->session(array of objects)
->[0]->time
->fee
My issue is that my session parameter is array and there can only ever be (1) so I don't need to to be an array and I would like to have object (json) instead.
Thank you!
You might have more success if you change your client-side code to work with an array of sessions each session having its speaker, that means your original query would be like
$sessions = Sessions::with([
'speaker', 'audiancesession.audiances', 'subjectsession.subjects'
])->where('application_period_id', (int) ApplicationPeriod::orderBy('id','DESC')->first())->get();
Note the order by -> first in the ApplicationPeriod makes it so you don't have to get all application periods from the database to memory.
Then your client side should handle an array of sessions.
You can transform the above slightly using to get a similar result to what you need:
$speakers = $sessions->map(function ($session) {
$speaker = collect($session->speaker->toArray());
$speaker->put('session', collect($session->toArray())->except('speaker'));
return $speaker;
})->orderBy('last_name','DESC');
Though I wouldn't guarantee the result here as I've not tested it on your (complex looking) data.

Laravel Schema Builder : Creating a binary(16) column

Using Laravel 5.5 and Mysql (10.1.19-MariaDB)
For a md5 hash I want a binary(16) column.
Let's call the colum url_hash
When using :
$table->binary('url_hash');
it will give me a BLOB column.
source : https://laravel.com/docs/5.5/migrations#creating-columns
I have seen all kind of hacks or plugins around the web for this , but what is the most simple one without any external plugins that could break on the next update?
Cheers
You can just set the character set to binary.
$table->char('url_hash', 16)->charset('binary');
This is actually shown as a real binary column type with a length of 16 in MySQL Workbench.
There shouldn't be any difference: https://stackoverflow.com/a/15335682/5412658
Extend the MySqlGrammar class, e.g. in app/MySqlGrammar.php:
namespace App;
use Illuminate\Support\Fluent;
class MySqlGrammar extends \Illuminate\Database\Schema\Grammars\MySqlGrammar {
protected function typeRealBinary(Fluent $column) {
return "binary({$column->length})";
}
}
Then use a macro to add your own column type:
DB::connection()->setSchemaGrammar(new \App\MySqlGrammar());
Blueprint::macro('realBinary', function($column, $length) {
return $this->addColumn('realBinary', $column, compact('length'));
});
Schema::create('table', function(Blueprint $table) {
$table->realBinary('url_hash', 16);
});
Laravel author recommends to do a DB:statement call and run the raw SQL.
If you are running migration, you could run this raw SQL after Schema::create:
DB::statement('ALTER TABLE table_name ADD url_hash binary(16) AFTER some_column');
Depends on use case, you could need to run this raw SQL to drop the column before dropping the table:
DB::statement('ALTER TABLE table_name DROP url_hash');

insert query symfony 2 without form

I'm working with Symfony 2 and I need to insert some data in a MySQL table. I know how to do it using a form:
$m=new table();
$form=$this->container->get('form.factory')->create(new tableType(),$m);
$request=$this->getRequest();
if($request->getMethod()=='POST')
{
$form->bind($request);
if ($form->isValid())
{
$rm=$this->container->get('doctrine')->getEntityManager();
$rm->persist($m);
$rm->flush();
}
that works but I dont want to use a pre-defined form because I need complex control on my input. I need to generate the value with jQuery.
So how can I proceed to insert the values of my input into my table?
Generally you can pass the whole request to the action as following
use Symfony\Component\HttpFoundation\Request;
// <...>
public function fooAction(Request $request)
{
$foo = $request->query->get('foo', 'default_value_for_foo'); // get the foo param from request query string (GET params)
$bar = $request->query->get('bar', 'default_value_for_bar'); // get the bar param from request POST params
}
Also you might be interested in collection form types which can allow you to generate multiple rows or entities for form (not fixed)
That's actually easier than using forms :D
<?php
// ...
// fetch your params
$m = new table();
$m->setWhatever($request->get('whatever'));
// persist
$em = $this->get('doctrine.orm.entity_manager');
$em->persist($m);
Note:
use camel-case PHP-class names: e.g. Table, NiceTable
$form->bind is deprecated I think, use $form->handleRequest
anyway if you need to validate your input, I recommend using validators and forms anyway. Setting up a model and validate it is quite smart. You don't need to create the view createView() of it of course, but the validation component in Symfony is very mighty :).

using spring framework's jdbcTemplate for complex filtering

I am using Spring framework's jdbcTemplate to get a list of records from the db. Now I want to add filtering to it by appending a constraint to the query.
For eg. say the list represents persons and shows - name, email and location
The original query is
String sql = "SELECT name, email, location FROM persons WHERE status = ?";
depending upon the filters, constraints will be appended to it
if(filters.containsKey("person_name")) {
sql += " AND name LIKE '%" + filters.get("person_name") + "%'";
}
//similarly
if(filters.containsKey("person_email")) {
....
}
//similarly
if(filters.containsKey("person_location")) {
....
}
Thus the query will be created and executed by passing it to the query method of the jdbcTemplate object
this.jdbcTemplate.query(sql, new Object[] { 1 }, RowMapper<Person> rowmapper)
My concern is that, by using the above method, it becomes vulnerable to injection as the values against which filters are applied are directly written in the query without any escaping.
Is it possible to create the second param (arguments array) dynamically as well just like the query is built ?
Is there an alternative approach for this using jdbcTemplate ?
Edit:
I am now using StringEscapeUtils.escapeSql from org.apache.commons.lang.StringEscapeUtils to escape the values. But still looking for better methods or those already provided by spring if any.
Thanks
I believe there is a better way. Take your code
if (filters.containsKey("person_name")) {
sql += " AND name LIKE '%" + filters.get("person_name") + "%'";
}
and change it to
if (filters.containsKey("person_name")) {
sql += " AND name LIKE '%?%";
}
You can then pass in the filters.get("person_name") in your query method. This will keep you safe from injection attacks.
In response to comment
I thought of this as well. But how to
create the array of Objects (new
Object[] { .. }) dynamically ?
You can use a java.util.List and call the toArray() method. Kind of like this
import java.util.List;
import java.util.ArrayList;
List<Object> args = new ArrayList<Object>();
if (filters.containsKey("person_name")) {
sql += " AND name LIKE '%?%";
args.add(filters.get("person_name"));
}
Then when you need the arguments as an array
args.toArray();