Skip to content

Commit

Permalink
Support method chaining, added .last_id for accessing lastrowid
Browse files Browse the repository at this point in the history
Also shipping as 0.5
  • Loading branch information
Simon Willison committed Aug 6, 2018
1 parent 19e1057 commit 72644b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import io
import os

VERSION = "0.4"
VERSION = "0.5"


def get_long_description():
Expand Down
11 changes: 8 additions & 3 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def create(self, columns, pk=None, foreign_keys=None):
columns = {name: value for (name, value) in columns.items()}
self.db.create_table(self.name, columns, pk=pk, foreign_keys=foreign_keys)
self.exists = True
return self

def create_index(self, columns, index_name=None):
if index_name is None:
Expand All @@ -177,6 +178,7 @@ def create_index(self, columns, index_name=None):
index_name=index_name, table_name=self.name, columns=", ".join(columns)
)
self.db.conn.execute(sql)
return self

def drop(self):
return self.db.conn.execute("DROP TABLE {}".format(self.name))
Expand All @@ -192,9 +194,9 @@ def add_foreign_key(self, column, column_type, other_table, other_column):
other_table=other_table,
other_column=other_column,
)
result = self.db.conn.execute(sql)
self.db.conn.execute(sql)
self.db.conn.commit()
return result
return self

def enable_fts(self, columns):
"Enables FTS on the specified columns"
Expand All @@ -208,6 +210,7 @@ def enable_fts(self, columns):
)
self.db.conn.executescript(sql)
self.populate_fts(columns)
return self

def populate_fts(self, columns):
sql = """
Expand All @@ -217,6 +220,7 @@ def populate_fts(self, columns):
table=self.name, columns=", ".join(columns)
)
self.db.conn.executescript(sql)
return self

def detect_column_types(self, records):
all_column_types = {}
Expand Down Expand Up @@ -294,7 +298,8 @@ def insert_all(
)
result = self.db.conn.execute(sql, values)
self.db.conn.commit()
return result
self.last_id = result.lastrowid
return self

def upsert(self, record, pk=None, foreign_keys=None):
return self.insert(record, pk=pk, foreign_keys=foreign_keys, upsert=True)
Expand Down

0 comments on commit 72644b6

Please sign in to comment.