extend compatibility for SQLite3
This commit is contained in:
@ -21,8 +21,9 @@ class Creator {
|
|||||||
|
|
||||||
public function interact() {
|
public function interact() {
|
||||||
$name = $this->readline('New model name: ');
|
$name = $this->readline('New model name: ');
|
||||||
$table = $this->readline('Table name: ');
|
$table = strtolower($name);
|
||||||
$entity = $this->readline('Entity name: ');
|
$table = $this->readline('Table name ['.$table.']: ', $table);
|
||||||
|
$entity = $this->readline('Entity name ['.$table.']: ', $table);
|
||||||
$namespace = $this->readline('Namespace [App\Models]: ', 'App\Models');
|
$namespace = $this->readline('Namespace [App\Models]: ', 'App\Models');
|
||||||
if (strlen($entity) <= 0) {
|
if (strlen($entity) <= 0) {
|
||||||
$entity = $table;
|
$entity = $table;
|
||||||
|
@ -154,6 +154,11 @@ class DBmodel
|
|||||||
return $where;
|
return $where;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getDBtype()
|
||||||
|
{
|
||||||
|
return $this->dbh->getAttribute(\PDO::ATTR_DRIVER_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
public function existTable($table_id)
|
public function existTable($table_id)
|
||||||
{
|
{
|
||||||
$query = sprintf(
|
$query = sprintf(
|
||||||
@ -269,11 +274,14 @@ class DBmodel
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (count($primary_key_names) > 1) return true;
|
if (count($primary_key_names) > 1) return true;
|
||||||
|
$last_id = $this->getLastInsertID();
|
||||||
|
/*
|
||||||
if (method_exists($this->dbh, 'getLastInsertID')) {
|
if (method_exists($this->dbh, 'getLastInsertID')) {
|
||||||
$last_id = $this->getLastInsertID();
|
$last_id = $this->getLastInsertID();
|
||||||
} else {
|
} else {
|
||||||
$last_id = $this->getOne('SELECT last_insert_id()');
|
$last_id = $this->getOne('SELECT last_insert_id()');
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
return $last_id;
|
return $last_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,12 +312,14 @@ class DBmodel
|
|||||||
$query = sprintf(
|
$query = sprintf(
|
||||||
'UPDATE %s'
|
'UPDATE %s'
|
||||||
. ' SET %s'
|
. ' SET %s'
|
||||||
. ' WHERE %s'
|
. ' WHERE %s',
|
||||||
. ' LIMIT 1',
|
|
||||||
$table,
|
$table,
|
||||||
$set,
|
$set,
|
||||||
$this->buildWherePrimaryKey($table_id, $primary_key)
|
$this->buildWherePrimaryKey($table_id, $primary_key)
|
||||||
);
|
);
|
||||||
|
if ($this->getDBtype() == 'mysql') {
|
||||||
|
$query .= ' LIMIT 1';
|
||||||
|
}
|
||||||
$this->_debug('[DBmodel][record][UPDATE]: ' . $query);
|
$this->_debug('[DBmodel][record][UPDATE]: ' . $query);
|
||||||
$ret = $this->query($query);
|
$ret = $this->query($query);
|
||||||
if ($ret === false) {
|
if ($ret === false) {
|
||||||
@ -331,11 +341,13 @@ class DBmodel
|
|||||||
}
|
}
|
||||||
$query = sprintf(
|
$query = sprintf(
|
||||||
'DELETE FROM %s'
|
'DELETE FROM %s'
|
||||||
. ' WHERE %s'
|
. ' WHERE %s',
|
||||||
. ' LIMIT 1',
|
|
||||||
$table,
|
$table,
|
||||||
$this->buildWherePrimaryKey($table_id, $primary_key)
|
$this->buildWherePrimaryKey($table_id, $primary_key)
|
||||||
);
|
);
|
||||||
|
if ($this->getDBtype() == 'mysql') {
|
||||||
|
$query .= ' LIMIT 1';
|
||||||
|
}
|
||||||
$this->_debug('[DBmodel][record][DELETE]: ' . $query);
|
$this->_debug('[DBmodel][record][DELETE]: ' . $query);
|
||||||
$ret = $this->query($query);
|
$ret = $this->query($query);
|
||||||
if ($ret === false) {
|
if ($ret === false) {
|
||||||
@ -548,6 +560,15 @@ class DBmodel
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getTableColumns($table_name) {
|
public function getTableColumns($table_name) {
|
||||||
|
$db_type = $this->getDBtype();
|
||||||
|
switch ($db_type) {
|
||||||
|
case 'mysql': return $this->getTableColumnsMYSQL($table_name);
|
||||||
|
case 'sqlite': return $this->getTableColumnsSQLITE($table_name);
|
||||||
|
default: new \Exception('Unknown DB type: ' . $db_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTableColumnsMYSQL($table_name) {
|
||||||
$desc = $this->getAll(sprintf('DESC %s', $table_name));
|
$desc = $this->getAll(sprintf('DESC %s', $table_name));
|
||||||
$desc = $this->arrayKeysToLowerCase($desc);
|
$desc = $this->arrayKeysToLowerCase($desc);
|
||||||
$pks = array();
|
$pks = array();
|
||||||
@ -567,6 +588,26 @@ class DBmodel
|
|||||||
return array('pks' => $pks, 'columns' => $columns, 'types' => $types);
|
return array('pks' => $pks, 'columns' => $columns, 'types' => $types);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getTableColumnsSQLITE($table_name) {
|
||||||
|
$desc = $this->getAll(sprintf('PRAGMA table_info(%s)', $table_name));
|
||||||
|
$desc = $this->arrayKeysToLowerCase($desc);
|
||||||
|
$pks = array();
|
||||||
|
$columns = array();
|
||||||
|
foreach ($desc as $d) {
|
||||||
|
$colname = $d['name'];
|
||||||
|
$types[$colname] = $d['type'];
|
||||||
|
if ($d['pk'] == 1) {
|
||||||
|
$pks[] = $colname;
|
||||||
|
if (strtoupper($d['type']) != 'INTEGER') {
|
||||||
|
$columns[] = $colname;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$columns[] = $colname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return array('pks' => $pks, 'columns' => $columns, 'types' => $types);
|
||||||
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------
|
/* ----------------------------------------------------
|
||||||
* HELPER METHODS
|
* HELPER METHODS
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user