/
database.go
executable file
·899 lines (785 loc) · 21.8 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// Copyright 2020-2021 P2PDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlite
import (
dbsql "database/sql"
"os"
"path/filepath"
"strings"
// log "github.com/sirupsen/logrus"
"github.com/Rock-liyi/p2pdb-store/entity"
"github.com/Rock-liyi/p2pdb-store/entity/value_object"
"github.com/Rock-liyi/p2pdb-store/event"
"github.com/Rock-liyi/p2pdb-store/sql"
conf "github.com/Rock-liyi/p2pdb/infrastructure/util/config"
"github.com/dolthub/vitess/go/sqltypes"
debug "github.com/favframework/debug"
//dbParse "github.com/Rock-liyi/p2pdb-store/sql/parse"
_ "github.com/mattn/go-sqlite3"
//"github.com/opentracing/opentracing-go/log"
log "github.com/Rock-liyi/p2pdb/infrastructure/util/log"
)
// Database is an in-sqlite database.
type Database struct {
*BaseDatabase
connection *dbsql.DB
address string
views map[string]string
}
type SqliteDatabase interface {
sql.Database
AddTable(name string, t sql.Table)
}
var _ sql.Database = (*Database)(nil)
var _ sql.TableCreator = (*Database)(nil)
var _ sql.TableDropper = (*Database)(nil)
var _ sql.TableRenamer = (*Database)(nil)
var _ sql.TriggerDatabase = (*Database)(nil)
var _ sql.StoredProcedureDatabase = (*Database)(nil)
var _ sql.ViewDatabase = (*Database)(nil)
// BaseDatabase is an in-sqlite database that can't store views, only for testing the engine
type BaseDatabase struct {
connection *dbsql.DB
address string
name string
tables map[string]sql.Table
triggers []sql.TriggerDefinition
storedProcedures []sql.StoredProcedureDetails
primaryKeyIndexes bool
}
var _ SqliteDatabase = (*Database)(nil)
var _ SqliteDatabase = (*BaseDatabase)(nil)
// NewDatabase creates a new database with the given name.
func NewDatabase(name string) *Database {
database := NewViewlessDatabase(name)
return &Database{
BaseDatabase: database,
connection: database.connection,
address: database.address,
views: make(map[string]string),
}
}
// NewViewlessDatabase creates a new database that doesn't persist views. Used only for testing. Use NewDatabase.
func NewViewlessDatabase(name string) *BaseDatabase {
dataPath := conf.GetDataPath()
// do something here to set environment depending on an environment variable
// or command-line flag
if dataPath != "" {
dataPath = dataPath + "/"
}
debug.Dump(dataPath)
binary, _ := os.Getwd()
root := filepath.Dir(binary)
if root != "" && dataPath == "" {
dataPath = root + "/"
}
debug.Dump(dataPath + name + ".db")
address := dataPath + name + ".db"
db, err := dbsql.Open("sqlite3", address)
if err != nil {
log.Error(err)
}
// rows, err := db.Query("select name from sqlite_master where name !='sqlite_sequence' ")
// debug.Dump("========->1 select name from sqlite_master where name !='sqlite_sequence'")
// if err != nil {
// log.Error(err)
// }
// debug.Dump(rows)
// //panic(err)
//defualt create database
dbSql := "create database " + name
_, err = db.Exec(dbSql)
if err != nil {
//debug.Dump(err)
log.Error(err)
}
//if you close the connection , the db will not work anymore
//defer db.Close()
database := BaseDatabase{
name: name,
tables: map[string]sql.Table{},
connection: db,
address: address,
}
//database.connection = db
return &database
}
// func checkErr(err error) {
// if err != nil {
// panic(err)
// }
// }
// EnablePrimaryKeyIndexes causes every table created in this database to use an index on its primary partitionKeys
func (d *BaseDatabase) EnablePrimaryKeyIndexes() {
d.primaryKeyIndexes = true
}
// Name returns the database name.
func (d *BaseDatabase) Name() string {
return d.name
}
// Connection returns the database connection.
func (d *BaseDatabase) Connection() *dbsql.DB {
return d.connection
}
func (d *BaseDatabase) Address() string {
return d.address
}
// Tables returns all tables in the database.
func (d *BaseDatabase) Tables() map[string]sql.Table {
debug.Dump("========-> Tables method")
//Get all of table , it not include sqlite_sequence table name
// rows, err := d.connection.Query("select name from sqlite_master where name !='sqlite_sequence'")
rows, err := d.connection.Query("select name from sqlite_master where name !='sqlite_sequence' ")
debug.Dump("========-> select name from sqlite_master where name !='sqlite_sequence'")
if err != nil {
log.Error(err)
}
var tableName string
//var Table sql.Table
for rows.Next() {
err := rows.Scan(&tableName)
if err != nil {
log.Error(err)
}
if strings.Contains(tableName, "sqlite_autoindex_") == false {
//debug.Dump(tableName)
d.tables[tableName] = d.getTable(tableName)
//d.tables[tableName] = Table
}
}
//debug.Dump(d.tables)
//it must to be closed or else will stuck
rows.Close()
return d.tables
}
// Tables returns all tables in the database.
func (d *BaseDatabase) InitTables() {
debug.Dump("========-> InitTables method")
//Get all of table , it not include sqlite_sequence table name
// rows, err := d.connection.Query("select name from sqlite_master where name !='sqlite_sequence'")
rows, err := d.connection.Query("select name from sqlite_master where name !='sqlite_sequence' ")
debug.Dump("========-> select name from sqlite_master where name !='sqlite_sequence'")
if err != nil {
log.Error(err)
}
var tableName string
//var Table sql.Table
for rows.Next() {
err := rows.Scan(&tableName)
if err != nil {
log.Error(err)
}
if strings.Contains(tableName, "sqlite_autoindex_") == false {
//debug.Dump(tableName)
var newTable = d.getTable(tableName)
d.AddTable(tableName, newTable)
ctx := sql.NewEmptyContext()
newTable.ApplyEdits(ctx)
}
}
//it must to be closed or else will stuck
rows.Close()
}
func (d *BaseDatabase) ParseColumnStringToSqlType(atype string) (sql.Type, error) {
// debug.Dump("========ParseColumnStringToSqlType")
types := []struct {
columnType string
expectedSqlType sql.Type
}{
{
"tinyint",
sql.Int8,
},
{
"SMALLINT",
sql.Int16,
},
{
"MeDiUmInT",
sql.Int24,
},
{
"INT",
sql.Int32,
},
{
"INT16",
sql.Int16,
},
{
"INT32",
sql.Int32,
},
{
"INT64",
sql.Int64,
},
{
"BIGINT",
sql.Int64,
},
{
"INTEGER",
sql.Int64,
},
{
"TINYINT UNSIGNED",
sql.Uint8,
},
{
"SMALLINT UNSIGNED",
sql.Uint16,
},
{
"MEDIUMINT UNSIGNED",
sql.Uint24,
},
{
"INT UNSIGNED",
sql.Uint32,
},
{
"BIGINT UNSIGNED",
sql.Uint64,
},
{
"BOOLEAN",
sql.Int8,
},
{
"FLOAT",
sql.Float32,
},
{
"DOUBLE",
sql.Float64,
},
{
"REAL",
sql.Float64,
},
{
"DECIMAL",
sql.MustCreateDecimalType(10, 0),
},
{
"DECIMAL(22)",
sql.MustCreateDecimalType(22, 0),
},
{
"DECIMAL(55, 13)",
sql.MustCreateDecimalType(55, 13),
},
{
"DEC(34, 2)",
sql.MustCreateDecimalType(34, 2),
},
{
"FIXED(4, 4)",
sql.MustCreateDecimalType(4, 4),
},
{
"BIT(31)",
sql.MustCreateBitType(31),
},
{
"TINYBLOB",
sql.TinyBlob,
},
{
"BLOB",
sql.Blob,
},
{
"MEDIUMBLOB",
sql.MediumBlob,
},
{
"LONGBLOB",
sql.LongBlob,
},
{
"TINYTEXT",
sql.TinyText,
},
{
"TEXT",
sql.Text,
},
{
"MEDIUMTEXT",
sql.MediumText,
},
{
"LONGTEXT",
sql.LongText,
},
{
"CHAR(5)",
sql.MustCreateStringWithDefaults(sqltypes.Char, 5),
},
{
"VARCHAR",
sql.MustCreateStringWithDefaults(sqltypes.VarChar, 255),
},
{
"VARCHAR(255)",
sql.MustCreateStringWithDefaults(sqltypes.VarChar, 255),
},
{
"VARCHAR(300) COLLATE cp1257_lithuanian_ci",
sql.MustCreateString(sqltypes.VarChar, 300, sql.Collation_cp1257_lithuanian_ci),
},
{
"BINARY(6)",
sql.MustCreateBinary(sqltypes.Binary, 6),
},
{
"VARBINARY(256)",
sql.MustCreateBinary(sqltypes.VarBinary, 256),
},
{
"YEAR",
sql.Year,
},
{
"DATE",
sql.Date,
},
{
"TIME",
sql.Time,
},
{
"TIMESTAMP",
sql.Timestamp,
},
{
"DATETIME",
sql.Datetime,
},
{
"JSON",
sql.Text,
},
}
for _, t := range types {
// t.Run(test.columnType, func(t *testing.T) {
if atype == t.columnType {
return t.expectedSqlType, nil
}
}
debug.Dump("can not find any sql type")
return nil, nil
}
type ParseColumn struct {
Cid int8
Name string
Type sql.Type
Notnull string
Dflt_value string
Pk string
}
// getTable select a Table with the given name in the database .
func (d *BaseDatabase) getTable(name string) *Table {
log.Info("======= getTable method start")
// rowss, err := d.connection.QueryContext(sql.NewEmptyContext(), "PRAGMA table_info('"+name+"')")
// debug.Dump(rowss.Columns())
// if err != nil {
// log.Error(err)
// }
rows, err := d.connection.Query("PRAGMA table_info('" + name + "')")
// rows, err := d.connection.Query("select name from sqlite_master where name !='sqlite_sequence' ")
// rows, err := d.connection.Query("select * from test_table5 ")
// debug.Dump("========-> PRAGMA table_info('" + name + "')")
if err != nil {
log.Error(err)
}
data := make(map[string]string)
for rows.Next() {
var Cid string
var Name string
var Type string
var Notnull string
var Dflt_value string
var Pk string
err := rows.Scan(&Cid, &Name, &Type, &Notnull, &Dflt_value, &Pk)
if err != nil {
log.Error(err)
}
// debug.Dump("===========aType")
// debug.Dump(Name)
// debug.Dump(Type)
if Type == "" || Name == "" {
break
}
data[Name] = Type
}
newSchemaWithoutCol := make(sql.Schema, len(data))
i := 0
for Name, Type := range data {
var sqlType sql.Type
sqlType, err = d.ParseColumnStringToSqlType(Type)
if err != nil {
log.Error(err)
}
newSchemaWithoutCol[i] = &sql.Column{
Name: Name,
Type: sqlType,
Source: name,
}
i++
}
// debug.Dump("========newSchemaWithoutCol is ")
// debug.Dump(newSchemaWithoutCol)
schema := sql.NewPrimaryKeySchema(newSchemaWithoutCol)
log.Info("======= getTable method end")
return NewPartitionedTable(name, schema, 0)
}
func (d *BaseDatabase) GetTableSchema(name string) (sql.PrimaryKeySchema, error) {
debug.Dump("======= getTable method")
// rowss, err := d.connection.QueryContext(sql.NewEmptyContext(), "PRAGMA table_info('"+name+"')")
// debug.Dump(rowss.Columns())
// if err != nil {
// log.Error(err)
// }
rows, err := d.connection.Query("PRAGMA table_info('" + name + "')")
// rows, err := d.connection.Query("select name from sqlite_master where name !='sqlite_sequence' ")
// rows, err := d.connection.Query("select * from test_table5 ")
// debug.Dump("========-> PRAGMA table_info('" + name + "')")
if err != nil {
log.Error(err)
}
data := make(map[string]string)
for rows.Next() {
var Cid string
var Name string
var Type string
var Notnull string
var Dflt_value string
var Pk string
err := rows.Scan(&Cid, &Name, &Type, &Notnull, &Dflt_value, &Pk)
if err != nil {
log.Error(err)
}
debug.Dump("===========aType")
debug.Dump(Name)
debug.Dump(Type)
if Type == "" || Name == "" {
break
}
data[Name] = Type
}
if len(data) == 0 {
return sql.PrimaryKeySchema{}, sql.NewEmptyContext().Err()
}
newSchemaWithoutCol := make(sql.Schema, len(data))
i := 0
for Name, Type := range data {
var sqlType sql.Type
sqlType, err = d.ParseColumnStringToSqlType(Type)
if err != nil {
log.Error(err)
}
newSchemaWithoutCol[i] = &sql.Column{
Name: Name,
Type: sqlType,
Source: name,
}
i++
}
debug.Dump("========newSchemaWithoutCol is ")
debug.Dump(newSchemaWithoutCol)
schema := sql.NewPrimaryKeySchema(newSchemaWithoutCol)
return schema, nil
}
func (d *BaseDatabase) GetTableInsensitive(ctx *sql.Context, tblName string) (sql.Table, bool, error) {
debug.Dump("========-> GetTableInsensitive")
//ctx := newPersistedSqlContext()
// debug.Dump(ctx.Query())
// debug.Dump(ctx.RawStatement())
// //debug.Dump("==========GetCurrentDatabase")
ctx.Session.SetCurrentDatabase(d.Name())
ctx.Session.SetAddress(d.Address())
ctx.Session.SetConnection(d.Connection())
debug.Dump(ctx.Session.GetCurrentDatabase())
debug.Dump(ctx.Session.Address())
//debug.Dump("========-> GetTableInsensitive sql.Row")
debug.Dump("========-> GetTableInsensitive tables1")
debug.Dump(d.tables)
// // d.tables = d.Tables()
tbl, ok := sql.GetTableInsensitive(tblName, d.tables)
if ok == false {
debug.Dump("GetTableInsensitive is false")
// schema, err := d.GetTableSchema(tblName)
// if err == nil {
// table := NewTable(tblName, sql.NewPrimaryKeySchema(schema.Schema))
// // debug.Dump(table)
// d.AddTable(tblName, table)
// debug.Dump("========-> GetTableInsensitive tables2")
// debug.Dump(d.tables)
// }
}
// tbl, ok = sql.GetTableInsensitive(tblName, d.tables)
debug.Dump("========-> GetTableInsensitive end")
return tbl, ok, nil
}
func (d *BaseDatabase) GetTableNames(ctx *sql.Context) ([]string, error) {
tblNames := make([]string, 0, len(d.tables))
for k := range d.tables {
tblNames = append(tblNames, k)
}
return tblNames, nil
}
func (d *BaseDatabase) GetTableNamesBak(ctx *sql.Context) ([]string, error) {
debug.Dump("========-> GetTableNames start")
d.tables = d.Tables()
tblNames := make([]string, 0, len(d.tables))
for k := range d.tables {
tblNames = append(tblNames, k)
}
debug.Dump("========-> GetTableNames end")
return tblNames, nil
}
// HistoryDatabase is a test-only VersionedDatabase implementation. It only supports exact lookups, not AS OF queries
// between two revisions. It's constructed just like its non-versioned sibling, but it can receive updates to particular
// tables via the AddTableAsOf method. Consecutive calls to AddTableAsOf with the same table must install new versions
// of the named table each time, with ascending version identifiers, for this to work.
type HistoryDatabase struct {
*Database
Revisions map[string]map[interface{}]sql.Table
currRevision interface{}
}
var _ sql.VersionedDatabase = (*HistoryDatabase)(nil)
func (db *HistoryDatabase) GetTableInsensitiveAsOf(ctx *sql.Context, tblName string, time interface{}) (sql.Table, bool, error) {
debug.Dump("============GetTableInsensitiveAsOf start")
table, ok := db.Revisions[strings.ToLower(tblName)][time]
if ok {
return table, true, nil
}
// If we have revisions for the named table, but not the named revision, consider it not found.
if _, ok := db.Revisions[strings.ToLower(tblName)]; ok {
return nil, false, sql.ErrTableNotFound.New(tblName)
}
debug.Dump("============GetTableInsensitiveAsOf end")
// Otherwise (this table has no revisions), return it as an unversioned lookup
return db.GetTableInsensitive(ctx, tblName)
}
func (db *HistoryDatabase) GetTableNamesAsOf(ctx *sql.Context, time interface{}) ([]string, error) {
debug.Dump("============GetTableNamesAsOf")
// TODO: this can't make any queries fail (only used for error messages on table lookup failure), but would be nice
// to support better.
return db.GetTableNames(ctx)
}
func NewHistoryDatabase(name string) *HistoryDatabase {
return &HistoryDatabase{
Database: NewDatabase(name),
Revisions: make(map[string]map[interface{}]sql.Table),
}
}
// Adds a table with an asOf revision key. The table given becomes the current version for the name given.
func (db *HistoryDatabase) AddTableAsOf(name string, t sql.Table, asOf interface{}) {
// TODO: this won't handle table names that vary only in case
if _, ok := db.Revisions[strings.ToLower(name)]; !ok {
db.Revisions[strings.ToLower(name)] = make(map[interface{}]sql.Table)
}
db.Revisions[strings.ToLower(name)][asOf] = t
db.tables[name] = t
}
// AddTable adds a new table to the database.
func (d *BaseDatabase) AddTable(name string, t sql.Table) {
debug.Dump("=======AddTable method")
table := NewTable(name, sql.NewPrimaryKeySchema(t.Schema()))
table.createTableToSqlite(table)
d.tables[name] = t
}
// CreateTable creates a table with the given name and schema
func (d *BaseDatabase) CreateTable(ctx *sql.Context, name string, schema sql.PrimaryKeySchema) error {
debug.Dump("=======CreateTable-1 start")
_, ok := d.tables[name]
if ok {
return sql.ErrTableAlreadyExists.New(name)
}
table := NewTable(name, schema)
if d.primaryKeyIndexes {
table.EnablePrimaryKeyIndexes()
}
if table.sqlStatement != "" {
table.createTableToSqlite(table)
}
d.tables[name] = table
//debug.Dump(d.tables)
debug.Dump("=======CreateTable-1 end")
return nil
}
// func (d *BaseDatabase) createTableToSqlite(table *Table) {
// //it create a file table by sqlite
// debug.Dump("=========createTableToSqlite method")
// debug.Dump(table.sqlStatement)
// _, err := d.connection.Exec(table.sqlStatement)
// if err != nil {
// log.Error(err)
// }
// event.PublishSyncEvent(commonEvent.StoreCreateTableEvent, table.sqlStatement)
// debug.Dump("=========createTableToSqlite method end")
// }
// DropTable drops the table with the given name
func (d *BaseDatabase) DropTable(ctx *sql.Context, name string) error {
log.Debug("=========DropTable method start")
d.tables = d.Tables()
_, ok := d.tables[name]
if !ok {
return sql.ErrTableNotFound.New(name)
}
//delete table from in the database
_, err := d.connection.Exec("DROP TABLE " + name)
if err != nil {
//debug.Dump("show error=========")
log.Error(err)
}
var eventData = entity.Data{TableName: name, SQLStatement: "DROP TABLE " + name, DDLActionType: value_object.TABLE, DDLType: value_object.DROP}
event.PublishSyncEvent(value_object.StoreDropTableEvent, eventData)
delete(d.tables, name)
log.Debug("=========DropTable method end")
return nil
}
func (d *BaseDatabase) RenameTable(ctx *sql.Context, oldName, newName string) error {
log.Debug("=========RenameTable method start")
//defulat code
tbl, ok := d.tables[oldName]
if !ok {
// Should be impossible (engine already checks this condition)
return sql.ErrTableNotFound.New(oldName)
}
_, ok = d.tables[newName]
if ok {
return sql.ErrTableAlreadyExists.New(newName)
}
//sqlite code
SQLStatement := "ALTER TABLE " + oldName + " RENAME TO " + newName
_, err := d.connection.Exec(SQLStatement)
if err != nil {
//debug.Dump("show error=========")
log.Error(err)
}
var eventData = entity.Data{TableName: oldName, SQLStatement: SQLStatement, DDLActionType: value_object.TABLE, DDLType: value_object.ALTER_TABLE_RENAME}
event.PublishSyncEvent(value_object.StoreAlterTableRenameEvent, eventData)
tbl.(*Table).name = newName
d.tables[newName] = tbl
delete(d.tables, oldName)
log.Debug("=========RenameTable method end")
return nil
}
func (d *BaseDatabase) GetTriggers(ctx *sql.Context) ([]sql.TriggerDefinition, error) {
var triggers []sql.TriggerDefinition
for _, def := range d.triggers {
triggers = append(triggers, def)
}
return triggers, nil
}
func (d *BaseDatabase) CreateTrigger(ctx *sql.Context, definition sql.TriggerDefinition) error {
d.triggers = append(d.triggers, definition)
return nil
}
func (d *BaseDatabase) DropTrigger(ctx *sql.Context, name string) error {
found := false
for i, trigger := range d.triggers {
if trigger.Name == name {
d.triggers = append(d.triggers[:i], d.triggers[i+1:]...)
found = true
break
}
}
if !found {
return sql.ErrTriggerDoesNotExist.New(name)
}
return nil
}
// GetStoredProcedures implements sql.StoredProcedureDatabase
func (d *BaseDatabase) GetStoredProcedures(ctx *sql.Context) ([]sql.StoredProcedureDetails, error) {
var spds []sql.StoredProcedureDetails
for _, spd := range d.storedProcedures {
spds = append(spds, spd)
}
return spds, nil
}
// SaveStoredProcedure implements sql.StoredProcedureDatabase
func (d *BaseDatabase) SaveStoredProcedure(ctx *sql.Context, spd sql.StoredProcedureDetails) error {
loweredName := strings.ToLower(spd.Name)
for _, existingSpd := range d.storedProcedures {
if strings.ToLower(existingSpd.Name) == loweredName {
return sql.ErrStoredProcedureAlreadyExists.New(spd.Name)
}
}
d.storedProcedures = append(d.storedProcedures, spd)
return nil
}
// DropStoredProcedure implements sql.StoredProcedureDatabase
func (d *BaseDatabase) DropStoredProcedure(ctx *sql.Context, name string) error {
loweredName := strings.ToLower(name)
found := false
for i, spd := range d.storedProcedures {
if strings.ToLower(spd.Name) == loweredName {
d.storedProcedures = append(d.storedProcedures[:i], d.storedProcedures[i+1:]...)
found = true
break
}
}
if !found {
return sql.ErrStoredProcedureDoesNotExist.New(name)
}
return nil
}
func (d *Database) CreateView(ctx *sql.Context, name string, selectStatement string) error {
_, ok := d.views[name]
if ok {
return sql.ErrExistingView.New(name)
}
d.views[name] = selectStatement
return nil
}
func (d *Database) DropView(ctx *sql.Context, name string) error {
_, ok := d.views[name]
if !ok {
return sql.ErrViewDoesNotExist.New(name)
}
delete(d.views, name)
return nil
}
func (d *Database) AllViews(ctx *sql.Context) ([]sql.ViewDefinition, error) {
debug.Dump("====AllViews")
var views []sql.ViewDefinition
for name, def := range d.views {
views = append(views, sql.ViewDefinition{
Name: name,
TextDefinition: def,
})
}
return views, nil
}
func (d *Database) GetView(ctx *sql.Context, viewName string) (string, bool, error) {
viewDef, ok := d.views[viewName]
return viewDef, ok, nil
}
type ReadOnlyDatabase struct {
*HistoryDatabase
}
var _ sql.ReadOnlyDatabase = ReadOnlyDatabase{}
func NewReadOnlyDatabase(name string) ReadOnlyDatabase {
h := NewHistoryDatabase(name)
return ReadOnlyDatabase{h}
}
func (d ReadOnlyDatabase) IsReadOnly() bool {
return true
}