Skip to content

Instantly share code, notes, and snippets.

View xivSolutions's full-sized avatar

John Atten xivSolutions

View GitHub Profile
@xivSolutions
xivSolutions / gist:1a2ebfa084f7911b06b9
Created June 12, 2015 03:32
MassiveJS Get Tables with Key Info v1
deallocate tables;
prepare tables(text, text, text) as
SELECT c.TABLE_NAME, c.COLUMN_NAME, kcu.CONSTRAINT_NAME, c.DATA_TYPE, c.CHARACTER_MAXIMUM_LENGTH, tc.CONSTRAINT_TYPE,
CASE tc.CONSTRAINT_TYPE WHEN 'PRIMARY KEY' THEN CAST(1 AS BIt) ELSE CAST(0 AS Bit) END AS IsPrimaryKey,
(CASE (
(SELECT CASE (LENGTH(pg_get_serial_sequence(c.TABLE_NAME, c.COLUMN_NAME)) > 0) WHEN true THEN 1 ELSE 0 END) +
(SELECT CASE (SELECT pgc.relkind FROM pg_class pgc WHERE pgc.relname = c.TABLE_NAME || '_' || c.COLUMN_NAME || '_' || 'seq') WHEN 'S"' THEN 1 ELSE 0 END))
WHEN 0 THEN false ELSE true END) AS IsAuto, c.column_default
FROM information_schema.columns c
LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
@xivSolutions
xivSolutions / gist:a691e341fe2329c64dc8
Last active August 29, 2015 14:19
PG: Aggregate parent/Child records into JSON object
select artists.id as artist_id, (select row_to_json(artist_albums)
from
(
select a.name,
(
select array_to_json(array_agg(albums))
from albums where artist_id = a.id
) as albums
from artists as a where a.id = artists.id
) as artist_albums) as artist from artists where artists.id = 12
@xivSolutions
xivSolutions / gist:1a9aa0df11c232f6b407
Created December 16, 2014 02:37
SQLite Artists Table - CREATE and INSERT
DROP TABLE IF EXISTS [Artist];
CREATE TABLE [Artist]
(
[ArtistId] INTEGER NOT NULL,
[Name] NVARCHAR(120),
CONSTRAINT [PK_Artist] PRIMARY KEY ([ArtistId])
);
-- BEGIN;
@xivSolutions
xivSolutions / gist:1d8ce8920fc8976e9262
Created October 5, 2014 06:08
Grab Next n Sequence Values from Posgres
SELECT nextval('my_sequence_name') FROM generate_series( 1, 100 ) n
@xivSolutions
xivSolutions / gist:0e41cf6c1c45da1b5bad
Created September 13, 2014 00:21
Deserialize ModelStateDictionary
Original Stack Overflow Answer from http://stackoverflow.com/a/23506548/586518
Original Code from SO answer:
```csharp
var value = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
var obj = new { message = "", ModelState = new Dictionary<string,string[]>() };
var x = JsonConvert.DeserializeAnonymousType(value, obj);
@xivSolutions
xivSolutions / gist:78826966dc3ffd81035c
Created September 6, 2014 14:59
C# Regex to split string and Preserve quoted text
splitArray = Regex.Split(subjectString, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")
@xivSolutions
xivSolutions / gist:50f4944acc3399133dd9
Created July 19, 2014 22:13
PG - Include inserted ID in JSON
INSERT INTO "ClientDocuments" ("ClientDocumentId", body) VALUES (nextval('"ClientDocuments_ClientDocumentId_seq"'::regclass), cast('{"id":"' || lastval() || '", "name":"John"}' as json))
@xivSolutions
xivSolutions / gist:a15ad3db384f2b1bcbff
Created July 5, 2014 23:46
Makefile for Harvard cs50 course excercises
# Hacked-together make file for use with Harvard cs50 on-line course excercises
# Type terminal commands as follows: make EXE=yourFileName (don't add extensions)
# the compiler: gcc for C program, define as g++ for C+ use clang for Harvard stuff
CC = clang
# compiler flags:
# -g adds debugging information to the executable file
# -Wall turns on most, but not all, compiler warnings
CFLAGS = -g -Wall
@xivSolutions
xivSolutions / gist:260de8e8e7b225cb8344
Created May 31, 2014 00:46
SQL Server Find Referenced Tables by Foreign Key
SELECT obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
-- In Postgres I can do THIS for a table with a composite PK:
DELETE FROM [Building] WHERE (PropertyId, BuildingId) IN ((1,6),(1,7),(1,8),(1,9),(1,10))
-- What is the SQL Server Equivelent? Or is there one? Am I stuck with some horrible nested subquery?