3 namespace DataAccess\DataMapper
;
5 use Domain\Entities\IDivineEntity
;
6 use DataAccess\IDatabaseFactory
;
7 use DataAccess\DataMapper\IDataMapper
;
8 use DataAccess\Queries\IQueryBuilder
;
9 use DataAccess\DataMapper\Helpers\AbstractPopulationHelper
;
12 class DataMapper
implements IDataMapper
17 public function __construct($maps, IDatabaseFactory
$databaseFactory)
19 $this->_db
= $databaseFactory->createInstance();
20 $this->_maps
= include $maps;
23 public function map($entityName, IQueryBuilder
$queryBuilder)
25 $queryString = $queryBuilder->buildQuery();
27 $statement = $this->_db
->prepare(sprintf($queryString,
28 $this->_maps
[$entityName]['table']
31 $statement->execute();
32 $rows = $statement->fetchAll();
36 foreach($rows as $row)
38 $className = $this->_maps
[$entityName]['class']; //the entity to instantiate and return
39 $constructors = AbstractPopulationHelper
::getConstrutorArray($this->_maps
, $entityName, $row, $this->_db
);
41 if(count($constructors) == 0)
43 $class = new $className;
45 $r = new ReflectionClass($className);
46 $class = $r->newInstanceArgs($constructors);
49 $class->setId($row['id']);
50 $entities[$row['id']] = $class;
56 public function save(IDivineEntity
$entity)
58 $queries = AbstractPopulationHelper
::generateUpdateSaveQuery($this->_maps
, $entity, $entity->getId(), $this->_db
);
61 echo 'pre flattened: <br />';
67 $flattened_tables = array();
68 foreach($queries as $index => $query)
70 $this_table = $query['table'];
71 $this_columns = $query['columns'];
73 for($i = $index+
1; $i<count($queries); $i++
)
75 if($queries[$i]['table'] == $this_table && !in_array($queries[$i]['table'], $flattened_tables) && !isset($query['id'])) //only merge create queries, updates are fine to run multiple times
77 //XXX: This whole biz is tricky. Basically the problem is that when creating a new simfile,
78 //the datamapper spews out a bunch of create queries. When parsing a simfile for example, there can
79 //be huge redundency - it may produce 5 queries that all create the same step artist, for example.
80 //We attempt to flatten equivalent queries. Originally I was basing it purely on the table name or something,
81 //but that is not enough. In the case of steps, it ends up mergin all the steps together, so we need to
82 //check if the arrays are equal as well, which is what this does.
83 if($this_columns === $queries[$i]['columns'])
85 $this_columns = array_merge($this_columns, $queries[$i]['columns']);
86 //need to keep track of what we merged as future queries might reference the old ids.
87 $mergeMap[$i] = $index;
89 //we need to add these unmerged ones here as further down we record that anything to
90 //do with this table has been sorted out.
91 // $prepared = isset($queries[$i]['prepared']) ? $queries[$i]['prepared'] : null;
92 // $id = isset($queries[$i]['id']) ? $queries[$i]['id'] : null;
93 // $flattened[] = array('columns' => $queries[$i]['columns'], 'table' => $queries[$i]['table'], 'prepared' => $prepared, 'id' => $id);
99 if(!in_array($this_table, $flattened_tables))
101 if($flatten) $flattened_tables[] = $this_table;
102 $prepared = isset($query['prepared']) ?
$query['prepared'] : null
;
103 $id = isset($query['id']) ?
$query['id'] : null
;
104 $flattened[] = array('columns' => $this_columns, 'table' => $this_table, 'prepared' => $prepared, 'id' => $id);
108 echo 'flattened: <br />';
115 foreach($flattened as $info)
117 if(isset($info['id']))
119 $query = $info['prepared'];
120 $query = substr($query, 0, -2);
121 $query .= sprintf(' WHERE id=%u', $info['id']);
123 $query = sprintf('INSERT INTO %s (%s) VALUES (%s)',
125 implode(', ', array_keys($info['columns'])),
126 implode(', ', $info['columns']));
132 // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE)
135 foreach($queries as $index => $query)
138 if (preg_match_all('/'.preg_quote('%').'(.*?)'.preg_quote('%').'/s', $query, $matches)) {
139 foreach($matches[1] as $index_ref)
141 if($index_ref != 'MAIN_QUERY_ID')
143 $index_id = str_replace('INDEX_REF_', '', $index_ref);
144 $query = str_replace('%INDEX_REF_' . $index_id . '%', $idMap['INDEX_REF_' . $index_id], $query);
153 $statement = $this->_db
->prepare($query);
154 $statement->execute();
155 //$refIndex = $index+1; This was being used as the index for idMap below. I have nfi why I was adding 1.
156 $idMap['INDEX_REF_' . $index] = $this->_db
->lastInsertId();
158 foreach($mergeMap as $oldIndex => $mergedIndex) {
159 if($mergedIndex == $index) {
160 $idMap['INDEX_REF_' . $oldIndex] = $idMap['INDEX_REF_' . $index];
164 unset($queries[$index]);
166 //update query so that other references are resolved.
167 $queries[$index] = $query;
171 //at this point we have queries left that depend on the main query id
172 foreach($queries as $query)
174 $query = str_replace('%MAIN_QUERY_ID%', end($idMap), $query);
175 $statement = $this->_db
->prepare($query);
176 $statement->execute();
180 if(!$entity->getId()) $entity->setId(end($idMap));
186 public function remove(IDivineEntity
$entity) {