class Import

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()

Import constructor.

bool
checkTimeout()

Checks whether timeout is getting close

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

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 $sql_data = [])

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 $import_run_buffer, 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(int $size = 32768)

Returns next part of imported file/buffer

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 $last_cumulative_size)

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

int
getDecimalScale(string $last_cumulative_size)

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 $last_cumulative_size, int|null $last_cumulative_type, int $curr_type, string $cell)

Obtains the size of the given cell

int
detectType(int|null $last_cumulative_type, 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 $db_name, array $tables, array|null $analyses = null, array|null $additional_sql = null, array|null $options = null, array $sql_data)

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

void
stop(Message $error_message)

Stops the import on (mostly upload/file related) error

void
handleSimulateDmlRequest()

Handles request for Simulation of UPDATE/DELETE queries.

array
getMatchedRows(array $analyzed_sql_results = [])

Find the matching rows for UPDATE/DELETE query.

string
getSimulatedUpdateQuery(array $analyzed_sql_results)

Transforms a UPDATE query into SELECT statement.

string
getSimulatedDeleteQuery(array $analyzed_sql_results)

Transforms a DELETE query into SELECT statement.

int
executeMatchedRowQuery(string $matched_row_query)

Executes the matched_row_query and returns the resultant row count.

void
handleRollbackRequest(string $sql_query)

Handles request for ROLLBACK.

bool
checkIfRollbackPossible(string $sql_query)

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

bool
isTableTransactional(string $table)

Checks if a table is 'InnoDB' or not.

Details

at line 48
__construct()

Import constructor.

at line 62
bool checkTimeout()

Checks whether timeout is getting close

Return Value

bool true if timeout is close

at line 89
void executeQuery(string $sql, string $full, array $sql_data)

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 $sql_data SQL parse data storage

Return Value

void

at line 176
void runQuery(string $sql = '', string $full = '', array $sql_data = [])

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 $sql_data SQL parse data storage

Return Value

void

at line 302
array|null runQueryPost(array|null $import_run_buffer, string $sql, string $full)

Return import run buffer

Parameters

array|null $import_run_buffer Buffer of queries for import
string $sql SQL query
string $full Query to display

Return Value

array|null Buffer of queries for import

at line 329
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)

at line 358
string|bool getNextChunk(int $size = 32768)

Returns next part of imported file/buffer

Parameters

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

Return Value

string|bool part of file/buffer

at line 451
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

at line 498
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

at line 533
int getDecimalPrecision(string $last_cumulative_size)

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

Parameters

string $last_cumulative_size Size of type decimal

Return Value

int Precision of the given decimal size notation

at line 551
int getDecimalScale(string $last_cumulative_size)

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

Parameters

string $last_cumulative_size Size of type decimal

Return Value

int Scale of the given decimal size notation

at line 569
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

at line 600
string|int detectSize(string|int $last_cumulative_size, int|null $last_cumulative_type, int $curr_type, string $cell)

Obtains the size of the given cell

Parameters

string|int $last_cumulative_size Last cumulative column size
int|null $last_cumulative_type Last cumulative column type (NONE or VARCHAR or DECIMAL or INT or BIGINT)
int $curr_type 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

at line 802
int detectType(int|null $last_cumulative_type, string|null $cell)

Determines what MySQL type a cell is

Parameters

int|null $last_cumulative_type 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)

at line 851
array|bool analyzeTable(array $table)

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

Parameters

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

Return Value

array|bool array(array $types, array $sizes)

at line 956
void buildSql(string $db_name, array $tables, array|null $analyses = null, array|null $additional_sql = null, array|null $options = null, array $sql_data)

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

Parameters

string $db_name Name of the database
array $tables Array of tables for the specified database
array|null $analyses Analyses of the tables
array|null $additional_sql Additional SQL statements to be executed
array|null $options Associative array of options
array $sql_data 2-element array with sql data

Return Value

void

at line 1337
void stop(Message $error_message)

Stops the import on (mostly upload/file related) error

Parameters

Message $error_message The error message

Return Value

void

at line 1365
void handleSimulateDmlRequest()

Handles request for Simulation of UPDATE/DELETE queries.

Return Value

void

at line 1432
array getMatchedRows(array $analyzed_sql_results = [])

Find the matching rows for UPDATE/DELETE query.

Parameters

array $analyzed_sql_results Analyzed SQL results from parser.

Return Value

array

at line 1467
string getSimulatedUpdateQuery(array $analyzed_sql_results)

Transforms a UPDATE query into SELECT statement.

Parameters

array $analyzed_sql_results Analyzed SQL results from parser.

Return Value

string SQL query

at line 1527
string getSimulatedDeleteQuery(array $analyzed_sql_results)

Transforms a DELETE query into SELECT statement.

Parameters

array $analyzed_sql_results Analyzed SQL results from parser.

Return Value

string SQL query

at line 1572
int executeMatchedRowQuery(string $matched_row_query)

Executes the matched_row_query and returns the resultant row count.

Parameters

string $matched_row_query SQL query

Return Value

int Number of rows returned

at line 1590
void handleRollbackRequest(string $sql_query)

Handles request for ROLLBACK.

Parameters

string $sql_query SQL query(s)

Return Value

void

at line 1635
bool checkIfRollbackPossible(string $sql_query)

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

Parameters

string $sql_query SQL query

Return Value

bool

at line 1674
bool isTableTransactional(string $table)

Checks if a table is 'InnoDB' or not.

Parameters

string $table Table details

Return Value

bool