|
32 | 32 | "outputs": [],
|
33 | 33 | "source": [
|
34 | 34 | "#| export\n",
|
35 |
| - "from dataclasses import field, make_dataclass, fields, Field, is_dataclass, MISSING\n", |
| 35 | + "from dataclasses import dataclass, field, make_dataclass, fields, Field, is_dataclass, MISSING\n", |
36 | 36 | "from typing import Any,Union,Optional\n",
|
| 37 | + "from inspect import get_annotations\n", |
37 | 38 | "\n",
|
38 | 39 | "from fastcore.utils import *\n",
|
39 | 40 | "from fastcore.xml import highlight\n",
|
40 | 41 | "from fastcore.xtras import hl_md, dataclass_src\n",
|
41 | 42 | "from sqlite_minutils.db import *\n",
|
| 43 | + "import types\n", |
42 | 44 | "\n",
|
43 | 45 | "try: from graphviz import Source\n",
|
44 | 46 | "except ImportError: pass"
|
|
366 | 368 | " return [(k, v|None, field(default=tbl.default_values.get(k,None)))\n",
|
367 | 369 | " for k,v in tbl.columns_dict.items()]\n",
|
368 | 370 | "\n",
|
369 |
| - "@patch\n", |
370 |
| - "def dataclass(self:Table, store=True, suf='')->type:\n", |
| 371 | + "def _dataclass(self:Table, store=True, suf='')->type:\n", |
371 | 372 | " \"Create a `dataclass` with the types and defaults of this table\"\n",
|
372 | 373 | " res = make_dataclass(self.name.title()+suf, _get_flds(self))\n",
|
373 | 374 | " if store: self.cls = res\n",
|
374 |
| - " return res" |
| 375 | + " return res\n", |
| 376 | + "\n", |
| 377 | + "Table.dataclass = _dataclass" |
375 | 378 | ]
|
376 | 379 | },
|
377 | 380 | {
|
|
734 | 737 | "dv.AccaDaccaAlbums()"
|
735 | 738 | ]
|
736 | 739 | },
|
| 740 | + { |
| 741 | + "cell_type": "code", |
| 742 | + "execution_count": null, |
| 743 | + "metadata": {}, |
| 744 | + "outputs": [], |
| 745 | + "source": [ |
| 746 | + "#| exports\n", |
| 747 | + "@patch\n", |
| 748 | + "def create(\n", |
| 749 | + " self: Database,\n", |
| 750 | + " cls=None, # Dataclass to create table from\n", |
| 751 | + " name=None, # Name of table to create\n", |
| 752 | + " pk='id', # Column(s) to use as a primary key\n", |
| 753 | + " foreign_keys=None, # Foreign key definitions\n", |
| 754 | + " defaults=None, # Database table defaults\n", |
| 755 | + " column_order=None, # Which columns should come first\n", |
| 756 | + " not_null=None, # Columns that should be created as ``NOT NULL``\n", |
| 757 | + " hash_id=None, # Column to be used as a primary key using hash\n", |
| 758 | + " hash_id_columns=None, # Columns used when calculating hash\n", |
| 759 | + " extracts=None, # Columns to be extracted during inserts\n", |
| 760 | + " if_not_exists=False, # Use `CREATE TABLE IF NOT EXISTS`\n", |
| 761 | + " replace=False, # Drop and replace table if it already exists\n", |
| 762 | + " ignore=True, # Silently do nothing if table already exists\n", |
| 763 | + " transform=False, # If table exists transform it to fit schema\n", |
| 764 | + " strict=False, # Apply STRICT mode to table\n", |
| 765 | + "):\n", |
| 766 | + " \"Create table from `cls`, default name to snake-case version of class name\"\n", |
| 767 | + " mk_dataclass(cls)\n", |
| 768 | + " if name is None: name = camel2snake(cls.__name__)\n", |
| 769 | + " typs = {o.name: o.type for o in fields(cls)}\n", |
| 770 | + " res = self.create_table(\n", |
| 771 | + " name, typs, defaults=defaults,\n", |
| 772 | + " pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null,\n", |
| 773 | + " hash_id=hash_id, hash_id_columns=hash_id_columns, extracts=extracts, transform=transform,\n", |
| 774 | + " if_not_exists=if_not_exists, replace=replace, ignore=ignore, strict=strict)\n", |
| 775 | + " res.cls = cls\n", |
| 776 | + " return res" |
| 777 | + ] |
| 778 | + }, |
| 779 | + { |
| 780 | + "cell_type": "markdown", |
| 781 | + "metadata": {}, |
| 782 | + "source": [ |
| 783 | + "The class you pass to `create` is converted to a dataclass where any fields missing a default are defaulted to `None`." |
| 784 | + ] |
| 785 | + }, |
| 786 | + { |
| 787 | + "cell_type": "code", |
| 788 | + "execution_count": null, |
| 789 | + "metadata": {}, |
| 790 | + "outputs": [ |
| 791 | + { |
| 792 | + "data": { |
| 793 | + "text/plain": [ |
| 794 | + "Cat(id=1, name=None, age=None, city='Unknown')" |
| 795 | + ] |
| 796 | + }, |
| 797 | + "execution_count": null, |
| 798 | + "metadata": {}, |
| 799 | + "output_type": "execute_result" |
| 800 | + } |
| 801 | + ], |
| 802 | + "source": [ |
| 803 | + "class Cat: id: int; name: str; age: int; city: str = \"Unknown\"\n", |
| 804 | + "cats = db.create(Cat)\n", |
| 805 | + "Cat(1)" |
| 806 | + ] |
| 807 | + }, |
| 808 | + { |
| 809 | + "cell_type": "code", |
| 810 | + "execution_count": null, |
| 811 | + "metadata": {}, |
| 812 | + "outputs": [ |
| 813 | + { |
| 814 | + "name": "stdout", |
| 815 | + "output_type": "stream", |
| 816 | + "text": [ |
| 817 | + "CREATE TABLE [cat] (\n", |
| 818 | + " [id] INTEGER PRIMARY KEY,\n", |
| 819 | + " [name] TEXT,\n", |
| 820 | + " [age] INTEGER,\n", |
| 821 | + " [city] TEXT\n", |
| 822 | + ")\n" |
| 823 | + ] |
| 824 | + } |
| 825 | + ], |
| 826 | + "source": [ |
| 827 | + "print(cats.schema)" |
| 828 | + ] |
| 829 | + }, |
| 830 | + { |
| 831 | + "cell_type": "code", |
| 832 | + "execution_count": null, |
| 833 | + "metadata": {}, |
| 834 | + "outputs": [], |
| 835 | + "source": [ |
| 836 | + "db.t.cat.drop()" |
| 837 | + ] |
| 838 | + }, |
737 | 839 | {
|
738 | 840 | "cell_type": "markdown",
|
739 | 841 | "metadata": {},
|
|
0 commit comments