edu.northwestern.at.utils.db.mysql

## Class MySQLInsertGenerator

- java.lang.Object
- - edu.northwestern.at.utils.db.mysql.MySQLInsertGenerator

-     public class MySQLInsertGenerator
      extends java.lang.Object

  A MySQL insert statement generator.

  This class provides for constructing a MySQL-specific SQL Insert statement that takes multiple value lists in one insert. This kind of insert typically runs an order of magnitude faster than individual inserts.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected java.lang.String[]` | `fieldNames` The name of the column fields, in order, for each data row.
    `protected java.lang.StringBuffer` | `insertBuffer` The string buffer in which to build the insert statement.
    `protected boolean[]` | `isNumeric` True if a field is a number, false otherwise.
    `protected java.lang.String` | `tableName` The name of the database table into which to insert the data rows.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `MySQLInsertGenerator(java.lang.String tableName, java.lang.String[] fieldNames, boolean[] isNumeric)` Create a MySQL insert generator.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addRow(java.lang.Object[] rowData)` Add a row of data.
    `protected java.lang.String` | `escapeSingleQuotes(java.lang.String value)` Escapes single quotes in a data value.
    `java.lang.String` | `getInsert()` Get the insert statement.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### tableName

          protected java.lang.String tableName

      The name of the database table into which to insert the data rows.

    - #### fieldNames

          protected java.lang.String[] fieldNames

      The name of the column fields, in order, for each data row.

    - #### isNumeric

          protected boolean[] isNumeric

      True if a field is a number, false otherwise. Use to determine how to format data values in the generated insert statement.

    - #### insertBuffer

          protected java.lang.StringBuffer insertBuffer

      The string buffer in which to build the insert statement.

  - ### Constructor Detail

    - #### MySQLInsertGenerator

          public MySQLInsertGenerator(java.lang.String tableName,
                              java.lang.String[] fieldNames,
                              boolean[] isNumeric)

      Create a MySQL insert generator.
      Parameters:
        `tableName` - The database table name to receive the data.
        `fieldNames` - The name of the column fields, in order, for each data row.
        `isNumeric` - True if the associated field is numeric, false for a string.

  - ### Method Detail

    - #### escapeSingleQuotes

          protected java.lang.String escapeSingleQuotes(java.lang.String value)

      Escapes single quotes in a data value.
      Parameters:
        `value` - The value to escape.
      Returns:
        The value with single quotes escaped using \' .

    - #### addRow

          public void addRow(java.lang.Object[] rowData)

      Add a row of data.
      Parameters:
        `rowData` - An Object[] array containing the data values.

        Each row value must have a proper toString() method defined. The number of row values must match the number of field names passed in the contructor. If there are two many values, the extra values are ignored. If there are too few values, database null values are added.

    - #### getInsert

          public java.lang.String getInsert()

      Get the insert statement.
      Returns:
        The completed insert statement.

        The string buffer used to build the insert statement is emptied. Any subsequent calls to addRow will start a new insert statement with the same table name and field names are defined in the constructor call.