github.com/jdgcs/sqlite3@v1.12.1-0.20210908114423-bc5f96e4dd51/tpch/driver/drivers/sqlite3.go (about)

     1  // Copyright 2032 The Sqlite Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package drivers
     6  
     7  import (
     8  	"database/sql"
     9  	"path/filepath"
    10  
    11  	_ "github.com/mattn/go-sqlite3"
    12  	"modernc.org/sqlite/tpch/driver"
    13  )
    14  
    15  const (
    16  	aQ1 = `select
    17  			l_returnflag,
    18  			l_linestatus,
    19  			sum(l_quantity)/100. as sum_qty,
    20  			sum(l_extendedprice)/100. as sum_base_price,
    21  			sum(l_extendedprice*(100-l_discount))/10000. as sum_disc_price,
    22  			sum(l_extendedprice*(100-l_discount)*(100+l_tax))/1000000. as sum_charge,
    23  			avg(l_quantity)/100 as avg_qty,
    24  			avg(l_extendedprice)/100 as avg_price,
    25  			avg(l_discount)/100 as avg_disc,
    26  			count(*) as count_order
    27  		from
    28  			lineitem
    29  		where
    30  			l_shipdate <= date('1998-12-01', printf('-%d day', ?1))
    31  		group by
    32  			l_returnflag,
    33  			l_linestatus
    34  		order by
    35  			l_returnflag,
    36  			l_linestatus;
    37  `
    38  	aQ2 = `select
    39  			s_acctbal,
    40  			s_name,
    41  			n_name,
    42  			p_partkey,
    43  			p_mfgr,
    44  			s_address,
    45  			s_phone,
    46  			s_comment
    47  		from
    48  			part,
    49  			supplier,
    50  			partsupp,
    51  			nation,
    52  			region
    53  		where
    54  			p_partkey == ps_partkey
    55  			and s_suppkey == ps_suppkey
    56  			and p_size == ?1
    57  			and p_type like '%' || ?2
    58  			and s_nationkey == n_nationkey
    59  			and n_regionkey == r_regionkey
    60  			and r_name == ?3
    61  			and ps_supplycost == (
    62  				select
    63  					min(ps_supplycost)
    64  				from
    65  					partsupp, supplier,
    66  					nation, region
    67  				where
    68  					p_partkey == ps_partkey
    69  					and s_suppkey == ps_suppkey
    70  					and s_nationkey == n_nationkey
    71  					and n_regionkey == r_regionkey
    72  					and r_name == ?3
    73  			)
    74  		order by
    75  			s_acctbal desc,
    76  			n_name,
    77  			s_name,
    78  			p_partkey
    79  		limit 100;
    80  `
    81  )
    82  
    83  func init() {
    84  	driver.Register(newSQLite3())
    85  }
    86  
    87  var _ driver.SUT = (*sqlite3)(nil)
    88  
    89  type sqlite3 struct {
    90  	db *sql.DB
    91  	wd string
    92  }
    93  
    94  func newSQLite3() *sqlite3 {
    95  	return &sqlite3{}
    96  }
    97  
    98  func (b *sqlite3) Name() string            { return "sqlite3" }
    99  func (b *sqlite3) SetWD(path string) error { b.wd = path; return nil }
   100  
   101  func (b *sqlite3) OpenDB() (*sql.DB, error) {
   102  	pth := filepath.Join(b.wd, "sqlite3.db")
   103  	db, err := sql.Open(b.Name(), pth)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	b.db = db
   109  	return db, nil
   110  }
   111  
   112  func (b *sqlite3) OpenMem() (driver.SUT, *sql.DB, error) {
   113  	db, err := sql.Open(b.Name(), "file::memory:")
   114  	if err != nil {
   115  		return nil, nil, err
   116  	}
   117  
   118  	return &sqlite3{db: db}, db, nil
   119  }
   120  
   121  func (b *sqlite3) CreateTables() error {
   122  	tx, err := b.db.Begin()
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	if _, err = tx.Exec(`
   128  		create table part (
   129  			p_partkey      int, -- SF*200,000 are populated
   130  			p_name         string,
   131  			p_mfgr         string,
   132  			p_brand        string,
   133  			p_type         string,
   134  			p_size         int,
   135  			p_container    string,
   136  			p_retail_price int,
   137  			p_comment      string
   138  		);
   139  
   140  		create table supplier (
   141  			s_suppkey   int, -- SF*10,000 are populated
   142  			s_name      string,
   143  			s_address   string,
   144  			s_nationkey int, -- Foreign Key to N_NATIONKEY
   145  			s_phone     string,
   146  			s_acctbal    int,
   147  			s_comment   string
   148  		);
   149  
   150  		create table partsupp (
   151  			ps_partkey    int, -- Foreign Key to P_PARTKEY
   152  			ps_suppkey    int, -- Foreign Key to S_SUPPKEY
   153  			ps_availqty   int,
   154  			ps_supplycost int,
   155  			ps_comment    string
   156  		);
   157  
   158  		create table customer (
   159  			c_custkey    int, -- SF*150,000 are populated
   160  			c_name       string,
   161  			c_address    string,
   162  			c_nationkey  int, -- Foreign Key to N_NATIONKEY
   163  			c_phone      string,
   164  			c_acctbal    int,
   165  			c_mktsegment string,
   166  			c_commnet    string
   167  		);
   168  
   169  		create table orders (
   170  			o_orderkey     int, -- SF*1,500,000 are sparsely populated
   171  			o_custkey       int, -- Foreign Key to C_CUSTKEY
   172  			o_orderstatus   string,
   173  			o_totalprice    int,
   174  			o_orderdate     time,
   175  			o_orderpriority string,
   176  			o_clerk         string,
   177  			o_shippriority  int,
   178  			o_comment       string
   179  		);
   180  
   181  		create table lineitem (
   182  			l_orderkey      int, -- Foreign Key to O_ORDERKEY
   183  			l_partkey       int, -- Foreign key to P_PARTKEY, first part of the compound Foreign Key to (PS_PARTKEY, PS_SUPPKEY) with L_SUPPKEY
   184  			l_suppkey       int, -- Foreign key to S_SUPPKEY, second part of the compound Foreign Key to (PS_PARTKEY, PS_SUPPKEY) with L_PARTKEY
   185  			l_linenumber    int,
   186  			l_quantity      int,
   187  			l_extendedprice int,
   188  			l_discount      int,
   189  			l_tax           int,
   190  			l_returnflag    string,
   191  			l_linestatus    string,
   192  			l_shipdate      time,
   193  			l_commitdate    time,
   194  			l_receiptdate   time,
   195  			l_shipinstruct  string,
   196  			l_shipmode      string,
   197  			l_comment       string
   198  		);
   199  
   200  		create table nation (
   201  			n_nationkey int, -- 25 nations are populated
   202  			n_name      string,
   203  			n_regionkey int, -- Foreign Key to R_REGIONKEY
   204  			n_comment   string
   205  		);
   206  
   207  		create table region (
   208  			r_regionkey int, -- 5 regions are populated
   209  			r_name      string,
   210  			r_comment   string
   211  		);
   212  
   213  		`); err != nil {
   214  		return err
   215  	}
   216  
   217  	return tx.Commit()
   218  }
   219  
   220  func (b *sqlite3) InsertSupplier() string {
   221  	return "insert into supplier values (?1, ?2, ?3, ?4, ?5, ?6, ?7)"
   222  }
   223  
   224  func (b *sqlite3) InsertPart() string {
   225  	return "insert into part values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)"
   226  }
   227  
   228  func (b *sqlite3) InsertPartSupp() string {
   229  	return "insert into partsupp values (?1, ?2, ?3, ?4, ?5)"
   230  }
   231  
   232  func (b *sqlite3) InsertCustomer() string {
   233  	return "insert into customer values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)"
   234  }
   235  
   236  func (b *sqlite3) InsertOrders() string {
   237  	return "insert into orders values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)"
   238  }
   239  
   240  func (b *sqlite3) InsertLineItem() string {
   241  	return "insert into lineitem values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16)"
   242  }
   243  
   244  func (b *sqlite3) InsertNation() string {
   245  	return "insert into nation values (?1, ?2, ?3, ?4)"
   246  }
   247  
   248  func (b *sqlite3) InsertRegion() string {
   249  	return "insert into region values (?1, ?2, ?3)"
   250  }
   251  
   252  func (b *sqlite3) QProperty() string {
   253  	return "select * from _property"
   254  }
   255  
   256  func (b *sqlite3) Q1() string {
   257  	return aQ1
   258  }
   259  
   260  func (b *sqlite3) Q2() string {
   261  	return aQ2
   262  }