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();
26 $statement = $this->_db
->prepare(sprintf($queryString,
27 $this->_maps
[$entityName]['table']
30 $statement->execute();
31 $rows = $statement->fetchAll();
35 foreach($rows as $row)
37 $className = $this->_maps
[$entityName]['class']; //the entity to instantiate and return
38 $constructors = AbstractPopulationHelper
::getConstrutorArray($this->_maps
, $entityName, $row, $this->_db
);
40 if(count($constructors) == 0)
42 $class = new $className;
44 $r = new ReflectionClass($className);
45 $class = $r->newInstanceArgs($constructors);
48 $class->setId($row['id']);
49 $entities[$row['id']] = $class;
55 public function save(IDivineEntity
$entity)
57 $queries = AbstractPopulationHelper
::generateUpdateSaveQuery($this->_maps
, $entity, $entity->getId(), $this->_db
);
61 foreach($queries as $index => $query)
63 $this_table = $query['table'];
64 $this_columns = $query['columns'];
67 for($i = $index+
1; $i<count($queries); $i++
)
70 $queries[$i]['table'] == $this_table &&
71 !array_key_exists($i, $mergeMap) &&
72 !isset($query['id'])) //only merge create queries, updates are fine to run multiple times
74 //XXX: This whole biz is tricky. Basically the problem is that when creating a new simfile,
75 //the datamapper spews out a bunch of create queries. When parsing a simfile for example, there can
76 //be huge redundency - it may produce 5 queries that all create the same step artist, for example.
77 //We attempt to flatten equivalent queries. Originally I was basing it purely on the table name or something,
78 //but that is not enough. In the case of steps, it ends up mergin all the steps together, so we need to
79 //check if the arrays are equal as well, which is what this does.
80 if($this_columns === $queries[$i]['columns'])
82 //need to keep track of what we merged as future queries might reference the old ids.
83 $mergeMap[$i] = $index;
86 //XXX: Another thing that might happen is we have to create queries running on the same table, but with unique columns.
87 //In this case, we can take the columns of one and put it into the other. Otherwise we create two records when we really
88 //should have only one. An example of this is when a user is created, a query to add the country to users_meta is run,
89 //and then _another_ to add firstname, lastname and user_id. It should really all be done in one query.
91 //Make sure both queries are for the same table, and the both relate back to the main query
92 if($this_table == $queries[$i]['table'] && in_array('%MAIN_QUERY_ID%', $this_columns) && in_array('%MAIN_QUERY_ID%', $queries[$i]['columns']))
94 $this_column_names = array_keys($this_columns);
95 $other_column_names = array_keys($queries[$i]['columns']);
97 foreach($this_column_names as $column_name)
99 if($this_columns[$column_name] != '%MAIN_QUERY_ID%' && in_array($column_name, $other_column_names))
107 $this_columns = array_merge($this_columns, $queries[$i]['columns']);
108 $mergeMap[$i] = $index;
114 if(!array_key_exists($index, $mergeMap)) {
115 $prepared = isset($query['prepared']) ?
$query['prepared'] : null
;
116 $id = isset($query['id']) ?
$query['id'] : null
;
118 $flattened[$index] = array(
119 'columns' => $this_columns,
120 'table' => $this_table,
121 'prepared' => $prepared,
129 foreach($flattened as $index => $info)
131 if(isset($info['id']))
133 $query = $info['prepared'];
134 $query = substr($query, 0, -2);
135 $query .= sprintf(' WHERE id=%u', $info['id']);
137 $query = sprintf('INSERT INTO %s (%s) VALUES (%s)',
139 implode(', ', array_keys($info['columns'])),
140 implode(', ', $info['columns']));
143 $queries[$index] = $query;
146 // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE)
149 foreach($queries as $index => $query)
152 if (preg_match_all('/'.preg_quote('%').'(.*?)'.preg_quote('%').'/s', $query, $matches)) {
153 foreach($matches[1] as $index_ref)
155 if($index_ref != 'MAIN_QUERY_ID')
157 $index_id = str_replace('INDEX_REF_', '', $index_ref);
158 $query = str_replace('%INDEX_REF_' . $index_id . '%', $idMap['INDEX_REF_' . $index_id], $query);
167 $statement = $this->_db
->prepare($query);
168 $statement->execute();
169 //$refIndex = $index+1; This was being used as the index for idMap below. I have nfi why I was adding 1.
170 $idMap['INDEX_REF_' . $index] = $this->_db
->lastInsertId();
172 foreach($mergeMap as $oldIndex => $mergedIndex) {
173 if($mergedIndex == $index) {
174 $idMap['INDEX_REF_' . $oldIndex] = $idMap['INDEX_REF_' . $index];
178 unset($queries[$index]);
180 //update query so that other references are resolved.
181 $queries[$index] = $query;
185 //at this point we have queries left that depend on the main query id
186 foreach($queries as $query)
188 $query = str_replace('%MAIN_QUERY_ID%', end($idMap), $query);
189 $statement = $this->_db
->prepare($query);
190 $statement->execute();
194 if(!$entity->getId()) $entity->setId(end($idMap));
200 public function remove(IDivineEntity
$entity) {