<commit_before>{_mk_func, compile-coffeescript, compile-livescript, xpression-to-body} = require \..
{plv8x-sql,operators-sql} = require \./sql

module.exports = (drop, cascade, done) ->
  if typeof drop is \function
    done = drop
    drop = false

  rows <~ @query "select version()"
  @conn
    ..query plv8x-sql drop, cascade

    [_, pg_version] = rows.0.version.match /^PostgreSQL ([\d\.]+)/
    if pg_version >= \9.1.0
      ..query '''
DO $$ BEGIN
    CREATE EXTENSION IF NOT EXISTS plv8;
EXCEPTION WHEN OTHERS THEN END; $$;
DO $$ BEGIN
    CREATE EXTENSION IF NOT EXISTS plls;
EXCEPTION WHEN OTHERS THEN END; $$;
'''
    else
      ..query '''
DO $$ BEGIN
  CREATE FUNCTION plv8_call_handler() RETURNS language_handler AS '$libdir/plv8' LANGUAGE C;
  CREATE FUNCTION plv8_inline_handler(internal) RETURNS void AS '$libdir/plv8' LANGUAGE C;
  CREATE FUNCTION plv8_call_validator(oid) RETURNS void AS '$libdir/plv8' LANGUAGE C;
  CREATE TRUSTED LANGUAGE plv8 HANDLER plv8_call_handler INLINE plv8_inline_handler VALIDATOR plv8_call_validator;
EXCEPTION WHEN OTHERS THEN END; $$;
DO $$ BEGIN
  CREATE FUNCTION plls_call_handler() RETURNS language_handler AS '$libdir/plv8' LANGUAGE C;
  CREATE FUNCTION plls_inline_handler(internal) RETURNS void AS '$libdir/plv8' LANGUAGE C;
  CREATE FUNCTION plls_call_validator(oid) RETURNS void AS '$libdir/plv8' LANGUAGE C;
  CREATE TRUSTED LANGUAGE plls HANDLER plls_call_handler INLINE plls_inline_handler VALIDATOR plls_call_validator;
EXCEPTION WHEN OTHERS THEN END; $$;
'''
    if pg_version < \9.2.0
      ..query '''
DO $$ BEGIN
    INSERT INTO pg_catalog.pg_type (SELECT 'json' AS typname,
            typnamespace , typowner , typlen , typbyval , typtype , typcategory
            , typispreferred , typisdefined , typdelim , typrelid , typelem ,
            typarray , typinput , typoutput , typreceive , typsend  , typmodin
            , typmodout , typanalyze , typalign , typstorage , typnotnull ,
            typbasetype , typtypmod , typndims , typdefaultbin , typdefault
            FROM pg_catalog.pg_type  where typname = 'text');
EXCEPTION WHEN OTHERS THEN END; $$;
'''
    ..query '''
DO $$ BEGIN
  CREATE DOMAIN plv8x.json AS json;
EXCEPTION WHEN OTHERS THEN END; $$;
'''
    ..query '''
select oid from pg_catalog.pg_type  where typname = 'json' and typtype ='b';
    ''', [], (err, res) ~>
      if res?rows?0
        @register-json-type that.oid
      else
        throw "json type not found"

  @conn<commit_msg>find json type oid for pg < 9.2 only<commit_after>{_mk_func, compile-coffeescript, compile-livescript, xpression-to-body} = require \..
{plv8x-sql,operators-sql} = require \./sql

module.exports = (drop, cascade, done) ->
  if typeof drop is \function
    done = drop
    drop = false

  rows <~ @query "select version()"
  @conn
    ..query plv8x-sql drop, cascade

    [_, pg_version] = rows.0.version.match /^PostgreSQL ([\d\.]+)/
    if pg_version >= \9.1.0
      ..query '''
DO $$ BEGIN
    CREATE EXTENSION IF NOT EXISTS plv8;
EXCEPTION WHEN OTHERS THEN END; $$;
DO $$ BEGIN
    CREATE EXTENSION IF NOT EXISTS plls;
EXCEPTION WHEN OTHERS THEN END; $$;
'''
    else
      ..query '''
DO $$ BEGIN
  CREATE FUNCTION plv8_call_handler() RETURNS language_handler AS '$libdir/plv8' LANGUAGE C;
  CREATE FUNCTION plv8_inline_handler(internal) RETURNS void AS '$libdir/plv8' LANGUAGE C;
  CREATE FUNCTION plv8_call_validator(oid) RETURNS void AS '$libdir/plv8' LANGUAGE C;
  CREATE TRUSTED LANGUAGE plv8 HANDLER plv8_call_handler INLINE plv8_inline_handler VALIDATOR plv8_call_validator;
EXCEPTION WHEN OTHERS THEN END; $$;
DO $$ BEGIN
  CREATE FUNCTION plls_call_handler() RETURNS language_handler AS '$libdir/plv8' LANGUAGE C;
  CREATE FUNCTION plls_inline_handler(internal) RETURNS void AS '$libdir/plv8' LANGUAGE C;
  CREATE FUNCTION plls_call_validator(oid) RETURNS void AS '$libdir/plv8' LANGUAGE C;
  CREATE TRUSTED LANGUAGE plls HANDLER plls_call_handler INLINE plls_inline_handler VALIDATOR plls_call_validator;
EXCEPTION WHEN OTHERS THEN END; $$;
'''
    if pg_version < \9.2.0
      ..query '''
DO $$ BEGIN
    INSERT INTO pg_catalog.pg_type (SELECT 'json' AS typname,
            typnamespace , typowner , typlen , typbyval , typtype , typcategory
            , typispreferred , typisdefined , typdelim , typrelid , typelem ,
            typarray , typinput , typoutput , typreceive , typsend  , typmodin
            , typmodout , typanalyze , typalign , typstorage , typnotnull ,
            typbasetype , typtypmod , typndims , typdefaultbin , typdefault
            FROM pg_catalog.pg_type  where typname = 'text');
EXCEPTION WHEN OTHERS THEN END; $$;
'''
      ..query '''
  select oid from pg_catalog.pg_type  where typname = 'json' and typtype ='b';
      ''', [], (err, res) ~>
        if res?rows?0
          @register-json-type that.oid
        else
          throw "json type not found"

    ..query '''
DO $$ BEGIN
  CREATE DOMAIN plv8x.json AS json;
EXCEPTION WHEN OTHERS THEN END; $$;
'''

  @conn
{'fraction_non_alphanumeric': 0.10515793682526989, 'fraction_numerical': 0.010395841663334666, 'mean_word_length': 3.354221061792863, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '182917', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1732, 'n_words': 488}