<commit_before># This is an implementation of the OT data backend for MySQL. It requires
# that you have two tables defined in your schema: one for the snapshots
# and one for the operations. You must also install the 'mysql' package.
#
#
# Example usage:
#
#     var connect = require('connect');
#     var share   = require('share').server;
#
#     var server = connect(connect.logger());
#
#     var options = {
#       db: {
#         type: 'mysql',
#         host     : 'example.org',
#         user     : 'bob',
#         password : '<PASSWORD>',
#         create_tables_automatically: true
#       }
#     };
#
#     share.attach(server, options);
#     server.listen(9000);
#
# You can run bin/setup_pg to create the SQL tables initially.

mysql = require('mysql')

defaultOptions =
  schema: 'sharejs'
  uri: null                         # An optional uri for connection
  create_tables_automatically: true
  operations_table: 'ops'
  snapshot_table: 'snapshots'

module.exports = MysqlDb = (options) ->
  return new Db if !(this instanceof MysqlDb)

  options ?= {}
  options[k] ?= v for k, v of defaultOptions

  client = options.client or mysql.createConnection options
  client.connect()

  snapshot_table = options.schema and "#{options.schema}.#{options.snapshot_table}" or options.snapshot_table
  operations_table = options.schema and "#{options.schema}.#{options.operations_table}" or options.operations_table

  @close = ->
    client.end()

  @initialize = (callback) ->
    console.warn 'Creating mysql database tables'

    sql = """
     CREATE SCHEMA #{options.schema};
    """
    client.query sql, (error, result) ->
      error?.message

    sql =  """
      CREATE TABLE #{snapshot_table} (
        doc varchar(256) NOT NULL,
        v int NOT NULL,
        type varchar(256) NOT NULL,
        snapshot varchar(256) NOT NULL,
        meta varchar(256) NOT NULL,
        created_at timestamp NOT NULL,
        CONSTRAINT snapshots_pkey PRIMARY KEY (doc, v)
      );
    """
    client.query sql, (error, result) ->
      error?.message

    sql = """
      CREATE TABLE #{operations_table} (
        doc varchar(256) NOT NULL,
        v int NOT NULL,
        op varchar(256) NOT NULL,
        meta varchar(256) NOT NULL,
        CONSTRAINT operations_pkey PRIMARY KEY (doc, v)
      );
    """
    client.query sql, (error, result) ->
      callback? error?.message

  # This will perminantly delete all data in the database.<commit_msg>fixed schema for mysql initialization<commit_after># This is an implementation of the OT data backend for MySQL. It requires
# that you have two tables defined in your schema: one for the snapshots
# and one for the operations. You must also install the 'mysql' package.
#
#
# Example usage:
#
#     var connect = require('connect');
#     var share   = require('share').server;
#
#     var server = connect(connect.logger());
#
#     var options = {
#       db: {
#         type: 'mysql',
#         host     : 'example.org',
#         user     : 'bob',
#         password : '<PASSWORD>',
#         create_tables_automatically: true
#       }
#     };
#
#     share.attach(server, options);
#     server.listen(9000);
#
# You can run bin/setup_pg to create the SQL tables initially.

mysql = require('mysql')

defaultOptions =
  schema: 'sharejs'
  uri: null                         # An optional uri for connection
  create_tables_automatically: true
  operations_table: 'ops'
  snapshot_table: 'snapshots'

module.exports = MysqlDb = (options) ->
  return new Db if !(this instanceof MysqlDb)

  options ?= {}
  options[k] ?= v for k, v of defaultOptions

  client = options.client or mysql.createConnection options
  client.connect()

  snapshot_table = options.schema and "#{options.schema}.#{options.snapshot_table}" or options.snapshot_table
  operations_table = options.schema and "#{options.schema}.#{options.operations_table}" or options.operations_table

  @close = ->
    client.end()

  @initialize = (callback) ->
    console.warn 'Creating mysql database tables'

    sql = """
     CREATE SCHEMA #{options.schema};
    """
    client.query sql, (error, result) ->
      error?.message

    sql =  """
      CREATE TABLE #{snapshot_table} (
        doc varchar(256) NOT NULL,
        v int NOT NULL,
        type varchar(256) NOT NULL,
        snapshot text NOT NULL,
        meta text NOT NULL,
        created_at timestamp NOT NULL,
        CONSTRAINT snapshots_pkey PRIMARY KEY (doc, v)
      );
    """
    client.query sql, (error, result) ->
      error?.message

    sql = """
      CREATE TABLE #{operations_table} (
        doc varchar(256) NOT NULL,
        v int NOT NULL,
        op text NOT NULL,
        meta text NOT NULL,
        CONSTRAINT operations_pkey PRIMARY KEY (doc, v)
      );
    """
    client.query sql, (error, result) ->
      callback? error?.message

  # This will perminantly delete all data in the database.
{'fraction_non_alphanumeric': 0.11178861788617886, 'fraction_numerical': 0.007723577235772358, 'mean_word_length': 2.605128205128205, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '2614630', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1411, 'n_words': 459}