Skip to content

Instantly share code, notes, and snippets.

View cchng's full-sized avatar

Carolyn Ch'ng cchng

View GitHub Profile
@cchng
cchng / user_membership.sql
Created September 6, 2018 17:24
postgres user audit
-- a version of response in https://dba.stackexchange.com/questions/56096/how-to-get-all-roles-that-a-user-is-a-member-of-including-inherited-roles
select usename, rolname from pg_user
join pg_auth_members on (pg_user.usesysid=pg_auth_members.member)
join pg_roles on (pg_roles.oid=pg_auth_members.roleid)
order by rolname;
-- a version of response by @SahapAsci in https://dba.stackexchange.com/questions/155332/find-objects-linked-to-a-postgresql-role
with d as (
SELECT
n.nspname AS schema_name,
c.relname AS rel_name,
c.relkind AS rel_kind,
pg_get_userbyid(c.relowner) AS owner_name
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
@cchng
cchng / table.component.ts
Created August 15, 2018 05:50
display a table with angular grid
import { Component, OnDestroy, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AgGridModule } from 'ag-grid-angular';
@Component({
templateUrl: 'myobject.component.html'
})
export class MyObjectComponent implements OnInit {
@cchng
cchng / salesforce.R
Created August 15, 2018 05:33
connect to Salesforce in R with RForcecom
library(RForcecom)
library(httr)
session <- rforcecom.login(<username?,
sprintf("%s%s", <password>, <securitytoken>))
httr.headers <- add_headers(Authorization=sprintf("Bearer %s", session["sessionID"]))
# unlike in simple_salesforce, full url should be provided here
url <- file.path(session["instanceURL"],
"services",
@cchng
cchng / salesforce.py
Last active November 25, 2018 10:21
connect to Salesforce with simple_salesforce in Python
from simple_salesforce import Salesforce
sf = Salesforce(username=...,
password=...,
security_token=...)
# SOQL
sf.query(<soql>)
# REST
# omit https://.../services/data/apiversion prefix from url
@cchng
cchng / kde.js
Created August 15, 2018 05:15
a variation of density plot in java script
// https://gist.github.com/curran/b595fde4d771c5784421 without histograms and with an axis
// created to mimick R density function with a default gaussian kernel and nrd bandwidth computation
// Based on http://bl.ocks.org/900762 by John Firebaugh
// data.json contains an array of floating points between 0-1
d3.json("data.json", function(faithful) {
data = faithful;
var w = 960,
h = 500,
margin = {top: 20, right: 30, bottom: 30, left: 40},
x = d3.scaleLinear().domain([-0.1, 1.1]).range([margin.left, w - margin.right]),
@cchng
cchng / unique_index.sql
Created August 15, 2018 04:28
creating unique indexes with json key value
-- https://www.postgresql.org/docs/9.3/static/indexes-unique.html
CREATE UNIQUE INDEX $indexname ON ("($jsoncolumn ->> $jsonkey::text)", $column2, ...)
@cchng
cchng / postgres_metadata.sql
Last active August 15, 2018 04:25
a query for useful information on your data in a specified schema
-- https://stackoverflow.com/questions/343138/retrieving-comments-from-a-postgresql-db
SELECT col.table_name,col.column_name, pgd.description, col.data_type
FROM pg_catalog.pg_statio_all_tables AS st
INNER JOIN pg_catalog.pg_description pgd ON (pgd.objoid=st.relid)
INNER JOIN information_schema.columns col ON (pgd.objsubid=col.ordinal_position
AND col.table_schema=st.schemaname AND col.table_name=st.relname)
WHERE col.table_schema = $schema_name;