MCPcopy
hub / github.com/gogf/gf / createTable

Function createTable

contrib/drivers/oracle/oracle_z_unit_init_test.go:107–163  ·  view source on GitHub ↗
(table ...string)

Source from the content-addressed store, hash-verified

105}
106
107func createTable(table ...string) (name string) {
108 if len(table) > 0 {
109 name = table[0]
110 } else {
111 name = fmt.Sprintf("user_%d", gtime.Timestamp())
112 }
113
114 dropTable(name)
115
116 // Step 1: Create table
117 createTableSQL := fmt.Sprintf(`
118 CREATE TABLE %s (
119 ID NUMBER(10) NOT NULL,
120 PASSPORT VARCHAR(45) NOT NULL,
121 PASSWORD CHAR(32) NOT NULL,
122 NICKNAME VARCHAR(45) NOT NULL,
123 CREATE_TIME VARCHAR(45),
124 SALARY NUMBER(18,2),
125 PRIMARY KEY (ID)
126 )`, name)
127
128 if _, err := db.Exec(ctx, createTableSQL); err != nil {
129 gtest.Fatal(err)
130 }
131
132 // Step 2: Create sequence
133 createSeqSQL := fmt.Sprintf(`
134 CREATE SEQUENCE %s_ID_SEQ
135 START WITH 1
136 INCREMENT BY 1
137 MINVALUE 1
138 MAXVALUE 9999999999
139 NOCYCLE
140 NOCACHE`, name)
141
142 if _, err := db.Exec(ctx, createSeqSQL); err != nil {
143 gtest.Fatal(err)
144 }
145
146 // Step 3: Create trigger - only set ID from sequence when it's NULL
147 createTriggerSQL := fmt.Sprintf(`
148CREATE OR REPLACE TRIGGER %s_ID_TRG
149BEFORE INSERT ON %s
150FOR EACH ROW
151BEGIN
152 IF :NEW.ID IS NULL THEN
153 :NEW.ID := %s_ID_SEQ.NEXTVAL;
154 END IF;
155END;`, name, name, name)
156
157 if _, err := db.Exec(ctx, createTriggerSQL); err != nil {
158 gtest.Fatal(err)
159 }
160
161 // db.Schema("test")
162 return
163}
164

Callers 15

createInitTableFunction · 0.70
Test_TablesFunction · 0.70
Test_Table_FieldsFunction · 0.70
Test_Do_InsertFunction · 0.70
Test_DB_InsertFunction · 0.70
Test_DB_BatchInsertFunction · 0.70
Test_Model_InsertFunction · 0.70
Test_Model_Insert_RawFunction · 0.70
Test_Model_Insert_TimeFunction · 0.70
Test_Model_BatchFunction · 0.70

Calls 4

TimestampFunction · 0.92
FatalFunction · 0.92
dropTableFunction · 0.70
ExecMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…