|
577 | 577 | "|sqlite3.dbapi2.OperationalError|apsw.SQLError|When an error is due to flawed SQL statements|\n", |
578 | 578 | "|sqlite3.ProgrammingError|apsw.ConnectionClosedError|Caused by an improperly closed database file|\n" |
579 | 579 | ] |
| 580 | + }, |
| 581 | + { |
| 582 | + "cell_type": "markdown", |
| 583 | + "metadata": {}, |
| 584 | + "source": [ |
| 585 | + "## Handling of default values" |
| 586 | + ] |
| 587 | + }, |
| 588 | + { |
| 589 | + "cell_type": "markdown", |
| 590 | + "metadata": {}, |
| 591 | + "source": [ |
| 592 | + "Default values are handled as expected, including expression-based default values:" |
| 593 | + ] |
| 594 | + }, |
| 595 | + { |
| 596 | + "cell_type": "code", |
| 597 | + "execution_count": null, |
| 598 | + "metadata": {}, |
| 599 | + "outputs": [ |
| 600 | + { |
| 601 | + "data": { |
| 602 | + "text/plain": [ |
| 603 | + "<apsw.Cursor>" |
| 604 | + ] |
| 605 | + }, |
| 606 | + "execution_count": null, |
| 607 | + "metadata": {}, |
| 608 | + "output_type": "execute_result" |
| 609 | + } |
| 610 | + ], |
| 611 | + "source": [ |
| 612 | + "db.execute(\"\"\"\n", |
| 613 | + "DROP TABLE IF EXISTS migrations;\n", |
| 614 | + "CREATE TABLE IF NOT EXISTS migrations (\n", |
| 615 | + " id INTEGER PRIMARY KEY,\n", |
| 616 | + " name TEXT DEFAULT 'foo',\n", |
| 617 | + " cexpr TEXT DEFAULT ('abra' || 'cadabra'),\n", |
| 618 | + " rand INTEGER DEFAULT (random()),\n", |
| 619 | + " unix_epoch FLOAT DEFAULT (unixepoch('subsec')),\n", |
| 620 | + " json_array JSON DEFAULT (json_array(1,2,3,4)),\n", |
| 621 | + " inserted_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL\n", |
| 622 | + ");\n", |
| 623 | + "\"\"\")" |
| 624 | + ] |
| 625 | + }, |
| 626 | + { |
| 627 | + "cell_type": "code", |
| 628 | + "execution_count": null, |
| 629 | + "metadata": {}, |
| 630 | + "outputs": [ |
| 631 | + { |
| 632 | + "data": { |
| 633 | + "text/plain": [ |
| 634 | + "{'name': 'foo',\n", |
| 635 | + " 'cexpr': SQLExpr: 'abra' || 'cadabra',\n", |
| 636 | + " 'rand': SQLExpr: random(),\n", |
| 637 | + " 'unix_epoch': SQLExpr: unixepoch('subsec'),\n", |
| 638 | + " 'json_array': SQLExpr: json_array(1,2,3,4),\n", |
| 639 | + " 'inserted_at': SQLExpr: CURRENT_TIMESTAMP}" |
| 640 | + ] |
| 641 | + }, |
| 642 | + "execution_count": null, |
| 643 | + "metadata": {}, |
| 644 | + "output_type": "execute_result" |
| 645 | + } |
| 646 | + ], |
| 647 | + "source": [ |
| 648 | + "migrations = Table(db, 'migrations')\n", |
| 649 | + "migrations.default_values" |
| 650 | + ] |
| 651 | + }, |
| 652 | + { |
| 653 | + "cell_type": "code", |
| 654 | + "execution_count": null, |
| 655 | + "metadata": {}, |
| 656 | + "outputs": [], |
| 657 | + "source": [ |
| 658 | + "assert all([type(x) is SQLExpr for x in list(migrations.default_values.values())[1:]])" |
| 659 | + ] |
| 660 | + }, |
| 661 | + { |
| 662 | + "cell_type": "code", |
| 663 | + "execution_count": null, |
| 664 | + "metadata": {}, |
| 665 | + "outputs": [ |
| 666 | + { |
| 667 | + "data": { |
| 668 | + "text/plain": [ |
| 669 | + "<Table migrations (id, name, cexpr, rand, unix_epoch, json_array, inserted_at)>" |
| 670 | + ] |
| 671 | + }, |
| 672 | + "execution_count": null, |
| 673 | + "metadata": {}, |
| 674 | + "output_type": "execute_result" |
| 675 | + } |
| 676 | + ], |
| 677 | + "source": [ |
| 678 | + "migrations.insert(dict(id=0))\n", |
| 679 | + "migrations.insert(dict(id=1))" |
| 680 | + ] |
| 681 | + }, |
| 682 | + { |
| 683 | + "cell_type": "markdown", |
| 684 | + "metadata": {}, |
| 685 | + "source": [ |
| 686 | + "Default expressions are executed independently for each row on row insertion:" |
| 687 | + ] |
| 688 | + }, |
| 689 | + { |
| 690 | + "cell_type": "code", |
| 691 | + "execution_count": null, |
| 692 | + "metadata": {}, |
| 693 | + "outputs": [ |
| 694 | + { |
| 695 | + "data": { |
| 696 | + "text/plain": [ |
| 697 | + "[{'id': 0,\n", |
| 698 | + " 'name': 'foo',\n", |
| 699 | + " 'cexpr': 'abracadabra',\n", |
| 700 | + " 'rand': 8201569685582150332,\n", |
| 701 | + " 'unix_epoch': 1741481111.188,\n", |
| 702 | + " 'json_array': '[1,2,3,4]',\n", |
| 703 | + " 'inserted_at': '2025-03-09 00:45:11'},\n", |
| 704 | + " {'id': 1,\n", |
| 705 | + " 'name': 'foo',\n", |
| 706 | + " 'cexpr': 'abracadabra',\n", |
| 707 | + " 'rand': 1625289491289542947,\n", |
| 708 | + " 'unix_epoch': 1741481111.19,\n", |
| 709 | + " 'json_array': '[1,2,3,4]',\n", |
| 710 | + " 'inserted_at': '2025-03-09 00:45:11'}]" |
| 711 | + ] |
| 712 | + }, |
| 713 | + "execution_count": null, |
| 714 | + "metadata": {}, |
| 715 | + "output_type": "execute_result" |
| 716 | + } |
| 717 | + ], |
| 718 | + "source": [ |
| 719 | + "rows = list(migrations.rows)\n", |
| 720 | + "rows" |
| 721 | + ] |
| 722 | + }, |
| 723 | + { |
| 724 | + "cell_type": "code", |
| 725 | + "execution_count": null, |
| 726 | + "metadata": {}, |
| 727 | + "outputs": [], |
| 728 | + "source": [ |
| 729 | + "assert rows[0]['rand'] != rows[1]['rand']" |
| 730 | + ] |
580 | 731 | } |
581 | 732 | ], |
582 | 733 | "metadata": { |
|
587 | 738 | } |
588 | 739 | }, |
589 | 740 | "nbformat": 4, |
590 | | - "nbformat_minor": 2 |
| 741 | + "nbformat_minor": 4 |
591 | 742 | } |
0 commit comments