Extract SQL Statements To Gateways
Summary of what happened: Of the long list below, there are only StatsModel and XhrMethods left. The former will probably be solved by !2176 (merged). The latter might still take a while.
We followed through the book's recommendations about adding SQL gateway classes, and implemented a PDO-based database connection and a unit test in https://gitlab.com/foodsharing-dev/foodsharing/commits/extract-database-gateways.
It basically:
- reimplements all the database access from the
*Modelclasses in*Gatewayclasses - uses PDO instead of mysqli
- use DI-style (pass in dependencies into constructor)
- supports unit testing
A few thoughts on how that went:
- it would be a lot of work to reimplement everything
- would probably want to fix the inconsistent return values (e.g. result on success, or boolean on failure)
So, we'd want to consider a bit more of the wider impact before proceeding.
A few options on how we could proceed:
- call the Gateway class from the existing Model class so the Control/Xhr classes don't need to change
- do more minimal change (as Model classes are already more-or-less the same concept as the gateways)
- remove all PREFIX values (not actually used) (all removed by Peter)
- use mysqli prepared statements (can only do by-index I think, not named)
- just switch model classes to use PDO and prepared statements with named parameters (keep everything else as similar as possible)
- go forward with deeper changes to the things that call the Model classes currently (would need more investigation and thought)
Use in model classes
- ActivityModel.php !1434 (merged)
- APIModel.php
- ApplicationModel.php
- BasketModel.php (partially)
- BellModel.php (partially)
- BlogModel.php
- BuddyModel.php
- BusinessCardModel.php
- ConsoleModel.php
- ContentModel.php (partially)
- DashboardModel.php
- Model.php (partially)
- ManualDb.php
- src/Lib/Db/Db.php (contains only legacy mysqli code, can be removed once the migration to PDO is complete)
- EmailTemplateAdminModel.php
- EventModel.php (partially)
- FairTeilerModel.php
- FoodsaverModel.php WIP in !1178 (merged)
- GeoCleanModel.php
- IndexModel.php
- InfoModel.php
- LoginModel.php
- LookupModel.php
- MailboxModel.php
- MailsModel.php
- MaintenanceModel.php !1371 (closed) !1394 (merged)
- Subtask: !1180 (merged) (broken bell deletion)
- Subtask: #648 (closed) & #232 (closed) (incomplete/broken email notification for empty slots) migrated via. !1394 (merged)
- Subtask: !1274 (merged) Remove stats and maintenance methods by seperate MR to be able to work on it if it is ever needed again later. See wiki entry.
- !1289 (merged) Make MaintenanceModel easier to read for future refactoring
- MessageModel.php gets deleted by: !887 (merged)
- NewAreaModel.php
- PassportGeneratorModel.php
- ProfileModel.php
- QuizModel.php
- RegionModel.php (-> RegionGateway & ForumGateway)
- ReportModel.php
- SearchModel.php
- SettingsModel.php (done: !1163 (merged) & !1173 (closed))
- StatisticsModel.php
- StatsModel.php (maybe just leave the fetchweight and -count functions untouched as this topic is very controversial) #622 (closed)
- Subtask: !1274 (merged) Remove stats and maintenance methods by seperate MR to be able to work on it if it is ever needed again later. See wiki.
- StatsmanModel.php
- StoreModel.php => See #1029 (closed) for current progress
- TeamModel.php
- WallPostModel.php
- WorkGroupModel.php
Use in other classes
Search for use Foodsharing\Lib\Db\Db; or Db $model, to see these db imports (currently 26x).
- LoginControl.php WIP in !1192 (merged)
- MailboxControl.php WIP in !1192 (merged)
- FoodSharePointControl.php WIP in !1192 (merged)
- ForumService.php in !1228 (merged)
- RegionXhr.php in !1228 (merged)
- MailboxXhr.php in !1228 (merged)
- XhrMethods.php See: #798 (closed)
- some in !1247 (merged)
- BasketXhr.php (is it even used?) in !1247 (merged)
- Session.php (1 method which might already exist as gateway or exists in one of the open merge requests) in !1247 (merged)
- Control.php (We could use DI to only import DB where it is still used instead of importing it in every controller)
- Index.php (1 method but needs testing)
- api.php (!1719 (merged))
- inc.php
- vMap.php (can be removed?)
- MessageService.php see: !887 (diffs) which might be possible to be cherry picked early on MaintenanceModel
Edited by Fridtjof