| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package db | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import "context" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Basic wraps basic database functionality. | 
					
						
							|  |  |  | type Basic interface { | 
					
						
							|  |  |  | 	// CreateTable creates a table for the given interface. | 
					
						
							|  |  |  | 	// For implementations that don't use tables, this can just return nil. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	CreateTable(ctx context.Context, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 16:15:25 +02:00
										 |  |  | 	// CreateAllTables creates *all* tables necessary for the running of GoToSocial. | 
					
						
							|  |  |  | 	// Because it uses the 'if not exists' parameter it is safe to run against a GtS that's already been initialized. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	CreateAllTables(ctx context.Context) error | 
					
						
							| 
									
										
										
										
											2021-09-09 16:15:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	// DropTable drops the table for the given interface. | 
					
						
							|  |  |  | 	// For implementations that don't use tables, this can just return nil. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	DropTable(ctx context.Context, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 	// Close should stop and close the database connection cleanly, returning an error if this is not possible. | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	// If the database implementation doesn't need to be stopped, this can just return nil. | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 	Close() error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// IsHealthy should return nil if the database connection is healthy, or an error if not. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	IsHealthy(ctx context.Context) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// GetByID gets one entry by its id. In a database like postgres, this might be the 'id' field of the entry, | 
					
						
							|  |  |  | 	// for other implementations (for example, in-memory) it might just be the key of a map. | 
					
						
							|  |  |  | 	// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice. | 
					
						
							|  |  |  | 	// In case of no entries, a 'no entries' error will be returned | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	GetByID(ctx context.Context, id string, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// GetWhere gets one entry where key = value. This is similar to GetByID but allows the caller to specify the | 
					
						
							|  |  |  | 	// name of the key to select from. | 
					
						
							|  |  |  | 	// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice. | 
					
						
							|  |  |  | 	// In case of no entries, a 'no entries' error will be returned | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	GetWhere(ctx context.Context, where []Where, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// GetAll will try to get all entries of type i. | 
					
						
							|  |  |  | 	// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice. | 
					
						
							|  |  |  | 	// In case of no entries, a 'no entries' error will be returned | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	GetAll(ctx context.Context, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Put simply stores i. It is up to the implementation to figure out how to store it, and using what key. | 
					
						
							|  |  |  | 	// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	Put(ctx context.Context, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 13:50:48 +02:00
										 |  |  | 	// UpdateByID updates values of i based on its id. | 
					
						
							| 
									
										
										
										
											2022-08-15 12:35:05 +02:00
										 |  |  | 	// If any columns are specified, these will be updated exclusively. | 
					
						
							|  |  |  | 	// Otherwise, the whole model will be updated. | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	UpdateByID(ctx context.Context, i interface{}, id string, columns ...string) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// UpdateWhere updates column key of interface i with the given value, where the given parameters apply. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	UpdateWhere(ctx context.Context, where []Where, key string, value interface{}, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// DeleteByID removes i with id id. | 
					
						
							|  |  |  | 	// If i didn't exist anyway, then no error should be returned. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	DeleteByID(ctx context.Context, id string, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// DeleteWhere deletes i where key = value | 
					
						
							|  |  |  | 	// If i didn't exist anyway, then no error should be returned. | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	DeleteWhere(ctx context.Context, where []Where, i interface{}) error | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | } |