Skip to content

Commit

Permalink
refs #1753 - add - DBModel 2.5.0
Browse files Browse the repository at this point in the history
 - Added some ontology-based aliases
 - Added getStructure method to read DBModel-compatible current structure from working table.
  • Loading branch information
inureyes committed May 4, 2015
1 parent 0056381 commit f7781f1
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 14 deletions.
58 changes: 47 additions & 11 deletions framework/data/Cubrid/Adapter.php
Expand Up @@ -90,11 +90,11 @@ public static function query($query, $compatibility = true) {
$query = str_replace('UNIX_TIMESTAMP()', Timestamp::getUNIXtime(), $query); // compatibility issue.
if (stripos($query, "ORDER BY") !== false) {
$origPagingInst = array(
'/(ASC|DESC) LIMIT ([0-9]+) OFFSET 0/si',
'/(ASC|DESC) LIMIT ([0-9]+) OFFSET ([0-9]+)/si',
'/(ASC|DESC) LIMIT (\d+) OFFSET 0/si',
'/(ASC|DESC) LIMIT (\d+) OFFSET (\d+)/si',
'/(ASC|DESC) LIMIT 1(^[0-9])/si',
'/(ASC|DESC) LIMIT ([0-9]+)/si',
'/RAND\(\) LIMIT ([0-9]+)/si'
'/(ASC|DESC) LIMIT (\d+)/si',
'/RAND\(\) LIMIT (\d+)/si'
);
$descPagingInst = array(
'$1 FOR ORDERBY_NUM() BETWEEN 1 AND $2',
Expand All @@ -106,11 +106,11 @@ public static function query($query, $compatibility = true) {
} else {
if (stripos($query, "GROUP BY") !== false) {
$origPagingInst = array(
'/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT ([0-9]+) OFFSET 0/si',
'/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT ([0-9]+) OFFSET ([0-9]+)/si',
'/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT (\d+) OFFSET 0/si',
'/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT (\d+) OFFSET ([0-9]+)/si',
'/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT 1(^[0-9])/si',
'/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT ([0-9]+)/si',
'/GROUP BY(.*)(ORDER BY)(.*)RAND\(\) LIMIT ([0-9]+)/si'
'/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT (\d+)/si',
'/GROUP BY(.*)(ORDER BY)(.*)RAND\(\) LIMIT (\d+)/si'
);
$descPagingInst = array(
'GROUP BY $1 HAVING GROUPBY_NUM() = $5 $2 $3 $4',
Expand All @@ -121,10 +121,10 @@ public static function query($query, $compatibility = true) {
);
} else {
$origPagingInst = array(
'/WHERE(.*)LIMIT ([0-9]+) OFFSET 0/si',
'/WHERE(.*)LIMIT ([0-9]+) OFFSET ([0-9]+)/si',
'/WHERE(.*)LIMIT (\d+) OFFSET 0/si',
'/WHERE(.*)LIMIT (\d+) OFFSET ([0-9]+)/si',
'/WHERE(.*)LIMIT 1(^[0-9])/si',
'/WHERE(.*)LIMIT ([0-9]+)/si',
'/WHERE(.*)LIMIT (\d+)/si',
'/SUM\((size|value)\)/si'
);
$descPagingInst = array(
Expand Down Expand Up @@ -446,6 +446,42 @@ public static function fieldType($abstractType) {
}
}

public static function structure($tableName) {
$result = self::queryAll("DESCRIBE ".$tableName);
$structure = array();
foreach ($result as $r) {
$structure[$r['Field']] = array();
preg_match('/(.*)\((\d+)\)/si',$r['Type'], $match);
switch (count($match)) {
case 2:
$type = array_search($match[1],self::$typeTable);
break;
case 3:
$type = array_search($match[1],self::$typeTable);
$structure[$r['Field']]['length'] = $match[2];
break;
}
$structure[$r['Field']]['type'] = $type;
if ($r['Null'] == 'NO') {
$structure[$r['Field']]['isNull'] = false;
} else {
$structure[$r['Field']]['isNull'] = true;
}
if ($r['Key'] == 'PRI') {
if (!isset($this->option['primary'])) {
$this->option['primary'] = array();
}
array_push($this->option['primary'], $r['Field']);
} elseif ($r['Key'] == 'MUL') {
$structure[$r['Field']]['index'] = true;
}
if ($r['Default'] != 'NULL') {
$structure[$r['Field']]['default'] = $r['Default'];
}
}
return $structure;
}

static $typeTable = array(
"integer" => "integer",
"int" => "integer",
Expand Down
18 changes: 17 additions & 1 deletion framework/data/DBModel.php
Expand Up @@ -36,11 +36,12 @@ function doesExistTable($tablename) {

/* DBModel : lightweight database abstraction implementation for codeball / Textcube.
2.4.1.20150212
2.5.0.20150504
Needlworks / Tatter Network Foundation. (http://www.needlworks.org)
*/

/*
2.5.0 (2015.5.4) : Added - aliases for ontology approaches.
2.4.2 (2015.2.19) : Added - Affirmation / Negation
2.4.1 (2015.2.12) : Added - setProjection accepts comma-separated input string.
2.4.0 (2015.2.10) : Added - store / restore method to keep track of nested queries.
Expand Down Expand Up @@ -666,6 +667,21 @@ public function discard() {
return false;
}

/* POD-oriented ontology approaches (basics) */
public function define() {
return $this->create();
}

public function purge(){
return $this->delete();
}

public function getStructure() {
$this->structure = POD::structure();
return $this->structure;
}

/* Private methods */
private function _getTableName($table = null) {
if (is_null($table)) {
$table = $this->table;
Expand Down
36 changes: 36 additions & 0 deletions framework/data/MySQL/Adapter.php
Expand Up @@ -384,6 +384,42 @@ public static function fieldType($abstractType) {
}
}

public static function structure($tableName) {
$result = self::queryAll("DESCRIBE ".$tableName);
$structure = array();
foreach ($result as $r) {
$structure[$r['Field']] = array();
preg_match('/(.*)\((\d+)\)/si',$r['Type'], $match);
switch (count($match)) {
case 2:
$type = array_search($match[1],self::$typeTable);
break;
case 3:
$type = array_search($match[1],self::$typeTable);
$structure[$r['Field']]['length'] = $match[2];
break;
}
$structure[$r['Field']]['type'] = $type;
if ($r['Null'] == 'NO') {
$structure[$r['Field']]['isNull'] = false;
} else {
$structure[$r['Field']]['isNull'] = true;
}
if ($r['Key'] == 'PRI') {
if (!isset($this->option['primary'])) {
$this->option['primary'] = array();
}
array_push($this->option['primary'], $r['Field']);
} elseif ($r['Key'] == 'MUL') {
$structure[$r['Field']]['index'] = true;
}
if ($r['Default'] != 'NULL') {
$structure[$r['Field']]['default'] = $r['Default'];
}
}
return $structure;
}

public static $typeTable = array(
"integer" => "int",
"int" => "int",
Expand Down
36 changes: 36 additions & 0 deletions framework/data/MySQLi/Adapter.php
Expand Up @@ -374,6 +374,42 @@ public static function fieldType($abstractType) {
}
}

public static function structure($tableName) {
$result = self::queryAll("DESCRIBE ".$tableName);
$structure = array();
foreach ($result as $r) {
$structure[$r['Field']] = array();
preg_match('/(.*)\((\d+)\)/si',$r['Type'], $match);
switch (count($match)) {
case 2:
$type = array_search($match[1],self::$typeTable);
break;
case 3:
$type = array_search($match[1],self::$typeTable);
$structure[$r['Field']]['length'] = $match[2];
break;
}
$structure[$r['Field']]['type'] = $type;
if ($r['Null'] == 'NO') {
$structure[$r['Field']]['isNull'] = false;
} else {
$structure[$r['Field']]['isNull'] = true;
}
if ($r['Key'] == 'PRI') {
if (!isset($this->option['primary'])) {
$this->option['primary'] = array();
}
array_push($this->option['primary'], $r['Field']);
} elseif ($r['Key'] == 'MUL') {
$structure[$r['Field']]['index'] = true;
}
if ($r['Default'] != 'NULL') {
$structure[$r['Field']]['default'] = $r['Default'];
}
}
return $structure;
}

private static $typeTable = array(
"integer" => "int",
"int" => "int",
Expand Down
36 changes: 36 additions & 0 deletions framework/data/PostgreSQL/Adapter.php
Expand Up @@ -419,6 +419,42 @@ public static function fieldType($abstractType) {
}
}

public static function structure($tableName) {
$result = self::queryAll("DESCRIBE ".$tableName);
$structure = array();
foreach ($result as $r) {
$structure[$r['Field']] = array();
preg_match('/(.*)\((\d+)\)/si',$r['Type'], $match);
switch (count($match)) {
case 2:
$type = array_search($match[1],self::$typeTable);
break;
case 3:
$type = array_search($match[1],self::$typeTable);
$structure[$r['Field']]['length'] = $match[2];
break;
}
$structure[$r['Field']]['type'] = $type;
if ($r['Null'] == 'NO') {
$structure[$r['Field']]['isNull'] = false;
} else {
$structure[$r['Field']]['isNull'] = true;
}
if ($r['Key'] == 'PRI') {
if (!isset($this->option['primary'])) {
$this->option['primary'] = array();
}
array_push($this->option['primary'], $r['Field']);
} elseif ($r['Key'] == 'MUL') {
$structure[$r['Field']]['index'] = true;
}
if ($r['Default'] != 'NULL') {
$structure[$r['Field']]['default'] = $r['Default'];
}
}
return $structure;
}

static $typeTable = array(
"integer" => "integer",
"int" => "integer",
Expand Down
38 changes: 37 additions & 1 deletion framework/data/SQLite3/Adapter.php
Expand Up @@ -346,7 +346,43 @@ public static function fieldType($abstractType) {
if(isset($typeTable[$abstractType])) return $typeTable[$abstractType];
}

static $typeTable = array(
public static function structure($tableName) {
$result = self::queryAll("DESCRIBE ".$tableName);
$structure = array();
foreach ($result as $r) {
$structure[$r['Field']] = array();
preg_match('/(.*)\((\d+)\)/si',$r['Type'], $match);
switch (count($match)) {
case 2:
$type = array_search($match[1],self::$typeTable);
break;
case 3:
$type = array_search($match[1],self::$typeTable);
$structure[$r['Field']]['length'] = $match[2];
break;
}
$structure[$r['Field']]['type'] = $type;
if ($r['Null'] == 'NO') {
$structure[$r['Field']]['isNull'] = false;
} else {
$structure[$r['Field']]['isNull'] = true;
}
if ($r['Key'] == 'PRI') {
if (!isset($this->option['primary'])) {
$this->option['primary'] = array();
}
array_push($this->option['primary'], $r['Field']);
} elseif ($r['Key'] == 'MUL') {
$structure[$r['Field']]['index'] = true;
}
if ($r['Default'] != 'NULL') {
$structure[$r['Field']]['default'] = $r['Default'];
}
}
return $structure;
}

static $typeTable = array(
"integer" => "int",
"int" => "int",
"float" => "float",
Expand Down
2 changes: 1 addition & 1 deletion framework/utils/Browser.php
Expand Up @@ -46,7 +46,7 @@ public function getBrowserName() {
$this->browserName = 'Webkit';
} else if (strpos($_SERVER['HTTP_USER_AGENT'],'IEMobile')) {
$this->browserName = 'IEMobile';
} else if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) {
} else if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') || strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')) {
$this->browserName = 'IE';
} else if (strpos($_SERVER['HTTP_USER_AGENT'],'Opera Mini')) {
$this->browserName = 'OperaMini';
Expand Down

0 comments on commit f7781f1

Please sign in to comment.