PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dbal/src/drivers/sqlite/lmbSqliteTableInfo.class.php

http://github.com/limb-php-framework/limb
PHP | 129 lines | 89 code | 21 blank | 19 comment | 14 complexity | 6e211ff2e79f8b5da2c1fce565c7c65d MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. lmb_require('limb/dbal/src/drivers/lmbDbTableInfo.class.php');
  10. lmb_require('limb/dbal/src/drivers/lmbDbIndexInfo.class.php');
  11. lmb_require('limb/dbal/src/drivers/sqlite/lmbSqliteColumnInfo.class.php');
  12. lmb_require('limb/dbal/src/drivers/sqlite/lmbSqliteIndexInfo.class.php');
  13. /**
  14. * class lmbSqliteTableInfo.
  15. *
  16. * @package dbal
  17. * @version $Id$
  18. */
  19. class lmbSqliteTableInfo extends lmbDbTableInfo
  20. {
  21. protected $isExisting = false;
  22. protected $isColumnsLoaded = false;
  23. protected $isIndexesLoaded = false;
  24. protected $database;
  25. private $pk = array();
  26. function __construct($database, $name, $isExisting = false)
  27. {
  28. parent::__construct($name);
  29. $this->database = $database;
  30. $this->isExisting = $isExisting;
  31. }
  32. function getDatabase()
  33. {
  34. return $this->database;
  35. }
  36. //Based on code from Creole
  37. function loadColumns()
  38. {
  39. if($this->isExisting && !$this->isColumnsLoaded)
  40. {
  41. $connection = $this->database->getConnection();
  42. $sql = "PRAGMA table_info('" . $this->name . "')";
  43. $queryId = $connection->execute($sql);
  44. while($row = sqlite_fetch_array($queryId, SQLITE_ASSOC))
  45. {
  46. $name = $row['name'];
  47. $fulltype = $row['type'];
  48. $size = null;
  49. $scale = null;
  50. if(preg_match('/^([^\(]+)\(\s*(\d+)\s*,\s*(\d+)\s*\)$/', $fulltype, $matches))
  51. {
  52. $type = $matches[1];
  53. $size = $matches[2];
  54. $scale = $matches[3]; // aka precision
  55. }
  56. elseif(preg_match('/^([^\(]+)\(\s*(\d+)\s*\)$/', $fulltype, $matches))
  57. {
  58. $type = $matches[1];
  59. $size = $matches[2];
  60. }
  61. else
  62. $type = $fulltype;
  63. // If column is primary key and of type INTEGER, it is auto increment
  64. // See: http://sqlite.org/faq.html#q1
  65. $is_auto_increment = ($row['pk'] == 1 && $fulltype == 'INTEGER');
  66. $is_nullable = !$row['notnull'];
  67. $default_val = $row['dflt_value'];
  68. $this->columns[$name] = new lmbSqliteColumnInfo($this, $name, $type, $size, $scale,
  69. $is_nullable, $default_val, $is_auto_increment);
  70. if(($row['pk'] == 1) || (strtolower($type) == 'integer primary key'))
  71. {
  72. //primary key handling...
  73. $this->pk[] = $name;
  74. }
  75. }
  76. $this->isColumnsLoaded = true;
  77. }
  78. }
  79. function loadIndexes()
  80. {
  81. if(!$this->isExisting || $this->isIndexesLoaded)
  82. return;
  83. $this->loadColumns();
  84. $connection_id = $this->database->getConnection()->getConnectionId();
  85. $rs = sqlite_array_query($connection_id, "PRAGMA index_list('$this->name')", SQLITE_ASSOC);
  86. foreach($rs as $item)
  87. {
  88. $index = new lmbSqliteIndexInfo();
  89. $index->table = $this;
  90. $index->name = $item['name'];
  91. list($index_info) = sqlite_array_query($connection_id, "PRAGMA index_info('$index->name')", SQLITE_ASSOC);
  92. $index->column_name = $index_info['name'];
  93. if (1 == $item['unique'])
  94. {
  95. // if column is defined exactly as INTEGER PRIMARY KEY, primary index is not created
  96. // instead it becomes an alias of system ROWID, see http://www.sqlite.org/lang_createtable.html#rowid for more info
  97. $index->type = in_array($index['column_name'], $this->pk)
  98. ? lmbDbIndexInfo::TYPE_PRIMARY : lmbDbIndexInfo::TYPE_UNIQUE;
  99. }
  100. else
  101. {
  102. $index->type = lmbDbIndexInfo::TYPE_COMMON;
  103. }
  104. $this->indexes[$index->name] = $index;
  105. }
  106. $this->isIndexesLoaded = true;
  107. }
  108. }