Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
PHP-MySQLi-Database-Class
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
PHP-MySQLi-Database-Class
Commits
dc1bf98e
Commit
dc1bf98e
authored
Oct 11, 2018
by
Can Arslan
Committed by
Alexander Butenko
Oct 10, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Better PHPDoc (#746)
PHPDoc updated for some param/return/throws parts
parent
bc561ccc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
308 additions
and
217 deletions
+308
-217
MysqliDb.php
MysqliDb.php
+308
-217
No files found.
MysqliDb.php
View file @
dc1bf98e
...
...
@@ -18,48 +18,56 @@ class MysqliDb
/**
* Static instance of self
*
* @var MysqliDb
*/
protected
static
$_instance
;
/**
* Table prefix
*
* @var string
*/
public
static
$prefix
=
''
;
/**
* MySQLi instances
*
* @var mysqli[]
*/
protected
$_mysqli
=
array
();
/**
* The SQL query to be prepared and executed
*
* @var string
*/
protected
$_query
;
/**
* The previously executed SQL query
*
* @var string
*/
protected
$_lastQuery
;
/**
* The SQL query options required after SELECT, INSERT, UPDATE or DELETE
*
* @var array
*/
protected
$_queryOptions
=
array
();
/**
* An array that holds where joins
*
* @var array
*/
protected
$_join
=
array
();
/**
* An array that holds where conditions
*
* @var array
*/
protected
$_where
=
array
();
...
...
@@ -73,78 +81,91 @@ class MysqliDb
/**
* An array that holds having conditions
*
* @var array
*/
protected
$_having
=
array
();
/**
* Dynamic type list for order by condition value
*
* @var array
*/
protected
$_orderBy
=
array
();
/**
* Dynamic type list for group by condition value
*
* @var array
*/
protected
$_groupBy
=
array
();
/**
* Dynamic type list for tempromary locking tables.
* @var array
*/
/**
* Dynamic type list for temporary locking tables.
*
* @var array
*/
protected
$_tableLocks
=
array
();
/**
* Variable which holds the current table lock method.
* @var string
*/
/**
* Variable which holds the current table lock method.
*
* @var string
*/
protected
$_tableLockMethod
=
"READ"
;
/**
* Dynamic array that holds a combination of where condition/table data value types and parameter references
*
* @var array
*/
protected
$_bindParams
=
array
(
''
);
// Create the empty 0 index
/**
* Variable which holds an amount of returned rows during get/getOne/select queries
*
* @var string
*/
public
$count
=
0
;
/**
* Variable which holds an amount of returned rows during get/getOne/select queries with withTotalCount()
*
* @var string
*/
public
$totalCount
=
0
;
/**
* Variable which holds last statement error
*
* @var string
*/
protected
$_stmtError
;
/**
* Variable which holds last statement error code
*
* @var int
*/
protected
$_stmtErrno
;
/**
* Is Subquery object
*
* @var bool
*/
protected
$isSubQuery
=
false
;
/**
* Name of the auto increment column
*
* @var int
*/
protected
$_lastInsertId
=
null
;
/**
* Column names for update when using onDuplicate method
*
* @var array
*/
protected
$_updateColumns
=
null
;
...
...
@@ -152,36 +173,42 @@ class MysqliDb
/**
* Return type: 'array' to return results as array, 'object' as object
* 'json' as json string
*
* @var string
*/
public
$returnType
=
'array'
;
/**
* Should join() results be nested by table
*
* @var bool
*/
protected
$_nestJoin
=
false
;
/**
* Table name (with prefix, if used)
* @var string
*
* @var string
*/
private
$_tableName
=
''
;
/**
* FOR UPDATE flag
*
* @var bool
*/
protected
$_forUpdate
=
false
;
/**
* LOCK IN SHARE MODE flag
*
* @var bool
*/
protected
$_lockInShareMode
=
false
;
/**
* Key field for Map()'ed result array
*
* @var string
*/
protected
$_mapKey
=
null
;
...
...
@@ -230,7 +257,7 @@ class MysqliDb
* @param string $username
* @param string $password
* @param string $db
* @param int $port
* @param int
$port
* @param string $charset
* @param string $socket
*/
...
...
@@ -271,6 +298,7 @@ class MysqliDb
* A method to connect to the database
*
* @param null|string $connectionName
*
* @throws Exception
* @return void
*/
...
...
@@ -304,6 +332,9 @@ class MysqliDb
$this
->
_mysqli
[
$connectionName
]
=
$mysqli
;
}
/**
* @throws Exception
*/
public
function
disconnectAll
()
{
foreach
(
array_keys
(
$this
->
_mysqli
)
as
$k
)
{
...
...
@@ -313,7 +344,9 @@ class MysqliDb
/**
* Set the connection name to use in the next query
*
* @param string $name
*
* @return $this
* @throws Exception
*/
...
...
@@ -330,7 +363,9 @@ class MysqliDb
* A method to disconnect from the database
*
* @params string $connection connection name to disconnect
* @throws Exception
*
* @param string $connection
*
* @return void
*/
public
function
disconnect
(
$connection
=
'default'
)
...
...
@@ -344,8 +379,10 @@ class MysqliDb
/**
* Create & store at _mysqli new mysqli instance
*
* @param string $name
* @param array $params
* @param array $params
*
* @return $this
*/
public
function
addConnection
(
$name
,
array
$params
)
...
...
@@ -368,8 +405,9 @@ class MysqliDb
/**
* A method to get mysqli object or create it in case needed
*
*
* @return mysqli
* @throws Exception
*/
public
function
mysqli
()
{
...
...
@@ -441,7 +479,7 @@ class MysqliDb
/**
* Helper function to create dbObject with array return type
* Added for consistency as thats default output type
* Added for consistency as that
'
s default output type
*
* @return MysqliDb
*/
...
...
@@ -465,8 +503,8 @@ class MysqliDb
/**
* Method to set a prefix
*
* @param string $prefix
Contains a table
prefix
*
* @param string $prefix
Contains a table
prefix
*
* @return MysqliDb
*/
public
function
setPrefix
(
$prefix
=
''
)
...
...
@@ -475,14 +513,18 @@ class MysqliDb
return
$this
;
}
/**
* Pushes a unprepared statement to the mysqli stack.
* WARNING: Use with caution.
* This method does not escape strings by default so make sure you'll never use it in production.
*
* @author Jonas Barascu
* @param [[Type]] $query [[Description]]
*/
/**
* Pushes a unprepared statement to the mysqli stack.
* WARNING: Use with caution.
* This method does not escape strings by default so make sure you'll never use it in production.
*
* @author Jonas Barascu
*
* @param [[Type]] $query [[Description]]
*
* @return bool|mysqli_result
* @throws Exception
*/
private
function
queryUnprepared
(
$query
)
{
// Execute query
...
...
@@ -508,6 +550,7 @@ class MysqliDb
* @param array $bindParams Variables array to bind to the SQL statement.
*
* @return array Contains the returned rows from the query.
* @throws Exception
*/
public
function
rawQuery
(
$query
,
$bindParams
=
null
)
{
...
...
@@ -544,6 +587,7 @@ class MysqliDb
* @param array $bindParams Variables array to bind to the SQL statement.
*
* @return array|null Contains the returned row from the query.
* @throws Exception
*/
public
function
rawQueryOne
(
$query
,
$bindParams
=
null
)
{
...
...
@@ -564,6 +608,7 @@ class MysqliDb
* @param array $bindParams Variables array to bind to the SQL statement.
*
* @return mixed Contains the returned rows from the query.
* @throws Exception
*/
public
function
rawQueryValue
(
$query
,
$bindParams
=
null
)
{
...
...
@@ -587,11 +632,12 @@ class MysqliDb
/**
* A method to perform select query
*
* @param string $query Contains a user-provided select query.
*
* @param string
$query Contains a user-provided select query.
* @param int|array $numRows Array to define SQL limit in format Array ($offset, $count)
*
* @return array Contains the returned rows from the query.
* @throws Exception
*/
public
function
query
(
$query
,
$numRows
=
null
)
{
...
...
@@ -611,8 +657,8 @@ class MysqliDb
*
* @uses $MySqliDb->setQueryOption('name');
*
* @param string|array $options The optons name of the query.
*
* @param string|array $options The opt
i
ons name of the query.
*
* @throws Exception
* @return MysqliDb
*/
...
...
@@ -650,6 +696,7 @@ class MysqliDb
* Function to enable SQL_CALC_FOUND_ROWS in the get queries
*
* @return MysqliDb
* @throws Exception
*/
public
function
withTotalCount
()
{
...
...
@@ -660,12 +707,13 @@ class MysqliDb
/**
* A convenient SELECT * function.
*
* @param string
$tableName
The name of the database table to work with.
* @param int|array $numRows Array to define SQL limit in format Array ($offset, $count)
* @param string
$tableName
The name of the database table to work with.
* @param int|array $numRows
Array to define SQL limit in format Array ($offset, $count)
* or only $count
* @param string
$columns
Desired columns
* @param string
$columns
Desired columns
*
* @return array Contains the returned rows from the select query.
* @return array|MysqliDb Contains the returned rows from the select query.
* @throws Exception
*/
public
function
get
(
$tableName
,
$numRows
=
null
,
$columns
=
'*'
)
{
...
...
@@ -701,10 +749,11 @@ class MysqliDb
/**
* A convenient SELECT * function to get one record.
*
* @param string
$tableName The name of the database table to work with.
* @param string
$columns
Desired columns
*
* @param string $tableName The name of the database table to work with.
* @param string
$columns
Desired columns
*
* @return array Contains the returned rows from the select query.
* @throws Exception
*/
public
function
getOne
(
$tableName
,
$columns
=
'*'
)
{
...
...
@@ -724,11 +773,12 @@ class MysqliDb
/**
* A convenient SELECT COLUMN function to get a single column value from one row
*
* @param string
$tableName The name of the database table to work with.
* @param string
$column The desired column
* @param int
$limit Limit of rows to select. Use null for unlimited..1 by default
* @param string $tableName The name of the database table to work with.
* @param string
$column The desired column
* @param int $limit Limit of rows to select. Use null for unlimited..1 by default
*
* @return mixed Contains the value of a returned column / array of values
* @throws Exception
*/
public
function
getValue
(
$tableName
,
$column
,
$limit
=
1
)
{
...
...
@@ -755,10 +805,11 @@ class MysqliDb
/**
* Insert method to add new row
*
* @param string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
* @param string $tableName
The name of the table.
* @param array
$insertData Data containing information for inserting into the DB.
*
* @return bool Boolean indicating whether the insert query was completed succesfully.
* @return bool Boolean indicating whether the insert query was completed successfully.
* @throws Exception
*/
public
function
insert
(
$tableName
,
$insertData
)
{
...
...
@@ -768,11 +819,12 @@ class MysqliDb
/**
* Insert method to add several rows at once
*
* @param string $tableName The name of the table.
* @param array
$multiInsertData Two-dimensi
nal Data-array containing information for inserting into the DB.
* @param array
$dataKeys Opti
nal Table Key names, if not set in insertDataSet.
* @param string $tableName
The name of the table.
* @param array
$multiInsertData Two-dimensio
nal Data-array containing information for inserting into the DB.
* @param array
$dataKeys Optio
nal Table Key names, if not set in insertDataSet.
*
* @return bool|array Boolean indicating the insertion failed (false), else return id-array ([int])
* @throws Exception
*/
public
function
insertMulti
(
$tableName
,
array
$multiInsertData
,
array
$dataKeys
=
null
)
{
...
...
@@ -810,10 +862,11 @@ class MysqliDb
/**
* Replace method to add new row
*
* @param string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
* @param string $tableName
The name of the table.
* @param array
$insertData Data containing information for inserting into the DB.
*
* @return bool Boolean indicating whether the insert query was completed succesfully.
* @return bool Boolean indicating whether the insert query was completed successfully.
* @throws Exception
*/
public
function
replace
(
$tableName
,
$insertData
)
{
...
...
@@ -824,9 +877,10 @@ class MysqliDb
* A convenient function that returns TRUE if exists at least an element that
* satisfy the where condition specified calling the "where" method before this one.
*
* @param string
$tableName The name of the database table to work with.
* @param string $tableName The name of the database table to work with.
*
* @return bool
* @throws Exception
*/
public
function
has
(
$tableName
)
{
...
...
@@ -842,6 +896,7 @@ class MysqliDb
* @param int $numRows Limit on the number of rows that can be updated.
*
* @return bool
* @throws Exception
*/
public
function
update
(
$tableName
,
$tableData
,
$numRows
=
null
)
{
...
...
@@ -864,11 +919,12 @@ class MysqliDb
/**
* Delete query. Call the "where" method first.
*
* @param string
$tableName
The name of the database table to work with.
* @param int|array $numRows Array to define SQL limit in format Array ($offset, $count)
* @param string
$tableName
The name of the database table to work with.
* @param int|array $numRows
Array to define SQL limit in format Array ($offset, $count)
* or only $count
*
* @return bool Indicates success. 0 or 1.
* @throws Exception
*/
public
function
delete
(
$tableName
,
$numRows
=
null
)
{
...
...
@@ -901,8 +957,8 @@ class MysqliDb
*
* @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* @param string $cond Condition of where statement (OR, AND)
* @param string $operator
Comparison operator. Default is =
* @param string $cond
Condition of where statement (OR, AND)
*
* @return MysqliDb
*/
...
...
@@ -926,9 +982,9 @@ class MysqliDb
* This function store update column's name and column name of the
* autoincrement column
*
* @param array $updateColumns Variable with values
* @param string $lastInsertId Variable value
*
* @param array
$updateColumns Variable with values
* @param string $lastInsertId
Variable value
*
* @return MysqliDb
*/
public
function
onDuplicate
(
$updateColumns
,
$lastInsertId
=
null
)
...
...
@@ -945,7 +1001,7 @@ class MysqliDb
*
* @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* @param string $operator
Comparison operator. Default is =
*
* @return MysqliDb
*/
...
...
@@ -953,7 +1009,7 @@ class MysqliDb
{
return
$this
->
where
(
$whereProp
,
$whereValue
,
$operator
,
'OR'
);
}
/**
* This method allows you to specify multiple (method chaining optional) AND HAVING statements for SQL queries.
*
...
...
@@ -961,7 +1017,9 @@ class MysqliDb
*
* @param string $havingProp The name of the database field.
* @param mixed $havingValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* @param string $operator Comparison operator. Default is =
*
* @param string $cond
*
* @return MysqliDb
*/
...
...
@@ -989,7 +1047,7 @@ class MysqliDb
*
* @param string $havingProp The name of the database field.
* @param mixed $havingValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* @param string $operator
Comparison operator. Default is =
*
* @return MysqliDb
*/
...
...
@@ -1003,10 +1061,10 @@ class MysqliDb
*
* @uses $MySqliDb->join('table1', 'field1 <> field2', 'LEFT')
*
* @param string $joinTable The name of the table.
* @param string $joinTable
The name of the table.
* @param string $joinCondition the condition.
* @param string $joinType 'LEFT', 'INNER' etc.
*
* @param string $joinType
'LEFT', 'INNER' etc.
*
* @throws Exception
* @return MysqliDb
*/
...
...
@@ -1027,84 +1085,88 @@ class MysqliDb
return
$this
;
}
/**
* This is a basic method which allows you to import raw .CSV data into a table
* Please check out http://dev.mysql.com/doc/refman/5.7/en/load-data.html for a valid .csv file.
* @author Jonas Barascu (Noneatme)
* @param string $importTable The database table where the data will be imported into.
* @param string $importFile The file to be imported. Please use double backslashes \\ and make sure you
* @param string $importSettings An Array defining the import settings as described in the README.md
* @return boolean
*/
/**
* This is a basic method which allows you to import raw .CSV data into a table
* Please check out http://dev.mysql.com/doc/refman/5.7/en/load-data.html for a valid .csv file.
*
* @author Jonas Barascu (Noneatme)
*
* @param string $importTable The database table where the data will be imported into.
* @param string $importFile The file to be imported. Please use double backslashes \\ and make sure you
* @param string $importSettings An Array defining the import settings as described in the README.md
*
* @return boolean
* @throws Exception
*/
public
function
loadData
(
$importTable
,
$importFile
,
$importSettings
=
null
)
{
{
// We have to check if the file exists
if
(
!
file_exists
(
$importFile
))
{
if
(
!
file_exists
(
$importFile
))
{
// Throw an exception
throw
new
Exception
(
"importCSV -> importFile "
.
$importFile
.
" does not exists!"
);
return
;
throw
new
Exception
(
"importCSV -> importFile "
.
$importFile
.
" does not exists!"
);
}
// Define the default values
// We will merge it later
$settings
=
Array
(
"fieldChar"
=>
';'
,
"lineChar"
=>
PHP_EOL
,
"linesToIgnore"
=>
1
);
// Check the import settings
if
(
gettype
(
$importSettings
)
==
"array"
)
{
// Check the import settings
if
(
gettype
(
$importSettings
)
==
"array"
)
{
// Merge the default array with the custom one
$settings
=
array_merge
(
$settings
,
$importSettings
);
}
// Add the prefix to the import table
$table
=
self
::
$prefix
.
$importTable
;
// Add 1 more slash to every slash so maria will interpret it as a path
$importFile
=
str_replace
(
"
\\
"
,
"
\\\\
"
,
$importFile
);
$importFile
=
str_replace
(
"
\\
"
,
"
\\\\
"
,
$importFile
);
// Switch between LOAD DATA and LOAD DATA LOCAL
$loadDataLocal
=
isset
(
$settings
[
"loadDataLocal"
])
?
'LOCAL'
:
''
;
// Build SQL Syntax
$sqlSyntax
=
sprintf
(
'LOAD DATA %s INFILE \'%s\' INTO TABLE %s'
,
$sqlSyntax
=
sprintf
(
'LOAD DATA %s INFILE \'%s\' INTO TABLE %s'
,
$loadDataLocal
,
$importFile
,
$table
);
// FIELDS
$sqlSyntax
.=
sprintf
(
' FIELDS TERMINATED BY \'%s\''
,
$settings
[
"fieldChar"
]);
if
(
isset
(
$settings
[
"fieldEnclosure"
]))
{
if
(
isset
(
$settings
[
"fieldEnclosure"
]))
{
$sqlSyntax
.=
sprintf
(
' ENCLOSED BY \'%s\''
,
$settings
[
"fieldEnclosure"
]);
}
// LINES
$sqlSyntax
.=
sprintf
(
' LINES TERMINATED BY \'%s\''
,
$settings
[
"lineChar"
]);
if
(
isset
(
$settings
[
"lineStarting"
]))
{
if
(
isset
(
$settings
[
"lineStarting"
]))
{
$sqlSyntax
.=
sprintf
(
' STARTING BY \'%s\''
,
$settings
[
"lineStarting"
]);
}
// IGNORE LINES
$sqlSyntax
.=
sprintf
(
' IGNORE %d LINES'
,
$settings
[
"linesToIgnore"
]);
// Ex
ce
ute the query unprepared because LOAD DATA only works with unprepared statements.
// Ex
ec
ute the query unprepared because LOAD DATA only works with unprepared statements.
$result
=
$this
->
queryUnprepared
(
$sqlSyntax
);
// Are there rows modified?
// Let the user know if the import failed / succeeded
return
(
bool
)
$result
;
}
/**
* This method is usefull for importing XML files into a specific table.
* Check out the LOAD XML syntax for your MySQL server.
*
* @author Jonas Barascu
* @param string $importTable The table in which the data will be imported to.
* @param string $importFile The file which contains the .XML data.
* @param string $importSettings An Array defining the import settings as described in the README.md
*
* @return boolean Returns true if the import succeeded, false if it failed.
*/
/**
* This method is useful for importing XML files into a specific table.
* Check out the LOAD XML syntax for your MySQL server.
*
* @author Jonas Barascu
*
* @param string $importTable The table in which the data will be imported to.
* @param string $importFile The file which contains the .XML data.
* @param string $importSettings An Array defining the import settings as described in the README.md
*
* @return boolean Returns true if the import succeeded, false if it failed.
* @throws Exception
*/
public
function
loadXml
(
$importTable
,
$importFile
,
$importSettings
=
null
)
{
// We have to check if the file exists
...
...
@@ -1113,33 +1175,33 @@ class MysqliDb
throw
new
Exception
(
"loadXml: Import file does not exists"
);
return
;
}
// Create default values
$settings
=
Array
(
"linesToIgnore"
=>
0
);
// Check the import settings
// Check the import settings
if
(
gettype
(
$importSettings
)
==
"array"
)
{
$settings
=
array_merge
(
$settings
,
$importSettings
);
}
// Add the prefix to the import table
$table
=
self
::
$prefix
.
$importTable
;
// Add 1 more slash to every slash so maria will interpret it as a path
$importFile
=
str_replace
(
"
\\
"
,
"
\\\\
"
,
$importFile
);
$importFile
=
str_replace
(
"
\\
"
,
"
\\\\
"
,
$importFile
);
// Build SQL Syntax
$sqlSyntax
=
sprintf
(
'LOAD XML INFILE \'%s\' INTO TABLE %s'
,
$sqlSyntax
=
sprintf
(
'LOAD XML INFILE \'%s\' INTO TABLE %s'
,
$importFile
,
$table
);
// FIELDS
if
(
isset
(
$settings
[
"rowTag"
]))
{
$sqlSyntax
.=
sprintf
(
' ROWS IDENTIFIED BY \'%s\''
,
$settings
[
"rowTag"
]);
}
// IGNORE LINES
$sqlSyntax
.=
sprintf
(
' IGNORE %d LINES'
,
$settings
[
"linesToIgnore"
]);
// Exceute the query unprepared because LOAD XML only works with unprepared statements.
$result
=
$this
->
queryUnprepared
(
$sqlSyntax
);
...
...
@@ -1153,12 +1215,12 @@ class MysqliDb
*
* @uses $MySqliDb->orderBy('id', 'desc')->orderBy('name', 'desc', '^[a-z]')->orderBy('name', 'desc');
*
* @param string $orderByField The name of the database field.
* @param string $orderByDirection Order direction.
* @param mixed $customFieldsOrRegExp Array with fieldset for ORDER BY FIELD() ordering or string with regular expresion for ORDER BY REGEXP ordering
*
* @throws Exception
* @param string $orderByField The name of the database field.
* @param string $orderbyDirection
* @param mixed $customFieldsOrRegExp Array with fieldset for ORDER BY FIELD() ordering or string with regular expression for ORDER BY REGEXP ordering
*
* @return MysqliDb
* @throws Exception
*/
public
function
orderBy
(
$orderByField
,
$orderbyDirection
=
"DESC"
,
$customFieldsOrRegExp
=
null
)
{
...
...
@@ -1207,17 +1269,18 @@ class MysqliDb
$this
->
_groupBy
[]
=
$groupByField
;
return
$this
;
}
/**
* This method sets the current table lock method.
*
* @author Jonas Barascu
* @param string $method The table lock method. Can be READ or WRITE.
*
* @throws Exception
* @return MysqliDb
*/
/**
* This method sets the current table lock method.
*
* @author Jonas Barascu
*
* @param string $method The table lock method. Can be READ or WRITE.
*
* @throws Exception
* @return MysqliDb
*/
public
function
setLockMethod
(
$method
)
{
// Switch the uppercase string
...
...
@@ -1234,21 +1297,22 @@ class MysqliDb
}
return
$this
;
}
/**
* Locks a table for R/W action.
*
* @author Jonas Barascu
* @param string $table The table to be locked. Can be a table or a view.
*
* @throws Exception
* @return MysqliDb if succeeeded;
*/
/**
* Locks a table for R/W action.
*
* @author Jonas Barascu
*
* @param string|array $table The table to be locked. Can be a table or a view.
*
* @return bool if succeeded;
* @throws Exception
*/
public
function
lock
(
$table
)
{
// Main Query
$this
->
_query
=
"LOCK TABLES"
;
// Is the table an array?
if
(
gettype
(
$table
)
==
"array"
)
{
// Loop trough it and attach it to the query
...
...
@@ -1264,20 +1328,20 @@ class MysqliDb
else
{
// Build the table prefix
$table
=
self
::
$prefix
.
$table
;
// Build the query
$this
->
_query
=
"LOCK TABLES "
.
$table
.
" "
.
$this
->
_tableLockMethod
;
}
// Ex
ce
ute the query unprepared because LOCK only works with unprepared statements.
// Ex
ec
ute the query unprepared because LOCK only works with unprepared statements.
$result
=
$this
->
queryUnprepared
(
$this
->
_query
);
$errno
=
$this
->
mysqli
()
->
errno
;
// Reset the query
$this
->
reset
();
// Are there rows modified?
if
(
$result
)
{
if
(
$result
)
{
// Return true
// We can't return ourself because if one table gets locked, all other ones get unlocked!
return
true
;
...
...
@@ -1290,20 +1354,21 @@ class MysqliDb
// Return the success value
return
false
;
}
/**
* Unlocks all tables in a database.
* Also commits transactions.
*
* @author Jonas Barascu
* @return MysqliDb
*/
/**
* Unlocks all tables in a database.
* Also commits transactions.
*
* @author Jonas Barascu
* @return MysqliDb
* @throws Exception
*/
public
function
unlock
()
{
// Build the query
$this
->
_query
=
"UNLOCK TABLES"
;
// Ex
ce
ute the query unprepared because UNLOCK and LOCK only works with unprepared statements.
// Ex
ec
ute the query unprepared because UNLOCK and LOCK only works with unprepared statements.
$result
=
$this
->
queryUnprepared
(
$this
->
_query
);
$errno
=
$this
->
mysqli
()
->
errno
;
...
...
@@ -1319,17 +1384,18 @@ class MysqliDb
else
{
throw
new
Exception
(
"Unlocking of tables failed"
,
$errno
);
}
// Return self
return
$this
;
}
/**
* This methods returns the ID of the last inserted item
*
* @return int The last inserted item ID.
* @throws Exception
*/
public
function
getInsertId
()
{
...
...
@@ -1342,6 +1408,7 @@ class MysqliDb
* @param string $str The string to escape.
*
* @return string The escaped string.
* @throws Exception
*/
public
function
escape
(
$str
)
{
...
...
@@ -1355,6 +1422,7 @@ class MysqliDb
* since _mysqli is protected.
*
* @return bool True if connection is up
* @throws Exception
*/
public
function
ping
()
{
...
...
@@ -1424,8 +1492,8 @@ class MysqliDb
* ' $operator ($subquery) ' formats
*
* @param string $operator
* @param mixed $value Variable with values
*
* @param mixed
$value Variable with values
*
* @return string
*/
protected
function
_buildPair
(
$operator
,
$value
)
...
...
@@ -1444,11 +1512,12 @@ class MysqliDb
/**
* Internal function to build and execute INSERT/REPLACE calls
*
* @param string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
* @param string $operation Type of operation (INSERT, REPLACE)
* @param string $tableName
The name of the table.
* @param array
$insertData Data containing information for inserting into the DB.
* @param string $operation
Type of operation (INSERT, REPLACE)
*
* @return bool Boolean indicating whether the insert query was completed succesfully.
* @return bool Boolean indicating whether the insert query was completed successfully.
* @throws Exception
*/
private
function
_buildInsert
(
$tableName
,
$insertData
,
$operation
)
{
...
...
@@ -1485,11 +1554,12 @@ class MysqliDb
* any passed update data, and the desired rows.
* It then builds the SQL query.
*
* @param int|array $numRows Array to define SQL limit in format Array ($offset, $count)
* @param int|array $numRows
Array to define SQL limit in format Array ($offset, $count)
* or only $count
* @param array
$tableData
Should contain an array of data for updating the database.
* @param array
$tableData
Should contain an array of data for updating the database.
*
* @return mysqli_stmt Returns the $stmt object.
* @return mysqli_stmt|bool Returns the $stmt object.
* @throws Exception
*/
protected
function
_buildQuery
(
$numRows
=
null
,
$tableData
=
null
)
{
...
...
@@ -1502,7 +1572,7 @@ class MysqliDb
$this
->
_buildOrderBy
();
$this
->
_buildLimit
(
$numRows
);
$this
->
_buildOnDuplicate
(
$tableData
);
if
(
$this
->
_forUpdate
)
{
$this
->
_query
.=
' FOR UPDATE'
;
}
...
...
@@ -1533,7 +1603,8 @@ class MysqliDb
*
* @param mysqli_stmt $stmt Equal to the prepared statement object.
*
* @return array The results of the SQL fetch.
* @return array|string The results of the SQL fetch.
* @throws Exception
*/
protected
function
_dynamicBindResults
(
mysqli_stmt
$stmt
)
{
...
...
@@ -1639,7 +1710,7 @@ class MysqliDb
/**
* Abstraction method that will build an JOIN part of the query
*
*
* @return void
*/
protected
function
_buildJoinOld
()
...
...
@@ -1665,11 +1736,11 @@ class MysqliDb
/**
* Insert/Update query helper
*
*
* @param array $tableData
* @param array $tableColumns
* @param bool $isInsert INSERT operation flag
*
* @param bool
$isInsert INSERT operation flag
*
* @throws Exception
*/
public
function
_buildDataPairs
(
$tableData
,
$tableColumns
,
$isInsert
)
...
...
@@ -1729,6 +1800,8 @@ class MysqliDb
* Helper function to add variables into the query statement
*
* @param array $tableData Variable with values
*
* @throws Exception
*/
protected
function
_buildOnDuplicate
(
$tableData
)
{
...
...
@@ -1753,8 +1826,10 @@ class MysqliDb
/**
* Abstraction method that will build an INSERT or UPDATE part of the query
*
*
* @param array $tableData
*
* @throws Exception
*/
protected
function
_buildInsertQuery
(
$tableData
)
{
...
...
@@ -1781,9 +1856,9 @@ class MysqliDb
/**
* Abstraction method that will build the part of the WHERE conditions
*
*
* @param string $operator
* @param array $conditions
* @param array
$conditions
*/
protected
function
_buildCondition
(
$operator
,
&
$conditions
)
{
...
...
@@ -1879,9 +1954,9 @@ class MysqliDb
/**
* Abstraction method that will build the LIMIT part of the WHERE statement
*
* @param int|array $numRows Array to define SQL limit in format Array ($offset, $count)
* @param int|array $numRows
Array to define SQL limit in format Array ($offset, $count)
* or only $count
*
*
* @return void
*/
protected
function
_buildLimit
(
$numRows
)
...
...
@@ -1919,7 +1994,7 @@ class MysqliDb
$this
->
autoReconnectCount
++
;
return
$this
->
_prepareQuery
();
}
$error
=
$this
->
mysqli
()
->
error
;
$query
=
$this
->
_query
;
$errno
=
$this
->
mysqli
()
->
errno
;
...
...
@@ -1929,7 +2004,7 @@ class MysqliDb
/**
* Referenced data array is required by mysqli since PHP 5.3+
*
*
* @param array $arr
*
* @return array
...
...
@@ -1951,9 +2026,9 @@ class MysqliDb
/**
* Function to replace ? with variables from bind variable
*
*
* @param string $str
* @param array $vals
* @param array
$vals
*
* @return string
*/
...
...
@@ -1995,6 +2070,7 @@ class MysqliDb
* Method returns mysql error
*
* @return string
* @throws Exception
*/
public
function
getLastError
()
{
...
...
@@ -2006,6 +2082,7 @@ class MysqliDb
/**
* Method returns mysql error code
*
* @return int
*/
public
function
getLastErrno
()
{
...
...
@@ -2032,16 +2109,16 @@ class MysqliDb
$this
->
reset
();
return
$val
;
}
/* Helper functions */
/**
* Method returns generated interval function as a string
*
* @param string $diff interval in the formats:
* "1", "-1d" or "- 1 day" -- For interval - 1 day
* Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
* Default null;
*
"1", "-1d" or "- 1 day" -- For interval - 1 day
*
Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
*
Default null;
* @param string $func Initial date
*
* @return string
...
...
@@ -2080,12 +2157,13 @@ class MysqliDb
* Method returns generated interval function as an insert/update function
*
* @param string $diff interval in the formats:
* "1", "-1d" or "- 1 day" -- For interval - 1 day
* Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
* Default null;
*
"1", "-1d" or "- 1 day" -- For interval - 1 day
*
Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
*
Default null;
* @param string $func Initial date
*
* @return array
* @throws Exception
*/
public
function
now
(
$diff
=
null
,
$func
=
"NOW()"
)
{
...
...
@@ -2094,9 +2172,9 @@ class MysqliDb
/**
* Method generates incremental function call
*
*
* @param int $num increment by int or float. 1 by default
*
*
* @throws Exception
* @return array
*/
...
...
@@ -2109,10 +2187,10 @@ class MysqliDb
}
/**
* Method generates decr
i
mental function call
*
* Method generates decr
e
mental function call
*
* @param int $num increment by int or float. 1 by default
*
*
* @return array
* @throws Exception
*/
...
...
@@ -2126,22 +2204,22 @@ class MysqliDb
/**
* Method generates change boolean function call
*
*
* @param string $col column name. null by default
*
*
* @return array
*/
public
function
not
(
$col
=
null
)
{
return
array
(
"[N]"
=>
(
string
)
$col
);
return
array
(
"[N]"
=>
(
string
)
$col
);
}
/**
* Method generates user defined function call
*
*
* @param string $expr user function body
* @param array $bindParams
*
* @param array
$bindParams
*
* @return array
*/
public
function
func
(
$expr
,
$bindParams
=
null
)
...
...
@@ -2151,9 +2229,9 @@ class MysqliDb
/**
* Method creates new mysqlidb object for a subquery generation
*
*
* @param string $subQueryAlias
*
*
* @return MysqliDb
*/
public
static
function
subQuery
(
$subQueryAlias
=
""
)
...
...
@@ -2178,6 +2256,7 @@ class MysqliDb
*
* @uses mysqli->autocommit(false)
* @uses register_shutdown_function(array($this, "_transaction_shutdown_check"))
* @throws Exception
*/
public
function
startTransaction
()
{
...
...
@@ -2191,6 +2270,7 @@ class MysqliDb
*
* @uses mysqli->commit();
* @uses mysqli->autocommit(true);
* @throws Exception
*/
public
function
commit
()
{
...
...
@@ -2205,6 +2285,7 @@ class MysqliDb
*
* @uses mysqli->rollback();
* @uses mysqli->autocommit(true);
* @throws Exception
*/
public
function
rollback
()
{
...
...
@@ -2219,6 +2300,7 @@ class MysqliDb
* atomic operations sane.
*
* @uses mysqli->rollback();
* @throws Exception
*/
public
function
_transaction_status_check
()
{
...
...
@@ -2229,11 +2311,11 @@ class MysqliDb
}
/**
* Query exection time tracking switch
* Query exec
u
tion time tracking switch
*
* @param bool
$enabled
Enable execution time tracking
* @param bool
$enabled
Enable execution time tracking
* @param string $stripPrefix Prefix to strip from the path in exec log
*
*
* @return MysqliDb
*/
public
function
setTrace
(
$enabled
,
$stripPrefix
=
null
)
...
...
@@ -2266,6 +2348,7 @@ class MysqliDb
* @param array $tables Table name or an Array of table names to check
*
* @return bool True if table exists
* @throws Exception
*/
public
function
tableExists
(
$tables
)
{
...
...
@@ -2286,9 +2369,9 @@ class MysqliDb
/**
* Return result as an associative array with $idField field value used as a record key
*
*
* Array Returns an array($k => $v) if get(.."param1, param2"), array ($k => array ($v, $v)) otherwise
*
*
* @param string $idField field name to use for a mapped element key
*
* @return MysqliDb
...
...
@@ -2300,13 +2383,16 @@ class MysqliDb
}
/**
* Pagination wraper to get()
* Pagination wrap
p
er to get()
*
* @access public
* @param string $table The name of the database table to work with
* @param int $page Page number
*
* @param string $table The name of the database table to work with
* @param int $page Page number
* @param array|string $fields Array or coma separated list of fields to fetch
*
* @return array
* @throws Exception
*/
public
function
paginate
(
$table
,
$page
,
$fields
=
null
)
{
$offset
=
$this
->
pageLimit
*
(
$page
-
1
);
...
...
@@ -2324,6 +2410,9 @@ class MysqliDb
* @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field.
*
* @param string $operator
* @param string $cond
*
* @return $this
*/
public
function
joinWhere
(
$whereJoin
,
$whereProp
,
$whereValue
=
'DBNULL'
,
$operator
=
'='
,
$cond
=
'AND'
)
...
...
@@ -2340,8 +2429,9 @@ class MysqliDb
* @param string $whereJoin The name of the table followed by its prefix.
* @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field.
* @param string $operator
*
* @return
dbWrapper
* @return
$this
*/
public
function
joinOrWhere
(
$whereJoin
,
$whereProp
,
$whereValue
=
'DBNULL'
,
$operator
=
'='
,
$cond
=
'AND'
)
{
...
...
@@ -2380,8 +2470,9 @@ class MysqliDb
/**
* Convert a condition and value into the sql string
* @param String $operator The where constraint operator
* @param String $val The where constraint value
*
* @param String $operator The where constraint operator
* @param String|array $val The where constraint value
*/
private
function
conditionToSql
(
$operator
,
$val
)
{
switch
(
strtolower
(
$operator
))
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment