~desarrollokumbia/kumbia/ActiveRecord

« back to all changes in this revision

Viewing changes to db_pool/adapters/mysql_db.php

  • Committer: Emilio Silveira
  • Date: 2010-07-30 03:22:48 UTC
  • Revision ID: emilio.rst@gmail.com-20100730032248-9n4vnvcist5rjze1
Cambios multiples.

Implementado en ActiveRecord create y update con validaciones y 
disparadores before y after

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
     **/
32
32
    public function describe($table, $schema=null)
33
33
    {
34
 
        if($schema) {
35
 
            $source = "$schema.$table";
36
 
        } else {
37
 
            $source = $table;
38
 
        }
39
 
        
40
 
        $tableMetaData = TableMetaData::getInstance($this->_connection, $schema, $table);
41
 
        if(!$tableMetaData->isLoaded()) {
42
 
            $stmt = $this->pdo()->query("DESCRIBE $source");
43
 
            
44
 
            $metadata = array();
45
 
            foreach($stmt as $row) {
46
 
                // aqui falta el codigo para ajustarlo que funcione con el mysql
 
34
        try {
 
35
            $results = $this->pdo()->query("DESCRIBE $table");
 
36
                        
 
37
            if ($results) {
 
38
                require_once CORE_PATH . 'libs/ActiveRecord/db_pool/metadata.php';
 
39
                $metadata = new Metadata();
 
40
                while ($field = $results->fetchObject()) {
 
41
                    //Nombre del Campo
 
42
                    $attribute = $metadata->attribute($field->Field);
 
43
                    //alias
 
44
                    $attribute->alias =  ucwords(strtr($field->Field,'_-','  '));
 
45
                    
 
46
                    // autoincremental
 
47
                    if ($field->Extra === 'auto_increment') {
 
48
                        $attribute->autoIncrement = TRUE;
 
49
                    }
 
50
                    
 
51
                    // valor por defecto
 
52
                    $attribute->default = $field->Default;
 
53
                    
 
54
                    //puede ser null?
 
55
                    if($field->Null == 'NO'){
 
56
                        $attribute->notNull = FALSE;
 
57
                    }
 
58
 
 
59
                    //tipo de dato y longitud
 
60
                    if(preg_match('/^(\w+)\((\w+)\)$/', $field->Type, $matches)) {
 
61
                        $attribute->type = $matches[1];
 
62
                        $attribute->length = $matches[2];
 
63
                    } else {
 
64
                        $attribute->type = $field->Type;
 
65
                        $attribute->length = NULL;
 
66
                    }
 
67
 
 
68
                    //indices
 
69
                    switch ($field->Key){
 
70
                        case 'PRI':
 
71
                            $metadata->setPK($field->Field);
 
72
                            $attribute->PK = TRUE;
 
73
                            break;
 
74
                        case 'FK':
 
75
                            $metadata->setFK($field->Field);
 
76
                            $attribute->FK = TRUE;
 
77
                            break;
 
78
                        case 'UNI':
 
79
                            $attribute->unique = TRUE;
 
80
                            break;
 
81
                    }
 
82
                }
47
83
            }
48
 
            
49
 
            $tableMetaData->setMetadata($metadata);
 
84
        } catch (PDOException $e) {
 
85
            throw new KumbiaException($e->getMessage());
50
86
        }
51
 
        
52
 
        return $tableMetaData;
 
87
        return $metadata;
53
88
    }
54
89
}