Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jonasraoni's full-sized avatar
☠️
Do what you want cause a pirate is free! You are a pirate!

Jonas Raoni Soares da Silva jonasraoni

☠️
Do what you want cause a pirate is free! You are a pirate!
View GitHub Profile
@jonasraoni
jonasraoni / pgsql-find-create-missing-indexes.sql
Created November 4, 2017 09:27
PostgreSQL SELECT statement to find/create foreign keys that are missing indexes
-- find missing indexes
SELECT
conrelid::regclass, conname, reltuples::bigint
FROM
pg_constraint
JOIN pg_class
ON conrelid = pg_class.oid
WHERE
contype = 'f'
AND NOT EXISTS (
@jonasraoni
jonasraoni / I8073_RemoveNotesWithoutQueriesAndRelatedObjects.php
Last active May 8, 2023 13:30
Fixed lib\pkp\classes\migration\upgrade\v3_4_0\I8073_RemoveNotesWithoutQueriesAndRelatedObjects.php
<?php
/**
* @file classes/migration/upgrade/v3_4_0/I8073_RemoveNotesWithoutQueriesAndRelatedObjects.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class I8073_RemoveNotesWithoutQueriesAndRelatedObjects
@jonasraoni
jonasraoni / example.php
Last active March 3, 2023 12:05
Performs an optimization at a SELECT statement which is intended to be enclosed by a counter query (e.g. "SELECT COUNT(0) FROM (YOUR_GENERIC_QUERY)") for pagination purposes
<?php
print_r(optimizeCountQuery(
'SELECT (SELECT drop FROM expensive WHERE true = ?) AS subquery
FROM table
WHERE (SELECT ?) = param
ORDER BY not_needed_for_count, drop = ?',
[true,'param', true]
));
/*
@jonasraoni
jonasraoni / vue-numeric-directive.js
Last active February 22, 2023 10:16
Numeric directive for Vue (v-decimal and v-integer, with an "unsigned" modifier). Useful to improve existing components which you can't modify.
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
export default class NumericDirective {
constructor(input, binding) {
Object.assign(this, {
input,
binding
});
input.addEventListener('keydown', this);
@jonasraoni
jonasraoni / format-number.js
Created January 12, 2021 09:37
Format number Vue directive
export default {
install: Vue => Vue.filter('format-number', (value, { thousand = ' ', decimal = '.', decimals = null, maxDecimals = decimals, minDecimals = decimals, normalize = true, roundToEven = true } = {}) => {
value = normalize ? normalizeNumber(value) : `${value != null ? value : ''}`;
let pieces = value.split('.');
if (!pieces[0].length) {
return;
}
if (minDecimals > 0) {
pieces[1] = (pieces[1] = pieces[1] || '').padEnd(minDecimals, '0');
@jonasraoni
jonasraoni / Flattener.cs
Created July 14, 2018 14:46
Flatten Array in C#
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Extends the Array with the Flatten method
/// </summary>
public static class Flattener {
/// <summary>
/// Given a N-dimensional array, flattens it into a new one-dimensional array without modifying the elements' order
@jonasraoni
jonasraoni / cloudSettings
Last active November 2, 2021 08:34
Visual Studio Code Settings Sync Gist
{"lastUpload":"2021-11-02T08:34:50.367Z","extensionVersion":"v3.4.3"}
@jonasraoni
jonasraoni / tootip-on-overflow-directive.js
Last active October 1, 2021 09:20
Vue directive: Tooltip on overflow for hidden text
export default class TooltipOnOverflowDirective {
static install (Vue) {
Vue.directive('tooltip-on-overflow', this.directive);
}
static directive = {
bind (el) {
for (const event of ['mouseover', 'mouseout']) {
el.addEventListener(event, TooltipOnOverflowDirective[event]);
}
@jonasraoni
jonasraoni / convert-latin1-utf8.sql
Created July 21, 2021 13:39
Detect and convert bad encoded UTF-8 data in MySQL
-- Convert
SELECT CONVERT(CAST(CONVERT('São Paulo' USING latin1) AS BINARY) USING utf8) -- São Paulo
-- Our lovely friends celebrate ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö \o/
-- Poor man's bad encoding detection (in my case there was LATIN1 data mixed with UTF8 in the database and I just wanted to find when things started to get mixed up)
-- When trying to convert, normally the amount of characters decrease...
SELECT field
FROM table
WHERE LENGTH(CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8)) < LENGTH(field)
@jonasraoni
jonasraoni / remove-trailing-zeros.cs
Created June 28, 2021 11:15
Remove trailing zeros from a decimal value (C#)
var value = 0.1230000000000000M;
Console.WriteLine(value);
Console.WriteLine(value / 1.0000000000000000000000000000M);