89     public function name($table)
 
   91         return $this->db->prefix($table);
 
  105     public function addColumn($table, $column, $attributes, $position = null)
 
  109             'position'=>$position,
 
  110             'attributes'=>$attributes
 
  114         if (isset($this->tables[$table])) {
 
  115             $tableDef = &$this->tables[$table];
 
  117             if (isset($tableDef[
'create']) && $tableDef[
'create']) {
 
  120                         array_unshift($tableDef[
'columns'], $columnDef);
 
  125                         array_push($tableDef[
'columns'], $columnDef);
 
  131                         foreach ($tableDef[
'columns'] as $col) {
 
  133                             if (strcasecmp($col[
'name'], $position)==0) {
 
  134                                 array_splice($tableDef[
'columns'], 
$i, 0, array($columnDef));
 
  142                 foreach ($tableDef[
'columns'] as $col) {
 
  143                     if (strcasecmp($col[
'name'], $column)==0) {
 
  157                         $pos=
"AFTER `{$position}`";
 
  159                 $this->queue[]=
"ALTER TABLE `{$tableDef['name']}`" 
  160                     . 
" ADD COLUMN {$column} {$columnDef['attributes']} {$pos} ";
 
  165             $this->lastErrNo = -1;
 
  184         if (isset($this->tables[$table])) {
 
  186                 = 
"ALTER TABLE `{$table}` ADD PRIMARY KEY({$column})";
 
  189             $this->lastErrNo = -1;
 
  207         if (isset($this->tables[$table])) {
 
  211         if (is_array($tableDef)) {
 
  212             $this->tables[$table] = $tableDef;
 
  216             if ($tableDef===
true) {
 
  219                       'name' => $this->db->prefix($table)
 
  220                     , 
'options' => 
'ENGINE=MyISAM');
 
  221                 $tableDef[
'create'] = 
true;
 
  222                 $this->tables[$table] = $tableDef;
 
  224                 $this->queue[]=array(
'createtable'=>$table);
 
  242         if (isset($this->tables[$table])) {
 
  246         if (is_array($tableDef)) {
 
  247             $this->tables[$table] = $tableDef;
 
  266     public function alterColumn($table, $column, $attributes, $newName = 
'', $position = null)
 
  268         if (empty($newName)) {
 
  272         if (isset($this->tables[$table])) {
 
  273             $tableDef = &$this->tables[$table];
 
  275             if (isset($tableDef[
'create']) && $tableDef[
'create']
 
  279                 foreach ($tableDef[
'columns'] as &$col) {
 
  280                     if (strcasecmp($col[
'name'], $column)==0) {
 
  281                         $col[
'name']=$newName;
 
  282                         $col[
'attributes']=$attributes;
 
  299                         $pos=
"AFTER `{$position}`";
 
  301                 $this->queue[]=
"ALTER TABLE `{$tableDef['name']}` " .
 
  302                     "CHANGE COLUMN `{$column}` `{$newName}` {$attributes} {$pos} ";
 
  306             $this->lastErrNo = -1;
 
  324     public function copyTable($table, $newTable, $withData = 
false)
 
  326         if (isset($this->tables[$newTable])) {
 
  330         $copy=$this->
name($newTable);
 
  331         $original=$this->
name($table);
 
  333         if (is_array($tableDef)) {
 
  334             $tableDef[
'name']=$copy;
 
  336                 $this->queue[] = 
"CREATE TABLE `{$copy}` LIKE `{$original}` ;";
 
  338                     = 
"INSERT INTO `{$copy}` SELECT * FROM `{$original}` ;";
 
  340                 $tableDef[
'create'] = 
true;
 
  341                 $this->queue[]=array(
'createtable'=>$newTable);
 
  343             $this->tables[$newTable]=$tableDef;
 
  365         if (isset($this->tables[$table])) {
 
  367             $add = ($unique?
'ADD UNIQUE INDEX':
'ADD INDEX');
 
  369                 = 
"ALTER TABLE `{$table}` {$add} {$name} ({$column})";
 
  372             $this->lastErrNo = -1;
 
  391         if (isset($this->tables[$table])) {
 
  392             $tableDef = &$this->tables[$table];
 
  394                 = 
"ALTER TABLE `{$tableDef['name']}` DROP COLUMN `{$column}`";
 
  397             $this->lastErrNo = -1;
 
  415         if (isset($this->tables[$table])) {
 
  416             $tableDef = &$this->tables[$table];
 
  417             $this->queue[]=
"ALTER TABLE `{$tableDef['name']}` DROP INDEX `{$name}`";
 
  420             $this->lastErrNo = -1;
 
  439         if (isset($this->tables[$table])) {
 
  440             $tableDef = &$this->tables[$table];
 
  442             if (isset($tableDef[
'create']) && $tableDef[
'create']) {
 
  444                 foreach ($tableDef[
'keys'] as $keyname => $key) {
 
  445                     if ($keyname!=
'PRIMARY') {
 
  446                         unset($tableDef[
'keys'][$keyname]);
 
  451                 foreach ($tableDef[
'keys'] as $keyname => $key) {
 
  452                     if ($keyname!=
'PRIMARY') {
 
  453                         $this->queue[] = 
"ALTER TABLE `{$tableDef['name']}`" 
  454                             . 
" DROP INDEX {$keyname}";
 
  460             $this->lastErrNo = -1;
 
  477         if (isset($this->tables[$table])) {
 
  478             $tableDef = &$this->tables[$table];
 
  479             $this->queue[]=
"ALTER TABLE `{$tableDef['name']}` DROP PRIMARY KEY ";
 
  482             $this->lastErrNo = -1;
 
  499         if (isset($this->tables[$table])) {
 
  500             $tableDef = &$this->tables[$table];
 
  501             $this->queue[]=
"DROP TABLE `{$tableDef['name']}` ";
 
  502             unset($this->tables[$table]);
 
  519         if (isset($this->tables[$table])) {
 
  520             $tableDef = &$this->tables[$table];
 
  521             $newTable = $this->
name($newName);
 
  523                 = 
"ALTER TABLE `{$tableDef['name']}` RENAME TO `{$newTable}`";
 
  524             $tableDef[
'name'] = $newTable;
 
  527             $this->lastErrNo = -1;
 
  547         if (isset($this->tables[$table])) {
 
  548             $tableDef = &$this->tables[$table];
 
  549             $this->queue[]=
"ALTER TABLE `{$tableDef['name']}` {$options} ";
 
  553             $this->lastErrNo = -1;
 
  569         $this->tables = array();
 
  570         $this->queue  = array();
 
  583         foreach ($this->queue as &$ddl) {
 
  584             if (is_array($ddl)) {
 
  585                 if (isset($ddl[
'createtable'])) {
 
  591                 $this->lastError = $this->db->error();
 
  592                 $this->lastErrNo = $this->db->errno();
 
  612         if (isset($this->tables[$table])) {
 
  613             $tableDef = &$this->tables[$table];
 
  616                 $where = 
'WHERE '.$criteria;
 
  620             $this->queue[]=
"DELETE FROM `{$tableDef['name']}` {$where}";
 
  623             $this->lastErrNo = -1;
 
  640         if (isset($this->tables[$table])) {
 
  641             $tableDef = &$this->tables[$table];
 
  644             foreach ($tableDef[
'columns'] as $col) {
 
  645                 $comma=empty($colsql)?
'':
', ';
 
  646                 if (isset($columns[$col[
'name']])) {
 
  647                     $colsql .= $comma.$col[
'name'];
 
  648                     $valsql .= $comma.$this->db->quote($columns[$col[
'name']]);
 
  651             $sql = 
"INSERT INTO `{$tableDef['name']}` ({$colsql}) VALUES({$valsql})";
 
  657             $this->lastErrNo = -1;
 
  674         if (isset($this->tables[$table])) {
 
  675             $tableDef = &$this->tables[$table];
 
  678                 $where = 
'WHERE '.$criteria;
 
  683             foreach ($tableDef[
'columns'] as $col) {
 
  684                 $comma=empty($colsql)?
'':
', ';
 
  685                 if (isset($columns[$col[
'name']])) {
 
  686                     $colsql .= $comma . $col[
'name'] . 
' = ' 
  687                         . $this->db->quote($columns[$col[
'name']]);
 
  690             $sql = 
"UPDATE `{$tableDef['name']}` SET {$colsql} {$where}";
 
  696             $this->lastErrNo = -1;
 
  711         if (isset($this->tables[$table])) {
 
  712             $tableDef = &$this->tables[$table];
 
  713             $this->queue[]=
"TRUNCATE TABLE `{$tableDef['name']}`";
 
  716             $this->lastErrNo = -1;
 
  736         if (isset($this->tables[$table])) {
 
  737             $tableDef = &$this->tables[$table];
 
  738             $tablename=($prefixed?$tableDef[
'name']:$table);
 
  739             $sql = 
"CREATE TABLE `{$tablename}` (\n";
 
  740             foreach ($tableDef[
'columns'] as $col) {
 
  741                 $sql .= 
"    {$col['name']}  {$col['attributes']},\n";
 
  744             foreach ($tableDef[
'keys'] as $keyname => $key) {
 
  745                 $comma = empty($keysql)?
'  ':
', ';
 
  746                 if ($keyname==
'PRIMARY') {
 
  747                     $keysql .= 
"  {$comma}PRIMARY KEY ({$key['columns']})\n";
 
  749                     $unique=$key[
'unique']?
'UNIQUE ':
'';
 
  750                     $keysql .= 
"  {$comma}{$unique}KEY {$keyname} " 
  751                         . 
" ({$key['columns']})\n";
 
  755             $sql .= 
") {$tableDef['options']};\n";
 
  760             $this->lastErrNo = -1;
 
  787             $this->lastError = $this->db->error();
 
  788             $this->lastErrNo = $this->db->errno();
 
  805         return $this->db->fetchArray(
$result);
 
  821         $sql  = 
'SELECT TABLE_NAME, ENGINE, CHARACTER_SET_NAME ';
 
  822         $sql .= 
' FROM `INFORMATION_SCHEMA`.`TABLES` t, ';
 
  823         $sql .= 
' `INFORMATION_SCHEMA`.`COLLATIONS` c ';
 
  825         $sql .= ' AND t.TABLE_NAME = \
'' . $this->
name($table) . 
'\' '; 
  826         $sql .= ' AND t.TABLE_COLLATION  = c.COLLATION_NAME 
'; 
  828         $result = $this->execSql($sql); 
  832         $tableSchema = $this->fetch($result); 
  833         if (empty($tableSchema)) { 
  836         $tableDef['name'] =  $tableSchema['TABLE_NAME
']; 
  837         $tableDef['options
'] = 'ENGINE=
' . $tableSchema['ENGINE
'] . ' ' 
  838             . 'DEFAULT CHARSET=
' . $tableSchema['CHARACTER_SET_NAME
']; 
  841         $sql .= ' FROM `INFORMATION_SCHEMA`.`COLUMNS` 
'; 
  843         $sql .= ' AND TABLE_NAME = \
'' . $this->
name($table) . 
'\' '; 
  844         $sql .= ' ORDER BY `ORDINAL_POSITION` 
'; 
  846         $result = $this->execSql($sql); 
  848         while ($column=$this->fetch($result)) { 
  849             $attributes = ' ' . $column['COLUMN_TYPE
'] . ' ' 
  850                 . (($column['IS_NULLABLE
'] == 'NO
') ? ' NOT NULL 
' : '' ) 
  851                 . (($column['COLUMN_DEFAULT
'] === null) ? '' : 
  852                         " DEFAULT '". $column['COLUMN_DEFAULT'] . "' ") 
  856                 'name'=>$column['COLUMN_NAME
'], 
  857                 'position
'=>$column['ORDINAL_POSITION
'], 
  858                 'attributes
'=>$attributes 
  861             $tableDef['columns
'][] = $columnDef; 
  864         $sql  = 'SELECT `INDEX_NAME`, `SEQ_IN_INDEX`, `NON_UNIQUE`, 
'; 
  865         $sql .= ' `COLUMN_NAME`, `SUB_PART` 
'; 
  866         $sql .= ' FROM `INFORMATION_SCHEMA`.`STATISTICS` 
'; 
  868         $sql .= ' AND TABLE_NAME = \
'' . $this->
name($table) . 
'\' '; 
  869         $sql .= ' ORDER BY `INDEX_NAME`, `SEQ_IN_INDEX` 
'; 
  871         $result = $this->execSql($sql); 
  876         while ($key=$this->fetch($result)) { 
  877             if ($lastkey != $key['INDEX_NAME
']) { 
  878                 if (!empty($lastkey)) { 
  879                     $tableDef['keys
'][$lastkey]['columns
'] = $keycols; 
  880                     $tableDef['keys
'][$lastkey]['unique
'] = $keyunique; 
  882                 $lastkey = $key['INDEX_NAME
']; 
  883                 $keycols = $key['COLUMN_NAME
']; 
  884                 if (!empty($key['SUB_PART
'])) { 
  885                     $keycols .= ' (
' . $key['SUB_PART
'] . ')
'; 
  887                 $keyunique = !$key['NON_UNIQUE
']; 
  889                 $keycols .= ', 
' . $key['COLUMN_NAME
']; 
  890                 if (!empty($key['SUB_PART
'])) { 
  891                     $keycols .= ' (
'.$key['SUB_PART
'].')
'; 
  894             //$tableDef['keys
'][$key['INDEX_NAME
']][$key['SEQ_IN_INDEX
']] = $key; 
  896         if (!empty($lastkey)) { 
  897             $tableDef['keys
'][$lastkey]['columns
'] = $keycols; 
  898             $tableDef['keys
'][$lastkey]['unique
'] = $keyunique; 
  913     private function expandQueue() 
  915         foreach ($this->queue as &$ddl) { 
  916             if (is_array($ddl)) { 
  917                 if (isset($ddl['createtable
'])) { 
  918                     $ddl=$this->renderTableCreate($ddl['createtable
'], true); 
  929     public function getLastError() 
  931         return $this->lastError; 
  939     public function getLastErrNo() 
  941         return $this->lastErrNo; 
  949     public function dumpTables() 
  951         return $this->tables; 
  959     public function dumpQueue() 
  961         $this->expandQueue(); 
update($table, $columns, $criteria)
addPrimaryKey($table, $column)
setTableOptions($table, $options)
renameTable($table, $newName)
copyTable($table, $newTable, $withData=false)
addColumn($table, $column, $attributes, $position=null)
const _DB_XMF_TABLE_IS_NOT_DEFINED
alterColumn($table, $column, $attributes, $newName= '', $position=null)
renderTableCreate($table, $prefixed=false)
& execSql($sql, $force=false)
static load($name, $domain= '', $language=null)
queueExecute($force=false)
dropColumn($table, $column)
createIndex($name, $table, $column, $unique=false)