Show:

Db.Mysql Class

Module: Db

MySQL connection class

Constructor

Db.Mysql

(
  • connName
  • dsn
)

Parameters:

  • connName String

    The name of connection

  • dsn String

    The DSN string to make connection

Methods

dbname

() String

Retrieves database name to use with connection

Returns:

DELETE

(
  • table_from
  • [table_using=null]
)
Db.Query.Mysql

Creates a query to delete rows.

Parameters:

  • table_from String

    The table to delete from

  • [table_using=null] String optional

    If set, adds a USING clause with this table. You can then use .join() with the resulting Db_Query.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

fromDate

(
  • date
)
Integer

Returns a timestamp from a Date string

Parameters:

  • date String

    The Date string that comes from the db

Returns:

Integer:

The timestamp

fromDateTime

(
  • datetime
)
Integer

Returns a timestamp from a DateTime string

Parameters:

  • datetime String

    The DateTime string that comes from the db

Returns:

Integer:

The timestamp

info

(
  • [shardName='']
  • [modifications={}]
)
Object

Retrieve connection information possibly modified for particular shard

Parameters:

  • [shardName=''] String optional

    The name of the shard, defaults to '' - i.e. main table

  • [modifications={}] Object optional

    Additional modifications to table information. If supplied override shard modifications

Returns:

Object:

Parsed dsn string with possible modifications

INSERT

(
  • table_into
  • fields
)
Db.Query.Mysql

Creates a query to insert a row into a table

Parameters:

  • table_into String

    The name of the table to insert into

  • fields Object

    The fields as an associative of {column: value} pairs

Returns:

Db.Query.Mysql:

The resulting Db.Query object

prefix

() String

Retrieves table prefix to use with connection

Returns:

rawQuery

(
  • query
  • parameters
)
Db.Query.Mysql

Creates a raw query.

Parameters:

  • query String

    The query string

  • parameters Object | Array

    The parameters to add to the query right away (to be bound when executing). Values corresponding to numeric keys replace question marks, while values corresponding to string keys replace ":key" placeholders, in the SQL.

Returns:

Db.Query.Mysql:

The resulting Db.Query object

reallyConnect

(
  • callback
  • [shardName='']
  • [modifications={}]
  • [dontReconnect=false]
)

Create mysql.Connection and connects to the database table

Parameters:

  • callback Function

    The callback is fired after connection is complete. mysql.Connection is passed as argument

  • [shardName=''] String optional

    The name of the shard to connect

  • [modifications={}] Object optional

    Additional modifications to table information. If supplied override shard modifications

  • [dontReconnect=false] Boolean optional

    Pass true here to avoid automatically reconnecting when connection is lost

rollback

(
  • $criteria
)
Db_Query_Mysql

Creates a query to rollback a previously started transaction.

Parameters:

  • $criteria Array

    The criteria to use, for sharding

Returns:

Db_Query_Mysql:

The resulting Db_Query object

SELECT

(
  • fields
  • tables
)
Db.Query.Mysql

Creates a query to select fields from a table. Needs to be used with Db.Query.from().

Parameters:

  • fields String | Object

    The fields as strings, or associative array of {alias: field} pairs

  • tables String | Object

    The tables as strings, or associative array of {alias: table} pairs

Returns:

Db.Query.Mysql:

The resulting Db.Query object

timestamp

(
  • callback
)
Integer

Returns the timestamp the db server would have, based on synchronization

Parameters:

  • callback Function

    receives (err, timestamp)

Returns:

Integer:

toDate

(
  • input
)
String

Returns a Date string to store in the database

Parameters:

  • input Date | String | Integer

    The UNIX timestamp, e.g. from a strtotime function

Returns:

String:

in "yyyy-mm-dd hh:mm:ss" format

toDateTime

(
  • input
)
String

Returns a DateTime string to store in the database

Parameters:

  • input Date | String | Integer

    a standard UNIX timestamp

Returns:

String:

in "yyyy-mm-dd hh:mm:ss" format

uniqueId

(
  • table
  • field
  • callback
  • [where={}]
  • [options={}]
)

Generate an ID that is unique in a table

Parameters:

  • table String

    The name of the table

  • field String

    The name of the field to check for uniqueness. You should probably have an index starting with this field.

  • callback Function

    When an acceptable unique ID is generated, this function is called with the ID as the first parameter.

  • [where={}] Object optional

    You can indicate conditions here to limit the search for an existing value. The result is an id that is unique within a certain partition.

  • [options={}] Object optional

    Optional hash used to override default options:

    • [length=8] Number optional

      The length of the ID to generate, after the prefix.

    • [characters='abcdefghijklmnopqrstuvwxyz'] String optional

      All the characters from which to construct the id

    • [prefix=''] String optional

      The prefix to prepend to the unique id.

    • [filter] Function optional

      A function that will take the generated string and check it. The filter function can modify the string by returning another string, or simply reject the string by returning false, in which case another string will be generated.

    • [onError] Function | Q.Event optional

      Triggered if an error occurs

UPDATE

(
  • table
)
Db.Query.Mysql

Creates a query to update rows. Must be used with Db.Query.set

Parameters:

  • table String

    The table to update

Returns:

Db.Query.Mysql:

The resulting Db.Query object

Properties

connected

Boolean

Wheather connection is connected to database

Default: false

connection

mysql.Connection

The connection created with mysql.createConnection()

Default: null

connections

Object private

Cache of connections

Default: {}

connName

String

The name of connection

info

Object private

Connection information