Overview

Namespaces

  • Kanryu
    • QuickCsv
      • MySQL

Classes

  • QuickCsvField
  • QuickCsvImporter
  • Overview
  • Namespace
  • Class

Class QuickCsvImporter

Module to import CSV very fast

  • Import your CSV data directly into a new/existing temporary table with an RDB-specific API.
  • Validation of input values and correlation record check are confirmed by executing SQL.
  • To move data from the temporary table to the final table, execute a normal INSERT / UPDATE queries.

Direct known subclasses

Kanryu\QuickCsv\MySQL\QuickCsvImporter
Namespace: Kanryu\QuickCsv
Located at QuickCsvImporter.php
Methods summary
public
# __construct( array $options = array() )

Several properties can be specified here, but no arguments are required.

Several properties can be specified here, but no arguments are required.

Parameters

$options
The name of the table for which data is finally imported
public
# setProperties( $options = array() )

Set properties in batch

Set properties in batch

public
# setValidator( string $name, function $func )

Set a function that generates and returns a validation expression from each property value of fieldSchema.

Set a function that generates and returns a validation expression from each property value of fieldSchema.

Parameters

$name
property of each field of fieldSchema
$func
function(&$validations, $fieldMap, $name) {}
public
# setValidatorForType( string $name, function $func )

Set function to generate and return validation expression of type from fieldSchema->type value.

Set function to generate and return validation expression of type from fieldSchema->type value.

Parameters

$name
property of each field of fieldSchema
$func
function(&$validations, $fieldMap, $name, $empty_check) {}
public
# setFieldSchema( $schema )

Specify associative array the relationship between each column of CSV to be imported and the column of import destination table.

Specify associative array the relationship between each column of CSV to be imported and the column of import destination table.

public
# setPdo( $pdo )

Set up an instance of PDO.

Set up an instance of PDO.

For MySQL, you need to initialize it with PDO::MYSQL_ATTR_LOCAL_INFILE.

public
# setQueryCallback( $query_callback )

Customize how queries are issued to the database.

Customize how queries are issued to the database.

If PDO is used directly, setPdo() can be used.

public
# execQuery( $sql, $params, $api )

Run the query to the database with query_callback().

Run the query to the database with query_callback().

public
# create( string $dataTableName = null, array $schema = null )

Create a temporary table

Create a temporary table

Parameters

$dataTableName
The name of the temporary table
$schema
Give the schema defining each column of the imported CSV as an associative array.
public
# import( string $path, array $dataTableName = null, $schema = null )

Specify CSV file and import data into the temporary table

Specify CSV file and import data into the temporary table

Parameters

$path
CSV file full path
$dataTableName
$schema Give the schema defining each column of the imported CSV as an associative array.
$schema
public array
# validateAllFields( )

Validate all records all fields at once

Validate all records all fields at once

Returns

array
Validation error lines. empty if there is no error,
public array
# validateBase( array $api, string $fields, $additional_tables = '' )

Execute the validation expression specified in the parameter and fetch the row that is true.

Execute the validation expression specified in the parameter and fetch the row that is true.

Parameters

$api
$fields An associative array with the validation field name as key and the validation expression as value.
$fields
$additional_tables Query string to insert when joining other tables.
$additional_tables

Returns

array
Validation error lines. empty if there is no error,
public array
# validateNonExistForeignKey( $field, $foreignKey, $foreignTableName, $condition = '' )

Returns records where the specified field does not meet foreign key constraints

Returns records where the specified field does not meet foreign key constraints

If the specified field is the primary key of an external table, the key must exist in the external table.

Parameters

$field
CSV field name
$foreignKey
Foreign key name (field name on the target table)
$foreignTableName
Target table name with foreign key
$condition
Expression that searches the foreign table

Returns

array
error lines
public array
# validateDuplicatedId( string|array $field )

Detect records with duplicate specified fields in CSV

Detect records with duplicate specified fields in CSV

Parameters

$field
Field to check

Returns

array
error lines
public
# getNotDefaultFormula( $field )

Returns a judgment expression that the value of the specified field is not the default value.

Returns a judgment expression that the value of the specified field is not the default value.

public
# updateFieldNumberByAutoCount( string $field, integer $baseNumber = 0, $destTableName = null )

Updates to the line where the numeric field of the specified required item is not entered with the automatically incremented value.

Updates to the line where the numeric field of the specified required item is not entered with the automatically incremented value.

Numbers are generated so that they do not overlap with existing records in the target table.

Parameters

$field
Field name to update
$baseNumber
Minimum value to auto increment
$destTableName
public
# updateFieldNumberByAutoCountWithPrefix( string $field, string $prefix, string $body = '#', integer $baseNumber = 0, $destTableName = null )

Updates to the line where the numeric field with prefix of the specified required item is not entered with the automatically incremented value.

Updates to the line where the numeric field with prefix of the specified required item is not entered with the automatically incremented value.

Numbers are generated so that they do not overlap with existing records in the target table.

Parameters

$field
Field name to update
$prefix
Prefix contained in field value
$body
The number of digits to interpolate with 0. If '0000', the value 12 must be '0012'.
$baseNumber
Minimum value to auto increment
$destTableName
public
# updateExistingRecords( array $immediates = array() )

Overwrite records existing in the target table with CSV

Overwrite records existing in the target table with CSV

Parameters

$immediates
Array for overwriting specified fields with immediates
public
# insertNonExistingRecords( array $immediates = array() )

Add a new record from CSV that does not exist in the target table

Add a new record from CSV that does not exist in the target table

Parameters

$immediates
Array for overwriting specified fields with immediates
Properties summary
public $pdo

This property is set when using PDO. Call setPdo().

This property is set when using PDO. Call setPdo().

# null
public $query_callback

Set this property when you want to define query execution independently. Automatically updated when using PDO.

Set this property when you want to define query execution independently. Automatically updated when using PDO.

# null
public array $fieldSchema

Give the schema defining each column of the imported CSV as an associative array.

Give the schema defining each column of the imported CSV as an associative array.

# array()
public array $fieldMap

associative array of $fieldSchema

associative array of $fieldSchema

# array()
public string $dataTableName

Temporary table name to specify as the CSV direct import destination.

Temporary table name to specify as the CSV direct import destination.

# 'tempCsvData'
public string $destTableName

Destination table name where data is finally entered.

Destination table name where data is finally entered.

# 'TARGET_TABLE'
public string $destPrimaryKey

Primary key of the target table where data is finally entered.

Primary key of the target table where data is finally entered.

# 'PRIMARY_KEY'
public string $csvSeparator
# "','"
public string $csvEncloser
# "'\"'"
public string $csvLineStart
# "''"
public string $csvLineSep
# "'\\r\\n'"
public string $csvRecordId

AUTO_INCREMENT field for returning row number on CSV

AUTO_INCREMENT field for returning row number on CSV

# 'id'
public boolean $hasCsvHeader

If true, the first line of CSV is ignored when importing.

If true, the first line of CSV is ignored when importing.

# true
public boolean $asTemporary

If true, the CSV import table is TEMPORARY TABLE. If false, the table remains after the import is complete. Although there is no practical meaning, the import result remains.

If true, the CSV import table is TEMPORARY TABLE. If false, the table remains after the import is complete. Although there is no practical meaning, the import result remains.

# true
public boolean $dumpSql

If true, dump sqls.

If true, dump sqls.

# false
public string $tableCharCode

Character code of temporary table

Character code of temporary table

# 'utf8'
public string $csvCharCode

CSV file character code

CSV file character code

# 'cp932'
public array $validators

An associative array of functions that return validation expressions from fieldSchema properties

An associative array of functions that return validation expressions from fieldSchema properties

# array()
public array $validatorTypes

An associative array of functions that return validation expressions from the type of fieldSchema

An associative array of functions that return validation expressions from the type of fieldSchema

# array()
API documentation generated by ApiGen