@@ -2,7 +2,6 @@ package sqlite
22
33import (
44 "database/sql"
5- "encoding/json"
65 "fmt"
76 "log"
87 "os"
@@ -19,12 +18,12 @@ type IDatabase interface {
1918 Close () error
2019 IsInitialized () bool
2120 CreateTable (sqlTables * string )
22- Insert (table Table )
23- Update (table Table , where []Condition )
24- Select (table Table , clauses SelectClauses , joinClauses []JoinClauses )
25- Delete (table Table , clauses SelectClauses )
21+ Insert (tableName Table , rows Rows )
22+ Update (tableName Table , rows Rows , where []Condition )
23+ Select (tableName Table , clauses SelectClauses , joinClauses []JoinClauses )
24+ Delete (tableName Table , clauses SelectClauses )
2625 DeleteDb () error
27- Apply () error
26+ Apply () ( * Queries , error )
2827}
2928
3029// SDatabase implements IDatabase
@@ -124,14 +123,14 @@ func (s *SDatabase) IsInitialized() bool {
124123
125124func (s * SDatabase ) query (
126125 addTo * []Query ,
127- table Table ,
128- buildQueryFunc func (table Table , columnNames , valuesPlaceholder []string ) string ,
126+ rows Rows ,
127+ buildQueryFunc func (rows Rows , columnNames , valuesPlaceholder []string ) string ,
129128) {
130- for _ , row := range table . Rows {
129+ for _ , row := range rows {
131130 columnNames , valuesPlaceholder , columnValues := processRow (row )
132131
133132 // Build the query using the provided function
134- query := buildQueryFunc (table , columnNames , valuesPlaceholder )
133+ query := buildQueryFunc (rows , columnNames , valuesPlaceholder )
135134
136135 // Add the query to the queries list
137136 s .addQuery (addTo , query , columnValues )
@@ -152,14 +151,14 @@ func (s *SDatabase) CreateTable(sqlTables *string) {
152151}
153152
154153// Insert inserts records into the table
155- func (s * SDatabase ) Insert (table Table ) {
154+ func (s * SDatabase ) Insert (tableName Table , rows Rows ) {
156155 s .query (
157156 & s .queries .Insert ,
158- table ,
159- func (table Table , columnNames , valuesPlaceholder []string ) string {
157+ rows ,
158+ func (rows Rows , columnNames , valuesPlaceholder []string ) string {
160159 var b strings.Builder
161160 b .WriteString ("INSERT INTO " )
162- b .WriteString (table . Name )
161+ b .WriteString (string ( tableName ) )
163162 b .WriteString (" (" )
164163 b .WriteString (strings .Join (columnNames , ", " ))
165164 b .WriteString (") VALUES (" )
@@ -171,14 +170,14 @@ func (s *SDatabase) Insert(table Table) {
171170}
172171
173172// Update updates a record in the table
174- func (s * SDatabase ) Update (table Table , where []Condition ) {
173+ func (s * SDatabase ) Update (tableName Table , rows Rows , where []Condition ) {
175174 s .query (
176175 & s .queries .Update ,
177- table ,
178- func (table Table , columnNames , valuesPlaceholder []string ) string {
176+ rows ,
177+ func (rows Rows , columnNames , valuesPlaceholder []string ) string {
179178 var b strings.Builder
180179 b .WriteString ("UPDATE " )
181- b .WriteString (table . Name )
180+ b .WriteString (string ( tableName ) )
182181 b .WriteString (" SET " )
183182
184183 var updates []string
@@ -195,11 +194,11 @@ func (s *SDatabase) Update(table Table, where []Condition) {
195194}
196195
197196// Select retrieves a record from the table
198- func (s * SDatabase ) Select (table Table , clauses SelectClauses , joinClauses []JoinClauses ) {
197+ func (s * SDatabase ) Select (tableName Table , clauses SelectClauses , joinClauses []JoinClauses ) {
199198 // Build the SELECT query
200199 var b strings.Builder
201200 b .WriteString ("SELECT * FROM " )
202- b .WriteString (table . Name )
201+ b .WriteString (string ( tableName ) )
203202
204203 // Build JOIN clauses, if any
205204 if joinClauses != nil {
@@ -229,10 +228,10 @@ func (s *SDatabase) Select(table Table, clauses SelectClauses, joinClauses []Joi
229228}
230229
231230// Delete deletes records from the table
232- func (s * SDatabase ) Delete (table Table , clauses SelectClauses ) {
231+ func (s * SDatabase ) Delete (tableName Table , clauses SelectClauses ) {
233232 var b strings.Builder
234233 b .WriteString ("DELETE FROM " )
235- b .WriteString (table . Name )
234+ b .WriteString (string ( tableName ) )
236235 b .WriteString (buildWhere (clauses .Where ))
237236 b .WriteString (";" )
238237
@@ -262,15 +261,15 @@ func (s *SDatabase) DeleteDb() error {
262261//
263262// Returns:
264263// - 🚨 error:
265- func (s * SDatabase ) Apply () error {
264+ func (s * SDatabase ) Apply () ( * Queries , error ) {
266265 if ! s .IsInitialized () {
267- return fmt .Errorf (ErrorDatabaseNotInitialized )
266+ return nil , fmt .Errorf (ErrorDatabaseNotInitialized )
268267 }
269268
270269 // Begin a transaction
271270 sqlTx , err := db .Begin ()
272271 if err != nil {
273- return fmt .Errorf ("error starting transaction: %w" , err )
272+ return nil , fmt .Errorf ("error starting transaction: %w" , err )
274273 }
275274
276275 /*
@@ -288,9 +287,9 @@ func (s *SDatabase) Apply() error {
288287 if err != nil {
289288 err := sqlTx .Rollback ()
290289 if err != nil {
291- return err
290+ return nil , err
292291 } // Rollback transaction if there's an error
293- return fmt .Errorf ("error creating table: %w" , err )
292+ return nil , fmt .Errorf ("error creating table: %w" , err )
294293 }
295294 }
296295
@@ -307,37 +306,37 @@ func (s *SDatabase) Apply() error {
307306 if err != nil {
308307 err := sqlTx .Rollback ()
309308 if err != nil {
310- return err
309+ return nil , err
311310 } // Rollback if any query fails
312- return err
311+ return nil , err
313312 }
314313 lastInsertId , err := dbResult .LastInsertId ()
315314 if err != nil {
316- return err
315+ return nil , err
317316 }
318317 q .Result = strconv .FormatInt (lastInsertId , 10 )
319318 }
320319 }
321320
322321 // Handle SELECT queries separately
323- for _ , q := range s .queries .Select {
324- q .Result , err = s .querySelect (sqlTx , q .Query , q .Values )
322+ for qi , q := range s .queries .Select {
323+ s . queries . Select [ qi ] .Result , err = s .querySelect (sqlTx , q .Query , q .Values )
325324 if err != nil {
326325 err := sqlTx .Rollback ()
327326 if err != nil {
328- return err
327+ return nil , err
329328 }
330- return err
329+ return nil , err
331330 }
332331 }
333332
334333 // **Commit transaction once after all queries execute**
335334 if err = sqlTx .Commit (); err != nil {
336335 err = sqlTx .Rollback ()
337336 if err != nil {
338- return err
337+ return nil , err
339338 } // Ensure rollback on commit failure
340- return fmt .Errorf ("error committing transaction: %w" , err )
339+ return nil , fmt .Errorf ("error committing transaction: %w" , err )
341340 }
342341
343342 // Optionally, clear the queries after applying
@@ -350,7 +349,7 @@ func (s *SDatabase) Apply() error {
350349 Select: []Query{},
351350 }*/
352351
353- return nil
352+ return s . queries , nil
354353}
355354
356355// exec loops through all the queries and executes them.
@@ -386,7 +385,7 @@ func (s *SDatabase) exec(sqlTx IDatabaseSqlTx, query string, values []any) (sql.
386385// Returns:
387386// -
388387// - 🚨 error:
389- func (s * SDatabase ) querySelect (sqlTx IDatabaseSqlTx , query string , values []any ) (string , error ) {
388+ func (s * SDatabase ) querySelect (sqlTx IDatabaseSqlTx , query string , values []any ) (any , error ) {
390389 rows , err := sqlTx .Query (query , values ... )
391390 if err != nil {
392391 return "" , fmt .Errorf ("error executing SELECT query (%s): %w" , query , err )
@@ -405,13 +404,13 @@ func (s *SDatabase) querySelect(sqlTx IDatabaseSqlTx, query string, values []any
405404 }
406405
407406 // Prepare a slice to store the results
408- var results []map [string ]interface {}
407+ var results []map [string ]any
409408
410409 // Iterate over rows
411410 for rows .Next () {
412- // Create a slice of `interface{} ` to hold each column value
413- values := make ([]interface {} , len (columns ))
414- valuePtrs := make ([]interface {} , len (columns ))
411+ // Create a slice of `any ` to hold each column value
412+ values := make ([]any , len (columns ))
413+ valuePtrs := make ([]any , len (columns ))
415414
416415 // Assign pointers to values slice
417416 for i := range values {
@@ -424,7 +423,7 @@ func (s *SDatabase) querySelect(sqlTx IDatabaseSqlTx, query string, values []any
424423 }
425424
426425 // Convert values to a map
427- rowMap := make (map [string ]interface {} )
426+ rowMap := make (map [string ]any )
428427 for i , colName := range columns {
429428 val := values [i ]
430429
@@ -439,11 +438,13 @@ func (s *SDatabase) querySelect(sqlTx IDatabaseSqlTx, query string, values []any
439438 results = append (results , rowMap )
440439 }
441440
441+ return results , nil
442+
442443 // Convert results to JSON
443- jsonData , err := json .Marshal (results )
444+ /* jsonData, err := json.Marshal(results)
444445 if err != nil {
445446 return "", fmt.Errorf("error converting result to JSON: %w", err)
446447 }
447448
448- return string (jsonData ), nil
449+ return string(jsonData), nil*/
449450}
0 commit comments