class Import (View source)

Library that provides common import functions that are used by import plugins

Constants

NONE

VARCHAR

INT

DECIMAL

BIGINT

GEOMETRY

M

D

FULL

TBL_NAME

COL_NAMES

ROWS

TYPES

SIZES

FORMATTEDSQL

Methods

__construct()

No description

bool
checkTimeout()

Checks whether timeout is getting close

void
executeQuery(string $sql, string $full, array $sqlData)

Runs query inside import buffer. This is needed to allow displaying of last SELECT, SHOW or HANDLER results and similar nice stuff.

void
runQuery(string $sql = '', string $full = '', array $sqlData = [])

Runs query inside import buffer. This is needed to allow displaying of last SELECT, SHOW or HANDLER results and similar nice stuff.

array|null
runQueryPost(array|null $importRunBuffer, string $sql, string $full)

Return import run buffer

array
lookForUse(string|null $buffer, string|null $db, bool|null $reload)

Looks for the presence of USE to possibly change current db

string|bool
getNextChunk(File|null $importHandle = null, int $size = 32768)

Returns next part of imported file/buffer

string
skipByteOrderMarksFromContents(string $contents)

Skip possible byte order marks (I do not think we need more charsets, but feel free to add more, you can use wikipedia for reference: https://en.wikipedia.org/wiki/Byte_Order_Mark)

string
getColumnAlphaName(int $num)

Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)

int
getColumnNumberFromName(string $name)

Returns the column number based on the Excel name.

int
getDecimalPrecision(string $lastCumulativeSize)

Obtains the precision (total # of digits) from a size of type decimal

int
getDecimalScale(string $lastCumulativeSize)

Obtains the scale (# of digits to the right of the decimal point) from a size of type decimal

array
getDecimalSize(string $cell)

Obtains the decimal size of a given cell

string|int
detectSize(string|int $lastCumulativeSize, int|null $lastCumulativeType, int $currentCellType, string $cell)

Obtains the size of the given cell

int
detectType(int|null $lastCumulativeType, string|null $cell)

Determines what MySQL type a cell is

array|bool
analyzeTable(array $table)

Determines if the column types are int, decimal, or string

void
buildSql(string $dbName, array $tables, array|null $analyses = null, array|null $additionalSql = null, array|null $options = null, array $sqlData = [])

Builds and executes SQL statements to create the database and tables as necessary, as well as insert all the data.

void
handleSimulateDmlRequest()

Handles request for Simulation of UPDATE/DELETE queries.

array
getMatchedRows(array $analyzedSqlResults = [])

Find the matching rows for UPDATE/DELETE query.

string
getSimulatedUpdateQuery(array $analyzedSqlResults)

Transforms a UPDATE query into SELECT statement.

string
getSimulatedDeleteQuery(array $analyzedSqlResults)

Transforms a DELETE query into SELECT statement.

int
executeMatchedRowQuery(string $matchedRowQuery)

Executes the matched_row_query and returns the resultant row count.

void
handleRollbackRequest(string $sqlQuery)

Handles request for ROLLBACK.

bool
checkIfRollbackPossible(string $sqlQuery)

Checks if ROLLBACK is possible for a SQL query or not.

bool
isTableTransactional(string $table)

Checks if a table is 'InnoDB' or not.

static array
getCompressions()

No description

static false|string
getLocalFiles(array $importList)

No description

Details

__construct()

No description

bool checkTimeout()

Checks whether timeout is getting close

Return Value

bool

true if timeout is close

void executeQuery(string $sql, string $full, array $sqlData)

Runs query inside import buffer. This is needed to allow displaying of last SELECT, SHOW or HANDLER results and similar nice stuff.

Parameters

string $sql

query to run

string $full

query to display, this might be commented

array $sqlData

SQL parse data storage

Return Value

void

void runQuery(string $sql = '', string $full = '', array $sqlData = [])

Runs query inside import buffer. This is needed to allow displaying of last SELECT, SHOW or HANDLER results and similar nice stuff.

Parameters

string $sql

query to run

string $full

query to display, this might be commented

array $sqlData

SQL parse data storage

Return Value

void

array|null runQueryPost(array|null $importRunBuffer, string $sql, string $full)

Return import run buffer

Parameters

array|null $importRunBuffer

Buffer of queries for import

string $sql

SQL query

string $full

Query to display

Return Value

array|null

Buffer of queries for import

array lookForUse(string|null $buffer, string|null $db, bool|null $reload)

Looks for the presence of USE to possibly change current db

Parameters

string|null $buffer

buffer to examine

string|null $db

current db

bool|null $reload

reload

Return Value

array

(current or new db, whether to reload)

string|bool getNextChunk(File|null $importHandle = null, int $size = 32768)

Returns next part of imported file/buffer

Parameters

File|null $importHandle
int $size

size of buffer to read (this is maximal size function will return)

Return Value

string|bool

part of file/buffer

string skipByteOrderMarksFromContents(string $contents)

Skip possible byte order marks (I do not think we need more charsets, but feel free to add more, you can use wikipedia for reference: https://en.wikipedia.org/wiki/Byte_Order_Mark)

BOM could be used for charset autodetection

Parameters

string $contents

The contents to strip BOM

Return Value

string

string getColumnAlphaName(int $num)

Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)

This functions uses recursion to build the Excel column name.

The column number (1-26) is converted to the responding ASCII character (A-Z) and returned.

If the column number is bigger than 26 (= num of letters in alphabet), an extra character needs to be added. To find this extra character, the number is divided by 26 and this value is passed to another instance of the same function (hence recursion). In that new instance the number is evaluated again, and if it is still bigger than 26, it is divided again and passed to another instance of the same function. This continues until the number is smaller than 26. Then the last called function returns the corresponding ASCII character to the function that called it. Each time a called function ends an extra character is added to the column name. When the first function is reached, the last character is added and the complete column name is returned.

Parameters

int $num

the column number

Return Value

string

The column's "Excel" name

int getColumnNumberFromName(string $name)

Returns the column number based on the Excel name.

So "A" = 1, "Z" = 26, "AA" = 27, etc.

Basically this is a base26 (A-Z) to base10 (0-9) conversion. It iterates through all characters in the column name and calculates the corresponding value, based on character value (A = 1, ..., Z = 26) and position in the string.

Parameters

string $name

column name(i.e. "A", or "BC", etc.)

Return Value

int

The column number

int getDecimalPrecision(string $lastCumulativeSize)

Obtains the precision (total # of digits) from a size of type decimal

Parameters

string $lastCumulativeSize

Size of type decimal

Return Value

int

Precision of the given decimal size notation

int getDecimalScale(string $lastCumulativeSize)

Obtains the scale (# of digits to the right of the decimal point) from a size of type decimal

Parameters

string $lastCumulativeSize

Size of type decimal

Return Value

int

Scale of the given decimal size notation

array getDecimalSize(string $cell)

Obtains the decimal size of a given cell

Parameters

string $cell

cell content

Return Value

array

Contains the precision, scale, and full size representation of the given decimal cell

string|int detectSize(string|int $lastCumulativeSize, int|null $lastCumulativeType, int $currentCellType, string $cell)

Obtains the size of the given cell

Handle the error cases more elegantly

Parameters

string|int $lastCumulativeSize

Last cumulative column size

int|null $lastCumulativeType

Last cumulative column type (NONE or VARCHAR or DECIMAL or INT or BIGINT)

int $currentCellType

Type of the current cell (NONE or VARCHAR or DECIMAL or INT or BIGINT)

string $cell

The current cell

Return Value

string|int

Size of the given cell in the type-appropriate format

int detectType(int|null $lastCumulativeType, string|null $cell)

Determines what MySQL type a cell is

Parameters

int|null $lastCumulativeType

Last cumulative column type (VARCHAR or INT or BIGINT or DECIMAL or NONE)

string|null $cell

String representation of the cell for which a best-fit type is to be determined

Return Value

int

The MySQL type representation (VARCHAR or INT or BIGINT or DECIMAL or NONE)

array|bool analyzeTable(array $table)

Determines if the column types are int, decimal, or string

Handle the error case more elegantly

Parameters

array $table

array(string $table_name, array $col_names, array $rows)

Return Value

array|bool

array(array $types, array $sizes)

void buildSql(string $dbName, array $tables, array|null $analyses = null, array|null $additionalSql = null, array|null $options = null, array $sqlData = [])

Builds and executes SQL statements to create the database and tables as necessary, as well as insert all the data.

Parameters

string $dbName

Name of the database

array $tables

Array of tables for the specified database

array|null $analyses

Analyses of the tables

array|null $additionalSql

Additional SQL statements to be executed

array|null $options

Associative array of options

array $sqlData

2-element array with sql data

Return Value

void

void handleSimulateDmlRequest()

Handles request for Simulation of UPDATE/DELETE queries.

Return Value

void

array getMatchedRows(array $analyzedSqlResults = [])

Find the matching rows for UPDATE/DELETE query.

Parameters

array $analyzedSqlResults

Analyzed SQL results from parser.

Return Value

array

string getSimulatedUpdateQuery(array $analyzedSqlResults)

Transforms a UPDATE query into SELECT statement.

Parameters

array $analyzedSqlResults

Analyzed SQL results from parser.

Return Value

string

SQL query

string getSimulatedDeleteQuery(array $analyzedSqlResults)

Transforms a DELETE query into SELECT statement.

Parameters

array $analyzedSqlResults

Analyzed SQL results from parser.

Return Value

string

SQL query

int executeMatchedRowQuery(string $matchedRowQuery)

Executes the matched_row_query and returns the resultant row count.

Parameters

string $matchedRowQuery

SQL query

Return Value

int

Number of rows returned

void handleRollbackRequest(string $sqlQuery)

Handles request for ROLLBACK.

Parameters

string $sqlQuery

SQL query(s)

Return Value

void

bool checkIfRollbackPossible(string $sqlQuery)

Checks if ROLLBACK is possible for a SQL query or not.

Parameters

string $sqlQuery

SQL query

Return Value

bool

bool isTableTransactional(string $table)

Checks if a table is 'InnoDB' or not.

Parameters

string $table

Table details

Return Value

bool

static array getCompressions()

No description

Return Value

array

static false|string getLocalFiles(array $importList)

No description

Parameters

array $importList

List of plugin instances.

Return Value

false|string